blueprint-auth 0.2.0-alpha.1

Blueprint HTTP/WS Authentication
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
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
#![cfg(test)]

use jsonwebtoken::{Algorithm, EncodingKey, Header, encode};
use openssl::rsa::Rsa;
use serde::Serialize;
use std::net::SocketAddr;
use std::sync::OnceLock;
use std::time::{SystemTime, UNIX_EPOCH};

use crate::oauth::ServiceOAuthPolicy;
use crate::proxy::AuthenticatedProxy;
use crate::types::ServiceId;

struct RsaMaterial {
    der: Vec<u8>,
    public_pem: String,
}

static RSA_MATERIAL: OnceLock<RsaMaterial> = OnceLock::new();

fn rsa_material() -> &'static RsaMaterial {
    RSA_MATERIAL.get_or_init(|| {
        let rsa = Rsa::generate(2048).unwrap();
        let der = rsa.private_key_to_der().unwrap();
        // Provide PKCS#1 public key as well (BEGIN RSA PUBLIC KEY)
        let public_pem = String::from_utf8(rsa.public_key_to_pem_pkcs1().unwrap()).unwrap();
        RsaMaterial { der, public_pem }
    })
}

fn rsa_encoding_key() -> EncodingKey {
    EncodingKey::from_rsa_der(&rsa_material().der)
}

fn rsa_public_pem() -> String {
    rsa_material().public_pem.clone()
}

fn now() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

fn start_proxy_with_policy(policy: ServiceOAuthPolicy) -> (SocketAddr, ServiceId) {
    use tempfile::tempdir;

    let tmp = tempdir().unwrap();
    let proxy = AuthenticatedProxy::new(tmp.path()).unwrap();
    let db = proxy.db();

    let service_id = ServiceId::new(7);
    let service = crate::models::ServiceModel {
        api_key_prefix: "test_".to_string(),
        owners: vec![],
        upstream_url: "http://127.0.0.1:9".to_string(),
        tls_profile: None,
    };
    service.save(service_id, &db).unwrap();
    policy.save(service_id, &db).unwrap();

    let app = proxy.router();
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    listener.set_nonblocking(true).unwrap();
    let addr = listener.local_addr().unwrap();
    let tcp = tokio::net::TcpListener::from_std(listener).unwrap();
    tokio::spawn(async move {
        axum::serve(tcp, app).await.unwrap();
    });
    (addr, service_id)
}

#[derive(Serialize)]
struct Claims {
    iss: String,
    sub: String,
    aud: Option<String>,
    iat: u64,
    exp: u64,
    jti: String,
    scope: Option<String>,
}

#[tokio::test]
async fn oauth_success_rs256() {
    let policy = ServiceOAuthPolicy {
        allowed_issuers: vec!["https://issuer.example.com".into()],
        required_audiences: vec!["https://proxy.example.com".into()],
        public_keys_pem: vec![rsa_public_pem()],
        allowed_scopes: Some(vec!["data:read".into(), "data:write".into()]),
        require_dpop: false,
        max_access_token_ttl_secs: 900,
        max_assertion_ttl_secs: 120,
    };
    let (addr, service_id) = start_proxy_with_policy(policy);

    let now = now();
    let claims = Claims {
        iss: "https://issuer.example.com".into(),
        sub: "user-1".into(),
        aud: Some("https://proxy.example.com".into()),
        iat: now,
        exp: now + 60,
        jti: uuid::Uuid::new_v4().to_string(),
        scope: Some("data:read extra:skip".into()),
    };
    let jwt = encode(&Header::new(Algorithm::RS256), &claims, &rsa_encoding_key()).unwrap();

    let client = reqwest::Client::new();
    let res = client
        .post(format!("http://{addr}/v1/oauth/token"))
        .header("content-type", "application/x-www-form-urlencoded")
        .header("x-service-id", service_id.to_string())
        .body(format!(
            "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion={jwt}"
        ))
        .send()
        .await
        .unwrap();
    let status = res.status();
    let body = res.text().await.unwrap();
    assert!(status.is_success(), "status={status} body={body}");
    assert!(body.contains("access_token"));
}

