oauth-as 0.9.3

An embeddable OAuth 2.1 Authorization Server library: spec-mirroring types (RFC 6749, RFC 8628, RFC 7636), a full device-authorization-grant state machine, and a storage trait the host implements. Deliberately host-agnostic with a tiny dependency set; nothing is allocated until the host constructs an AuthorizationServer, so an embedding host pays zero memory until its config enables the feature.
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
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (C) 2026 Matthew Jackson

//! RFC 7009 token revocation.
//!
//! Most of what is worth pinning here is what the RFC insists does NOT change the wire answer:
//! revoking a token nobody has ever seen still gets a 200 (section 2.2), a wrong `token_type_hint`
//! still finds and kills the right token (section 2.1), and revoking twice is still success. The
//! sharpest case is ownership: a client must never be able to revoke another client's token, and
//! the other client's token must still work afterwards, because "any registered client can end any
//! other client's session" is a denial of service, not a feature.

mod support;

use oauth_as::{ClientId, ErrorCode, IntrospectionResponse, TokenRequest, TokenTypeHint};
use support::{
    confidential_client, mint_code_token, other_confidential_client, server_with, ManualClock,
    CONFIDENTIAL_REDIRECT, CONFIDENTIAL_SECRET, OTHER_CONFIDENTIAL_SECRET,
};

/// RFC 7009 s2.1: revoking an access token must make it dead, verified through the RFC 7662
/// introspection surface rather than the host-only `introspect` shortcut, since the wire-visible
/// answer is what actually matters.
#[tokio::test]
async fn revoking_an_access_token_makes_it_inactive_to_introspection() {
    let clock = ManualClock::at_epoch();
    let srv = server_with(clock, vec![confidential_client()]).await;
    let issued = mint_code_token(
        &srv,
        "confidential-app",
        Some(CONFIDENTIAL_SECRET),
        CONFIDENTIAL_REDIRECT,
        "read",
        "user-1",
    )
    .await;

    srv.revoke(
        &ClientId::new("confidential-app"),
        Some(CONFIDENTIAL_SECRET),
        &issued.access_token,
        Some(TokenTypeHint::AccessToken),
    )
    .await
    .expect("revoking a live access token must succeed");

    let resp = srv
        .introspection_response(
            &ClientId::new("confidential-app"),
            Some(CONFIDENTIAL_SECRET),
            &issued.access_token,
        )
        .await
        .unwrap();
    assert_eq!(resp, IntrospectionResponse::inactive());
}

/// RFC 7009 s2.1: "If the particular token is a refresh token and the authorization server
/// supports the revocation of access tokens, then the authorization server SHOULD also invalidate
/// all access tokens based on the same authorization grant."
///
/// This is the half of revocation a client cannot do for itself. A client that revokes its refresh
/// token has said "this session is over"; leaving the access token minted from the same grant live
/// means the session is over for the party that asked and not for whoever holds the token, which is
/// exactly inverted when the reason for revoking is that something leaked.
#[tokio::test]
async fn revoking_a_refresh_token_also_kills_the_access_tokens_of_the_same_grant() {
    let clock = ManualClock::at_epoch();
    let srv = server_with(clock, vec![confidential_client()]).await;
    let issued = mint_code_token(
        &srv,
        "confidential-app",
        Some(CONFIDENTIAL_SECRET),
        CONFIDENTIAL_REDIRECT,
        "read",
        "user-1",
    )
    .await;
    let refresh_token = issued.refresh_token.clone().expect("a refresh token");

    srv.revoke(
        &ClientId::new("confidential-app"),
        Some(CONFIDENTIAL_SECRET),
        &refresh_token,
        Some(TokenTypeHint::RefreshToken),
    )
    .await
    .expect("revoking a live refresh token must succeed");

    let resp = srv
        .introspection_response(
            &ClientId::new("confidential-app"),
            Some(CONFIDENTIAL_SECRET),
            &issued.access_token,
        )
        .await
        .unwrap();
    assert_eq!(
        resp,
        IntrospectionResponse::inactive(),
        "the access token issued with the revoked refresh token is still live"
    );
}

