davidrs 0.1.1

A small, feature-gated framework for AWS Lambda functions: one ordered pipeline per trigger, absolute invocation deadlines, bounded work and safe built-in error responses.
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
//! The configurable access policy: identifying callers from gateway claims,
//! requiring them, selecting tenants and applying permission rules.
#![cfg(feature = "http")]

mod support;

use std::sync::Arc;

use davidrs::http::access::{Access, Claims, Grant, Refusals, Requirement, Tenancy};
use davidrs::http::{
    Api, ErrorDefinition, Failure, FailureKind, Json, PlainErrors, Policy, Request, StatusCode,
    codes,
};
use davidrs::{Context, Deadline, Invocation};
use serde_json::{Value, json};

/// The application's caller in these tests.
#[derive(Debug, Clone, PartialEq)]
struct User {
    id: String,
    tenants: Vec<String>,
    admin: bool,
}

/// Claims describe a user when they carry a subject.
fn user(claims: &Claims) -> Option<User> {
    Some(User {
        id: claims.subject()?.to_owned(),
        tenants: claims.list("tenants"),
        admin: claims.contains("groups", "admin"),
    })
}

/// Membership in a tenant, as the application defines it.
fn member(user: &User, tenant: &str) -> bool {
    user.tenants.iter().any(|own| own == tenant)
}

/// An HTTP API event, optionally carrying an authorizer and headers.
fn http_api(authorizer: Option<Value>, headers: Value) -> lambda_http::Request {
    let mut context = json!({
        "http": { "method": "GET", "path": "/orders", "sourceIp": "203.0.113.9" },
        "requestId": "r1",
        "stage": "$default",
        "timeEpoch": 0
    });
    if let Some(authorizer) = authorizer {
        context["authorizer"] = authorizer;
    }
    let event = json!({
        "version": "2.0",
        "rawPath": "/orders",
        "rawQueryString": "",
        "headers": headers,
        "requestContext": context,
        "isBase64Encoded": false
    });
    lambda_http::request::from_str(&event.to_string()).expect("an HTTP API event")
}

/// An HTTP API request whose JWT authorizer verified these claims.
fn signed_in(claims: Value, headers: Value) -> lambda_http::Request {
    http_api(
        Some(json!({ "jwt": { "claims": claims, "scopes": null } })),
        headers,
    )
}

fn alice() -> Value {
    json!({ "sub": "alice", "tenants": "[tenant-a tenant-b]", "groups": "[admin]" })
}

fn invocation() -> Invocation {
    Invocation::new("r1", Deadline::after(std::time::Duration::from_secs(5)))
}

async fn authorize<P: Policy>(
    policy: &P,
    request: &lambda_http::Request,
) -> Result<P::Scope, Failure> {
    policy
        .authorize(&Request::new(request), &invocation())
        .await
}

fn refused<T>(result: Result<T, Failure>) -> Failure {
    match result {
        Ok(_) => panic!("the policy should have refused"),
        Err(failure) => failure,
    }
}

#[tokio::test]
async fn an_optional_caller_is_identified_from_http_api_jwt_claims() {
    let grant = authorize(&Access::new(user), &signed_in(alice(), json!({})))
        .await
        .expect("grant");
    let caller = grant.caller().as_ref().expect("a caller");
    assert_eq!(caller.id, "alice");
    assert_eq!(caller.tenants, ["tenant-a", "tenant-b"]);
    assert!(caller.admin);
    assert_eq!(grant.tenant(), &());
}

#[tokio::test]
async fn an_anonymous_request_passes_an_optional_policy_without_a_caller() {
    let grant = authorize(&Access::new(user), &http_api(None, json!({})))
        .await
        .expect("grant");
    assert!(grant.caller().is_none());
}

#[tokio::test]
async fn a_request_built_by_hand_without_a_request_context_is_anonymous() {
    let request = lambda_http::http::Request::builder()
        .uri("https://example.com/orders")
        .body(lambda_http::Body::Empty)
        .expect("request");
    let grant = authorize(&Access::new(user), &request)
        .await
        .expect("grant");
    assert!(grant.caller().is_none());
}

