uselesskey-axum 0.9.0

axum auth-test helpers backed by deterministic uselesskey fixtures.
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
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
#![forbid(unsafe_code)]

//! `axum` auth-test helpers built on deterministic `uselesskey` fixtures.
//!
//! This crate is intentionally test-focused and scoped to common drop-in needs:
//! - JWKS and OIDC discovery routers
//! - Bearer token verification middleware for tests
//! - Typed deterministic auth context extraction/injection

use axum::extract::{FromRequestParts, State};
use axum::http::{Request, StatusCode, header::AUTHORIZATION, request::Parts};
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use axum::routing::get;
use axum::{Json, Router};
use jsonwebtoken::{
    Algorithm, DecodingKey, EncodingKey, Header, Validation, decode, decode_header, encode,
};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::sync::Arc;
use uselesskey_core::{Factory, Seed};
use uselesskey_rsa::{RsaFactoryExt, RsaKeyPair, RsaSpec};

const DEFAULT_JWKS_PATH: &str = "/.well-known/jwks.json";
const DEFAULT_OIDC_PATH: &str = "/.well-known/openid-configuration";

/// Expected JWT shape for test verification.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthExpectations {
    /// Expected `iss` claim.
    pub issuer: String,
    /// Expected `aud` claim.
    pub audience: String,
    /// Expected key id from JWT header.
    pub kid: String,
}

impl AuthExpectations {
    /// Build expected issuer/audience/kid values.
    pub fn new(
        issuer: impl Into<String>,
        audience: impl Into<String>,
        kid: impl Into<String>,
    ) -> Self {
        Self {
            issuer: issuer.into(),
            audience: audience.into(),
            kid: kid.into(),
        }
    }

    /// Replace expected issuer.
    pub fn with_issuer(mut self, issuer: impl Into<String>) -> Self {
        self.issuer = issuer.into();
        self
    }

    /// Replace expected audience.
    pub fn with_audience(mut self, audience: impl Into<String>) -> Self {
        self.audience = audience.into();
        self
    }

    /// Replace expected kid.
    pub fn with_kid(mut self, kid: impl Into<String>) -> Self {
        self.kid = kid.into();
        self
    }
}

/// Deterministic JWT rotation phase.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RotationPhase {
    /// Primary signing key.
    Primary,
    /// Next key in deterministic rotation sequence.
    Next,
}

impl RotationPhase {
    fn suffix(self) -> &'static str {
        match self {
            Self::Primary => "primary",
            Self::Next => "next",
        }
    }
}

/// Deterministic signer + JWKS test fixture for one rotation phase.
#[derive(Clone)]
pub struct DeterministicJwksPhase {
    keypair: RsaKeyPair,
    expectations: AuthExpectations,
}

impl DeterministicJwksPhase {
    /// Build deterministic material for a rotation phase.
    pub fn new(
        seed: Seed,
        label: impl AsRef<str>,
        phase: RotationPhase,
        issuer: impl Into<String>,
        audience: impl Into<String>,
    ) -> Self {
        let fx = Factory::deterministic(seed);
        let keypair = fx.rsa(
            format!("{}:{}", label.as_ref(), phase.suffix()),
            RsaSpec::rs256(),
        );
        let kid = keypair.kid();
        Self {
            keypair,
            expectations: AuthExpectations::new(issuer, audience, kid),
        }
    }

    /// Public JWKS payload for this phase.
    pub fn jwks_json(&self) -> Value {
        self.keypair.public_jwks_json()
    }

    /// Expected issuer/audience/kid values.
    pub fn expectations(&self) -> &AuthExpectations {
        &self.expectations
    }

    /// Create RS256 bearer token for test claims.
    pub fn issue_token(&self, mut claims: Value, ttl_seconds: u64) -> String {
        let now = current_unix_seconds();
        if claims.get("iss").is_none() {
            claims["iss"] = Value::String(self.expectations.issuer.clone());
        }
        if claims.get("aud").is_none() {
            claims["aud"] = Value::String(self.expectations.audience.clone());
        }
        if claims.get("iat").is_none() {
            claims["iat"] = Value::Number((now as u64).into());
        }
        if claims.get("exp").is_none() {
            claims["exp"] = Value::Number((now as u64 + ttl_seconds).into());
        }

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some(self.expectations.kid.clone());

        encode(
            &header,
            &claims,
            &EncodingKey::from_rsa_pem(self.keypair.private_key_pkcs8_pem().as_bytes())
                .expect("deterministic fixture key should produce valid RSA encoding key"),
        )
        .expect("deterministic fixture key should produce valid JWT")
    }

    fn decoding_key(&self) -> DecodingKey {
        DecodingKey::from_rsa_pem(self.keypair.public_key_spki_pem().as_bytes())
            .expect("deterministic fixture key should produce valid RSA decoding key")
    }
}

