huskarl 0.9.1

A modern OAuth2 client library.
Documentation
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
//! Token exchange grant (RFC 8693).
//!
//! Used to issue a new security token by exchanging an existing token, supporting
//! impersonation and delegation without requiring user re-authentication. The
//! existing token is the grant's authorization, so client authentication is
//! optional.
//!
//! See the [token exchange how-to guide](crate::_docs::guide::token_exchange) for
//! step-by-step setup.

use std::sync::Arc;

use bon::Builder;
use serde::Serialize;

use crate::{
    cache::GrantParametersSource,
    core::{
        EndpointUrl, Error,
        client_auth::ClientAuthentication,
        dpop::{AuthorizationServerDPoP, NoDPoP},
        http::HttpClient,
        platform::MaybeSendBoxFuture,
        secrets::SecretString,
    },
    grant::{
        core::{OAuth2ExchangeGrant, join_space},
        refresh::RefreshGrant,
    },
};

/// An `OAuth2` token exchange grant.
///
/// This grant is used to issue a new security token by exchanging an existing
/// token, without requiring user re-authentication. It supports impersonation
/// and delegation use cases by allowing the exchange of one token type for another.
///
/// See the [module documentation][crate::grant::token_exchange] for a usage guide.
#[huskarl_macros::from_metadata(metadata = crate::core::server_metadata::AuthorizationServerMetadata)]
#[derive(Builder)]
#[builder(on(String, into))]
pub struct TokenExchangeGrant {
    /// The client ID. Optional: omit it for an unidentified client (RFC 8693 §2
    /// leaves client identification to the authorization server's discretion).
    client_id: Option<String>,

    /// The HTTP client used for token requests.
    #[builder(with = |client: impl HttpClient + 'static| Arc::new(client) as Arc<dyn HttpClient>)]
    http_client: Arc<dyn HttpClient>,

    /// The client authentication method. Optional — the subject token is the
    /// grant. Omit it to send no client credentials, supply
    /// [`NoAuth`](crate::core::client_auth::NoAuth) to send the `client_id`
    /// without credentials, or any other [`ClientAuthentication`] to
    /// authenticate.
    #[builder(with = |auth: impl ClientAuthentication + 'static| Arc::new(auth) as Arc<dyn ClientAuthentication>)]
    client_auth: Option<Arc<dyn ClientAuthentication>>,

    /// The `DPoP` signer. Defaults to [`NoDPoP`] (no token sender-constraining).
    #[builder(
        with = |dpop: impl AuthorizationServerDPoP + 'static| Arc::new(dpop) as Arc<dyn AuthorizationServerDPoP>,
        default = Arc::new(NoDPoP),
    )]
    dpop: Arc<dyn AuthorizationServerDPoP>,

    /// The issuer for tokens created by the authorization server.
    #[from_metadata(path = "issuer")]
    issuer: Option<String>,

    /// The URL of the token endpoint.
    #[from_metadata(path = "token_endpoint")]
    token_endpoint: EndpointUrl,

    /// The mTLS alias for the token endpoint (RFC 8705 §5).
    #[from_metadata(path = "mtls_endpoint_aliases?.token_endpoint?")]
    mtls_token_endpoint: Option<EndpointUrl>,

    /// The endpoint used for token requests: the mTLS alias when the HTTP
    /// client uses mTLS, the primary token endpoint otherwise.
    #[builder(skip = crate::grant::core::resolve_mtls_alias(http_client.as_ref(), &token_endpoint, mtls_token_endpoint.as_ref()))]
    effective_token_endpoint: EndpointUrl,

    /// Supported endpoint auth methods; used to auto-select basic or
    /// form auth for client secrets.
    #[from_metadata(path = "token_endpoint_auth_methods_supported")]
    token_endpoint_auth_methods_supported: Option<Vec<String>>,
}