#[tokio::test]
async fn a_required_caller_is_refused_with_401_when_nobody_is_identified() {
    let failure = refused(
        authorize(
            &Access::new(user).require_caller(),
            &http_api(None, json!({})),
        )
        .await,
    );
    assert_eq!(failure.status(), StatusCode::UNAUTHORIZED);
    assert_eq!(failure.code(), codes::UNAUTHENTICATED);
    assert_eq!(failure.kind(), FailureKind::Policy);
}

#[tokio::test]
async fn a_required_caller_reaches_the_grant_unwrapped() {
    let grant: Grant<User, ()> = authorize(
        &Access::new(user).require_caller(),
        &signed_in(alice(), json!({})),
    )
    .await
    .expect("grant");
    assert_eq!(grant.caller().id, "alice");
    let (caller, tenant) = grant.into_parts();
    assert_eq!((caller.id.as_str(), tenant), ("alice", ()));
}

#[tokio::test]
async fn claims_that_describe_no_caller_leave_the_request_anonymous() {
    let request = signed_in(json!({ "email": "no-subject@example.com" }), json!({}));
    assert!(
        authorize(&Access::new(user), &request)
            .await
            .expect("grant")
            .caller()
            .is_none()
    );
    let failure = refused(authorize(&Access::new(user).require_caller(), &request).await);
    assert_eq!(failure.code(), codes::UNAUTHENTICATED);
}

#[tokio::test]
async fn gateway_claims_can_be_ignored() {
    let policy = Access::new(user).gateway_claims(false);
    let grant = authorize(&policy, &signed_in(alice(), json!({})))
        .await
        .expect("grant");
    assert!(
        grant.caller().is_none(),
        "claims are not trusted when turned off"
    );
}

#[tokio::test]
async fn a_lambda_authorizer_context_is_read_as_claims() {
    let request = http_api(
        Some(json!({ "lambda": { "sub": "bob", "tenants": ["tenant-a"] } })),
        json!({}),
    );
    let grant = authorize(&Access::new(user).require_caller(), &request)
        .await
        .expect("grant");
    assert_eq!(grant.caller().id, "bob");
    assert_eq!(grant.caller().tenants, ["tenant-a"]);
}

#[tokio::test]
async fn a_member_selects_a_tenant_with_the_header() {
    let policy = Access::new(user)
        .require_caller()
        .tenancy(Tenancy::header("x-tenant-id", member));
    let grant = authorize(
        &policy,
        &signed_in(alice(), json!({ "x-tenant-id": " tenant-b " })),
    )
    .await
    .expect("grant");
    assert_eq!(
        grant.tenant().as_deref(),
        Some("tenant-b"),
        "the header is trimmed"
    );
}

#[tokio::test]
async fn a_caller_outside_the_requested_tenant_is_refused_with_403() {
    let policy = Access::new(user).tenancy(Tenancy::header("x-tenant-id", member));
    let failure = refused(
        authorize(
            &policy,
            &signed_in(alice(), json!({ "x-tenant-id": "tenant-c" })),
        )
        .await,
    );
    assert_eq!(failure.status(), StatusCode::FORBIDDEN);
    assert_eq!(failure.code(), codes::FORBIDDEN);
    assert_eq!(failure.kind(), FailureKind::Policy);
}

#[tokio::test]
async fn an_anonymous_request_that_names_a_tenant_is_refused() {
    let policy = Access::new(user).tenancy(Tenancy::header("x-tenant-id", member));
    let failure = refused(
        authorize(
            &policy,
            &http_api(None, json!({ "x-tenant-id": "tenant-a" })),
        )
        .await,
    );
    assert_eq!(failure.status(), StatusCode::FORBIDDEN);
    assert_eq!(failure.code(), codes::FORBIDDEN);
}