/// Typed auth context inserted by helpers and extracted in handlers.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TestAuthContext {
    pub sub: String,
    pub iss: String,
    pub aud: String,
    pub kid: String,
    pub exp: u64,
}

impl<S> FromRequestParts<S> for TestAuthContext
where
    S: Send + Sync,
{
    type Rejection = (StatusCode, &'static str);

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        parts
            .extensions
            .get::<Self>()
            .cloned()
            .ok_or((StatusCode::UNAUTHORIZED, "missing auth context"))
    }
}

/// Middleware verification state.
#[derive(Clone)]
pub struct MockJwtVerifierState {
    signer: DeterministicJwksPhase,
}

impl MockJwtVerifierState {
    /// Build middleware state from a deterministic phase.
    pub fn new(signer: DeterministicJwksPhase) -> Self {
        Self { signer }
    }

    /// Produce JWKS JSON served by [`jwks_router`].
    pub fn jwks_json(&self) -> Value {
        self.signer.jwks_json()
    }

    /// Produce OIDC discovery JSON served by [`oidc_router`].
    pub fn oidc_json(&self, base_url: impl AsRef<str>) -> Value {
        let base = base_url.as_ref().trim_end_matches('/');
        json!({
            "issuer": self.signer.expectations().issuer,
            "jwks_uri": format!("{base}{DEFAULT_JWKS_PATH}"),
            "id_token_signing_alg_values_supported": ["RS256"],
            "token_endpoint_auth_methods_supported": ["none"],
            "response_types_supported": ["token"],
            "subject_types_supported": ["public"],
        })
    }

    /// Generate a valid bearer token for this state.
    pub fn issue_token(&self, claims: Value, ttl_seconds: u64) -> String {
        self.signer.issue_token(claims, ttl_seconds)
    }

    /// Clone expected claims checks.
    pub fn expectations(&self) -> AuthExpectations {
        self.signer.expectations().clone()
    }
}

/// Build a JWKS router mounted at `/.well-known/jwks.json`.
pub fn jwks_router(state: MockJwtVerifierState) -> Router {
    Router::new()
        .route(DEFAULT_JWKS_PATH, get(jwks_handler))
        .with_state(state)
}

/// Build an OIDC discovery router mounted at `/.well-known/openid-configuration`.
pub fn oidc_router(state: MockJwtVerifierState, base_url: impl Into<String>) -> Router {
    let state = OidcState {
        verifier: state,
        base_url: base_url.into(),
    };
    Router::new()
        .route(DEFAULT_OIDC_PATH, get(oidc_handler))
        .with_state(state)
}

/// Attach a middleware layer that verifies bearer tokens and inserts [`TestAuthContext`].
pub fn mock_jwt_verifier_layer(router: Router, state: MockJwtVerifierState) -> Router {
    let state = Arc::new(state);
    router.layer(axum::middleware::from_fn(move |request, next| {
        let state = Arc::clone(&state);
        async move { verify_bearer_token(state.as_ref().clone(), request, next).await }
    }))
}

/// Attach a middleware layer that injects a deterministic auth context without JWT parsing.
pub fn inject_auth_context_layer(router: Router, context: TestAuthContext) -> Router {
    let context = Arc::new(context);
    router.layer(axum::middleware::from_fn(move |request, next| {
        let context = Arc::clone(&context);
        async move { inject_auth_context(context.as_ref().clone(), request, next).await }
    }))
}

#[derive(Clone)]
struct OidcState {
    verifier: MockJwtVerifierState,
    base_url: String,
}

async fn jwks_handler(State(state): State<MockJwtVerifierState>) -> Json<Value> {
    Json(state.jwks_json())
}

async fn oidc_handler(State(state): State<OidcState>) -> Json<Value> {
    Json(state.verifier.oidc_json(&state.base_url))
}

async fn inject_auth_context(
    context: TestAuthContext,
    mut request: Request<axum::body::Body>,
    next: Next,
) -> Response {
    request.extensions_mut().insert(context);
    next.run(request).await
}

