github.com/blend/go-sdk@v1.20220411.3/oauth/main_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package oauth
     9  
    10  import (
    11  	"crypto/rsa"
    12  	"encoding/base64"
    13  	"encoding/json"
    14  	"math/big"
    15  	"time"
    16  
    17  	"github.com/golang-jwt/jwt"
    18  
    19  	"github.com/blend/go-sdk/jwk"
    20  	"github.com/blend/go-sdk/uuid"
    21  )
    22  
    23  const (
    24  	pk0pem = "-----BEGIN RSA PRIVATE KEY-----\nMIIEoQIBAAKCAQEAy4zZIH5mtomtQisfKhhr79LMbJqtWJWRDJytoxWb3h0gNniz\n2uxLVBqIyIibiqDr9dG0waQXlZgWip7WnL0Rip+UjGN+i0jhHAqNfjpK0KVI/epa\nFeL3rP+jfTZXOedUwt7kAuxCI07Dokqyarm7WpAEaShkd8ZuQ1KsQ9ZblKrw77uO\n6fB34npf+2Lahi++P0FFpgHNW2vfSc0PrXoGy27DkWDMnHGiElm8VX9nwOGI+JN1\n3RdQzTSF4VNoXjRynLNdPt4XKLQS0HFW3kQHogi0uf0KG6sZpLBU5KgORFD7ScXm\nAd402NH7qYdzggdhaSMsTZ16RpcdBtaX/KF+GwIDAQABAoIBABrbR56w7s5w1epg\nFCmStVMcRhqiQfLpMQ0v8v0Mkdc5kpF9VYWyHbJIGfoThCpDVz7E34uZIf975KV/\nlaNyksjui0QGsKCiCgmQHuEjwdFLrZjK/f3bR4CM7j5MGDAspJNdo0n7cDKGZuuX\n3XiVbvHhBKP3T2I6TTwWwWHl+4le1FNRvcPPK1V4SXrdknuZl1Rbzz2xt2AC4hZR\niiWhA893pXnai4IbjHwcOUGRKT6i63TNjsik/o0ANJruoyVfdMyRBnTQ3cUUM1/k\n+qWEki1fBHLVcSrhCdAuq0rezCZnTjd6Z2t97XOYMm/t9ak0f8ZvtLntomLxv8Mv\n5BJel2ECgYEA5wF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----"
    27  )
    28  
    29  func createJWK(pk *rsa.PrivateKey) jwk.JWK {
    30  	eBytes := big.Int{}
    31  	eBytes.SetInt64(int64(pk.PublicKey.E))
    32  	e := base64.RawURLEncoding.EncodeToString(eBytes.Bytes())
    33  	n := base64.RawURLEncoding.EncodeToString(pk.PublicKey.N.Bytes())
    34  	return jwk.JWK{
    35  		KID: uuid.V4().String(),
    36  		ALG: "RS256",
    37  		KTY: "RSA",
    38  		USE: "sig",
    39  		E:   e,
    40  		N:   n,
    41  	}
    42  }
    43  
    44  func createCodeResponse(aud, keyID string, pk *rsa.PrivateKey) ([]byte, error) {
    45  	jwtToken := jwt.NewWithClaims(jwt.SigningMethodRS256, &GoogleClaims{
    46  		StandardClaims: jwt.StandardClaims{
    47  			Audience:  aud,
    48  			ExpiresAt: time.Now().UTC().AddDate(0, 0, 1).Unix(),
    49  			IssuedAt:  time.Now().UTC().Unix(),
    50  			Issuer:    GoogleIssuer,
    51  		},
    52  		HD:            "test.blend.com",
    53  		Email:         "example-string@test.blend.com",
    54  		EmailVerified: "true",
    55  	})
    56  	jwtToken.Header["kid"] = keyID
    57  	jwtTokenSigned, err := jwtToken.SignedString(pk)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	type tokenJSON struct {
    62  		AccessToken  string `json:"access_token"`
    63  		IDToken      string `json:"id_token"`
    64  		TokenType    string `json:"token_type"`
    65  		RefreshToken string `json:"refresh_token"`
    66  		ExpiresIn    int    `json:"expires_in"`
    67  		Expires      int    `json:"expires"`
    68  	}
    69  	return json.Marshal(tokenJSON{
    70  		AccessToken: "test_access_token",
    71  		IDToken:     jwtTokenSigned,
    72  	})
    73  }