File: token_exchange.go

package info (click to toggle)
golang-github-zitadel-oidc 3.44.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,520 kB
  • sloc: makefile: 5
file content (432 lines) | stat: -rw-r--r-- 13,541 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package op

import (
	"context"
	"net/http"
	"net/url"
	"strings"
	"time"

	httphelper "github.com/zitadel/oidc/v3/pkg/http"
	"github.com/zitadel/oidc/v3/pkg/oidc"
)

type TokenExchangeRequest interface {
	GetAMR() []string
	GetAudience() []string
	GetResourses() []string
	GetAuthTime() time.Time
	GetClientID() string
	GetScopes() []string
	GetSubject() string
	GetRequestedTokenType() oidc.TokenType

	GetExchangeSubject() string
	GetExchangeSubjectTokenType() oidc.TokenType
	GetExchangeSubjectTokenIDOrToken() string
	GetExchangeSubjectTokenClaims() map[string]any

	GetExchangeActor() string
	GetExchangeActorTokenType() oidc.TokenType
	GetExchangeActorTokenIDOrToken() string
	GetExchangeActorTokenClaims() map[string]any

	SetCurrentScopes(scopes []string)
	SetRequestedTokenType(tt oidc.TokenType)
	SetSubject(subject string)
}

type tokenExchangeRequest struct {
	exchangeSubjectTokenIDOrToken string
	exchangeSubjectTokenType      oidc.TokenType
	exchangeSubject               string
	exchangeSubjectTokenClaims    map[string]any

	exchangeActorTokenIDOrToken string
	exchangeActorTokenType      oidc.TokenType
	exchangeActor               string
	exchangeActorTokenClaims    map[string]any

	resource           []string
	audience           oidc.Audience
	scopes             oidc.SpaceDelimitedArray
	requestedTokenType oidc.TokenType
	clientID           string
	authTime           time.Time
	subject            string
}

func (r *tokenExchangeRequest) GetAMR() []string {
	return []string{}
}

func (r *tokenExchangeRequest) GetAudience() []string {
	return r.audience
}

func (r *tokenExchangeRequest) GetResourses() []string {
	return r.resource
}

func (r *tokenExchangeRequest) GetAuthTime() time.Time {
	return r.authTime
}

func (r *tokenExchangeRequest) GetClientID() string {
	return r.clientID
}

func (r *tokenExchangeRequest) GetScopes() []string {
	return r.scopes
}

func (r *tokenExchangeRequest) GetRequestedTokenType() oidc.TokenType {
	return r.requestedTokenType
}

func (r *tokenExchangeRequest) GetExchangeSubject() string {
	return r.exchangeSubject
}

func (r *tokenExchangeRequest) GetExchangeSubjectTokenType() oidc.TokenType {
	return r.exchangeSubjectTokenType
}

func (r *tokenExchangeRequest) GetExchangeSubjectTokenIDOrToken() string {
	return r.exchangeSubjectTokenIDOrToken
}

func (r *tokenExchangeRequest) GetExchangeSubjectTokenClaims() map[string]any {
	return r.exchangeSubjectTokenClaims
}

func (r *tokenExchangeRequest) GetExchangeActor() string {
	return r.exchangeActor
}

func (r *tokenExchangeRequest) GetExchangeActorTokenType() oidc.TokenType {
	return r.exchangeActorTokenType
}

func (r *tokenExchangeRequest) GetExchangeActorTokenIDOrToken() string {
	return r.exchangeActorTokenIDOrToken
}

func (r *tokenExchangeRequest) GetExchangeActorTokenClaims() map[string]any {
	return r.exchangeActorTokenClaims
}

func (r *tokenExchangeRequest) GetSubject() string {
	return r.subject
}

func (r *tokenExchangeRequest) SetCurrentScopes(scopes []string) {
	r.scopes = scopes
}

func (r *tokenExchangeRequest) SetRequestedTokenType(tt oidc.TokenType) {
	r.requestedTokenType = tt
}

func (r *tokenExchangeRequest) SetSubject(subject string) {
	r.subject = subject
}