impl core::fmt::Debug for TokenExchangeGrant {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("TokenExchangeGrant")
            .field("client_id", &self.client_id)
            .field("issuer", &self.issuer)
            .field("token_endpoint", &self.token_endpoint)
            .field("mtls_token_endpoint", &self.mtls_token_endpoint)
            .finish_non_exhaustive()
    }
}

impl OAuth2ExchangeGrant for TokenExchangeGrant {
    type Parameters = TokenExchangeGrantParameters;
    type Form<'a> = TokenExchangeGrantForm;

    fn client_id(&self) -> Option<&str> {
        self.client_id.as_deref()
    }

    fn issuer(&self) -> Option<&str> {
        self.issuer.as_deref()
    }

    fn client_auth(&self) -> Option<&dyn ClientAuthentication> {
        self.client_auth.as_deref()
    }

    fn token_endpoint(&self) -> &EndpointUrl {
        &self.token_endpoint
    }

    fn effective_token_endpoint(&self) -> &EndpointUrl {
        &self.effective_token_endpoint
    }

    fn dpop(&self) -> &dyn AuthorizationServerDPoP {
        self.dpop.as_ref()
    }

    fn http_client(&self) -> &dyn HttpClient {
        self.http_client.as_ref()
    }

    fn allowed_auth_methods(&self) -> Option<&[String]> {
        self.token_endpoint_auth_methods_supported.as_deref()
    }

    fn to_refresh_grant(&self) -> RefreshGrant {
        RefreshGrant::builder()
            .maybe_client_id(self.client_id.clone())
            .maybe_issuer(self.issuer.clone())
            .http_client(self.http_client.clone())
            .maybe_client_auth(self.client_auth.clone())
            .dpop(self.dpop.clone())
            .token_endpoint(self.effective_token_endpoint.clone())
            .maybe_token_endpoint_auth_methods_supported(
                self.token_endpoint_auth_methods_supported.clone(),
            )
            .build()
    }

    fn build_form(&self, params: Self::Parameters) -> Self::Form<'_> {
        TokenExchangeGrantForm {
            grant_type: "urn:ietf:params:oauth:grant-type:token-exchange",
            resource: params.resource,
            authorization_details: params.authorization_details,
            audience: params.audience,
            scope: join_space(params.scope.as_deref()),
            requested_token_type: params.requested_token_type,
            subject_token: params.subject.token,
            subject_token_type: params.subject.token_type,
            actor_token: params.actor.as_ref().map(|t| t.token.clone()),
            actor_token_type: params.actor.as_ref().map(|t| t.token_type.clone()),
        }
    }
}

/// Parameters when requesting a token using the token exchange grant.
#[derive(Debug, Clone, Builder)]
#[builder(on(String, into), on(SecurityToken, into))]
pub struct TokenExchangeGrantParameters {
    /// The security token to exchange (the subject of the exchange).
    subject: SecurityToken,
    /// The URI of a resource server where the requested token will be used.
    resource: Option<Vec<String>>,
    /// RFC 9396 `authorization_details` requested for the issued access token.
    authorization_details: Option<Vec<crate::core::AuthorizationDetail>>,
    /// The logical name of the target service or resource where the requested token will be used.
    audience: Option<String>,
    /// The requested scope(s) for the issued security token.
    scope: Option<Vec<String>>,
    /// The type of the requested security token (e.g. `urn:ietf:params:oauth:token-type:access_token`).
    requested_token_type: Option<String>,
    /// An optional security token representing the party acting on behalf of the subject.
    actor: Option<SecurityToken>,
}

/// Token exchange parameters are reusable: subject and actor tokens may be
/// exchanged repeatedly while they remain valid, so the cache clones them for
/// each exchange. Pass a value directly to the cache builder; for tokens that
/// must be refetched per request, use [`from_fn`](crate::cache::from_fn).
impl GrantParametersSource<Self> for TokenExchangeGrantParameters {
    fn acquire(&self) -> MaybeSendBoxFuture<'_, Result<Option<Self>, Error>> {
        let params = self.clone();
        Box::pin(async move { Ok(Some(params)) })
    }

    fn discard_after_rejection(&self) -> bool {
        true
    }
}