async fn verify_bearer_token(
    state: MockJwtVerifierState,
    mut request: Request<axum::body::Body>,
    next: Next,
) -> Response {
    let bearer = match extract_bearer(request.headers()) {
        Ok(token) => token,
        Err((code, msg)) => return (code, msg).into_response(),
    };

    let header = match decode_header(bearer) {
        Ok(header) => header,
        Err(_) => return (StatusCode::UNAUTHORIZED, "invalid jwt header").into_response(),
    };

    let expected = state.expectations();
    if header.kid.as_deref() != Some(expected.kid.as_str()) {
        return (StatusCode::UNAUTHORIZED, "unexpected kid").into_response();
    }

    let mut validation = Validation::new(Algorithm::RS256);
    validation.set_issuer(std::slice::from_ref(&expected.issuer));
    validation.set_audience(std::slice::from_ref(&expected.audience));
    validation.leeway = 0;

    let token = match decode::<Value>(bearer, &state.signer.decoding_key(), &validation) {
        Ok(token) => token,
        Err(_) => return (StatusCode::UNAUTHORIZED, "token verification failed").into_response(),
    };

    let sub = token
        .claims
        .get("sub")
        .and_then(Value::as_str)
        .unwrap_or("unknown")
        .to_owned();
    let iss = token
        .claims
        .get("iss")
        .and_then(Value::as_str)
        .unwrap_or_default()
        .to_owned();
    let aud = token
        .claims
        .get("aud")
        .and_then(Value::as_str)
        .unwrap_or_default()
        .to_owned();
    let exp = token
        .claims
        .get("exp")
        .and_then(Value::as_u64)
        .unwrap_or_default();

    request.extensions_mut().insert(TestAuthContext {
        sub,
        iss,
        aud,
        kid: expected.kid,
        exp,
    });

    next.run(request).await
}

fn extract_bearer(headers: &axum::http::HeaderMap) -> Result<&str, (StatusCode, &'static str)> {
    let header = headers
        .get(AUTHORIZATION)
        .and_then(|value| value.to_str().ok())
        .ok_or((StatusCode::UNAUTHORIZED, "missing authorization header"))?;
    let token = header
        .strip_prefix("Bearer ")
        .ok_or((StatusCode::UNAUTHORIZED, "invalid authorization scheme"))?;
    if token.is_empty() {
        return Err((StatusCode::UNAUTHORIZED, "empty bearer token"));
    }
    Ok(token)
}

fn current_unix_seconds() -> usize {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .expect("current time should be >= unix epoch")
        .as_secs() as usize
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::body::Body;
    use axum::http::Request;
    use axum::response::IntoResponse;
    use axum::routing::get;
    use tower::ServiceExt;

    fn phase(phase: RotationPhase) -> DeterministicJwksPhase {
        let seed = Seed::from_env_value("uselesskey-axum-tests").expect("seed parse");
        DeterministicJwksPhase::new(
            seed,
            "auth-suite",
            phase,
            "https://issuer.example.test",
            "api://example-aud",
        )
    }

    #[tokio::test]
    async fn jwks_and_oidc_routes_respond() {
        let state = MockJwtVerifierState::new(phase(RotationPhase::Primary));
        let app = jwks_router(state.clone()).merge(oidc_router(state, "http://localhost:3000"));

        let jwks_res = app
            .clone()
            .oneshot(
                Request::builder()
                    .uri(DEFAULT_JWKS_PATH)
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(jwks_res.status(), StatusCode::OK);

        let oidc_res = app
            .oneshot(
                Request::builder()
                    .uri(DEFAULT_OIDC_PATH)
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(oidc_res.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn rotation_phase_produces_distinct_kids() {
        let primary = phase(RotationPhase::Primary);
        let next = phase(RotationPhase::Next);
        assert_ne!(primary.expectations().kid, next.expectations().kid);
    }

    #[tokio::test]
    async fn verifier_rejects_wrong_audience() {
        let state = MockJwtVerifierState::new(phase(RotationPhase::Primary));
        let token = state.issue_token(json!({"sub":"alice", "aud":"api://wrong-aud"}), 300);

        let app = mock_jwt_verifier_layer(
            Router::new().route(
                "/me",
                get(|auth: TestAuthContext| async move {
                    Json(json!({"sub": auth.sub})).into_response()
                }),
            ),
            state,
        );

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/me")
                    .header(AUTHORIZATION, format!("Bearer {token}"))
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn verifier_rejects_expired_token() {
        let state = MockJwtVerifierState::new(phase(RotationPhase::Primary));
        let now = current_unix_seconds() as u64;
        let token = state.issue_token(
            json!({"sub":"alice", "exp": now.saturating_sub(5), "iat": now.saturating_sub(10)}),
            300,
        );

        let app = mock_jwt_verifier_layer(
            Router::new().route("/me", get(|| async { StatusCode::OK })),
            state,
        );

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/me")
                    .header(AUTHORIZATION, format!("Bearer {token}"))
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn deterministic_auth_context_injection_works() {
        let app = inject_auth_context_layer(
            Router::new().route(
                "/me",
                get(|auth: TestAuthContext| async move {
                    Json(json!({"sub": auth.sub, "kid": auth.kid})).into_response()
                }),
            ),
            TestAuthContext {
                sub: "test-user".into(),
                iss: "iss".into(),
                aud: "aud".into(),
                kid: "kid-1".into(),
                exp: 42,
            },
        );

        let response = app
            .oneshot(Request::builder().uri("/me").body(Body::empty()).unwrap())
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);
    }
}