| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"net/http" |
| 6 |
"net/url" |
| 7 |
"strings" |
| 8 |
"time" |
| 9 |
|
| 10 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 11 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 12 |
) |
| 13 |
|
| 14 |
type TokenExchangeRequest interface { |
| 15 |
GetAMR() []string |
| 16 |
GetAudience() []string |
| 17 |
GetResourses() []string |
| 18 |
GetAuthTime() time.Time |
| 19 |
GetClientID() string |
| 20 |
GetScopes() []string |
| 21 |
GetSubject() string |
| 22 |
GetRequestedTokenType() oidc.TokenType |
| 23 |
|
| 24 |
GetExchangeSubject() string |
| 25 |
GetExchangeSubjectTokenType() oidc.TokenType |
| 26 |
GetExchangeSubjectTokenIDOrToken() string |
| 27 |
GetExchangeSubjectTokenClaims() map[string]any |
| 28 |
|
| 29 |
GetExchangeActor() string |
| 30 |
GetExchangeActorTokenType() oidc.TokenType |
| 31 |
GetExchangeActorTokenIDOrToken() string |
| 32 |
GetExchangeActorTokenClaims() map[string]any |
| 33 |
|
| 34 |
SetCurrentScopes(scopes []string) |
| 35 |
SetRequestedTokenType(tt oidc.TokenType) |
| 36 |
SetSubject(subject string) |
| 37 |
} |
| 38 |
|
| 39 |
type tokenExchangeRequest struct { |
| 40 |
exchangeSubjectTokenIDOrToken string |
| 41 |
exchangeSubjectTokenType oidc.TokenType |
| 42 |
exchangeSubject string |
| 43 |
exchangeSubjectTokenClaims map[string]any |
| 44 |
|
| 45 |
exchangeActorTokenIDOrToken string |
| 46 |
exchangeActorTokenType oidc.TokenType |
| 47 |
exchangeActor string |
| 48 |
exchangeActorTokenClaims map[string]any |
| 49 |
|
| 50 |
resource []string |
| 51 |
audience oidc.Audience |
| 52 |
scopes oidc.SpaceDelimitedArray |
| 53 |
requestedTokenType oidc.TokenType |
| 54 |
clientID string |
| 55 |
authTime time.Time |
| 56 |
subject string |
| 57 |
} |
| 58 |
|
| 59 |
func (r *tokenExchangeRequest) GetAMR() []string { |
| 60 |
return []string{} |
| 61 |
} |
| 62 |
|
| 63 |
func (r *tokenExchangeRequest) GetAudience() []string { |
| 64 |
return r.audience |
| 65 |
} |
| 66 |
|
| 67 |
func (r *tokenExchangeRequest) GetResourses() []string { |
| 68 |
return r.resource |
| 69 |
} |
| 70 |
|
| 71 |
func (r *tokenExchangeRequest) GetAuthTime() time.Time { |
| 72 |
return r.authTime |
| 73 |
} |
| 74 |
|
| 75 |
func (r *tokenExchangeRequest) GetClientID() string { |
| 76 |
return r.clientID |
| 77 |
} |
| 78 |
|
| 79 |
func (r *tokenExchangeRequest) GetScopes() []string { |
| 80 |
return r.scopes |
| 81 |
} |
| 82 |
|
| 83 |
func (r *tokenExchangeRequest) GetRequestedTokenType() oidc.TokenType { |
| 84 |
return r.requestedTokenType |
| 85 |
} |
| 86 |
|
| 87 |
func (r *tokenExchangeRequest) GetExchangeSubject() string { |
| 88 |
return r.exchangeSubject |
| 89 |
} |
| 90 |
|
| 91 |
func (r *tokenExchangeRequest) GetExchangeSubjectTokenType() oidc.TokenType { |
| 92 |
return r.exchangeSubjectTokenType |
| 93 |
} |
| 94 |
|
| 95 |
func (r *tokenExchangeRequest) GetExchangeSubjectTokenIDOrToken() string { |
| 96 |
return r.exchangeSubjectTokenIDOrToken |
| 97 |
} |
| 98 |
|
| 99 |
func (r *tokenExchangeRequest) GetExchangeSubjectTokenClaims() map[string]any { |
| 100 |
return r.exchangeSubjectTokenClaims |
| 101 |
} |
| 102 |
|
| 103 |
func (r *tokenExchangeRequest) GetExchangeActor() string { |
| 104 |
return r.exchangeActor |
| 105 |
} |
| 106 |
|
| 107 |
func (r *tokenExchangeRequest) GetExchangeActorTokenType() oidc.TokenType { |
| 108 |
return r.exchangeActorTokenType |
| 109 |
} |
| 110 |
|
| 111 |
func (r *tokenExchangeRequest) GetExchangeActorTokenIDOrToken() string { |
| 112 |
return r.exchangeActorTokenIDOrToken |
| 113 |
} |
| 114 |
|
| 115 |
func (r *tokenExchangeRequest) GetExchangeActorTokenClaims() map[string]any { |
| 116 |
return r.exchangeActorTokenClaims |
| 117 |
} |
| 118 |
|
| 119 |
func (r *tokenExchangeRequest) GetSubject() string { |
| 120 |
return r.subject |
| 121 |
} |
| 122 |
|
| 123 |
func (r *tokenExchangeRequest) SetCurrentScopes(scopes []string) { |
| 124 |
r.scopes = scopes |
| 125 |
} |
| 126 |
|
| 127 |
func (r *tokenExchangeRequest) SetRequestedTokenType(tt oidc.TokenType) { |
| 128 |
r.requestedTokenType = tt |
| 129 |
} |
| 130 |
|
| 131 |
func (r *tokenExchangeRequest) SetSubject(subject string) { |
| 132 |
r.subject = subject |
| 133 |
} |
| 134 |
|
| 135 |
// TokenExchange handles the OAuth 2.0 token exchange grant ("urn:ietf:params:oauth:grant-type:token-exchange") |
| 136 |
func TokenExchange(w http.ResponseWriter, r *http.Request, exchanger Exchanger) { |
| 137 |
ctx, span := Tracer.Start(r.Context(), "TokenExchange") |
| 138 |
defer span.End() |
| 139 |
r = r.WithContext(ctx) |
| 140 |
|
| 141 |
tokenExchangeReq, clientID, clientSecret, err := ParseTokenExchangeRequest(r, exchanger.Decoder()) |
| 142 |
if err != nil { |
| 143 |
RequestError(w, r, err, nil) |
| 144 |
return |
| 145 |
} |
| 146 |
|
| 147 |
tokenExchangeRequest, client, err := ValidateTokenExchangeRequest(r.Context(), tokenExchangeReq, clientID, clientSecret, exchanger) |
| 148 |
if err != nil { |
| 149 |
RequestError(w, r, err, nil) |
| 150 |
return |
| 151 |
} |
| 152 |
resp, err := CreateTokenExchangeResponse(r.Context(), tokenExchangeRequest, client, exchanger) |
| 153 |
if err != nil { |
| 154 |
RequestError(w, r, err, nil) |
| 155 |
return |
| 156 |
} |
| 157 |
httphelper.MarshalJSON(w, resp) |
| 158 |
} |
| 159 |
|
| 160 |
// ParseTokenExchangeRequest parses the http request into oidc.TokenExchangeRequest |
| 161 |
func ParseTokenExchangeRequest(r *http.Request, decoder httphelper.Decoder) (_ *oidc.TokenExchangeRequest, clientID, clientSecret string, err error) { |
| 162 |
err = r.ParseForm() |
| 163 |
if err != nil { |
| 164 |
return nil, "", "", oidc.ErrInvalidRequest().WithDescription("error parsing form").WithParent(err) |
| 165 |
} |
| 166 |
|
| 167 |
request := new(oidc.TokenExchangeRequest) |
| 168 |
err = decoder.Decode(request, r.Form) |
| 169 |
if err != nil { |
| 170 |
return nil, "", "", oidc.ErrInvalidRequest().WithDescription("error decoding form").WithParent(err) |
| 171 |
} |
| 172 |
|
| 173 |
var ok bool |
| 174 |
if clientID, clientSecret, ok = r.BasicAuth(); ok { |
| 175 |
clientID, err = url.QueryUnescape(clientID) |
| 176 |
if err != nil { |
| 177 |
return nil, "", "", oidc.ErrInvalidClient().WithDescription("invalid basic auth header").WithParent(err) |
| 178 |
} |
| 179 |
|
| 180 |
clientSecret, err = url.QueryUnescape(clientSecret) |
| 181 |
if err != nil { |
| 182 |
return nil, "", "", oidc.ErrInvalidClient().WithDescription("invalid basic auth header").WithParent(err) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
return request, clientID, clientSecret, nil |
| 187 |
} |
| 188 |
|
| 189 |
// ValidateTokenExchangeRequest validates the token exchange request parameters including authorization check of the client, |
| 190 |
// subject_token and actor_token |
| 191 |
func ValidateTokenExchangeRequest( |
| 192 |
ctx context.Context, |
| 193 |
oidcTokenExchangeRequest *oidc.TokenExchangeRequest, |
| 194 |
clientID, clientSecret string, |
| 195 |
exchanger Exchanger, |
| 196 |
) (TokenExchangeRequest, Client, error) { |
| 197 |
ctx, span := Tracer.Start(ctx, "ValidateTokenExchangeRequest") |
| 198 |
defer span.End() |
| 199 |
|
| 200 |
if oidcTokenExchangeRequest.SubjectToken == "" { |
| 201 |
return nil, nil, oidc.ErrInvalidRequest().WithDescription("subject_token missing") |
| 202 |
} |
| 203 |
|
| 204 |
if oidcTokenExchangeRequest.SubjectTokenType == "" { |
| 205 |
return nil, nil, oidc.ErrInvalidRequest().WithDescription("subject_token_type missing") |
| 206 |
} |
| 207 |
|
| 208 |
client, err := AuthorizeTokenExchangeClient(ctx, clientID, clientSecret, exchanger) |
| 209 |
if err != nil { |
| 210 |
return nil, nil, err |
| 211 |
} |
| 212 |
|
| 213 |
if oidcTokenExchangeRequest.RequestedTokenType != "" && !oidcTokenExchangeRequest.RequestedTokenType.IsSupported() { |
| 214 |
return nil, nil, oidc.ErrInvalidRequest().WithDescription("requested_token_type is not supported") |
| 215 |
} |
| 216 |
|
| 217 |
if !oidcTokenExchangeRequest.SubjectTokenType.IsSupported() { |
| 218 |
return nil, nil, oidc.ErrInvalidRequest().WithDescription("subject_token_type is not supported") |
| 219 |
} |
| 220 |
|
| 221 |
if oidcTokenExchangeRequest.ActorTokenType != "" && !oidcTokenExchangeRequest.ActorTokenType.IsSupported() { |
| 222 |
return nil, nil, oidc.ErrInvalidRequest().WithDescription("actor_token_type is not supported") |
| 223 |
} |
| 224 |
|
| 225 |
req, err := CreateTokenExchangeRequest(ctx, oidcTokenExchangeRequest, client, exchanger) |
| 226 |
if err != nil { |
| 227 |
return nil, nil, err |
| 228 |
} |
| 229 |
return req, client, nil |
| 230 |
} |
| 231 |
|
| 232 |
func CreateTokenExchangeRequest( |
| 233 |
ctx context.Context, |
| 234 |
oidcTokenExchangeRequest *oidc.TokenExchangeRequest, |
| 235 |
client Client, |
| 236 |
exchanger Exchanger, |
| 237 |
) (TokenExchangeRequest, error) { |
| 238 |
ctx, span := Tracer.Start(ctx, "CreateTokenExchangeRequest") |
| 239 |
defer span.End() |
| 240 |
|
| 241 |
teStorage, ok := exchanger.Storage().(TokenExchangeStorage) |
| 242 |
if !ok { |
| 243 |
return nil, unimplementedGrantError(oidc.GrantTypeTokenExchange) |
| 244 |
} |
| 245 |
|
| 246 |
exchangeSubjectTokenIDOrToken, exchangeSubject, exchangeSubjectTokenClaims, ok := GetTokenIDAndSubjectFromToken(ctx, exchanger, |
| 247 |
oidcTokenExchangeRequest.SubjectToken, oidcTokenExchangeRequest.SubjectTokenType, false) |
| 248 |
if !ok { |
| 249 |
return nil, oidc.ErrInvalidRequest().WithDescription("subject_token is invalid") |
| 250 |
} |
| 251 |
|
| 252 |
var ( |
| 253 |
exchangeActorTokenIDOrToken, exchangeActor string |
| 254 |
exchangeActorTokenClaims map[string]any |
| 255 |
) |
| 256 |
if oidcTokenExchangeRequest.ActorToken != "" { |
| 257 |
exchangeActorTokenIDOrToken, exchangeActor, exchangeActorTokenClaims, ok = GetTokenIDAndSubjectFromToken(ctx, exchanger, |
| 258 |
oidcTokenExchangeRequest.ActorToken, oidcTokenExchangeRequest.ActorTokenType, true) |
| 259 |
if !ok { |
| 260 |
return nil, oidc.ErrInvalidRequest().WithDescription("actor_token is invalid") |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
req := &tokenExchangeRequest{ |
| 265 |
exchangeSubjectTokenIDOrToken: exchangeSubjectTokenIDOrToken, |
| 266 |
exchangeSubjectTokenType: oidcTokenExchangeRequest.SubjectTokenType, |
| 267 |
exchangeSubject: exchangeSubject, |
| 268 |
exchangeSubjectTokenClaims: exchangeSubjectTokenClaims, |
| 269 |
|
| 270 |
exchangeActorTokenIDOrToken: exchangeActorTokenIDOrToken, |
| 271 |
exchangeActorTokenType: oidcTokenExchangeRequest.ActorTokenType, |
| 272 |
exchangeActor: exchangeActor, |
| 273 |
exchangeActorTokenClaims: exchangeActorTokenClaims, |
| 274 |
|
| 275 |
subject: exchangeSubject, |
| 276 |
resource: oidcTokenExchangeRequest.Resource, |
| 277 |
audience: oidcTokenExchangeRequest.Audience, |
| 278 |
scopes: oidcTokenExchangeRequest.Scopes, |
| 279 |
requestedTokenType: oidcTokenExchangeRequest.RequestedTokenType, |
| 280 |
clientID: client.GetID(), |
| 281 |
authTime: time.Now(), |
| 282 |
} |
| 283 |
|
| 284 |
err := teStorage.ValidateTokenExchangeRequest(ctx, req) |
| 285 |
if err != nil { |
| 286 |
return nil, err |
| 287 |
} |
| 288 |
|
| 289 |
err = teStorage.CreateTokenExchangeRequest(ctx, req) |
| 290 |
if err != nil { |
| 291 |
return nil, err |
| 292 |
} |
| 293 |
|
| 294 |
return req, nil |
| 295 |
} |
| 296 |
|
| 297 |
func GetTokenIDAndSubjectFromToken( |
| 298 |
ctx context.Context, |
| 299 |
exchanger Exchanger, |
| 300 |
token string, |
| 301 |
tokenType oidc.TokenType, |
| 302 |
isActor bool, |
| 303 |
) (tokenIDOrToken, subject string, claims map[string]any, ok bool) { |
| 304 |
ctx, span := Tracer.Start(ctx, "GetTokenIDAndSubjectFromToken") |
| 305 |
defer span.End() |
| 306 |
|
| 307 |
switch tokenType { |
| 308 |
case oidc.AccessTokenType: |
| 309 |
var accessTokenClaims *oidc.AccessTokenClaims |
| 310 |
tokenIDOrToken, subject, accessTokenClaims, ok = getTokenIDAndClaims(ctx, exchanger, token) |
| 311 |
if !ok { |
| 312 |
break |
| 313 |
} |
| 314 |
// opaque access tokens carry no JWT claims, so accessTokenClaims can be |
| 315 |
// nil even when ok is true. Leave claims empty instead of dereferencing. |
| 316 |
if accessTokenClaims != nil { |
| 317 |
claims = accessTokenClaims.Claims |
| 318 |
} |
| 319 |
case oidc.RefreshTokenType: |
| 320 |
refreshTokenRequest, err := exchanger.Storage().TokenRequestByRefreshToken(ctx, token) |
| 321 |
if err != nil { |
| 322 |
break |
| 323 |
} |
| 324 |
|
| 325 |
tokenIDOrToken, subject, ok = token, refreshTokenRequest.GetSubject(), true |
| 326 |
case oidc.IDTokenType: |
| 327 |
idTokenClaims, err := VerifyIDTokenHint[*oidc.IDTokenClaims](ctx, token, exchanger.IDTokenHintVerifier(ctx)) |
| 328 |
if err != nil { |
| 329 |
break |
| 330 |
} |
| 331 |
|
| 332 |
tokenIDOrToken, subject, claims, ok = token, idTokenClaims.Subject, idTokenClaims.Claims, true |
| 333 |
} |
| 334 |
|
| 335 |
if !ok { |
| 336 |
if verifier, ok := exchanger.Storage().(TokenExchangeTokensVerifierStorage); ok { |
| 337 |
var err error |
| 338 |
if isActor { |
| 339 |
tokenIDOrToken, subject, claims, err = verifier.VerifyExchangeActorToken(ctx, token, tokenType) |
| 340 |
} else { |
| 341 |
tokenIDOrToken, subject, claims, err = verifier.VerifyExchangeSubjectToken(ctx, token, tokenType) |
| 342 |
} |
| 343 |
if err != nil { |
| 344 |
return "", "", nil, false |
| 345 |
} |
| 346 |
|
| 347 |
return tokenIDOrToken, subject, claims, true |
| 348 |
} |
| 349 |
|
| 350 |
return "", "", nil, false |
| 351 |
} |
| 352 |
|
| 353 |
return tokenIDOrToken, subject, claims, true |
| 354 |
} |
| 355 |
|
| 356 |
// AuthorizeTokenExchangeClient authorizes a client by validating the client_id and client_secret |
| 357 |
func AuthorizeTokenExchangeClient(ctx context.Context, clientID, clientSecret string, exchanger Exchanger) (client Client, err error) { |
| 358 |
ctx, span := Tracer.Start(ctx, "AuthorizeTokenExchangeClient") |
| 359 |
defer span.End() |
| 360 |
|
| 361 |
if err := AuthorizeClientIDSecret(ctx, clientID, clientSecret, exchanger.Storage()); err != nil { |
| 362 |
return nil, err |
| 363 |
} |
| 364 |
|
| 365 |
client, err = exchanger.Storage().GetClientByClientID(ctx, clientID) |
| 366 |
if err != nil { |
| 367 |
return nil, oidc.ErrInvalidClient().WithParent(err) |
| 368 |
} |
| 369 |
|
| 370 |
return client, nil |
| 371 |
} |
| 372 |
|
| 373 |
func CreateTokenExchangeResponse( |
| 374 |
ctx context.Context, |
| 375 |
tokenExchangeRequest TokenExchangeRequest, |
| 376 |
client Client, |
| 377 |
creator TokenCreator, |
| 378 |
) (_ *oidc.TokenExchangeResponse, err error) { |
| 379 |
ctx, span := Tracer.Start(ctx, "CreateTokenExchangeResponse") |
| 380 |
defer span.End() |
| 381 |
|
| 382 |
var ( |
| 383 |
token, refreshToken, tokenType string |
| 384 |
validity time.Duration |
| 385 |
) |
| 386 |
|
| 387 |
switch tokenExchangeRequest.GetRequestedTokenType() { |
| 388 |
case oidc.AccessTokenType, oidc.RefreshTokenType: |
| 389 |
token, refreshToken, validity, err = CreateAccessToken(ctx, tokenExchangeRequest, client.AccessTokenType(), creator, client, "") |
| 390 |
if err != nil { |
| 391 |
return nil, err |
| 392 |
} |
| 393 |
|
| 394 |
tokenType = oidc.BearerToken |
| 395 |
case oidc.IDTokenType: |
| 396 |
token, err = CreateIDToken(ctx, IssuerFromContext(ctx), tokenExchangeRequest, client.IDTokenLifetime(), "", "", creator.Storage(), client) |
| 397 |
if err != nil { |
| 398 |
return nil, err |
| 399 |
} |
| 400 |
|
| 401 |
// not applicable (see https://datatracker.ietf.org/doc/html/rfc8693#section-2-2-1-2-6) |
| 402 |
tokenType = "N_A" |
| 403 |
default: |
| 404 |
// oidc.JWTTokenType and other custom token types are not supported for issuing. |
| 405 |
// In the future it can be considered to have custom tokens generation logic injected via op configuration |
| 406 |
// or via expanding Storage interface |
| 407 |
return nil, oidc.ErrInvalidRequest().WithDescription("requested_token_type is invalid") |
| 408 |
} |
| 409 |
|
| 410 |
exp := uint64(validity.Seconds()) |
| 411 |
return &oidc.TokenExchangeResponse{ |
| 412 |
AccessToken: token, |
| 413 |
IssuedTokenType: tokenExchangeRequest.GetRequestedTokenType(), |
| 414 |
TokenType: tokenType, |
| 415 |
ExpiresIn: exp, |
| 416 |
RefreshToken: refreshToken, |
| 417 |
Scopes: tokenExchangeRequest.GetScopes(), |
| 418 |
}, nil |
| 419 |
} |
| 420 |
|
| 421 |
func getTokenIDAndClaims(ctx context.Context, userinfoProvider UserinfoProvider, accessToken string) (string, string, *oidc.AccessTokenClaims, bool) { |
| 422 |
tokenIDSubject, err := userinfoProvider.Crypto().Decrypt(accessToken) |
| 423 |
if err == nil { |
| 424 |
splitToken := strings.Split(tokenIDSubject, ":") |
| 425 |
if len(splitToken) != 2 { |
| 426 |
return "", "", nil, false |
| 427 |
} |
| 428 |
|
| 429 |
return splitToken[0], splitToken[1], nil, true |
| 430 |
} |
| 431 |
accessTokenClaims, err := VerifyAccessToken[*oidc.AccessTokenClaims](ctx, accessToken, userinfoProvider.AccessTokenVerifier(ctx)) |
| 432 |
if err != nil { |
| 433 |
return "", "", nil, false |
| 434 |
} |
| 435 |
|
| 436 |
return accessTokenClaims.JWTID, accessTokenClaims.Subject, accessTokenClaims, true |
| 437 |
} |