// TokenExchange handles the OAuth 2.0 token exchange grant ("urn:ietf:params:oauth:grant-type:token-exchange")
func TokenExchange(w http.ResponseWriter, r *http.Request, exchanger Exchanger) {
	ctx, span := tracer.Start(r.Context(), "TokenExchange")
	defer span.End()
	r = r.WithContext(ctx)

	tokenExchangeReq, clientID, clientSecret, err := ParseTokenExchangeRequest(r, exchanger.Decoder())
	if err != nil {
		RequestError(w, r, err, exchanger.Logger())
	}

	tokenExchangeRequest, client, err := ValidateTokenExchangeRequest(r.Context(), tokenExchangeReq, clientID, clientSecret, exchanger)
	if err != nil {
		RequestError(w, r, err, exchanger.Logger())
		return
	}
	resp, err := CreateTokenExchangeResponse(r.Context(), tokenExchangeRequest, client, exchanger)
	if err != nil {
		RequestError(w, r, err, exchanger.Logger())
		return
	}
	httphelper.MarshalJSON(w, resp)
}

// ParseTokenExchangeRequest parses the http request into oidc.TokenExchangeRequest
func ParseTokenExchangeRequest(r *http.Request, decoder httphelper.Decoder) (_ *oidc.TokenExchangeRequest, clientID, clientSecret string, err error) {
	err = r.ParseForm()
	if err != nil {
		return nil, "", "", oidc.ErrInvalidRequest().WithDescription("error parsing form").WithParent(err)
	}

	request := new(oidc.TokenExchangeRequest)
	err = decoder.Decode(request, r.Form)
	if err != nil {
		return nil, "", "", oidc.ErrInvalidRequest().WithDescription("error decoding form").WithParent(err)
	}

	var ok bool
	if clientID, clientSecret, ok = r.BasicAuth(); ok {
		clientID, err = url.QueryUnescape(clientID)
		if err != nil {
			return nil, "", "", oidc.ErrInvalidClient().WithDescription("invalid basic auth header").WithParent(err)
		}

		clientSecret, err = url.QueryUnescape(clientSecret)
		if err != nil {
			return nil, "", "", oidc.ErrInvalidClient().WithDescription("invalid basic auth header").WithParent(err)
		}
	}

	return request, clientID, clientSecret, nil
}

// ValidateTokenExchangeRequest validates the token exchange request parameters including authorization check of the client,
// subject_token and actor_token
func ValidateTokenExchangeRequest(
	ctx context.Context,
	oidcTokenExchangeRequest *oidc.TokenExchangeRequest,
	clientID, clientSecret string,
	exchanger Exchanger,
) (TokenExchangeRequest, Client, error) {
	ctx, span := tracer.Start(ctx, "ValidateTokenExchangeRequest")
	defer span.End()

	if oidcTokenExchangeRequest.SubjectToken == "" {
		return nil, nil, oidc.ErrInvalidRequest().WithDescription("subject_token missing")
	}

	if oidcTokenExchangeRequest.SubjectTokenType == "" {
		return nil, nil, oidc.ErrInvalidRequest().WithDescription("subject_token_type missing")
	}

	client, err := AuthorizeTokenExchangeClient(ctx, clientID, clientSecret, exchanger)
	if err != nil {
		return nil, nil, err
	}

	if oidcTokenExchangeRequest.RequestedTokenType != "" && !oidcTokenExchangeRequest.RequestedTokenType.IsSupported() {
		return nil, nil, oidc.ErrInvalidRequest().WithDescription("requested_token_type is not supported")
	}

	if !oidcTokenExchangeRequest.SubjectTokenType.IsSupported() {
		return nil, nil, oidc.ErrInvalidRequest().WithDescription("subject_token_type is not supported")
	}

	if oidcTokenExchangeRequest.ActorTokenType != "" && !oidcTokenExchangeRequest.ActorTokenType.IsSupported() {
		return nil, nil, oidc.ErrInvalidRequest().WithDescription("actor_token_type is not supported")
	}

	req, err := CreateTokenExchangeRequest(ctx, oidcTokenExchangeRequest, client, exchanger)
	if err != nil {
		return nil, nil, err
	}
	return req, client, nil
}

