| 1 |
package profile |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"net/http" |
| 6 |
"time" |
| 7 |
|
| 8 |
jose "github.com/go-jose/go-jose/v4" |
| 9 |
"golang.org/x/oauth2" |
| 10 |
|
| 11 |
"github.com/zitadel/oidc/v3/pkg/client" |
| 12 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 13 |
) |
| 14 |
|
| 15 |
type TokenSource interface { |
| 16 |
oauth2.TokenSource |
| 17 |
TokenCtx(context.Context) (*oauth2.Token, error) |
| 18 |
} |
| 19 |
|
| 20 |
// jwtProfileTokenSource implement the oauth2.TokenSource |
| 21 |
// it will request a token using the OAuth2 JWT Profile Grant |
| 22 |
// therefore sending an `assertion` by signing a JWT with the provided private key |
| 23 |
type jwtProfileTokenSource struct { |
| 24 |
clientID string |
| 25 |
audience []string |
| 26 |
signer jose.Signer |
| 27 |
scopes []string |
| 28 |
httpClient *http.Client |
| 29 |
tokenEndpoint string |
| 30 |
} |
| 31 |
|
| 32 |
// NewJWTProfileTokenSourceFromKeyFile returns an implementation of TokenSource |
| 33 |
// It will request a token using the OAuth2 JWT Profile Grant, |
| 34 |
// therefore sending an `assertion` by singing a JWT with the provided private key from jsonFile. |
| 35 |
// |
| 36 |
// The passed context is only used for the call to the Discover endpoint. |
| 37 |
// |
| 38 |
// Deprecated: use [github.com/zitadel/zitadel-go/v3/pkg/client.ConfigFromKeyFileData] instead. |
| 39 |
// The function will be removed in the next major release. |
| 40 |
func NewJWTProfileTokenSourceFromKeyFile(ctx context.Context, issuer, jsonFile string, scopes []string, options ...func(source *jwtProfileTokenSource)) (TokenSource, error) { |
| 41 |
keyData, err := client.ConfigFromKeyFile(jsonFile) |
| 42 |
if err != nil { |
| 43 |
return nil, err |
| 44 |
} |
| 45 |
return NewJWTProfileTokenSource(ctx, issuer, keyData.UserID, keyData.KeyID, []byte(keyData.Key), scopes, options...) |
| 46 |
} |
| 47 |
|
| 48 |
// NewJWTProfileTokenSourceFromKeyFileData returns an implementation of oauth2.TokenSource |
| 49 |
// It will request a token using the OAuth2 JWT Profile Grant, |
| 50 |
// therefore sending an `assertion` by singing a JWT with the provided private key in jsonData. |
| 51 |
// |
| 52 |
// The passed context is only used for the call to the Discover endpoint. |
| 53 |
// |
| 54 |
// Deprecated: use [github.com/zitadel/zitadel-go/v3/pkg/client.ConfigFromKeyFileData] instead. |
| 55 |
// The function will be removed in the next major release. |
| 56 |
func NewJWTProfileTokenSourceFromKeyFileData(ctx context.Context, issuer string, jsonData []byte, scopes []string, options ...func(source *jwtProfileTokenSource)) (TokenSource, error) { |
| 57 |
keyData, err := client.ConfigFromKeyFileData(jsonData) |
| 58 |
if err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
return NewJWTProfileTokenSource(ctx, issuer, keyData.UserID, keyData.KeyID, []byte(keyData.Key), scopes, options...) |
| 62 |
} |
| 63 |
|
| 64 |
// NewJWTProfileTokenSource returns an implementation of oauth2.TokenSource |
| 65 |
// It will request a token using the OAuth2 JWT Profile Grant, |
| 66 |
// therefore sending an `assertion` by singing a JWT with the provided private key. |
| 67 |
// |
| 68 |
// The passed context is only used for the call to the Discover endpoint. |
| 69 |
func NewJWTProfileTokenSource(ctx context.Context, issuer, clientID, keyID string, key []byte, scopes []string, options ...func(source *jwtProfileTokenSource)) (TokenSource, error) { |
| 70 |
signer, err := client.NewSignerFromPrivateKeyByte(key, keyID) |
| 71 |
if err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
source := &jwtProfileTokenSource{ |
| 75 |
clientID: clientID, |
| 76 |
audience: []string{issuer}, |
| 77 |
signer: signer, |
| 78 |
scopes: scopes, |
| 79 |
httpClient: http.DefaultClient, |
| 80 |
} |
| 81 |
for _, opt := range options { |
| 82 |
opt(source) |
| 83 |
} |
| 84 |
if source.tokenEndpoint == "" { |
| 85 |
config, err := client.Discover(ctx, issuer, source.httpClient) |
| 86 |
if err != nil { |
| 87 |
return nil, err |
| 88 |
} |
| 89 |
source.tokenEndpoint = config.TokenEndpoint |
| 90 |
} |
| 91 |
return source, nil |
| 92 |
} |
| 93 |
|
| 94 |
func WithHTTPClient(client *http.Client) func(source *jwtProfileTokenSource) { |
| 95 |
return func(source *jwtProfileTokenSource) { |
| 96 |
source.httpClient = client |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func WithStaticTokenEndpoint(issuer, tokenEndpoint string) func(source *jwtProfileTokenSource) { |
| 101 |
return func(source *jwtProfileTokenSource) { |
| 102 |
source.tokenEndpoint = tokenEndpoint |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func (j *jwtProfileTokenSource) TokenEndpoint() string { |
| 107 |
return j.tokenEndpoint |
| 108 |
} |
| 109 |
|
| 110 |
func (j *jwtProfileTokenSource) HttpClient() *http.Client { |
| 111 |
return j.httpClient |
| 112 |
} |
| 113 |
|
| 114 |
func (j *jwtProfileTokenSource) Token() (*oauth2.Token, error) { |
| 115 |
return j.TokenCtx(context.Background()) |
| 116 |
} |
| 117 |
|
| 118 |
func (j *jwtProfileTokenSource) TokenCtx(ctx context.Context) (*oauth2.Token, error) { |
| 119 |
assertion, err := client.SignedJWTProfileAssertion(j.clientID, j.audience, time.Hour, j.signer) |
| 120 |
if err != nil { |
| 121 |
return nil, err |
| 122 |
} |
| 123 |
return client.JWTProfileExchange(ctx, oidc.NewJWTProfileGrantRequest(assertion, j.scopes...), j) |
| 124 |
} |