-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_token_endpoint.go
1689 lines (1366 loc) · 66.9 KB
/
api_token_endpoint.go
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Authlete API
Authlete API Document.
API version: 2.3.12
*/
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
package authlete
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"net/url"
)
type TokenEndpointApi interface {
/*
AuthTokenApi /api/auth/token API
This API parses request parameters of an authorization request and returns necessary data for the
authorization server implementation to process the authorization request further.
<br>
<details>
<summary>Description</summary>
This API is supposed to be called from with the implementation of the token endpoint of the service.
The endpoint implementation must extract the request parameters from the token request from the
client application and pass them as the value of parameters request parameter to Authlete's `/auth/token` API.
The value of parameters is the entire entity body (which is formatted in `application/x-www-form-urlencoded`)
of the token request.
In addition, if the token endpoint of the authorization server implementation supports basic authentication
as a means of [client authentication](https://datatracker.ietf.org/doc/html/rfc6749#section-2.3),
the client credentials must be extracted from `Authorization` header and they must be passed as
`clientId` request parameter and `clientSecret` request parameter to Authlete's `/auth/token` API.
The following code snippet is an example in JAX-RS showing how to extract request parameters from
the token request and client credentials from Authorization header.
```java
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post(
@HeaderParam(HttpHeaders.AUTHORIZATION) String auth,
String parameters)
{
// Convert the value of Authorization header (credentials of
// the client application), if any, into BasicCredentials.
BasicCredentials credentials = BasicCredentials.parse(auth);
// The credentials of the client application extracted from
// 'Authorization' header. These may be null.
String clientId = credentials == null ? null
: credentials.getUserId();
String clientSecret = credentials == null ? null
: credentials.getPassword();
// Process the given parameters.
return process(parameters, clientId, clientSecret);
}
```
The response from `/auth/token` API has some parameters. Among them, it is action parameter that
the service implementation should check first because it denotes the next action that the authorization
server implementation should take. According to the value of action, the authorization server
implementation must take the steps described below.
**INTERNAL_SERVER_ERROR**
When the value of `action` is `INTERNAL_SERVER_ERROR`, it means that the request from the authorization
server implementation was wrong or that an error occurred in Authlete.
In either case, from the viewpoint of the client application, it is an error on the server side.
Therefore, the service implementation should generate a response to the client application with
HTTP status of "500 Internal Server Error". Authlete recommends `application/json` as the content
type although OAuth 2.0 specification does not mention the format of the error response when the
redirect URI is not usable.
The value of `responseContent` is a JSON string which describes the error, so it can be
used as the entity body of the response.
The following illustrates the response which the service implementation should generate and return
to the client application.
```
HTTP/1.1 500 Internal Server Error
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
The endpoint implementation may return another different response to the client application
since "500 Internal Server Error" is not required by OAuth 2.0.
**INVALID_CLIENT**
When the value of `action` is `INVALID_CLIENT`, it means that authentication of the client failed.
In this case, the HTTP status of the response to the client application is either "400 Bad Request"
or "401 Unauthorized". This requirement comes from [RFC 6749, 5.2. Error Response](https://datatracker.ietf.org/doc/html/rfc6749#section-5.2).
The description about `invalid_client` shown below is an excerpt from RFC 6749.
Client authentication failed (e.g., unknown client, no client authentication included, or unsupported
authentication method). The authorization server MAY return an HTTP 401 (Unauthorized) status code
to indicate which HTTP authentication schemes are supported. If the client attempted to authenticate
via the `Authorization` request header field, the authorization server MUST respond with an HTTP
401 (Unauthorized) status code and include the `WWW-Authenticate` response header field matching
the authentication scheme used by the client.
In either case, the value of `responseContent` is a JSON string which can be used as the entity
body of the response to the client application.
The following illustrate responses which the service implementation must generate and return to
the client application.
```
HTTP/1.1 400 Bad Request
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
<br>
```
HTTP/1.1 401 Unauthorized
WWW-Authenticate: {challenge}
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
**BAD_REQUEST**
When the value of `action` is `BAD_REQUEST`, it means that the request from the client application
is invalid.
A response with HTTP status of "400 Bad Request" must be returned to the client application and
the content type must be `application/json`.
The value of `responseContent` is a JSON string which describes the error, so it can be used as
the entity body of the response.
The following illustrates the response which the service implementation should generate and return
to the client application.
```
HTTP/1.1 400 Bad Request
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
**PASSWORD**
When the value of `"action"` is `"PASSWORD"`, it means that
the request from the client application is valid and `grant_type`
is `"password"`. That is, the flow is
<a href="https://www.rfc-editor.org/rfc/rfc6749.html#section-4.3">"Resource Owner
Password Credentials"</a>.
In this case, {@link #getUsername()} returns the value of `"username"`
request parameter and {@link #getPassword()} returns the value of {@code
"password"} request parameter which were contained in the token request
from the client application. The service implementation must validate the
credentials of the resource owner (= end-user) and take either of the
actions below according to the validation result.
1. When the credentials are valid, call Authlete's /auth/token/issue} API to generate an access token for the client
application. The API requires `"ticket"` request parameter and
`"subject"` request parameter.
Use the value returned from {@link #getTicket()} method as the value
for `"ticket"` parameter.
2. The response from `/auth/token/issue` API ({@link
TokenIssueResponse}) contains data (an access token and others)
which should be returned to the client application. Use the data
to generate a response to the client application.
3. When the credentials are invalid</b>, call Authlete's {@code
/auth/token/fail} API with `reason=`{@link
TokenFailRequest.Reason#INVALID_RESOURCE_OWNER_CREDENTIALS
INVALID_RESOURCE_OWNER_CREDENTIALS} to generate an error response
for the client application. The API requires `"ticket"`
request parameter. Use the value returned from {@link #getTicket()}
method as the value for `"ticket"` parameter.
4. The response from `/auth/token/fail` API ({@link
TokenFailResponse}) contains error information which should be
returned to the client application. Use it to generate a response
to the client application.
**OK**
When the value of `action` is `OK`, it means that the request from the client application is valid
and an access token, and optionally an ID token, is ready to be issued.
The HTTP status of the response returned to the client application must be "200 OK" and the content
type must be `application/json`.
The value of `responseContent` is a JSON string which contains an access token (and optionally
an ID token), so it can be used as the entity body of the response.
The following illustrates the response which the service implementation must generate and return
to the client application.
```
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
**TOKEN_EXCHANGE (Authlete 2.3 onwards)**
When the value of `"action"` is `"TOKEN_EXCHANGE"`, it means
that the request from the client application is a valid token exchange
request (cf. <a href="https://www.rfc-editor.org/rfc/rfc8693.html">RFC
8693 OAuth 2.0 Token Exchange</a>) and that the request has already passed
the following validation steps.
1. Confirm that the value of the `requested_token_type` request parameter
is one of the registered token type identifiers if the request parameter is
given and its value is not empty.
2. Confirm that the `subject_token` request parameter is given and its
value is not empty.
3. Confirm that the `subject_token_type` request parameter is given and
its value is one of the registered token type identifiers.
4. Confirm that the `actor_token_type` request parameter is given and
its value is one of the registered token type identifiers if the
`actor_token` request parameter is given and its value is not empty.
5. Confirm that the `actor_token_type` request parameter is not given
or its value is empty when the `actor_token` request parameter is
not given or its value is empty.
Furthermore, Authlete performs additional validation on the tokens specified
by the `subject_token` request parameter and the `actor_token`
request parameter according to their respective token types as shown below.
**Token Validation Steps**
*Token Type: `urn:ietf:params:oauth:token-type:jwt`*
1. Confirm that the format conforms to the JWT specification [RFC 7519][https://www.rfc-editor.org/rfc/rfc7519.html].
2. Check if the JWT is encrypted and if it is encrypted, then (a) reject
the token exchange request when the {@link
Service#isTokenExchangeEncryptedJwtRejected()
tokenExchangeEncryptedJwtRejected} flag of the service is `true`
or (b) skip remaining validation steps when the flag is `false`.
Note that Authlete does not verify an encrypted JWT because there is
no standard way to obtain the key to decrypt the JWT with. This means
that you must verify an encrypted JWT by yourself when one is used as
an input token with the token type
{ @code "urn:ietf:params:oauth:token-type:jwt" }.
3. Confirm that the current time has not reached the time indicated by
the `exp` claim if the JWT contains the claim.
4. Confirm that the current time is equal to or after the time indicated
by the `iat` claim if the JWT contains the claim.
5.Confirm that the current time is equal to or after the time indicated
by the `nbf` claim if the JWT contains the claim.
6. Check if the JWT is signed and if it is not signed, then (a) reject
the token exchange request when the {@link
Service#isTokenExchangeUnsignedJwtRejected()
tokenExchangeUnsignedJwtRejected} flag of the service is `true`
or (b) finish validation on the input token. Note that Authlete does
not verify the signature of the JWT because there is no standard way
to obtain the key to verify the signature of a JWT with. This means
that you must verify the signature by yourself when a signed JWT is
used as an input token with the token type
`"urn:ietf:params:oauth:token-type:jwt"`.
*Token Type: `urn:ietf:params:oauth:token-type:access_token`*
1. Confirm that the token is an access token that has been issued by
the Authlete server of your service. This implies that access
tokens issued by other systems cannot be used as a subject token
or an actor token with the token type
<code>urn:ietf:params:oauth:token-type:access_token</code>.
2. Confirm that the access token has not expired.
3. Confirm that the access token belongs to the service.
*Token Type: `urn:ietf:params:oauth:token-type:refresh_token`*
1. Confirm that the token is a refresh token that has been issued by
the Authlete server of your service. This implies that refresh
tokens issued by other systems cannot be used as a subject token
or an actor token with the token type
<code>urn:ietf:params:oauth:token-type:refresh_token</code>.
2. Confirm that the refresh token has not expired.
3. Confirm that the refresh token belongs to the service.
*Token Type: `urn:ietf:params:oauth:token-type:id_token`*
1. Confirm that the format conforms to the JWT specification (<a href=
"https://www.rfc-editor.org/rfc/rfc7519.html">RFC 7519</a>).
2. Check if the ID Token is encrypted and if it is encrypted, then (a)
reject the token exchange request when the {@link
Service#isTokenExchangeEncryptedJwtRejected()
tokenExchangeEncryptedJwtRejected} flag of the service is `true`
or (b) skip remaining validation steps when the flag is `false`.
Note that Authlete does not verify an encrypted ID Token because
there is no standard way to obtain the key to decrypt the ID Token
with in the context of token exchange where the client ID for the
encrypted ID Token cannot be determined. This means that you must
verify an encrypted ID Token by yourself when one is used as an
input token with the token type
`"urn:ietf:params:oauth:token-type:id_token"`.
3. Confirm that the ID Token contains the `exp` claim and the
current time has not reached the time indicated by the claim.
4. Confirm that the ID Token contains the `iat` claim and the
current time is equal to or after the time indicated by the claim.
5. Confirm that the current time is equal to or after the time indicated
by the `nbf` claim if the ID Token contains the claim.
6. Confirm that the ID Token contains the `iss` claim and the
value is a valid URI. In addition, confirm that the URI has the
`https` scheme, no query component and no fragment component.
7. Confirm that the ID Token contains the `aud` claim and its
value is a JSON string or an array of JSON strings.
8. Confirm that the value of the `nonce` claim is a JSON string
if the ID Token contains the claim.
9. Check if the ID Token is signed and if it is not signed, then (a)
reject the token exchange request when the {@link
Service#isTokenExchangeUnsignedJwtRejected()
tokenExchangeUnsignedJwtRejected} flag of the service is `true`
or (b) finish validation on the input token.
10. Confirm that the signature algorithm is asymmetric. This implies that
ID Tokens whose signature algorithm is symmetric (`HS256`,
`HS384` or `HS512`) cannot be used as a subject token or
an actor token with the token type
`urn:ietf:params:oauth:token-type:id_token`.
11. Verify the signature of the ID Token. Signature verification is
performed even in the case where the issuer of the ID Token is not
your service. But in that case, the issuer must support the discovery
endpoint defined in <a href=
"https://openid.net/specs/openid-connect-discovery-1_0.html">OpenID
Connect Discovery 1.0</a>. Otherwise, signature verification fails.
*Token Type: `urn:ietf:params:oauth:token-type:saml1`*
(Authlete does not perform any validation for this token type.)
*Token Type: `urn:ietf:params:oauth:token-type:saml2`*
(Authlete does not perform any validation for this token type.)
The specification of Token Exchange (<a href=
"https://www.rfc-editor.org/rfc/rfc8693.html">RFC 8693</a>) is very
flexible. In other words, the specification has abandoned the task of
determining details. Therefore, for secure token exchange, you have
to complement the specification with your own rules. For that purpose,
Authlete provides some configuration options as listed below.
Authorization server implementers may utilize them and/or implement
their own rules.
In the case of {@link Action#TOKEN_EXCHANGE TOKEN_EXCHANGE}, the {@link
#getResponseContent()} method returns `null`. You have to construct
the token response by yourself.
For example, you may generate an access token by calling Authlete's
`/api/auth/token/create` API and construct a token response like
below.
```
HTTP/1.1 401 Unauthorized
WWW-Authenticate: {challenge}
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
```
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-cache, no-store
{
"access_token": "{@link TokenCreateResponse#getAccessToken()}",
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
"token_type": "Bearer",
"expires_in": { @link TokenCreateResponse#getExpiresIn() },
"scope": "String.join(" ", {@link TokenCreateResponse#getScopes()})"
}
```
**JWT_BEARER JWT_BEARER (Authlete 2.3 onwards)**
When the value of `"action"` is `"JWT_BEARER"`, it means that
the request from the client application is a valid token request with the
grant type `"urn:ietf:params:oauth:grant-type:jwt-bearer"` (<a href=
"https://www.rfc-editor.org/rfc/rfc7523.html">RFC 7523 JSON Web Token (JWT)
Profile for OAuth 2.0 Client Authentication and Authorization Grants</a>)
and that the request has already passed the following validation steps.
1. Confirm that the `assertion` request parameter is given and its value
is not empty.
2. Confirm that the format of the assertion conforms to the JWT specification
(<a href="https://www.rfc-editor.org/rfc/rfc7519.html">RFC 7519</a>).
3. Check if the JWT is encrypted and if it is encrypted, then (a) reject the
token request when the {@link Service#isJwtGrantEncryptedJwtRejected()
jwtGrantEncryptedJwtRejected} flag of the service is `true` or (b)
skip remaining validation steps when the flag is `false`. Note that
Authlete does not verify an encrypted JWT because there is no standard way
to obtain the key to decrypt the JWT with. This means that you must verify
an encrypted JWT by yourself.
4. Confirm that the JWT contains the `iss` claim and its value is a
JSON string.
5. Confirm that the JWT contains the `sub` claim and its value is a
JSON string.
6. Confirm that the JWT contains the `aud` claim and its value is
either a JSON string or an array of JSON strings.
7. Confirm that the issuer identifier of the service (cf. {@link Service#getIssuer()})
or the URL of the token endpoint (cf. {@link Service#getTokenEndpoint()})
is listed as audience in the `aud` claim.
8. Confirm that the JWT contains the `exp` claim and the current time
has not reached the time indicated by the claim.
9. Confirm that the current time is equal to or after the time indicated by
by the `iat` claim if the JWT contains the claim.
10. Confirm that the current time is equal to or after the time indicated by
by the `nbf` claim if the JWT contains the claim.
11. Check if the JWT is signed and if it is not signed, then (a) reject the
token request when the {@link Service#isJwtGrantUnsignedJwtRejected()
jwtGrantUnsignedJwtRejected} flag of the service is `true` or (b)
finish validation on the JWT. Note that Authlete does not verify the
signature of the JWT because there is no standard way to obtain the key
to verify the signature of a JWT with. This means that you must verify
the signature by yourself.
Authlete provides some configuration options for the grant type as listed
below. Authorization server implementers may utilize them and/or implement
their own rules.
```
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-cache, no-store
{
"access_token": "{@link TokenCreateResponse#getAccessToken()}",
"token_type": "Bearer",
"expires_in": {@link TokenCreateResponse#getExpiresIn()},
"scope": "String.join(" ", {@link TokenCreateResponse#getScopes()})"
}
```
Finally, note again that Authlete does not verify the signature of the JWT
specified by the `assertion` request parameter. You must verify the
signature by yourself.
</details>
@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
@return ApiAuthTokenApiRequest
*/
AuthTokenApi(ctx context.Context) ApiAuthTokenApiRequest
// AuthTokenApiExecute executes the request
// @return TokenResponse
AuthTokenApiExecute(r ApiAuthTokenApiRequest) (*TokenResponse, *http.Response, error)
/*
AuthTokenFailApi /api/auth/token/fail API
This API generates a content of an error token response that the authorization server implementation
returns to the client application.
<br>
<details>
<summary>Description</summary>
This API is supposed to be called from within the implementation of the token endpoint of the service
in order to generate an error response to the client application.
The description of the `/auth/token` API describes the timing when this API should be called. See
the description for the case of `action=PASSWORD`.
The response from `/auth/token/fail` API has some parameters. Among them, it is `action` parameter
that the authorization server implementation should check first because it denotes the next action
that the authorization server implementation should take. According to the value of `action`, the
authorization server implementation must take the steps described below.
**INTERNAL_SERVER_ERROR**
When the value of `action` is `INTERNAL_SERVER_ERROR`, it means that the request from the authorization
server implementation was wrong or that an error occurred in Authlete.
In either case, from the viewpoint of the client application, it is an error on the server side.
Therefore, the service implementation should generate a response to the client application with
HTTP status of "500 Internal Server Error".
The value of `responseContent` is a JSON string which describes the error, so it can be used
as the entity body of the response.
The following illustrates the response which the service implementation should generate and return
to the client application.
```
HTTP/1.1 500 Internal Server Error
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
The endpoint implementation may return another different response to the client application
since "500 Internal Server Error" is not required by OAuth 2.0.
**BAD_REQUEST**
When the value of `action` is `BAD_REQUEST`, it means that Authlete's `/auth/token/fail` API successfully
generated an error response for the client application.
The HTTP status of the response returned to the client application must be "400 Bad Request" and
the content type must be `application/json`.
The value of `responseContent` is a JSON string which describes the error, so it can be used
as the entity body of the response.
The following illustrates the response which the service implementation should generate and return
to the client application.
```
HTTP/1.1 400 Bad Request
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
</details>
@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
@return ApiAuthTokenFailApiRequest
*/
AuthTokenFailApi(ctx context.Context) ApiAuthTokenFailApiRequest
// AuthTokenFailApiExecute executes the request
// @return TokenFailResponse
AuthTokenFailApiExecute(r ApiAuthTokenFailApiRequest) (*TokenFailResponse, *http.Response, error)
/*
AuthTokenIssueApi /api/auth/token/issue API
This API generates a content of a successful token response that the authorization server implementation
returns to the client application.
<br>
<details>
<summary>Description</summary>
This API is supposed to be called from within the implementation of the token endpoint of the service
in order to generate a successful response to the client application.
The description of the `/auth/token` API describes the timing when this API should be called. See
the description for the case of `action=PASSWORD`.
The response from `/auth/token/issue` API has some parameters. Among them, it is `action` parameter
that the authorization server implementation should check first because it denotes the next action
that the authorization server implementation should take. According to the value of `action`, the
authorization server implementation must take the steps described below.
**INTERNAL_SERVER_ERROR**
When the value of `action` is `INTERNAL_SERVER_ERROR`, it means that the request from the authorization
server implementation was wrong or that an error occurred in Authlete.
In either case, from the viewpoint of the client application, it is an error on the server side.
Therefore, the service implementation should generate a response to the client application with
HTTP status of "500 Internal Server Error".
The value of `responseContent` is a JSON string which describes the error, so it can be used
as the entity body of the response.
The following illustrates the response which the service implementation should generate and return
to the client application.
```
HTTP/1.1 500 Internal Server Error
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
The endpoint implementation may return another different response to the client application
since "500 Internal Server Error" is not required by OAuth 2.0.
**OK**
When the value of `action` is `OK`, it means that Authlete's `/auth/token/issue` API successfully
generated an access token.
The HTTP status of the response returned to the client application must be "200 OK" and the content
type must be`application/json`.
The value of `responseContent` is a JSON string which contains an access token, so it can be used
as the entity body of the response.
The following illustrates the response which the service implementation must generate and return
to the client application.
```
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
</details>
@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
@return ApiAuthTokenIssueApiRequest
*/
AuthTokenIssueApi(ctx context.Context) ApiAuthTokenIssueApiRequest
// AuthTokenIssueApiExecute executes the request
// @return TokenIssueResponse
AuthTokenIssueApiExecute(r ApiAuthTokenIssueApiRequest) (*TokenIssueResponse, *http.Response, error)
}
// TokenEndpointApiService TokenEndpointApi service
type TokenEndpointApiService service
type ApiAuthTokenApiRequest struct {
ctx context.Context
ApiService TokenEndpointApi
tokenRequest *TokenRequest
}
func (r ApiAuthTokenApiRequest) TokenRequest(tokenRequest TokenRequest) ApiAuthTokenApiRequest {
r.tokenRequest = &tokenRequest
return r
}
func (r ApiAuthTokenApiRequest) Execute() (*TokenResponse, *http.Response, error) {
return r.ApiService.AuthTokenApiExecute(r)
}
/*
AuthTokenApi /api/auth/token API
This API parses request parameters of an authorization request and returns necessary data for the
authorization server implementation to process the authorization request further.
<br>
<details>
<summary>Description</summary>
This API is supposed to be called from with the implementation of the token endpoint of the service.
The endpoint implementation must extract the request parameters from the token request from the
client application and pass them as the value of parameters request parameter to Authlete's `/auth/token` API.
The value of parameters is the entire entity body (which is formatted in `application/x-www-form-urlencoded`)
of the token request.
In addition, if the token endpoint of the authorization server implementation supports basic authentication
as a means of [client authentication](https://datatracker.ietf.org/doc/html/rfc6749#section-2.3),
the client credentials must be extracted from `Authorization` header and they must be passed as
`clientId` request parameter and `clientSecret` request parameter to Authlete's `/auth/token` API.
The following code snippet is an example in JAX-RS showing how to extract request parameters from
the token request and client credentials from Authorization header.
```java
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post(
@HeaderParam(HttpHeaders.AUTHORIZATION) String auth,
String parameters)
{
// Convert the value of Authorization header (credentials of
// the client application), if any, into BasicCredentials.
BasicCredentials credentials = BasicCredentials.parse(auth);
// The credentials of the client application extracted from
// 'Authorization' header. These may be null.
String clientId = credentials == null ? null
: credentials.getUserId();
String clientSecret = credentials == null ? null
: credentials.getPassword();
// Process the given parameters.
return process(parameters, clientId, clientSecret);
}
```
The response from `/auth/token` API has some parameters. Among them, it is action parameter that
the service implementation should check first because it denotes the next action that the authorization
server implementation should take. According to the value of action, the authorization server
implementation must take the steps described below.
**INTERNAL_SERVER_ERROR**
When the value of `action` is `INTERNAL_SERVER_ERROR`, it means that the request from the authorization
server implementation was wrong or that an error occurred in Authlete.
In either case, from the viewpoint of the client application, it is an error on the server side.
Therefore, the service implementation should generate a response to the client application with
HTTP status of "500 Internal Server Error". Authlete recommends `application/json` as the content
type although OAuth 2.0 specification does not mention the format of the error response when the
redirect URI is not usable.
The value of `responseContent` is a JSON string which describes the error, so it can be
used as the entity body of the response.
The following illustrates the response which the service implementation should generate and return
to the client application.
```
HTTP/1.1 500 Internal Server Error
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
The endpoint implementation may return another different response to the client application
since "500 Internal Server Error" is not required by OAuth 2.0.
**INVALID_CLIENT**
When the value of `action` is `INVALID_CLIENT`, it means that authentication of the client failed.
In this case, the HTTP status of the response to the client application is either "400 Bad Request"
or "401 Unauthorized". This requirement comes from [RFC 6749, 5.2. Error Response](https://datatracker.ietf.org/doc/html/rfc6749#section-5.2).
The description about `invalid_client` shown below is an excerpt from RFC 6749.
Client authentication failed (e.g., unknown client, no client authentication included, or unsupported
authentication method). The authorization server MAY return an HTTP 401 (Unauthorized) status code
to indicate which HTTP authentication schemes are supported. If the client attempted to authenticate
via the `Authorization` request header field, the authorization server MUST respond with an HTTP
401 (Unauthorized) status code and include the `WWW-Authenticate` response header field matching
the authentication scheme used by the client.
In either case, the value of `responseContent` is a JSON string which can be used as the entity
body of the response to the client application.
The following illustrate responses which the service implementation must generate and return to
the client application.
```
HTTP/1.1 400 Bad Request
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
<br>
```
HTTP/1.1 401 Unauthorized
WWW-Authenticate: {challenge}
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
**BAD_REQUEST**
When the value of `action` is `BAD_REQUEST`, it means that the request from the client application
is invalid.
A response with HTTP status of "400 Bad Request" must be returned to the client application and
the content type must be `application/json`.
The value of `responseContent` is a JSON string which describes the error, so it can be used as
the entity body of the response.
The following illustrates the response which the service implementation should generate and return
to the client application.
```
HTTP/1.1 400 Bad Request
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
**PASSWORD**
When the value of `"action"` is `"PASSWORD"`, it means that
the request from the client application is valid and `grant_type`
is `"password"`. That is, the flow is
<a href="https://www.rfc-editor.org/rfc/rfc6749.html#section-4.3">"Resource Owner
Password Credentials"</a>.
In this case, {@link #getUsername()} returns the value of `"username"`
request parameter and {@link #getPassword()} returns the value of {@code
"password"} request parameter which were contained in the token request
from the client application. The service implementation must validate the
credentials of the resource owner (= end-user) and take either of the
actions below according to the validation result.
1. When the credentials are valid, call Authlete's /auth/token/issue} API to generate an access token for the client
application. The API requires `"ticket"` request parameter and
`"subject"` request parameter.
Use the value returned from {@link #getTicket()} method as the value
for `"ticket"` parameter.
2. The response from `/auth/token/issue` API ({@link
TokenIssueResponse}) contains data (an access token and others)
which should be returned to the client application. Use the data
to generate a response to the client application.
3. When the credentials are invalid</b>, call Authlete's {@code
/auth/token/fail} API with `reason=`{@link
TokenFailRequest.Reason#INVALID_RESOURCE_OWNER_CREDENTIALS
INVALID_RESOURCE_OWNER_CREDENTIALS} to generate an error response
for the client application. The API requires `"ticket"`
request parameter. Use the value returned from {@link #getTicket()}
method as the value for `"ticket"` parameter.
4. The response from `/auth/token/fail` API ({@link
TokenFailResponse}) contains error information which should be
returned to the client application. Use it to generate a response
to the client application.
**OK**
When the value of `action` is `OK`, it means that the request from the client application is valid
and an access token, and optionally an ID token, is ready to be issued.
The HTTP status of the response returned to the client application must be "200 OK" and the content
type must be `application/json`.
The value of `responseContent` is a JSON string which contains an access token (and optionally
an ID token), so it can be used as the entity body of the response.
The following illustrates the response which the service implementation must generate and return
to the client application.
```
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{responseContent}
```
**TOKEN_EXCHANGE (Authlete 2.3 onwards)**
When the value of `"action"` is `"TOKEN_EXCHANGE"`, it means
that the request from the client application is a valid token exchange
request (cf. <a href="https://www.rfc-editor.org/rfc/rfc8693.html">RFC
8693 OAuth 2.0 Token Exchange</a>) and that the request has already passed
the following validation steps.
1. Confirm that the value of the `requested_token_type` request parameter
is one of the registered token type identifiers if the request parameter is
given and its value is not empty.
2. Confirm that the `subject_token` request parameter is given and its
value is not empty.
3. Confirm that the `subject_token_type` request parameter is given and
its value is one of the registered token type identifiers.
4. Confirm that the `actor_token_type` request parameter is given and
its value is one of the registered token type identifiers if the
`actor_token` request parameter is given and its value is not empty.
5. Confirm that the `actor_token_type` request parameter is not given
or its value is empty when the `actor_token` request parameter is
not given or its value is empty.
Furthermore, Authlete performs additional validation on the tokens specified
by the `subject_token` request parameter and the `actor_token`
request parameter according to their respective token types as shown below.
**Token Validation Steps**
*Token Type: `urn:ietf:params:oauth:token-type:jwt`*
1. Confirm that the format conforms to the JWT specification [RFC 7519][https://www.rfc-editor.org/rfc/rfc7519.html].
2. Check if the JWT is encrypted and if it is encrypted, then (a) reject
the token exchange request when the {@link
Service#isTokenExchangeEncryptedJwtRejected()
tokenExchangeEncryptedJwtRejected} flag of the service is `true`
or (b) skip remaining validation steps when the flag is `false`.
Note that Authlete does not verify an encrypted JWT because there is
no standard way to obtain the key to decrypt the JWT with. This means
that you must verify an encrypted JWT by yourself when one is used as
an input token with the token type
{ @code "urn:ietf:params:oauth:token-type:jwt" }.
3. Confirm that the current time has not reached the time indicated by
the `exp` claim if the JWT contains the claim.
4. Confirm that the current time is equal to or after the time indicated
by the `iat` claim if the JWT contains the claim.
5.Confirm that the current time is equal to or after the time indicated
by the `nbf` claim if the JWT contains the claim.
6. Check if the JWT is signed and if it is not signed, then (a) reject
the token exchange request when the {@link
Service#isTokenExchangeUnsignedJwtRejected()
tokenExchangeUnsignedJwtRejected} flag of the service is `true`
or (b) finish validation on the input token. Note that Authlete does
not verify the signature of the JWT because there is no standard way
to obtain the key to verify the signature of a JWT with. This means
that you must verify the signature by yourself when a signed JWT is
used as an input token with the token type
`"urn:ietf:params:oauth:token-type:jwt"`.
*Token Type: `urn:ietf:params:oauth:token-type:access_token`*
1. Confirm that the token is an access token that has been issued by
the Authlete server of your service. This implies that access
tokens issued by other systems cannot be used as a subject token
or an actor token with the token type
<code>urn:ietf:params:oauth:token-type:access_token</code>.
2. Confirm that the access token has not expired.
3. Confirm that the access token belongs to the service.
*Token Type: `urn:ietf:params:oauth:token-type:refresh_token`*
1. Confirm that the token is a refresh token that has been issued by
the Authlete server of your service. This implies that refresh
tokens issued by other systems cannot be used as a subject token
or an actor token with the token type
<code>urn:ietf:params:oauth:token-type:refresh_token</code>.
2. Confirm that the refresh token has not expired.
3. Confirm that the refresh token belongs to the service.
*Token Type: `urn:ietf:params:oauth:token-type:id_token`*
1. Confirm that the format conforms to the JWT specification (<a href=
"https://www.rfc-editor.org/rfc/rfc7519.html">RFC 7519</a>).
2. Check if the ID Token is encrypted and if it is encrypted, then (a)
reject the token exchange request when the {@link
Service#isTokenExchangeEncryptedJwtRejected()
tokenExchangeEncryptedJwtRejected} flag of the service is `true`
or (b) skip remaining validation steps when the flag is `false`.
Note that Authlete does not verify an encrypted ID Token because
there is no standard way to obtain the key to decrypt the ID Token
with in the context of token exchange where the client ID for the
encrypted ID Token cannot be determined. This means that you must
verify an encrypted ID Token by yourself when one is used as an
input token with the token type
`"urn:ietf:params:oauth:token-type:id_token"`.
3. Confirm that the ID Token contains the `exp` claim and the
current time has not reached the time indicated by the claim.
4. Confirm that the ID Token contains the `iat` claim and the
current time is equal to or after the time indicated by the claim.
5. Confirm that the current time is equal to or after the time indicated
by the `nbf` claim if the ID Token contains the claim.
6. Confirm that the ID Token contains the `iss` claim and the
value is a valid URI. In addition, confirm that the URI has the
`https` scheme, no query component and no fragment component.
7. Confirm that the ID Token contains the `aud` claim and its
value is a JSON string or an array of JSON strings.
8. Confirm that the value of the `nonce` claim is a JSON string
if the ID Token contains the claim.
9. Check if the ID Token is signed and if it is not signed, then (a)
reject the token exchange request when the {@link
Service#isTokenExchangeUnsignedJwtRejected()
tokenExchangeUnsignedJwtRejected} flag of the service is `true`
or (b) finish validation on the input token.
10. Confirm that the signature algorithm is asymmetric. This implies that
ID Tokens whose signature algorithm is symmetric (`HS256`,
`HS384` or `HS512`) cannot be used as a subject token or
an actor token with the token type
`urn:ietf:params:oauth:token-type:id_token`.
11. Verify the signature of the ID Token. Signature verification is
performed even in the case where the issuer of the ID Token is not
your service. But in that case, the issuer must support the discovery
endpoint defined in <a href=
"https://openid.net/specs/openid-connect-discovery-1_0.html">OpenID
Connect Discovery 1.0</a>. Otherwise, signature verification fails.
*Token Type: `urn:ietf:params:oauth:token-type:saml1`*
(Authlete does not perform any validation for this token type.)
*Token Type: `urn:ietf:params:oauth:token-type:saml2`*