func CreateTokenExchangeRequest(
	ctx context.Context,
	oidcTokenExchangeRequest *oidc.TokenExchangeRequest,
	client Client,
	exchanger Exchanger,
) (TokenExchangeRequest, error) {
	ctx, span := tracer.Start(ctx, "CreateTokenExchangeRequest")
	defer span.End()

	teStorage, ok := exchanger.Storage().(TokenExchangeStorage)
	if !ok {
		return nil, unimplementedGrantError(oidc.GrantTypeTokenExchange)
	}

	exchangeSubjectTokenIDOrToken, exchangeSubject, exchangeSubjectTokenClaims, ok := GetTokenIDAndSubjectFromToken(ctx, exchanger,
		oidcTokenExchangeRequest.SubjectToken, oidcTokenExchangeRequest.SubjectTokenType, false)
	if !ok {
		return nil, oidc.ErrInvalidRequest().WithDescription("subject_token is invalid")
	}

	var (
		exchangeActorTokenIDOrToken, exchangeActor string
		exchangeActorTokenClaims                   map[string]any
	)
	if oidcTokenExchangeRequest.ActorToken != "" {
		exchangeActorTokenIDOrToken, exchangeActor, exchangeActorTokenClaims, ok = GetTokenIDAndSubjectFromToken(ctx, exchanger,
			oidcTokenExchangeRequest.ActorToken, oidcTokenExchangeRequest.ActorTokenType, true)
		if !ok {
			return nil, oidc.ErrInvalidRequest().WithDescription("actor_token is invalid")
		}
	}

	req := &tokenExchangeRequest{
		exchangeSubjectTokenIDOrToken: exchangeSubjectTokenIDOrToken,
		exchangeSubjectTokenType:      oidcTokenExchangeRequest.SubjectTokenType,
		exchangeSubject:               exchangeSubject,
		exchangeSubjectTokenClaims:    exchangeSubjectTokenClaims,

		exchangeActorTokenIDOrToken: exchangeActorTokenIDOrToken,
		exchangeActorTokenType:      oidcTokenExchangeRequest.ActorTokenType,
		exchangeActor:               exchangeActor,
		exchangeActorTokenClaims:    exchangeActorTokenClaims,

		subject:            exchangeSubject,
		resource:           oidcTokenExchangeRequest.Resource,
		audience:           oidcTokenExchangeRequest.Audience,
		scopes:             oidcTokenExchangeRequest.Scopes,
		requestedTokenType: oidcTokenExchangeRequest.RequestedTokenType,
		clientID:           client.GetID(),
		authTime:           time.Now(),
	}

	err := teStorage.ValidateTokenExchangeRequest(ctx, req)
	if err != nil {
		return nil, err
	}

	err = teStorage.CreateTokenExchangeRequest(ctx, req)
	if err != nil {
		return nil, err
	}

	return req, nil
}

func GetTokenIDAndSubjectFromToken(
	ctx context.Context,
	exchanger Exchanger,
	token string,
	tokenType oidc.TokenType,
	isActor bool,
) (tokenIDOrToken, subject string, claims map[string]any, ok bool) {
	ctx, span := tracer.Start(ctx, "GetTokenIDAndSubjectFromToken")
	defer span.End()

	switch tokenType {
	case oidc.AccessTokenType:
		var accessTokenClaims *oidc.AccessTokenClaims
		tokenIDOrToken, subject, accessTokenClaims, ok = getTokenIDAndClaims(ctx, exchanger, token)
		if !ok {
			break
		}
		claims = accessTokenClaims.Claims
	case oidc.RefreshTokenType:
		refreshTokenRequest, err := exchanger.Storage().TokenRequestByRefreshToken(ctx, token)
		if err != nil {
			break
		}

		tokenIDOrToken, subject, ok = token, refreshTokenRequest.GetSubject(), true
	case oidc.IDTokenType:
		idTokenClaims, err := VerifyIDTokenHint[*oidc.IDTokenClaims](ctx, token, exchanger.IDTokenHintVerifier(ctx))
		if err != nil {
			break
		}

		tokenIDOrToken, subject, claims, ok = token, idTokenClaims.Subject, idTokenClaims.Claims, true
	}

	if !ok {
		if verifier, ok := exchanger.Storage().(TokenExchangeTokensVerifierStorage); ok {
			var err error
			if isActor {
				tokenIDOrToken, subject, claims, err = verifier.VerifyExchangeActorToken(ctx, token, tokenType)
			} else {
				tokenIDOrToken, subject, claims, err = verifier.VerifyExchangeSubjectToken(ctx, token, tokenType)
			}
			if err != nil {
				return "", "", nil, false
			}

			return tokenIDOrToken, subject, claims, true
		}

		return "", "", nil, false
	}

	return tokenIDOrToken, subject, claims, true
}