/// Only a public tenancy lets an anonymous request name a tenant, as context.
#[tokio::test]
async fn a_public_tenancy_lets_an_anonymous_caller_name_a_tenant_as_context() {
    let policy = Access::new(user).tenancy(Tenancy::header("x-tenant-id", member).public());
    let grant = authorize(
        &policy,
        &http_api(None, json!({ "x-tenant-id": "tenant-a" })),
    )
    .await
    .expect("grant");
    assert!(grant.caller().is_none());
    assert_eq!(grant.tenant().as_deref(), Some("tenant-a"));
    let outsider = refused(
        authorize(
            &policy,
            &signed_in(alice(), json!({ "x-tenant-id": "tenant-c" })),
        )
        .await,
    );
    assert_eq!(
        outsider.code(),
        codes::FORBIDDEN,
        "members are still checked"
    );
}

#[tokio::test]
async fn without_a_header_the_fallback_names_the_callers_tenant() {
    let only = |user: &User| (user.tenants.len() == 1).then(|| user.tenants[0].clone());
    let policy = Access::new(user).tenancy(Tenancy::header("x-tenant-id", member).or_else(only));
    let single = signed_in(
        json!({ "sub": "carol", "tenants": "[tenant-a]" }),
        json!({}),
    );
    assert_eq!(
        authorize(&policy, &single)
            .await
            .expect("grant")
            .tenant()
            .as_deref(),
        Some("tenant-a")
    );
    let several = signed_in(alice(), json!({ "x-tenant-id": "   " }));
    assert_eq!(
        authorize(&policy, &several).await.expect("grant").tenant(),
        &None,
        "a blank header is absent, and two tenants give no default"
    );
}

#[tokio::test]
async fn a_fallback_cannot_grant_a_tenant_outside_the_callers_membership() {
    let policy = Access::new(user)
        .tenancy(Tenancy::header("x-tenant-id", member).or_else(|_| Some("tenant-c".to_owned())));
    let failure = refused(authorize(&policy, &signed_in(alice(), json!({}))).await);
    assert_eq!(failure.code(), codes::FORBIDDEN);
}

#[tokio::test]
async fn without_a_header_or_a_fallback_no_tenant_is_selected() {
    let policy = Access::new(user).tenancy(Tenancy::header("x-tenant-id", member));
    assert_eq!(
        authorize(&policy, &signed_in(alice(), json!({})))
            .await
            .expect("grant")
            .tenant(),
        &None
    );
    assert_eq!(
        authorize(&policy, &http_api(None, json!({})))
            .await
            .expect("grant")
            .tenant(),
        &None
    );
}

#[tokio::test]
async fn a_required_tenant_is_refused_with_400_when_none_is_selected() {
    let policy = Access::new(user)
        .tenancy(Tenancy::header("x-tenant-id", member))
        .require_tenant();
    let failure = refused(authorize(&policy, &signed_in(alice(), json!({}))).await);
    assert_eq!(failure.status(), StatusCode::BAD_REQUEST);
    assert_eq!(failure.code(), codes::TENANT_REQUIRED);
    let grant: Grant<Option<User>, String> = authorize(
        &policy,
        &signed_in(alice(), json!({ "x-tenant-id": "tenant-a" })),
    )
    .await
    .expect("grant");
    assert_eq!(grant.tenant(), "tenant-a");
}

#[tokio::test]
async fn a_permission_rule_sees_the_caller_and_tenant_and_refuses_with_403() {
    let policy = Access::new(user)
        .tenancy(Tenancy::header("x-tenant-id", member).public())
        .permit(|user, tenant| user.is_some_and(|user| user.admin) && tenant == Some("tenant-a"));
    assert!(
        authorize(
            &policy,
            &signed_in(alice(), json!({ "x-tenant-id": "tenant-a" }))
        )
        .await
        .is_ok()
    );
    let wrong_tenant = refused(
        authorize(
            &policy,
            &signed_in(alice(), json!({ "x-tenant-id": "tenant-b" })),
        )
        .await,
    );
    assert_eq!(wrong_tenant.code(), codes::FORBIDDEN);
    let anonymous = refused(
        authorize(
            &policy,
            &http_api(None, json!({ "x-tenant-id": "tenant-a" })),
        )
        .await,
    );
    assert_eq!(
        anonymous.status(),
        StatusCode::FORBIDDEN,
        "the rule decides for anonymous callers too"
    );
}