/// RFC 7009 s2.1: revoking a refresh token must break the chain it belonged to, so the next
/// attempt to use it is `invalid_grant` rather than a fresh token.
#[tokio::test]
async fn revoking_a_refresh_token_makes_the_next_refresh_invalid_grant() {
    let clock = ManualClock::at_epoch();
    let srv = server_with(clock, vec![confidential_client()]).await;
    let issued = mint_code_token(
        &srv,
        "confidential-app",
        Some(CONFIDENTIAL_SECRET),
        CONFIDENTIAL_REDIRECT,
        "read",
        "user-1",
    )
    .await;
    let refresh_token = issued.refresh_token.expect("a refresh token was issued");

    srv.revoke(
        &ClientId::new("confidential-app"),
        Some(CONFIDENTIAL_SECRET),
        &refresh_token,
        Some(TokenTypeHint::RefreshToken),
    )
    .await
    .expect("revoking a live refresh token must succeed");

    let err = srv
        .token(TokenRequest::RefreshToken {
            client_id: ClientId::new("confidential-app"),
            client_secret: Some(CONFIDENTIAL_SECRET.to_string()),
            refresh_token,
            scope: None,
        })
        .await
        .expect_err("a revoked refresh token must not redeem");
    assert_eq!(err.error, ErrorCode::InvalidGrant);
}

/// RFC 7009 s2.2: the server MUST respond with a 200 even when the presented token was never
/// issued, because refusing it would let a caller distinguish "revoked" from "never existed" and
/// so test whether an arbitrary string is a real token.
#[tokio::test]
async fn revoking_an_unknown_token_still_returns_ok() {
    let clock = ManualClock::at_epoch();
    let srv = server_with(clock, vec![confidential_client()]).await;

    srv.revoke(
        &ClientId::new("confidential-app"),
        Some(CONFIDENTIAL_SECRET),
        "this-token-was-never-issued",
        None,
    )
    .await
    .expect("RFC 7009 s2.2: an unknown token must not produce an error response");
}

/// Revocation is idempotent: nothing about a second revocation of the same token is a failure.
#[tokio::test]
async fn revocation_is_idempotent() {
    let clock = ManualClock::at_epoch();
    let srv = server_with(clock, vec![confidential_client()]).await;
    let issued = mint_code_token(
        &srv,
        "confidential-app",
        Some(CONFIDENTIAL_SECRET),
        CONFIDENTIAL_REDIRECT,
        "read",
        "user-1",
    )
    .await;

    for attempt in 0..2 {
        srv.revoke(
            &ClientId::new("confidential-app"),
            Some(CONFIDENTIAL_SECRET),
            &issued.access_token,
            Some(TokenTypeHint::AccessToken),
        )
        .await
        .unwrap_or_else(|e| panic!("revocation attempt {attempt} must succeed, got {e:?}"));
    }
}

/// RFC 7009 s2.1 requires the server to verify the token belongs to the authenticated client. A
/// client cannot revoke another client's token, and critically the target token must survive the
/// attempt intact: silently destroying another client's refresh chain from an unrelated,
/// legitimately registered client would be a denial of service available to anyone who can
/// register a client. Both the access token and the refresh token halves are checked.
#[tokio::test]
async fn a_client_cannot_revoke_another_clients_token_and_it_still_works() {
    let clock = ManualClock::at_epoch();
    let srv = server_with(
        clock,
        vec![confidential_client(), other_confidential_client()],
    )
    .await;
    let issued = mint_code_token(
        &srv,
        "confidential-app",
        Some(CONFIDENTIAL_SECRET),
        CONFIDENTIAL_REDIRECT,
        "read write",
        "user-1",
    )
    .await;
    let refresh_token = issued
        .refresh_token
        .clone()
        .expect("a refresh token was issued");

    // The attempt itself must still answer 200 (an attacker must not be able to distinguish "not
    // yours" from "unknown" either), but it must not touch the token.
    srv.revoke(
        &ClientId::new("other-app"),
        Some(OTHER_CONFIDENTIAL_SECRET),
        &issued.access_token,
        Some(TokenTypeHint::AccessToken),
    )
    .await
    .expect("the wire answer for someone else's token is still success");

    let after_access_attempt = srv
        .introspection_response(
            &ClientId::new("confidential-app"),
            Some(CONFIDENTIAL_SECRET),
            &issued.access_token,
        )
        .await
        .unwrap();
    assert!(
        after_access_attempt.active,
        "another client's revoke attempt must not touch the access token"
    );

    srv.revoke(
        &ClientId::new("other-app"),
        Some(OTHER_CONFIDENTIAL_SECRET),
        &refresh_token,
        Some(TokenTypeHint::RefreshToken),
    )
    .await
    .expect("the wire answer for someone else's refresh token is still success");

    // The refresh chain must still be redeemable by its real owner.
    srv.token(TokenRequest::RefreshToken {
        client_id: ClientId::new("confidential-app"),
        client_secret: Some(CONFIDENTIAL_SECRET.to_string()),
        refresh_token,
        scope: None,
    })
    .await
    .expect("another client's failed revoke attempt must not have destroyed the refresh chain");
}

