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
use serde::Serialize;
use crate::{
core::{
EndpointUrl, Error, ErrorKind,
client_auth::{AuthenticationParams, ClientAuthentication},
dpop::AuthorizationServerDPoP,
http::HttpClient,
platform::{MaybeSend, MaybeSendSync},
},
grant::{
core::{
form::{OAuth2FormRequest, with_dpop_nonce_retry},
token_response::{RawTokenResponse, TokenResponse},
},
refresh::RefreshGrant,
},
};
/// An `OAuth2` exchange grant.
///
/// This represents an `OAuth2` grant implementation. It provides
/// the ability of the grant to provide features like parameters,
/// authentication, its `DPoP` configuration, and so forth.
pub trait OAuth2ExchangeGrant: MaybeSendSync {
/// Parameters exchanged when making the token request.
type Parameters: Clone + MaybeSendSync;
/// The request body.
type Form<'a>: MaybeSendSync + Serialize
where
Self: 'a;
/// Whether [`Self::Parameters`] may safely be submitted more than once.
///
/// `false` for grants whose parameters contain single-use credentials —
/// an authorization code (RFC 6749 §4.1.2), a device code, or a possibly
/// rotating refresh token. Replaying those not only fails but may cause
/// the authorization server to revoke tokens already issued for them.
/// [`InMemoryTokenCache`](crate::cache::InMemoryTokenCache) consumes such
/// parameters on first use instead of replaying them after a failed refresh.
fn reusable_parameters(&self) -> bool {
false
}
/// Returns the configured client ID, if the client is identified.
///
/// `None` for a grant that presents no client identification (e.g. an
/// anonymous JWT bearer or token exchange request).
fn client_id(&self) -> Option<&str>;
/// Returns the configured issuer.
fn issuer(&self) -> Option<&str>;
/// Returns the configured client auth, if the client authenticates.
///
/// `None` for a grant that does not authenticate the client to the token
/// endpoint. The grant carries its own authorization (an assertion or an
/// existing token), so client authentication is an independent, optional
/// concern (RFC 7523 §3.1, RFC 8693 §2).
fn client_auth(&self) -> Option<&dyn ClientAuthentication>;
/// Returns the bound `DPoP` thumbprint for the session.
///
/// Often bound for authorization code grants or refresh grants.
fn bound_dpop_jkt(_params: &Self::Parameters) -> Option<&str> {
None
}
/// Returns the token endpoint URL as published in authorization server
/// metadata.
///
/// This is the authorization server's canonical token endpoint, before any
/// RFC 8705 §5 mTLS-alias resolution, and the audience for
/// [`Audience::TokenEndpoint`] client assertions — which identify the
/// authorization server by its token endpoint rather than the
/// (possibly mTLS-aliased) endpoint actually contacted.
///
/// [`Audience::TokenEndpoint`]: crate::core::client_auth::Audience::TokenEndpoint
fn token_endpoint(&self) -> &EndpointUrl;
/// Returns the endpoint that token requests are actually sent to.
///
/// This is [`Self::token_endpoint`] resolved to the RFC 8705 §5 mTLS alias
/// when the grant's HTTP client uses mTLS, and so the `target_endpoint` for
/// client authentication (see [`Audience::TargetEndpoint`]). Defaults to
/// [`Self::token_endpoint`]; grants that resolve an mTLS alias override it.
///
/// [`Audience::TargetEndpoint`]: crate::core::client_auth::Audience::TargetEndpoint
fn effective_token_endpoint(&self) -> &EndpointUrl {
self.token_endpoint()
}
/// Returns the configured `DPoP` implementation (if any).
fn dpop(&self) -> &dyn AuthorizationServerDPoP;
/// Returns the HTTP client used for token requests.
fn http_client(&self) -> &dyn HttpClient;
/// Builds the body for the request.
fn build_form(&self, params: Self::Parameters) -> Self::Form<'_>;
/// Returns allowed authentication methods (formatted as in authorization server metadata).
fn allowed_auth_methods(&self) -> Option<&[String]>;
/// Returns the authentication parameters for this grant.
fn authentication_params(
&self,
) -> impl Future<Output = Result<AuthenticationParams<'_>, Error>> + MaybeSend {
async {
// No client authentication: present nothing (no credentials, and no
// `client_id`). The grant's own authorization stands on its own.
let Some(client_auth) = self.client_auth() else {
return Ok(AuthenticationParams::builder().build());
};
// Every authentication method identifies the client, so it needs a
// client ID (e.g. the basic-auth username, or the assertion subject).
let client_id = self.client_id().ok_or_else(|| {
Error::new(
ErrorKind::Auth,
"client authentication requires a client ID",
)
})?;
client_auth
.authentication_params(
client_id,
self.issuer(),
Some(self.token_endpoint()),
self.effective_token_endpoint(),
self.allowed_auth_methods(),
)
.await
}
}
/// Exchange the parameters for an access token.
fn exchange(
&self,
params: Self::Parameters,
) -> impl Future<Output = Result<TokenResponse, Error>> + MaybeSend {
async move {
let dpop_jkt = Self::bound_dpop_jkt(¶ms)
.map(ToString::to_string)
.or_else(|| self.dpop().get_current_thumbprint());
let http_client = self.http_client();
let endpoint = self.effective_token_endpoint();
let form = self.build_form(params);
let raw_token_response: RawTokenResponse = with_dpop_nonce_retry!({
let auth_params = self.authentication_params().await?;
OAuth2FormRequest::builder()
.auth_params(auth_params)
.dpop(self.dpop())
.maybe_dpop_jkt(dpop_jkt.as_deref())
.form(&form)
.uri(endpoint.as_uri())
.build()
.execute(http_client)
.await
})?;
raw_token_response
.into_token_response(dpop_jkt, crate::core::platform::SystemTime::now())
.map_err(|source| Error::new(ErrorKind::Protocol, source))
}
}
/// Creates a refresh grant from this grant's configuration.
fn to_refresh_grant(&self) -> RefreshGrant;
}