/// Each refusal happens at its own step, so a request with several problems
/// always gets the earliest one.
#[tokio::test]
async fn the_order_of_checks_decides_which_refusal_wins() {
    let strict = Access::new(user)
        .require_caller()
        .tenancy(Tenancy::header("x-tenant-id", member))
        .require_tenant()
        .permit(|_, _| false);
    let anonymous = refused(
        authorize(
            &strict,
            &http_api(None, json!({ "x-tenant-id": "tenant-c" })),
        )
        .await,
    );
    assert_eq!(
        anonymous.code(),
        codes::UNAUTHENTICATED,
        "the caller before the tenant"
    );
    let outsider = refused(
        authorize(
            &strict,
            &signed_in(alice(), json!({ "x-tenant-id": "tenant-c" })),
        )
        .await,
    );
    assert_eq!(
        outsider.code(),
        codes::FORBIDDEN,
        "membership before anything later"
    );
    let unselected = refused(authorize(&strict, &signed_in(alice(), json!({}))).await);
    assert_eq!(
        unselected.code(),
        codes::TENANT_REQUIRED,
        "the tenant before the permission rule"
    );
    let denied = refused(
        authorize(
            &strict,
            &signed_in(alice(), json!({ "x-tenant-id": "tenant-a" })),
        )
        .await,
    );
    assert_eq!(denied.code(), codes::FORBIDDEN, "the permission rule last");
}

#[tokio::test]
async fn an_application_keeps_its_own_refusal_codes() {
    let errors = Refusals {
        unauthenticated: ErrorDefinition::new(
            "ERROR_SIGN_IN",
            StatusCode::UNAUTHORIZED,
            "Please sign in",
        ),
        ..Refusals::default()
    };
    let failure = refused(
        authorize(
            &Access::new(user).require_caller().refusals(errors),
            &http_api(None, json!({})),
        )
        .await,
    );
    assert_eq!(
        (failure.code(), failure.public_message()),
        ("ERROR_SIGN_IN", "Please sign in")
    );
}

#[test]
fn the_default_refusals_use_the_crate_codes() {
    let errors = Refusals::default();
    assert_eq!(
        [
            errors.invalid_token,
            errors.unauthenticated,
            errors.forbidden,
            errors.tenant_required
        ]
        .map(|definition| (definition.code, definition.status.as_u16())),
        [
            (codes::INVALID_TOKEN, 401),
            (codes::UNAUTHENTICATED, 401),
            (codes::FORBIDDEN, 403),
            (codes::TENANT_REQUIRED, 400)
        ]
    );
}