/// A security token used as the subject or actor in a token exchange.
#[derive(Debug, Clone, Builder)]
pub struct SecurityToken {
    /// The raw token.
    ///
    /// Accepts anything that converts into a
    /// [`SecretString`] — a `&str`, `String`,
    /// or the `SecretString` you already hold (e.g. from a
    /// [`TokenResponse`](crate::grant::core::TokenResponse)). Held redacted;
    /// serialized only when the request is sent.
    #[builder(into)]
    token: SecretString,
    /// The type of the token.
    token_type: SecurityTokenType,
}

/// The type of a [`SecurityToken`] in an RFC 8693 token exchange.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub enum SecurityTokenType {
    /// `urn:ietf:params:oauth:token-type:access_token`
    #[serde(rename = "urn:ietf:params:oauth:token-type:access_token")]
    AccessToken,
    /// `urn:ietf:params:oauth:token-type:refresh_token`
    #[serde(rename = "urn:ietf:params:oauth:token-type:refresh_token")]
    RefreshToken,
    /// `urn:ietf:params:oauth:token-type:id_token`
    #[serde(rename = "urn:ietf:params:oauth:token-type:id_token")]
    IdToken,
    /// `urn:ietf:params:oauth:token-type:saml1`
    #[serde(rename = "urn:ietf:params:oauth:token-type:saml1")]
    Saml1,
    /// `urn:ietf:params:oauth:token-type:saml2`
    #[serde(rename = "urn:ietf:params:oauth:token-type:saml2")]
    Saml2,
    /// `urn:ietf:params:oauth:token-type:jwt`
    #[serde(rename = "urn:ietf:params:oauth:token-type:jwt")]
    Jwt,
    /// An extension token type not covered by the standard variants.
    #[serde(untagged)]
    Other(String),
}

#[cfg(test)]
mod tests {
    use bytes::Bytes;
    use http::Request;
    use serde_json::json;

    use super::*;
    use crate::core::{
        Error,
        http::{HttpClient, HttpResponse, Idempotency},
        platform::MaybeSendBoxFuture,
    };

    /// An [`HttpClient`] that must never be called — `build_form` does no I/O.
    struct UnusedClient;