/// RFC 7009 s2.1: `token_type_hint` is an optimisation only. The server MUST keep looking when the
/// hint does not match, so every hint/kind combination, including the two mismatched ones, must
/// still revoke the right token.
#[tokio::test]
async fn wrong_type_hint_still_revokes_the_right_token() {
    for hint_for_access in [
        None,
        Some(TokenTypeHint::AccessToken),
        Some(TokenTypeHint::RefreshToken),
    ] {
        let clock = ManualClock::at_epoch();
        let srv = server_with(clock, vec![confidential_client()]).await;
        let issued = mint_code_token(
            &srv,
            "confidential-app",
            Some(CONFIDENTIAL_SECRET),
            CONFIDENTIAL_REDIRECT,
            "read",
            "user-1",
        )
        .await;

        srv.revoke(
            &ClientId::new("confidential-app"),
            Some(CONFIDENTIAL_SECRET),
            &issued.access_token,
            hint_for_access,
        )
        .await
        .unwrap();

        let resp = srv
            .introspection_response(
                &ClientId::new("confidential-app"),
                Some(CONFIDENTIAL_SECRET),
                &issued.access_token,
            )
            .await
            .unwrap();
        assert_eq!(
            resp,
            IntrospectionResponse::inactive(),
            "hint {hint_for_access:?} must not stop the access token from being revoked"
        );
    }

    for hint_for_refresh in [
        None,
        Some(TokenTypeHint::AccessToken),
        Some(TokenTypeHint::RefreshToken),
    ] {
        let clock = ManualClock::at_epoch();
        let srv = server_with(clock, vec![confidential_client()]).await;
        let issued = mint_code_token(
            &srv,
            "confidential-app",
            Some(CONFIDENTIAL_SECRET),
            CONFIDENTIAL_REDIRECT,
            "read",
            "user-1",
        )
        .await;
        let refresh_token = issued.refresh_token.expect("a refresh token was issued");

        srv.revoke(
            &ClientId::new("confidential-app"),
            Some(CONFIDENTIAL_SECRET),
            &refresh_token,
            hint_for_refresh,
        )
        .await
        .unwrap();

        let err = srv
            .token(TokenRequest::RefreshToken {
                client_id: ClientId::new("confidential-app"),
                client_secret: Some(CONFIDENTIAL_SECRET.to_string()),
                refresh_token,
                scope: None,
            })
            .await
            .expect_err(&format!(
                "hint {hint_for_refresh:?} must not stop the refresh token from being revoked"
            ));
        assert_eq!(err.error, ErrorCode::InvalidGrant);
    }
}

/// Revocation is a protected endpoint exactly like introspection: no credential, or the wrong
/// one, gets `invalid_client` rather than an answer about the token.
#[tokio::test]
async fn revocation_requires_client_authentication() {
    let clock = ManualClock::at_epoch();
    let srv = server_with(clock, vec![confidential_client()]).await;
    let issued = mint_code_token(
        &srv,
        "confidential-app",
        Some(CONFIDENTIAL_SECRET),
        CONFIDENTIAL_REDIRECT,
        "read",
        "user-1",
    )
    .await;

    let err = srv
        .revoke(
            &ClientId::new("confidential-app"),
            None,
            &issued.access_token,
            None,
        )
        .await
        .expect_err("a confidential client presenting no secret must not be authenticated");
    assert_eq!(err.error, ErrorCode::InvalidClient);

    let err = srv
        .revoke(
            &ClientId::new("confidential-app"),
            Some("not-the-real-secret"),
            &issued.access_token,
            None,
        )
        .await
        .expect_err("the wrong secret must not authenticate");
    assert_eq!(err.error, ErrorCode::InvalidClient);

    let err = srv
        .revoke(
            &ClientId::new("no-such-client"),
            Some("anything"),
            &issued.access_token,
            None,
        )
        .await
        .expect_err("an unregistered client_id must not be authenticated");
    assert_eq!(err.error, ErrorCode::InvalidClient);

    // The token must have survived every one of those failed attempts.
    let resp = srv
        .introspection_response(
            &ClientId::new("confidential-app"),
            Some(CONFIDENTIAL_SECRET),
            &issued.access_token,
        )
        .await
        .unwrap();
    assert!(
        resp.active,
        "an unauthenticated revoke attempt must not have revoked anything"
    );
}