#[tokio::test]
async fn the_grant_reaches_the_handler_through_the_pipeline_and_refusals_are_rendered() {
    async fn whoami(
        _app: Arc<()>,
        _input: (),
        context: Context<Grant<User, ()>>,
    ) -> Result<Json<Value>, Failure> {
        Ok(Json(json!({ "id": context.scope().caller().id })))
    }
    let api = Api::new("whoami", Access::new(user).require_caller(), PlainErrors);
    let decode = |_: &Request<'_>| Ok(());
    let ok = api
        .handle(
            Arc::new(()),
            signed_in(alice(), json!({})),
            &decode,
            &whoami,
        )
        .await;
    assert_eq!(ok.status(), StatusCode::OK);
    assert_eq!(
        ok.body(),
        &lambda_http::Body::Text(r#"{"id":"alice"}"#.to_owned())
    );
    let refused = api
        .handle(Arc::new(()), http_api(None, json!({})), &decode, &whoami)
        .await;
    assert_eq!(refused.status(), StatusCode::UNAUTHORIZED);
    let lambda_http::Body::Text(body) = refused.body() else {
        panic!("a text body")
    };
    assert!(body.contains(codes::UNAUTHENTICATED));
}

#[test]
fn a_requirement_states_whether_a_missing_value_is_refused() {
    assert_eq!(
        [
            <String as Requirement<String>>::REQUIRED,
            <Option<String> as Requirement<String>>::REQUIRED,
            <() as Requirement<String>>::REQUIRED,
        ],
        [true, false, false]
    );
    assert_eq!(<String as Requirement<String>>::fulfill(None), None);
    assert_eq!(
        <Option<String> as Requirement<String>>::fulfill(None),
        Some(None)
    );
    assert_eq!(
        <() as Requirement<String>>::fulfill(Some("tenant-a".to_owned())),
        Some(())
    );
}

#[test]
fn claims_read_every_shape_a_list_arrives_in() {
    let claims = Claims::new(
        serde_json::from_value(json!({
            "sub": "alice",
            "array": ["a", 1, true, null, {}],
            "json_text": "[\"a\",\"b\"]",
            "bracketed": "[a b]",
            "spaced": " a  b ",
            "commas": "a,b, c",
            "empty": "",
            "number": 7
        }))
        .expect("claims"),
    );
    assert_eq!(claims.list("array"), ["a", "1", "true"]);
    assert_eq!(claims.list("json_text"), ["a", "b"]);
    assert_eq!(claims.list("bracketed"), ["a", "b"]);
    assert_eq!(claims.list("spaced"), ["a", "b"]);
    assert_eq!(claims.list("commas"), ["a", "b", "c"]);
    assert!(claims.list("empty").is_empty());
    assert!(
        claims.list("number").is_empty(),
        "a scalar that is not text is not a list"
    );
    assert!(claims.list("missing").is_empty());
    assert!(claims.contains("commas", "c"));
    assert!(!claims.contains("commas", "d"));
}

#[test]
fn claims_expose_single_values_and_the_whole_set() {
    let map: serde_json::Map<String, Value> =
        serde_json::from_value(json!({ "sub": "alice", "level": 3 })).expect("claims");
    let claims = Claims::from(map.clone());
    assert_eq!(claims.subject(), Some("alice"));
    assert_eq!(claims.string("level"), None, "only strings are text");
    assert_eq!(claims.get("level"), Some(&json!(3)));
    assert_eq!(claims.all(), &map);
    assert_eq!(Claims::default().subject(), None);
}

#[test]
fn debug_output_shows_the_configuration_and_no_closures() {
    let policy = Access::new(user).tenancy(
        Tenancy::header("x-tenant-id", member)
            .or_else(|_| None)
            .public(),
    );
    let text = format!("{policy:?}");
    assert!(text.contains("gateway_claims: true"), "{text}");
    assert!(text.contains("gateway_token_claims: false"), "{text}");
    assert!(text.contains("x-tenant-id"), "{text}");
    assert!(text.contains("fallback: true"), "{text}");
    assert!(text.contains("public: true"), "{text}");
}

/// The issuer of the tokens an authorizer verified in these tests.
const GATEWAY_ISSUER: &str = "https://id.example.com";

/// A JWT as the authorizer received it. The signature is never read here.
fn jwt(claims: &Value) -> String {
    use base64::Engine as _;
    let b64 = base64::engine::general_purpose::URL_SAFE_NO_PAD;
    format!(
        "{}.{}.c2ln",
        b64.encode(r#"{"alg":"RS256","kid":"k1"}"#),
        b64.encode(claims.to_string())
    )
}

/// An `Authorization` header carrying `claims` as a bearer token.
fn bearer(claims: &Value) -> Value {
    json!({ "authorization": format!("Bearer {}", jwt(claims)) })
}

/// Claims whose realm roles are nested in an object, as Keycloak writes them.
fn nested() -> Value {
    json!({
        "iss": GATEWAY_ISSUER,
        "sub": "alice",
        "exp": 4_102_444_800_u64,
        "realm_access": { "roles": ["admin"] }
    })
}

/// What an HTTP API JWT authorizer passes on for [`nested`]: strings only.
fn flattened() -> Value {
    json!({
        "iss": GATEWAY_ISSUER,
        "sub": "alice",
        "exp": "4102444800",
        "realm_access": "map[roles:[admin]]"
    })
}

#[test]
fn a_gateway_verified_token_keeps_the_json_types_of_its_claims() {
    let request = signed_in(flattened(), bearer(&nested()));
    let claims = Claims::from_gateway_token(&Request::new(&request)).expect("claims");
    assert_eq!(
        claims.get("realm_access"),
        Some(&json!({ "roles": ["admin"] }))
    );
    assert_eq!(claims.get("exp"), Some(&json!(4_102_444_800_u64)));
}

/// A token is read only where an authorizer ran and only when it is the
/// token the authorizer verified: no authorizer, another subject, another
/// issuer, gateway claims without an issuer, a value that is not a JWT, an
/// oversized token and a missing header all read as `None`.
#[test]
fn a_bearer_token_is_read_only_when_it_matches_the_gateway_claims() {
    let mut other_subject = nested();
    other_subject["sub"] = json!("mallory");
    let mut other_issuer = nested();
    other_issuer["iss"] = json!("https://id.example.org");
    let mut oversized = nested();
    oversized["padding"] = json!("x".repeat(9 * 1024));
    for request in [
        http_api(None, bearer(&nested())),
        signed_in(flattened(), bearer(&other_subject)),
        signed_in(flattened(), bearer(&other_issuer)),
        signed_in(json!({ "sub": "alice" }), bearer(&nested())),
        signed_in(flattened(), json!({ "authorization": "Bearer not-a-jwt" })),
        signed_in(flattened(), bearer(&oversized)),
        signed_in(flattened(), json!({})),
    ] {
        assert_eq!(Claims::from_gateway_token(&Request::new(&request)), None);
    }
}

#[tokio::test]
async fn gateway_token_claims_give_the_policy_nested_claims() {
    let admin = |claims: &Claims| {
        let roles = claims
            .get("realm_access")?
            .get("roles")?
            .as_array()?
            .clone();
        Some(roles.contains(&json!("admin")))
    };
    let request = signed_in(flattened(), bearer(&nested()));
    let flat = authorize(&Access::new(admin), &request)
        .await
        .expect("grant");
    assert_eq!(flat.caller(), &None);
    let structured = authorize(&Access::new(admin).gateway_token_claims(true), &request)
        .await
        .expect("grant");
    assert_eq!(structured.caller(), &Some(true));
}

/// A request whose token cannot be read falls back to the authorizer's own
/// claims.
#[tokio::test]
async fn gateway_token_claims_fall_back_to_the_authorizer_claims() {
    let request = signed_in(alice(), json!({}));
    let grant = authorize(&Access::new(user).gateway_token_claims(true), &request)
        .await
        .expect("grant");
    assert_eq!(
        grant.caller().as_ref().map(|user| user.id.as_str()),
        Some("alice")
    );
}

/// A REST API event whose authorizer attached these fields.
#[cfg(feature = "apigw-rest")]
fn rest_api(authorizer: Value) -> lambda_http::Request {
    let event = json!({
        "resource": "/orders",
        "path": "/orders",
        "httpMethod": "GET",
        "headers": {},
        "multiValueHeaders": {},
        "queryStringParameters": null,
        "requestContext": {
            "resourcePath": "/orders",
            "httpMethod": "GET",
            "path": "/prod/orders",
            "stage": "prod",
            "requestId": "r1",
            "accountId": "123456789012",
            "identity": { "sourceIp": "203.0.113.9" },
            "authorizer": authorizer
        },
        "body": null,
        "isBase64Encoded": false
    });
    lambda_http::request::from_str(&event.to_string()).expect("a REST API event")
}

#[cfg(feature = "apigw-rest")]
#[tokio::test]
async fn a_rest_api_cognito_authorizer_provides_its_claims() {
    let request = rest_api(json!({ "claims": { "sub": "dave", "cognito:groups": "admin,staff" } }));
    let grant = authorize(&Access::new(user).require_caller(), &request)
        .await
        .expect("grant");
    assert_eq!(grant.caller().id, "dave");
}

#[cfg(feature = "apigw-rest")]
#[tokio::test]
async fn a_rest_api_lambda_authorizer_context_is_read_as_claims() {
    let request = rest_api(json!({ "principalId": "erin", "sub": "erin", "tenants": "tenant-a" }));
    let grant = authorize(&Access::new(user).require_caller(), &request)
        .await
        .expect("grant");
    assert_eq!(
        (
            grant.caller().id.as_str(),
            grant.caller().tenants.as_slice()
        ),
        ("erin", &["tenant-a".to_owned()][..])
    );
}

#[cfg(feature = "apigw-rest")]
#[tokio::test]
async fn a_rest_api_request_without_an_authorizer_is_anonymous() {
    assert!(
        authorize(&Access::new(user), &rest_api(Value::Null))
            .await
            .expect("grant")
            .caller()
            .is_none()
    );
}

/// Bearer tokens verified by the function itself, against a local JWKS.
#[cfg(feature = "auth")]
mod bearer {
    use std::sync::Arc;

    use davidrs::http::access::Access;
    use davidrs::http::codes;
    use serde_json::json;

    use super::support::tokens::{ISSUER, Jwks, rs256, token};
    use super::{authorize, http_api, refused, signed_in, user};

    /// The audience every test token is issued for.
    const AUDIENCE: &str = "orders-api";

    /// A verifier over a JWKS publishing the test key as `k1`.
    async fn verifier() -> (Jwks, Arc<davidrs::auth::Verifier>) {
        let jwks = Jwks::serving(vec![super::support::tokens::jwk("k1")]).await;
        let verifier = Arc::new(
            jwks.load(
                davidrs::auth::VerifierConfig::new(ISSUER, jwks.server.url("/jwks"))
                    .with_audiences(vec![AUDIENCE.to_owned()]),
            )
            .await,
        );
        (jwks, verifier)
    }

    /// A signed token for `sub`, valid for an hour.
    fn signed_for(sub: &str) -> String {
        let claims = json!({ "iss": ISSUER, "aud": AUDIENCE, "sub": sub, "exp": super::support::tokens::now() + 3600 });
        token(&rs256("k1"), &claims)
    }

    #[tokio::test]
    async fn a_verified_bearer_token_identifies_the_caller_in_either_header_form() {
        let (_jwks, verifier) = verifier().await;
        let policy = Access::new(user).require_caller().verify_bearer(verifier);
        for header in [
            format!("Bearer {}", signed_for("frank")),
            format!("bearer  {}", signed_for("frank")),
            signed_for("frank"),
        ] {
            let grant = authorize(&policy, &http_api(None, json!({ "authorization": header })))
                .await
                .expect("grant");
            assert_eq!(grant.caller().id, "frank");
        }
    }

    #[tokio::test]
    async fn a_token_that_does_not_verify_is_refused_before_anything_else() {
        let (_jwks, verifier) = verifier().await;
        let policy = Access::new(user).require_caller().verify_bearer(verifier);
        let failure = refused(
            authorize(
                &policy,
                &http_api(None, json!({ "authorization": "Bearer not.a.token" })),
            )
            .await,
        );
        assert_eq!(failure.status(), davidrs::http::StatusCode::UNAUTHORIZED);
        assert_eq!(failure.code(), codes::INVALID_TOKEN);
    }

    #[tokio::test]
    async fn a_verified_token_whose_claims_describe_no_caller_is_invalid() {
        let (_jwks, verifier) = verifier().await;
        let claims =
            json!({ "iss": ISSUER, "aud": AUDIENCE, "exp": super::support::tokens::now() + 3600 });
        let request = http_api(
            None,
            json!({ "authorization": format!("Bearer {}", token(&rs256("k1"), &claims)) }),
        );
        let failure =
            refused(authorize(&Access::new(user).verify_bearer(verifier), &request).await);
        assert_eq!(failure.code(), codes::INVALID_TOKEN);
    }

    #[tokio::test]
    async fn another_authorization_scheme_is_not_a_caller_token() {
        let (jwks, verifier) = verifier().await;
        let before = jwks.fetches();
        let policy = Access::new(user).verify_bearer(verifier);
        for header in [
            "Basic dXNlcjpwYXNz",
            "Signed link-123",
            "Bearer ",
            "bearer",
            "   ",
        ] {
            let grant = authorize(&policy, &http_api(None, json!({ "authorization": header })))
                .await
                .expect("grant");
            assert!(
                grant.caller().is_none(),
                "{header:?} leaves the request anonymous"
            );
        }
        assert_eq!(jwks.fetches(), before, "nothing was verified");
    }

    #[tokio::test]
    async fn a_scheme_name_alone_is_not_a_caller_token() {
        let (jwks, verifier) = verifier().await;
        let before = jwks.fetches();
        let policy = Access::new(user).verify_bearer(verifier);
        for header in ["Basic", "Digest", "Signed", "Negotiate", "NTLM", "HOBA"] {
            let grant = authorize(&policy, &http_api(None, json!({ "authorization": header })))
                .await
                .expect("grant");
            assert!(
                grant.caller().is_none(),
                "{header:?} alone leaves the request anonymous"
            );
        }
        assert_eq!(jwks.fetches(), before, "nothing was verified");
    }

    #[tokio::test]
    async fn gateway_claims_win_over_a_bearer_token() {
        let (_jwks, verifier) = verifier().await;
        let request = signed_in(
            json!({ "sub": "alice" }),
            json!({ "authorization": format!("Bearer {}", signed_for("frank")) }),
        );
        let grant = authorize(&Access::new(user).verify_bearer(verifier), &request)
            .await
            .expect("grant");
        assert_eq!(
            grant.caller().as_ref().map(|user| user.id.as_str()),
            Some("alice")
        );
    }

    #[tokio::test]
    async fn without_gateway_trust_only_the_bearer_token_counts() {
        let (_jwks, verifier) = verifier().await;
        let request = signed_in(
            json!({ "sub": "alice" }),
            json!({ "authorization": format!("Bearer {}", signed_for("frank")) }),
        );
        let policy = Access::new(user)
            .gateway_claims(false)
            .verify_bearer(verifier);
        let grant = authorize(&policy, &request).await.expect("grant");
        assert_eq!(
            grant.caller().as_ref().map(|user| user.id.as_str()),
            Some("frank")
        );
        assert!(format!("{policy:?}").contains("verify_bearer: true"));
    }
}

#[test]
fn from_gateway_reads_the_claims_the_authorizer_verified_and_nothing_else() {
    let request = signed_in(alice(), json!({ "x-sub": "mallory" }));
    let claims = Claims::from_gateway(&Request::new(&request)).expect("claims");
    assert_eq!(claims.subject(), Some("alice"), "headers are never claims");
    assert_eq!(
        Claims::from_gateway(&Request::new(&http_api(None, json!({})))),
        None
    );
    let hand_built = lambda_http::http::Request::builder()
        .uri("https://example.com/orders")
        .body(lambda_http::Body::Empty)
        .expect("request");
    assert_eq!(Claims::from_gateway(&Request::new(&hand_built)), None);
}