| 1 |
package tokenexchange |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"net/http" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/go-jose/go-jose/v4" |
| 10 |
"github.com/zitadel/oidc/v3/pkg/client" |
| 11 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 12 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 13 |
) |
| 14 |
|
| 15 |
type TokenExchanger interface { |
| 16 |
TokenEndpoint() string |
| 17 |
HttpClient() *http.Client |
| 18 |
AuthFn() (any, error) |
| 19 |
} |
| 20 |
|
| 21 |
type OAuthTokenExchange struct { |
| 22 |
httpClient *http.Client |
| 23 |
tokenEndpoint string |
| 24 |
authFn func() (any, error) |
| 25 |
} |
| 26 |
|
| 27 |
func NewTokenExchanger(ctx context.Context, issuer string, options ...func(source *OAuthTokenExchange)) (TokenExchanger, error) { |
| 28 |
return newOAuthTokenExchange(ctx, issuer, nil, options...) |
| 29 |
} |
| 30 |
|
| 31 |
func NewTokenExchangerClientCredentials(ctx context.Context, issuer, clientID, clientSecret string, options ...func(source *OAuthTokenExchange)) (TokenExchanger, error) { |
| 32 |
authorizer := func() (any, error) { |
| 33 |
return httphelper.AuthorizeBasic(clientID, clientSecret), nil |
| 34 |
} |
| 35 |
return newOAuthTokenExchange(ctx, issuer, authorizer, options...) |
| 36 |
} |
| 37 |
|
| 38 |
func NewTokenExchangerJWTProfile(ctx context.Context, issuer, clientID string, signer jose.Signer, options ...func(source *OAuthTokenExchange)) (TokenExchanger, error) { |
| 39 |
authorizer := func() (any, error) { |
| 40 |
assertion, err := client.SignedJWTProfileAssertion(clientID, []string{issuer}, time.Hour, signer) |
| 41 |
if err != nil { |
| 42 |
return nil, err |
| 43 |
} |
| 44 |
return client.ClientAssertionFormAuthorization(assertion), nil |
| 45 |
} |
| 46 |
return newOAuthTokenExchange(ctx, issuer, authorizer, options...) |
| 47 |
} |
| 48 |
|
| 49 |
func newOAuthTokenExchange(ctx context.Context, issuer string, authorizer func() (any, error), options ...func(source *OAuthTokenExchange)) (*OAuthTokenExchange, error) { |
| 50 |
te := &OAuthTokenExchange{ |
| 51 |
httpClient: httphelper.DefaultHTTPClient, |
| 52 |
} |
| 53 |
for _, opt := range options { |
| 54 |
opt(te) |
| 55 |
} |
| 56 |
|
| 57 |
if te.tokenEndpoint == "" { |
| 58 |
config, err := client.Discover(ctx, issuer, te.httpClient) |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
|
| 63 |
te.tokenEndpoint = config.TokenEndpoint |
| 64 |
} |
| 65 |
|
| 66 |
if te.tokenEndpoint == "" { |
| 67 |
return nil, errors.New("tokenURL is empty: please provide with either `WithStaticTokenEndpoint` or a discovery url") |
| 68 |
} |
| 69 |
|
| 70 |
te.authFn = authorizer |
| 71 |
|
| 72 |
return te, nil |
| 73 |
} |
| 74 |
|
| 75 |
func WithHTTPClient(client *http.Client) func(*OAuthTokenExchange) { |
| 76 |
return func(source *OAuthTokenExchange) { |
| 77 |
source.httpClient = client |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func WithStaticTokenEndpoint(issuer, tokenEndpoint string) func(*OAuthTokenExchange) { |
| 82 |
return func(source *OAuthTokenExchange) { |
| 83 |
source.tokenEndpoint = tokenEndpoint |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func (te *OAuthTokenExchange) TokenEndpoint() string { |
| 88 |
return te.tokenEndpoint |
| 89 |
} |
| 90 |
|
| 91 |
func (te *OAuthTokenExchange) HttpClient() *http.Client { |
| 92 |
return te.httpClient |
| 93 |
} |
| 94 |
|
| 95 |
func (te *OAuthTokenExchange) AuthFn() (any, error) { |
| 96 |
if te.authFn != nil { |
| 97 |
return te.authFn() |
| 98 |
} |
| 99 |
|
| 100 |
return nil, nil |
| 101 |
} |
| 102 |
|
| 103 |
// ExchangeToken sends a token exchange request (rfc 8693) to te's token endpoint. |
| 104 |
// SubjectToken and SubjectTokenType are required parameters. |
| 105 |
func ExchangeToken( |
| 106 |
ctx context.Context, |
| 107 |
te TokenExchanger, |
| 108 |
SubjectToken string, |
| 109 |
SubjectTokenType oidc.TokenType, |
| 110 |
ActorToken string, |
| 111 |
ActorTokenType oidc.TokenType, |
| 112 |
Resource []string, |
| 113 |
Audience []string, |
| 114 |
Scopes []string, |
| 115 |
RequestedTokenType oidc.TokenType, |
| 116 |
) (*oidc.TokenExchangeResponse, error) { |
| 117 |
ctx, span := client.Tracer.Start(ctx, "ExchangeToken") |
| 118 |
defer span.End() |
| 119 |
|
| 120 |
if SubjectToken == "" { |
| 121 |
return nil, errors.New("empty subject_token") |
| 122 |
} |
| 123 |
if SubjectTokenType == "" { |
| 124 |
return nil, errors.New("empty subject_token_type") |
| 125 |
} |
| 126 |
|
| 127 |
authFn, err := te.AuthFn() |
| 128 |
if err != nil { |
| 129 |
return nil, err |
| 130 |
} |
| 131 |
|
| 132 |
request := oidc.TokenExchangeRequest{ |
| 133 |
GrantType: oidc.GrantTypeTokenExchange, |
| 134 |
SubjectToken: SubjectToken, |
| 135 |
SubjectTokenType: SubjectTokenType, |
| 136 |
ActorToken: ActorToken, |
| 137 |
ActorTokenType: ActorTokenType, |
| 138 |
Resource: Resource, |
| 139 |
Audience: Audience, |
| 140 |
Scopes: Scopes, |
| 141 |
RequestedTokenType: RequestedTokenType, |
| 142 |
} |
| 143 |
|
| 144 |
return client.CallTokenExchangeEndpoint(ctx, request, authFn, te) |
| 145 |
} |