    impl HttpClient for UnusedClient {
        fn execute(
            &self,
            _request: Request<Bytes>,
            _idempotency: Idempotency,
        ) -> MaybeSendBoxFuture<'_, Result<HttpResponse, Error>> {
            Box::pin(async { unreachable!("build_form performs no HTTP request") })
        }
    }

    fn grant() -> TokenExchangeGrant {
        TokenExchangeGrant::builder()
            .token_endpoint("https://as.example/token".parse::<EndpointUrl>().unwrap())
            .client_id("exchange-client")
            .http_client(UnusedClient)
            .build()
    }

    fn subject() -> SecurityToken {
        SecurityToken::builder()
            .token("subject-tok")
            .token_type(SecurityTokenType::AccessToken)
            .build()
    }

    #[test]
    fn build_form_maps_all_parameters_onto_the_wire() {
        let params = TokenExchangeGrantParameters::builder()
            .subject(subject())
            .actor(
                SecurityToken::builder()
                    .token("actor-tok")
                    .token_type(SecurityTokenType::Jwt)
                    .build(),
            )
            .audience("https://api.example")
            .scope(bon::vec!["read", "write"])
            .requested_token_type("urn:ietf:params:oauth:token-type:access_token")
            .authorization_details(vec![
                crate::core::AuthorizationDetail::builder("payment_initiation")
                    .with("actions", serde_json::json!(["initiate"]))
                    .build(),
            ])
            .build();

        let encoded = crate::core::oauth_form::to_string(&grant().build_form(params)).unwrap();

        for expected in [
            "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange",
            "subject_token=subject-tok",
            "subject_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token",
            "actor_token=actor-tok",
            "actor_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Ajwt",
            "audience=https%3A%2F%2Fapi.example",
            "scope=read+write",
            "requested_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token",
            // RFC 9396 §6: one parameter carrying URL-encoded JSON (`%5B%7B` = `[{`).
            "authorization_details=%5B%7B",
        ] {
            assert!(
                encoded.contains(expected),
                "missing {expected} in: {encoded}"
            );
        }
    }

    #[test]
    fn build_form_omits_absent_optional_fields() {
        // Only the subject is supplied; every other field is optional.
        let params = TokenExchangeGrantParameters::builder()
            .subject(subject())
            .scope(vec![])
            .build();

        let encoded = crate::core::oauth_form::to_string(&grant().build_form(params)).unwrap();

        // The subject and the constant grant type are always present.
        assert!(encoded.contains("grant_type="));
        assert!(encoded.contains("subject_token=subject-tok"));
        // Absent optionals are skipped entirely (no empty `key=`).
        for absent in [
            "resource",
            "audience",
            "scope",
            "requested_token_type",
            "actor_token",
            "actor_token_type",
        ] {
            assert!(
                !encoded.contains(absent),
                "{absent} should be omitted, but found it in: {encoded}"
            );
        }
    }

    #[test]
    fn resource_serializes_as_repeated_keys() {
        let params = TokenExchangeGrantParameters::builder()
            .subject(subject())
            .resource(vec![
                "https://api.example.com".to_string(),
                "https://other.example.com".to_string(),
            ])
            .scope(vec![])
            .build();

        let encoded = crate::core::oauth_form::to_string(&grant().build_form(params)).unwrap();

        assert!(encoded.contains("resource=https%3A%2F%2Fapi.example.com"));
        assert!(encoded.contains("resource=https%3A%2F%2Fother.example.com"));
        assert!(
            !encoded.contains(','),
            "resource must not be comma-joined: {encoded}"
        );
    }

    #[test]
    fn security_token_type_serializes_to_rfc8693_urns() {
        for (ty, urn) in [
            (
                SecurityTokenType::AccessToken,
                "urn:ietf:params:oauth:token-type:access_token",
            ),
            (
                SecurityTokenType::RefreshToken,
                "urn:ietf:params:oauth:token-type:refresh_token",
            ),
            (
                SecurityTokenType::IdToken,
                "urn:ietf:params:oauth:token-type:id_token",
            ),
            (
                SecurityTokenType::Saml1,
                "urn:ietf:params:oauth:token-type:saml1",
            ),
            (
                SecurityTokenType::Saml2,
                "urn:ietf:params:oauth:token-type:saml2",
            ),
            (
                SecurityTokenType::Jwt,
                "urn:ietf:params:oauth:token-type:jwt",
            ),
        ] {
            assert_eq!(serde_json::to_value(&ty).unwrap(), json!(urn));
        }

        // The `Other` extension variant serializes untagged as its raw string.
        assert_eq!(
            serde_json::to_value(SecurityTokenType::Other(
                "urn:example:custom-token".to_string()
            ))
            .unwrap(),
            json!("urn:example:custom-token")
        );
    }
}

/// Token exchange grant body.
#[derive(Debug, Serialize)]
pub struct TokenExchangeGrantForm {
    grant_type: &'static str,
    resource: Option<Vec<String>>,
    /// RFC 9396 `authorization_details` requested for the issued access token.
    authorization_details: Option<Vec<crate::core::AuthorizationDetail>>,
    audience: Option<String>,
    scope: Option<String>,
    requested_token_type: Option<String>,
    subject_token: SecretString,
    subject_token_type: SecurityTokenType,
    actor_token: Option<SecretString>,
    actor_token_type: Option<SecurityTokenType>,
}