#[tokio::test]
async fn oauth_rejects_unsupported_alg_hs256() {
    let policy = ServiceOAuthPolicy {
        allowed_issuers: vec!["https://issuer.example.com".into()],
        required_audiences: vec![],
        public_keys_pem: vec![rsa_public_pem()],
        allowed_scopes: None,
        require_dpop: false,
        max_access_token_ttl_secs: 900,
        max_assertion_ttl_secs: 120,
    };
    let (addr, service_id) = start_proxy_with_policy(policy);

    #[derive(Serialize)]
    struct HsClaims {
        iss: String,
        sub: String,
        iat: u64,
        exp: u64,
        jti: String,
    }
    let now = now();
    let claims = HsClaims {
        iss: "https://issuer.example.com".into(),
        sub: "u".into(),
        iat: now,
        exp: now + 60,
        jti: uuid::Uuid::new_v4().to_string(),
    };
    let jwt = jsonwebtoken::encode(
        &Header::new(Algorithm::HS256),
        &claims,
        &jsonwebtoken::EncodingKey::from_secret(b"secret"),
    )
    .unwrap();

    let client = reqwest::Client::new();
    let res = client
        .post(format!("http://{addr}/v1/oauth/token"))
        .header("content-type", "application/x-www-form-urlencoded")
        .header("x-service-id", service_id.to_string())
        .body(format!(
            "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion={jwt}"
        ))
        .send()
        .await
        .unwrap();
    assert_eq!(res.status(), axum::http::StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn oauth_rejects_expired_and_future_iat() {
    let policy = ServiceOAuthPolicy {
        allowed_issuers: vec!["https://issuer.example.com".into()],
        required_audiences: vec![],
        public_keys_pem: vec![rsa_public_pem()],
        allowed_scopes: None,
        require_dpop: false,
        max_access_token_ttl_secs: 900,
        max_assertion_ttl_secs: 60,
    };
    let (addr, service_id) = start_proxy_with_policy(policy);
    let client = reqwest::Client::new();

    // expired
    let now = now();
    let expired = Claims {
        iss: "https://issuer.example.com".into(),
        sub: "u".into(),
        aud: None,
        iat: now - 120,
        exp: now - 60,
        jti: uuid::Uuid::new_v4().to_string(),
        scope: None,
    };
    let jwt_expired = encode(
        &Header::new(Algorithm::RS256),
        &expired,
        &rsa_encoding_key(),
    )
    .unwrap();
    let res = client
        .post(format!("http://{addr}/v1/oauth/token"))
        .header("content-type", "application/x-www-form-urlencoded")
        .header("x-service-id", service_id.to_string())
        .body(format!(
            "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion={jwt_expired}"
        ))
        .send()
        .await
        .unwrap();
    assert_eq!(res.status(), axum::http::StatusCode::BAD_REQUEST);

    // future iat (beyond skew)
    let future = Claims {
        iss: "https://issuer.example.com".into(),
        sub: "u".into(),
        aud: None,
        iat: now + 600,
        exp: now + 660,
        jti: uuid::Uuid::new_v4().to_string(),
        scope: None,
    };
    let jwt_future = encode(&Header::new(Algorithm::RS256), &future, &rsa_encoding_key()).unwrap();
    let res2 = client
        .post(format!("http://{addr}/v1/oauth/token"))
        .header("content-type", "application/x-www-form-urlencoded")
        .header("x-service-id", service_id.to_string())
        .body(format!(
            "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion={jwt_future}"
        ))
        .send()
        .await
        .unwrap();
    assert_eq!(res2.status(), axum::http::StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn oauth_rejects_replay_jti() {
    let policy = ServiceOAuthPolicy {
        allowed_issuers: vec!["https://issuer.example.com".into()],
        required_audiences: vec![],
        public_keys_pem: vec![rsa_public_pem()],
        allowed_scopes: None,
        require_dpop: false,
        max_access_token_ttl_secs: 900,
        max_assertion_ttl_secs: 120,
    };
    let (addr, service_id) = start_proxy_with_policy(policy);
    let client = reqwest::Client::new();

    let now = now();
    let jti = uuid::Uuid::new_v4().to_string();
    let claims = Claims {
        iss: "https://issuer.example.com".into(),
        sub: "u".into(),
        aud: None,
        iat: now,
        exp: now + 60,
        jti: jti.clone(),
        scope: None,
    };
    let jwt = encode(&Header::new(Algorithm::RS256), &claims, &rsa_encoding_key()).unwrap();

    let body = format!("grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion={jwt}");
    let ok = client
        .post(format!("http://{addr}/v1/oauth/token"))
        .header("content-type", "application/x-www-form-urlencoded")
        .header("x-service-id", service_id.to_string())
        .body(body.clone())
        .send()
        .await
        .unwrap();
    let s = ok.status();
    let b = ok.text().await.unwrap();
    assert!(s.is_success(), "status={s} body={b}");
    let replay = client
        .post(format!("http://{addr}/v1/oauth/token"))
        .header("content-type", "application/x-www-form-urlencoded")
        .header("x-service-id", service_id.to_string())
        .body(body)
        .send()
        .await
        .unwrap();
    assert_eq!(replay.status(), axum::http::StatusCode::BAD_REQUEST);
}

#[derive(serde::Deserialize)]
struct TokenResponse {
    access_token: String,
    token_type: String,
    expires_at: u64,
    expires_in: u64,
}

#[tokio::test]
async fn oauth_scopes_are_forwarded_and_normalized_and_client_scopes_stripped() {
    use axum::{Json, Router, routing::get};
    use std::collections::BTreeMap;
    use std::net::Ipv4Addr;
    use tempfile::tempdir;

    // Upstream echo server that returns all headers as JSON (lowercased keys)
    let echo_router = Router::new().route(
        "/echo",
        get(|headers: axum::http::HeaderMap| async move {
            let mut map = BTreeMap::new();
            for (name, value) in headers.iter() {
                let k = name.as_str().to_ascii_lowercase();
                let v = value.to_str().unwrap_or("").to_string();
                map.insert(k, v);
            }
            Json(map)
        }),
    );
    let (echo_task, echo_addr) = {
        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
            .await
            .expect("bind echo");
        let addr = listener.local_addr().unwrap();
        let task = tokio::spawn(async move {
            if let Err(e) = axum::serve(listener, echo_router).await {
                eprintln!("echo server error: {e}");
            }
        });
        (task, addr)
    };

    // Start proxy with service pointing to echo server
    let tmp = tempdir().unwrap();
    let proxy = AuthenticatedProxy::new(tmp.path()).unwrap();
    let db = proxy.db();
    let service_id = ServiceId::new(8);
    let service = crate::models::ServiceModel {
        api_key_prefix: "test_".to_string(),
        owners: vec![],
        upstream_url: format!("http://{echo_addr}"),
        tls_profile: None,
    };
    service.save(service_id, &db).unwrap();
    let policy = ServiceOAuthPolicy {
        allowed_issuers: vec!["https://issuer.example.com".into()],
        required_audiences: vec!["https://proxy.example.com".into()],
        public_keys_pem: vec![rsa_public_pem()],
        allowed_scopes: Some(vec!["data:read".into(), "mcp:invoke".into()]),
        require_dpop: false,
        max_access_token_ttl_secs: 900,
        max_assertion_ttl_secs: 120,
    };
    policy.save(service_id, &db).unwrap();

    let app = proxy.router();
    let listener = std::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
    listener.set_nonblocking(true).unwrap();
    let proxy_addr = listener.local_addr().unwrap();
    let tcp = tokio::net::TcpListener::from_std(listener).unwrap();
    tokio::spawn(async move {
        axum::serve(tcp, app).await.unwrap();
    });

    // Mint OAuth assertion containing messy/mixed scopes; intersection should yield [data:read, mcp:invoke]
    let current_time = now();
    let claims = Claims {
        iss: "https://issuer.example.com".into(),
        sub: "user-42".into(),
        aud: Some("https://proxy.example.com".into()),
        iat: current_time,
        exp: current_time + 60,
        jti: uuid::Uuid::new_v4().to_string(),
        scope: Some("DATA:READ data:read extra:skip Mcp:InvokE".into()),
    };
    let jwt = encode(&Header::new(Algorithm::RS256), &claims, &rsa_encoding_key()).unwrap();

    // Exchange for Paseto
    let client = reqwest::Client::new();
    let token_res = client
        .post(format!("http://{proxy_addr}/v1/oauth/token"))
        .header("content-type", "application/x-www-form-urlencoded")
        .header("x-service-id", service_id.to_string())
        .body(format!(
            "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion={jwt}"
        ))
        .send()
        .await
        .unwrap();
    let status = token_res.status();
    assert!(status.is_success());
    let token_body = token_res.text().await.unwrap();
    let token: TokenResponse = serde_json::from_str(&token_body).unwrap();

    // Validate token metadata fields
    assert_eq!(token.token_type, "Bearer", "token type should be 'Bearer'");
    assert!(
        token.expires_at > current_time,
        "token should have future expiration time"
    );
    assert!(
        token.expires_in > 0 && token.expires_in <= 900,
        "token should have reasonable expires_in duration"
    );

    // Call upstream via proxy with malicious client x-scopes header; it must be stripped and replaced by canonical
    let res = client
        .get(format!("http://{proxy_addr}/echo"))
        .header("authorization", format!("Bearer {}", token.access_token))
        .header("x-scopes", "evil:root")
        .send()
        .await
        .unwrap();
    assert!(res.status().is_success());

    let echoed: BTreeMap<String, String> = res.json().await.unwrap();
    // Expect normalized, deduped scopes injected by proxy
    assert_eq!(
        echoed.get("x-scopes").cloned(),
        Some("data:read mcp:invoke".to_string())
    );
    // Ensure lowercased header names
    assert!(!echoed.contains_key("X-Scopes"));

    // Shutdown echo server task
    drop(echo_task);
}

#[tokio::test]
async fn oauth_scopes_absent_when_not_allowed_and_client_header_stripped() {
    use axum::{Json, Router, routing::get};
    use std::collections::BTreeMap;
    use std::net::Ipv4Addr;
    use tempfile::tempdir;

    // Upstream echo
    let echo_router = Router::new().route(
        "/echo",
        get(|headers: axum::http::HeaderMap| async move {
            let mut map = BTreeMap::new();
            for (name, value) in headers.iter() {
                map.insert(
                    name.as_str().to_ascii_lowercase(),
                    value.to_str().unwrap_or("").to_string(),
                );
            }
            Json(map)
        }),
    );
    let (echo_task, echo_addr) = {
        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
            .await
            .expect("bind echo");
        let addr = listener.local_addr().unwrap();
        let task = tokio::spawn(async move { axum::serve(listener, echo_router).await.unwrap() });
        (task, addr)
    };

    // Proxy + service with no allowed_scopes
    let tmp = tempdir().unwrap();
    let proxy = AuthenticatedProxy::new(tmp.path()).unwrap();
    let db = proxy.db();
    let service_id = ServiceId::new(9);
    let service = crate::models::ServiceModel {
        api_key_prefix: "test_".to_string(),
        owners: vec![],
        upstream_url: format!("http://{echo_addr}"),
        tls_profile: None,
    };
    service.save(service_id, &db).unwrap();
    let policy = ServiceOAuthPolicy {
        allowed_issuers: vec!["https://issuer.example.com".into()],
        required_audiences: vec![],
        public_keys_pem: vec![rsa_public_pem()],
        allowed_scopes: None, // scopes not allowed -> should not be forwarded
        require_dpop: false,
        max_access_token_ttl_secs: 900,
        max_assertion_ttl_secs: 120,
    };
    policy.save(service_id, &db).unwrap();

    let app = proxy.router();
    let listener = std::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
    listener.set_nonblocking(true).unwrap();
    let proxy_addr = listener.local_addr().unwrap();
    let tcp = tokio::net::TcpListener::from_std(listener).unwrap();
    tokio::spawn(async move { axum::serve(tcp, app).await.unwrap() });

    // Assertion with a scope but policy disallows -> Paseto will carry None, proxy must not inject x-scopes
    let current_time = now();
    let claims = Claims {
        iss: "https://issuer.example.com".into(),
        sub: "user-7".into(),
        aud: None,
        iat: current_time,
        exp: current_time + 60,
        jti: uuid::Uuid::new_v4().to_string(),
        scope: Some("logs:read".into()),
    };
    let jwt = encode(&Header::new(Algorithm::RS256), &claims, &rsa_encoding_key()).unwrap();

    let client = reqwest::Client::new();
    let token_res = client
        .post(format!("http://{proxy_addr}/v1/oauth/token"))
        .header("content-type", "application/x-www-form-urlencoded")
        .header("x-service-id", service_id.to_string())
        .body(format!(
            "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion={jwt}"
        ))
        .send()
        .await
        .unwrap();
    assert!(token_res.status().is_success());
    let token: TokenResponse = token_res.json().await.unwrap();

    // Validate token metadata fields
    assert_eq!(token.token_type, "Bearer", "token type should be 'Bearer'");
    assert!(
        token.expires_at > current_time,
        "token should have future expiration time"
    );
    assert!(
        token.expires_in > 0 && token.expires_in <= 900,
        "token should have reasonable expires_in duration"
    );

    let res = client
        .get(format!("http://{proxy_addr}/echo"))
        .header("authorization", format!("Bearer {}", token.access_token))
        .header("x-scopes", "logs:admin") // should be stripped, not forwarded
        .send()
        .await
        .unwrap();
    assert!(res.status().is_success());
    let echoed: BTreeMap<String, String> = res.json().await.unwrap();
    assert!(
        !echoed.contains_key("x-scopes"),
        "x-scopes must not be forwarded when policy disallows scopes"
    );

    drop(echo_task);
}