-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathclient.rs
1470 lines (1426 loc) · 42.8 KB
/
client.rs
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
use crate::{
AccessToken, AdditionalClaims, AdditionalProviderMetadata, AuthDisplay, AuthPrompt, AuthType,
AuthUrl, AuthenticationFlow, AuthorizationCode, AuthorizationRequest, ClaimName, ClaimType,
ClientAuthMethod, ClientCredentialsTokenRequest, ClientId, ClientSecret, CodeTokenRequest,
ConfigurationError, CsrfToken, DeviceAccessTokenRequest, DeviceAuthorizationRequest,
DeviceAuthorizationResponse, DeviceAuthorizationUrl, EndpointMaybeSet, EndpointNotSet,
EndpointSet, EndpointState, ErrorResponse, ExtraDeviceAuthorizationFields, GenderClaim,
GrantType, IdTokenVerifier, IntrospectionRequest, IntrospectionUrl, IssuerUrl, JsonWebKey,
JsonWebKeySet, JweContentEncryptionAlgorithm, JweKeyManagementAlgorithm, JwsSigningAlgorithm,
Nonce, PasswordTokenRequest, ProviderMetadata, RedirectUrl, RefreshToken, RefreshTokenRequest,
ResourceOwnerPassword, ResourceOwnerUsername, ResponseMode, ResponseType, RevocableToken,
RevocationRequest, RevocationUrl, Scope, SubjectIdentifier, SubjectIdentifierType,
TokenIntrospectionResponse, TokenResponse, TokenUrl, UserInfoRequest, UserInfoUrl,
};
use std::marker::PhantomData;
const OPENID_SCOPE: &str = "openid";
/// OpenID Connect client.
///
/// # Error Types
///
/// To enable compile time verification that only the correct and complete set of errors for the `Client` function being
/// invoked are exposed to the caller, the `Client` type is specialized on multiple implementations of the
/// [`ErrorResponse`] trait. The exact [`ErrorResponse`] implementation returned varies by the RFC that the invoked
/// `Client` function implements:
///
/// - Generic type `TE` (aka Token Error) for errors defined by [RFC 6749 OAuth 2.0 Authorization Framework](https://tools.ietf.org/html/rfc6749).
/// - Generic type `TRE` (aka Token Revocation Error) for errors defined by [RFC 7009 OAuth 2.0 Token Revocation](https://tools.ietf.org/html/rfc7009).
///
/// For example when revoking a token, error code `unsupported_token_type` (from RFC 7009) may be returned:
/// ```rust
/// # use http::status::StatusCode;
/// # use http::header::{HeaderValue, CONTENT_TYPE};
/// # use openidconnect::core::CoreClient;
/// # use openidconnect::{
/// # AccessToken,
/// # AuthUrl,
/// # ClientId,
/// # ClientSecret,
/// # HttpResponse,
/// # IssuerUrl,
/// # JsonWebKeySet,
/// # RequestTokenError,
/// # RevocationErrorResponseType,
/// # RevocationUrl,
/// # TokenUrl,
/// # };
/// # use thiserror::Error;
/// #
/// # let client =
/// # CoreClient::new(
/// # ClientId::new("aaa".to_string()),
/// # IssuerUrl::new("https://example".to_string()).unwrap(),
/// # JsonWebKeySet::default(),
/// # )
/// # .set_client_secret(ClientSecret::new("bbb".to_string()))
/// # .set_auth_uri(AuthUrl::new("https://example/authorize".to_string()).unwrap())
/// # .set_token_uri(TokenUrl::new("https://example/token".to_string()).unwrap())
/// # .set_revocation_url(RevocationUrl::new("https://revocation/url".to_string()).unwrap());
/// #
/// # #[derive(Debug, Error)]
/// # enum FakeError {
/// # #[error("error")]
/// # Err,
/// # }
/// #
/// # let http_client = |_| -> Result<HttpResponse, FakeError> {
/// # Ok(http::Response::builder()
/// # .status(StatusCode::BAD_REQUEST)
/// # .header(CONTENT_TYPE, HeaderValue::from_str("application/json").unwrap())
/// # .body(
/// # r#"{"error": "unsupported_token_type",
/// # "error_description": "stuff happened",
/// # "error_uri": "https://errors"}"#
/// # .to_string()
/// # .into_bytes(),
/// # )
/// # .unwrap())
/// # };
/// #
/// let res = client
/// .revoke_token(AccessToken::new("some token".to_string()).into())
/// .unwrap()
/// .request(&http_client);
///
/// assert!(matches!(res, Err(
/// RequestTokenError::ServerResponse(err)) if matches!(err.error(),
/// RevocationErrorResponseType::UnsupportedTokenType)));
/// ```
#[derive(Clone, Debug)]
pub struct Client<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
HasTokenUrl,
HasUserInfoUrl,
> where
AC: AdditionalClaims,
AD: AuthDisplay,
GC: GenderClaim,
JE: JweContentEncryptionAlgorithm<
KeyType = <K::SigningAlgorithm as JwsSigningAlgorithm>::KeyType,
>,
K: JsonWebKey,
P: AuthPrompt,
TE: ErrorResponse,
TR: TokenResponse<AC, GC, JE, K::SigningAlgorithm>,
TIR: TokenIntrospectionResponse,
RT: RevocableToken,
TRE: ErrorResponse,
HasAuthUrl: EndpointState,
HasDeviceAuthUrl: EndpointState,
HasIntrospectionUrl: EndpointState,
HasRevocationUrl: EndpointState,
HasTokenUrl: EndpointState,
HasUserInfoUrl: EndpointState,
{
oauth2_client: oauth2::Client<
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
HasTokenUrl,
>,
pub(crate) client_id: ClientId,
client_secret: Option<ClientSecret>,
pub(crate) issuer: IssuerUrl,
userinfo_endpoint: Option<UserInfoUrl>,
pub(crate) jwks: JsonWebKeySet<K>,
id_token_signing_algs: Option<Vec<K::SigningAlgorithm>>,
use_openid_scope: bool,
_phantom: PhantomData<(AC, AD, GC, JE, P, HasUserInfoUrl)>,
}
impl<AC, AD, GC, JE, K, P, TE, TR, TIR, RT, TRE>
Client<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
EndpointNotSet,
EndpointNotSet,
EndpointNotSet,
EndpointNotSet,
EndpointNotSet,
EndpointNotSet,
>
where
AC: AdditionalClaims,
AD: AuthDisplay,
GC: GenderClaim,
JE: JweContentEncryptionAlgorithm<
KeyType = <K::SigningAlgorithm as JwsSigningAlgorithm>::KeyType,
>,
K: JsonWebKey,
P: AuthPrompt,
TE: ErrorResponse + 'static,
TR: TokenResponse<AC, GC, JE, K::SigningAlgorithm>,
TIR: TokenIntrospectionResponse,
RT: RevocableToken,
TRE: ErrorResponse + 'static,
{
/// Initialize an OpenID Connect client.
pub fn new(client_id: ClientId, issuer: IssuerUrl, jwks: JsonWebKeySet<K>) -> Self {
Client {
oauth2_client: oauth2::Client::new(client_id.clone()),
client_id,
client_secret: None,
issuer,
userinfo_endpoint: None,
jwks,
id_token_signing_algs: None,
use_openid_scope: true,
_phantom: PhantomData,
}
}
}
impl<AC, AD, GC, JE, K, P, TE, TR, TIR, RT, TRE>
Client<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
EndpointSet,
EndpointNotSet,
EndpointNotSet,
EndpointNotSet,
EndpointMaybeSet,
EndpointMaybeSet,
>
where
AC: AdditionalClaims,
AD: AuthDisplay,
GC: GenderClaim,
JE: JweContentEncryptionAlgorithm<
KeyType = <K::SigningAlgorithm as JwsSigningAlgorithm>::KeyType,
>,
K: JsonWebKey,
P: AuthPrompt,
TE: ErrorResponse + 'static,
TR: TokenResponse<AC, GC, JE, K::SigningAlgorithm>,
TIR: TokenIntrospectionResponse,
RT: RevocableToken,
TRE: ErrorResponse + 'static,
{
/// Initialize an OpenID Connect client from OpenID Connect Discovery provider metadata.
///
/// Use [`ProviderMetadata::discover`] or
/// [`ProviderMetadata::discover_async`] to fetch the provider metadata.
pub fn from_provider_metadata<A, CA, CN, CT, G, JK, RM, RS, S>(
provider_metadata: ProviderMetadata<A, AD, CA, CN, CT, G, JE, JK, K, RM, RS, S>,
client_id: ClientId,
client_secret: Option<ClientSecret>,
) -> Self
where
A: AdditionalProviderMetadata,
CA: ClientAuthMethod,
CN: ClaimName,
CT: ClaimType,
G: GrantType,
JK: JweKeyManagementAlgorithm,
RM: ResponseMode,
RS: ResponseType,
S: SubjectIdentifierType,
{
let mut oauth2_client = oauth2::Client::new(client_id.clone())
.set_auth_uri(provider_metadata.authorization_endpoint().clone())
.set_token_uri_option(provider_metadata.token_endpoint().cloned());
if let Some(ref client_secret) = client_secret {
oauth2_client = oauth2_client.set_client_secret(client_secret.to_owned());
}
Client {
oauth2_client,
client_id,
client_secret,
issuer: provider_metadata.issuer().clone(),
userinfo_endpoint: provider_metadata.userinfo_endpoint().cloned(),
jwks: provider_metadata.jwks().to_owned(),
id_token_signing_algs: Some(
provider_metadata
.id_token_signing_alg_values_supported()
.to_owned(),
),
use_openid_scope: true,
_phantom: PhantomData,
}
}
}
impl<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
HasTokenUrl,
HasUserInfoUrl,
>
Client<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
HasTokenUrl,
HasUserInfoUrl,
>
where
AC: AdditionalClaims,
AD: AuthDisplay,
GC: GenderClaim,
JE: JweContentEncryptionAlgorithm<
KeyType = <K::SigningAlgorithm as JwsSigningAlgorithm>::KeyType,
>,
K: JsonWebKey,
P: AuthPrompt,
TE: ErrorResponse + 'static,
TR: TokenResponse<AC, GC, JE, K::SigningAlgorithm>,
TIR: TokenIntrospectionResponse,
RT: RevocableToken,
TRE: ErrorResponse + 'static,
HasAuthUrl: EndpointState,
HasDeviceAuthUrl: EndpointState,
HasIntrospectionUrl: EndpointState,
HasRevocationUrl: EndpointState,
HasTokenUrl: EndpointState,
HasUserInfoUrl: EndpointState,
{
/// Set the type of client authentication used for communicating with the authorization
/// server.
///
/// The default is to use HTTP Basic authentication, as recommended in
/// [Section 2.3.1 of RFC 6749](https://tools.ietf.org/html/rfc6749#section-2.3.1). Note that
/// if a client secret is omitted (i.e., [`set_client_secret()`](Self::set_client_secret) is not
/// called), [`AuthType::RequestBody`] is used regardless of the `auth_type` passed to
/// this function.
pub fn set_auth_type(mut self, auth_type: AuthType) -> Self {
self.oauth2_client = self.oauth2_client.set_auth_type(auth_type);
self
}
/// Return the type of client authentication used for communicating with the authorization
/// server.
pub fn auth_type(&self) -> &AuthType {
self.oauth2_client.auth_type()
}
/// Set the authorization endpoint.
///
/// The client uses the authorization endpoint to obtain authorization from the resource owner
/// via user-agent redirection. This URL is used in all standard OAuth2 flows except the
/// [Resource Owner Password Credentials Grant](https://tools.ietf.org/html/rfc6749#section-4.3)
/// and the [Client Credentials Grant](https://tools.ietf.org/html/rfc6749#section-4.4).
pub fn set_auth_uri(
self,
auth_uri: AuthUrl,
) -> Client<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
EndpointSet,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
HasTokenUrl,
HasUserInfoUrl,
> {
Client {
oauth2_client: self.oauth2_client.set_auth_uri(auth_uri),
client_id: self.client_id,
client_secret: self.client_secret,
issuer: self.issuer,
userinfo_endpoint: self.userinfo_endpoint,
jwks: self.jwks,
id_token_signing_algs: self.id_token_signing_algs,
use_openid_scope: self.use_openid_scope,
_phantom: PhantomData,
}
}
/// Return the Client ID.
pub fn client_id(&self) -> &ClientId {
&self.client_id
}
/// Set the client secret.
///
/// A client secret is generally used for confidential (i.e., server-side) OAuth2 clients and
/// omitted from public (browser or native app) OAuth2 clients (see
/// [RFC 8252](https://tools.ietf.org/html/rfc8252)).
pub fn set_client_secret(mut self, client_secret: ClientSecret) -> Self {
self.oauth2_client = self.oauth2_client.set_client_secret(client_secret.clone());
self.client_secret = Some(client_secret);
self
}
/// Set the [RFC 8628](https://tools.ietf.org/html/rfc8628) device authorization endpoint used
/// for the Device Authorization Flow.
///
/// See [`exchange_device_code()`](Self::exchange_device_code).
pub fn set_device_authorization_url(
self,
device_authorization_url: DeviceAuthorizationUrl,
) -> Client<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
EndpointSet,
HasIntrospectionUrl,
HasRevocationUrl,
HasTokenUrl,
HasUserInfoUrl,
> {
Client {
oauth2_client: self
.oauth2_client
.set_device_authorization_url(device_authorization_url),
client_id: self.client_id,
client_secret: self.client_secret,
issuer: self.issuer,
userinfo_endpoint: self.userinfo_endpoint,
jwks: self.jwks,
id_token_signing_algs: self.id_token_signing_algs,
use_openid_scope: self.use_openid_scope,
_phantom: PhantomData,
}
}
/// Set the [RFC 7662](https://tools.ietf.org/html/rfc7662) introspection endpoint.
///
/// See [`introspect()`](Self::introspect).
pub fn set_introspection_url(
self,
introspection_url: IntrospectionUrl,
) -> Client<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
HasDeviceAuthUrl,
EndpointSet,
HasRevocationUrl,
HasTokenUrl,
HasUserInfoUrl,
> {
Client {
oauth2_client: self.oauth2_client.set_introspection_url(introspection_url),
client_id: self.client_id,
client_secret: self.client_secret,
issuer: self.issuer,
userinfo_endpoint: self.userinfo_endpoint,
jwks: self.jwks,
id_token_signing_algs: self.id_token_signing_algs,
use_openid_scope: self.use_openid_scope,
_phantom: PhantomData,
}
}
/// Set the redirect URL used by the authorization endpoint.
pub fn set_redirect_uri(mut self, redirect_url: RedirectUrl) -> Self {
self.oauth2_client = self.oauth2_client.set_redirect_uri(redirect_url);
self
}
/// Return the redirect URL used by the authorization endpoint.
pub fn redirect_uri(&self) -> Option<&RedirectUrl> {
self.oauth2_client.redirect_uri()
}
/// Set the [RFC 7009](https://tools.ietf.org/html/rfc7009) revocation endpoint.
///
/// See [`revoke_token()`](Self::revoke_token).
pub fn set_revocation_url(
self,
revocation_uri: RevocationUrl,
) -> Client<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
HasDeviceAuthUrl,
HasIntrospectionUrl,
EndpointSet,
HasTokenUrl,
HasUserInfoUrl,
> {
Client {
oauth2_client: self.oauth2_client.set_revocation_url(revocation_uri),
client_id: self.client_id,
client_secret: self.client_secret,
issuer: self.issuer,
userinfo_endpoint: self.userinfo_endpoint,
jwks: self.jwks,
id_token_signing_algs: self.id_token_signing_algs,
use_openid_scope: self.use_openid_scope,
_phantom: PhantomData,
}
}
/// Set the token endpoint.
///
/// The client uses the token endpoint to exchange an authorization code for an access token,
/// typically with client authentication. This URL is used in
/// all standard OAuth2 flows except the
/// [Implicit Grant](https://tools.ietf.org/html/rfc6749#section-4.2).
pub fn set_token_uri(
self,
token_uri: TokenUrl,
) -> Client<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
EndpointSet,
HasUserInfoUrl,
> {
Client {
oauth2_client: self.oauth2_client.set_token_uri(token_uri),
client_id: self.client_id,
client_secret: self.client_secret,
issuer: self.issuer,
userinfo_endpoint: self.userinfo_endpoint,
jwks: self.jwks,
id_token_signing_algs: self.id_token_signing_algs,
use_openid_scope: self.use_openid_scope,
_phantom: PhantomData,
}
}
/// Set the user info endpoint.
///
/// See [`user_info()`](Self::user_info).
pub fn set_user_info_url(
self,
userinfo_endpoint: UserInfoUrl,
) -> Client<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
HasTokenUrl,
EndpointSet,
> {
Client {
oauth2_client: self.oauth2_client,
client_id: self.client_id,
client_secret: self.client_secret,
issuer: self.issuer,
userinfo_endpoint: Some(userinfo_endpoint),
jwks: self.jwks,
id_token_signing_algs: self.id_token_signing_algs,
use_openid_scope: self.use_openid_scope,
_phantom: PhantomData,
}
}
/// Enable the `openid` scope to be requested automatically.
///
/// This scope is requested by default, so this function is only useful after previous calls to
/// [`disable_openid_scope`][Client::disable_openid_scope].
pub fn enable_openid_scope(mut self) -> Self {
self.use_openid_scope = true;
self
}
/// Disable the `openid` scope from being requested automatically.
pub fn disable_openid_scope(mut self) -> Self {
self.use_openid_scope = false;
self
}
/// Return an ID token verifier for use with the [`IdToken::claims`](crate::IdToken::claims)
/// method.
pub fn id_token_verifier(&self) -> IdTokenVerifier<K> {
let verifier = if let Some(ref client_secret) = self.client_secret {
IdTokenVerifier::new_confidential_client(
self.client_id.clone(),
client_secret.clone(),
self.issuer.clone(),
self.jwks.clone(),
)
} else {
IdTokenVerifier::new_public_client(
self.client_id.clone(),
self.issuer.clone(),
self.jwks.clone(),
)
};
if let Some(id_token_signing_algs) = self.id_token_signing_algs.clone() {
verifier.set_allowed_algs(id_token_signing_algs)
} else {
verifier
}
}
}
/// Methods requiring an authorization endpoint.
impl<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
HasTokenUrl,
HasUserInfoUrl,
>
Client<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
EndpointSet,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
HasTokenUrl,
HasUserInfoUrl,
>
where
AC: AdditionalClaims,
AD: AuthDisplay,
GC: GenderClaim,
JE: JweContentEncryptionAlgorithm<
KeyType = <K::SigningAlgorithm as JwsSigningAlgorithm>::KeyType,
>,
K: JsonWebKey,
P: AuthPrompt,
TE: ErrorResponse + 'static,
TR: TokenResponse<AC, GC, JE, K::SigningAlgorithm>,
TIR: TokenIntrospectionResponse,
RT: RevocableToken,
TRE: ErrorResponse + 'static,
HasDeviceAuthUrl: EndpointState,
HasIntrospectionUrl: EndpointState,
HasRevocationUrl: EndpointState,
HasTokenUrl: EndpointState,
HasUserInfoUrl: EndpointState,
{
/// Return the authorization endpoint.
pub fn auth_uri(&self) -> &AuthUrl {
self.oauth2_client.auth_uri()
}
/// Generate an authorization URL for a new authorization request.
///
/// Requires [`set_auth_uri()`](Self::set_auth_uri) to have been previously
/// called to set the authorization endpoint.
///
/// NOTE: [Passing authorization request parameters as a JSON Web Token
/// ](https://openid.net/specs/openid-connect-core-1_0.html#JWTRequests)
/// instead of URL query parameters is not currently supported. The
/// [`claims` parameter](https://openid.net/specs/openid-connect-core-1_0.html#ClaimsParameter)
/// is also not directly supported, although the [`AuthorizationRequest::add_extra_param`]
/// method can be used to add custom parameters, including `claims`.
///
/// # Arguments
///
/// * `authentication_flow` - The authentication flow to use (code, implicit, or hybrid).
/// * `state_fn` - A function that returns an opaque value used by the client to maintain state
/// between the request and callback. The authorization server includes this value when
/// redirecting the user-agent back to the client.
/// * `nonce_fn` - Similar to `state_fn`, but used to generate an opaque nonce to be used
/// when verifying the ID token returned by the OpenID Connect Provider.
///
/// # Security Warning
///
/// Callers should use a fresh, unpredictable `state` for each authorization request and verify
/// that this value matches the `state` parameter passed by the authorization server to the
/// redirect URI. Doing so mitigates
/// [Cross-Site Request Forgery](https://tools.ietf.org/html/rfc6749#section-10.12)
/// attacks.
///
/// Similarly, callers should use a fresh, unpredictable `nonce` to help protect against ID
/// token reuse and forgery.
pub fn authorize_url<NF, RS, SF>(
&self,
authentication_flow: AuthenticationFlow<RS>,
state_fn: SF,
nonce_fn: NF,
) -> AuthorizationRequest<AD, P, RS>
where
NF: FnOnce() -> Nonce + 'static,
RS: ResponseType,
SF: FnOnce() -> CsrfToken + 'static,
{
let request = AuthorizationRequest {
inner: self.oauth2_client.authorize_url(state_fn),
acr_values: Vec::new(),
authentication_flow,
claims_locales: Vec::new(),
display: None,
id_token_hint: None,
login_hint: None,
max_age: None,
nonce: nonce_fn(),
prompts: Vec::new(),
ui_locales: Vec::new(),
};
if self.use_openid_scope {
request.add_scope(Scope::new(OPENID_SCOPE.to_string()))
} else {
request
}
}
}
/// Methods requiring a token endpoint.
impl<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
HasUserInfoUrl,
>
Client<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
EndpointSet,
HasUserInfoUrl,
>
where
AC: AdditionalClaims,
AD: AuthDisplay,
GC: GenderClaim,
JE: JweContentEncryptionAlgorithm<
KeyType = <K::SigningAlgorithm as JwsSigningAlgorithm>::KeyType,
>,
K: JsonWebKey,
P: AuthPrompt,
TE: ErrorResponse + 'static,
TR: TokenResponse<AC, GC, JE, K::SigningAlgorithm>,
TIR: TokenIntrospectionResponse,
RT: RevocableToken,
TRE: ErrorResponse + 'static,
HasAuthUrl: EndpointState,
HasDeviceAuthUrl: EndpointState,
HasIntrospectionUrl: EndpointState,
HasRevocationUrl: EndpointState,
HasUserInfoUrl: EndpointState,
{
/// Request an access token using the
/// [Client Credentials Flow](https://datatracker.ietf.org/doc/html/rfc6749#section-4.4).
///
/// Requires [`set_token_uri()`](Self::set_token_uri) to have been previously
/// called to set the token endpoint.
pub fn exchange_client_credentials(&self) -> ClientCredentialsTokenRequest<TE, TR> {
self.oauth2_client.exchange_client_credentials()
}
/// Exchange a code returned during the
/// [Authorization Code Flow](https://datatracker.ietf.org/doc/html/rfc6749#section-4.1)
/// for an access token.
///
/// Acquires ownership of the `code` because authorization codes may only be used once to
/// retrieve an access token from the authorization server.
///
/// Requires [`set_token_uri()`](Self::set_token_uri) to have been previously
/// called to set the token endpoint.
pub fn exchange_code(&self, code: AuthorizationCode) -> CodeTokenRequest<TE, TR> {
self.oauth2_client.exchange_code(code)
}
/// Exchange an [RFC 8628](https://tools.ietf.org/html/rfc8628#section-3.2) Device Authorization
/// Response returned by [`exchange_device_code()`](Self::exchange_device_code) for an access
/// token.
///
/// Requires [`set_token_uri()`](Self::set_token_uri) to have been previously
/// called to set the token endpoint.
pub fn exchange_device_access_token<'a, EF>(
&'a self,
auth_response: &'a DeviceAuthorizationResponse<EF>,
) -> DeviceAccessTokenRequest<'a, 'static, TR, EF>
where
EF: ExtraDeviceAuthorizationFields,
{
self.oauth2_client
.exchange_device_access_token(auth_response)
}
/// Request an access token using the
/// [Resource Owner Password Credentials Flow](https://datatracker.ietf.org/doc/html/rfc6749#section-4.3).
///
/// Requires
/// [`set_token_uri()`](Self::set_token_uri) to have
/// been previously called to set the token endpoint.
pub fn exchange_password<'a>(
&'a self,
username: &'a ResourceOwnerUsername,
password: &'a ResourceOwnerPassword,
) -> PasswordTokenRequest<'a, TE, TR> {
self.oauth2_client.exchange_password(username, password)
}
/// Exchange a refresh token for an access token.
///
/// See <https://tools.ietf.org/html/rfc6749#section-6>.
///
/// Requires
/// [`set_token_uri()`](Self::set_token_uri) to have
/// been previously called to set the token endpoint.
pub fn exchange_refresh_token<'a>(
&'a self,
refresh_token: &'a RefreshToken,
) -> RefreshTokenRequest<'a, TE, TR> {
self.oauth2_client.exchange_refresh_token(refresh_token)
}
/// Return the token endpoint.
pub fn token_uri(&self) -> &TokenUrl {
self.oauth2_client.token_uri()
}
}
/// Methods with a possibly-set token endpoint after calling
/// [`from_provider_metadata()`](Self::from_provider_metadata).
impl<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
HasUserInfoUrl,
>
Client<
AC,
AD,
GC,
JE,
K,
P,
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
EndpointMaybeSet,
HasUserInfoUrl,
>
where
AC: AdditionalClaims,
AD: AuthDisplay,
GC: GenderClaim,
JE: JweContentEncryptionAlgorithm<
KeyType = <K::SigningAlgorithm as JwsSigningAlgorithm>::KeyType,
>,
K: JsonWebKey,
P: AuthPrompt,
TE: ErrorResponse + 'static,
TR: TokenResponse<AC, GC, JE, K::SigningAlgorithm>,
TIR: TokenIntrospectionResponse,
RT: RevocableToken,
TRE: ErrorResponse + 'static,
HasAuthUrl: EndpointState,
HasDeviceAuthUrl: EndpointState,
HasIntrospectionUrl: EndpointState,
HasRevocationUrl: EndpointState,
HasUserInfoUrl: EndpointState,
{
/// Request an access token using the
/// [Client Credentials Flow](https://datatracker.ietf.org/doc/html/rfc6749#section-4.4).
///
/// Requires [`from_provider_metadata()`](Self::from_provider_metadata) to have been previously
/// called to construct the client.
pub fn exchange_client_credentials(
&self,
) -> Result<ClientCredentialsTokenRequest<TE, TR>, ConfigurationError> {
self.oauth2_client.exchange_client_credentials()
}
/// Exchange a code returned during the
/// [Authorization Code Flow](https://datatracker.ietf.org/doc/html/rfc6749#section-4.1)
/// for an access token.
///
/// Acquires ownership of the `code` because authorization codes may only be used once to
/// retrieve an access token from the authorization server.
///
/// Requires [`from_provider_metadata()`](Self::from_provider_metadata) to have been previously