// AuthorizeTokenExchangeClient authorizes a client by validating the client_id and client_secret
func AuthorizeTokenExchangeClient(ctx context.Context, clientID, clientSecret string, exchanger Exchanger) (client Client, err error) {
	ctx, span := tracer.Start(ctx, "AuthorizeTokenExchangeClient")
	defer span.End()

	if err := AuthorizeClientIDSecret(ctx, clientID, clientSecret, exchanger.Storage()); err != nil {
		return nil, err
	}

	client, err = exchanger.Storage().GetClientByClientID(ctx, clientID)
	if err != nil {
		return nil, oidc.ErrInvalidClient().WithParent(err)
	}

	return client, nil
}

func CreateTokenExchangeResponse(
	ctx context.Context,
	tokenExchangeRequest TokenExchangeRequest,
	client Client,
	creator TokenCreator,
) (_ *oidc.TokenExchangeResponse, err error) {
	ctx, span := tracer.Start(ctx, "CreateTokenExchangeResponse")
	defer span.End()

	var (
		token, refreshToken, tokenType string
		validity                       time.Duration
	)

	switch tokenExchangeRequest.GetRequestedTokenType() {
	case oidc.AccessTokenType, oidc.RefreshTokenType:
		token, refreshToken, validity, err = CreateAccessToken(ctx, tokenExchangeRequest, client.AccessTokenType(), creator, client, "")
		if err != nil {
			return nil, err
		}

		tokenType = oidc.BearerToken
	case oidc.IDTokenType:
		token, err = CreateIDToken(ctx, IssuerFromContext(ctx), tokenExchangeRequest, client.IDTokenLifetime(), "", "", creator.Storage(), client)
		if err != nil {
			return nil, err
		}

		// not applicable (see https://datatracker.ietf.org/doc/html/rfc8693#section-2-2-1-2-6)
		tokenType = "N_A"
	default:
		// oidc.JWTTokenType and other custom token types are not supported for issuing.
		// In the future it can be considered to have custom tokens generation logic injected via op configuration
		// or via expanding Storage interface
		oidc.ErrInvalidRequest().WithDescription("requested_token_type is invalid")
	}

	exp := uint64(validity.Seconds())
	return &oidc.TokenExchangeResponse{
		AccessToken:     token,
		IssuedTokenType: tokenExchangeRequest.GetRequestedTokenType(),
		TokenType:       tokenType,
		ExpiresIn:       exp,
		RefreshToken:    refreshToken,
		Scopes:          tokenExchangeRequest.GetScopes(),
	}, nil
}

func getTokenIDAndClaims(ctx context.Context, userinfoProvider UserinfoProvider, accessToken string) (string, string, *oidc.AccessTokenClaims, bool) {
	tokenIDSubject, err := userinfoProvider.Crypto().Decrypt(accessToken)
	if err == nil {
		splitToken := strings.Split(tokenIDSubject, ":")
		if len(splitToken) != 2 {
			return "", "", nil, false
		}

		return splitToken[0], splitToken[1], nil, true
	}
	accessTokenClaims, err := VerifyAccessToken[*oidc.AccessTokenClaims](ctx, accessToken, userinfoProvider.AccessTokenVerifier(ctx))
	if err != nil {
		return "", "", nil, false
	}

	return accessTokenClaims.JWTID, accessTokenClaims.Subject, accessTokenClaims, true
}