camel-auth 0.29.0

Provider-neutral authentication and claim mapping for rust-camel
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
use async_trait::async_trait;
use std::sync::Arc;

use camel_api::security_policy::{
    AuthorizationDecision, CredentialSource, Principal, SecurityPolicy, principal_from_exchange,
    store_principal_properties,
};
use camel_api::{CamelError, Exchange};

use crate::credential_source::extract_token_from_exchange;
use crate::token_authenticator::TokenAuthenticator;

/// Property key used to store the authenticated principal in the exchange.
///
/// Re-exported from the camel-api contract so `camel_auth::built_in::PRINCIPAL_KEY`
/// and `camel_api::security_policy::PRINCIPAL_KEY` are the same constant.
pub use camel_api::security_policy::PRINCIPAL_KEY;

/// Extracts and validates a credential from the route-declared sources.
///
/// If a source yields a token, validates it via the supplied [`TokenAuthenticator`] and stores
/// the resulting [`Principal`] in `PRINCIPAL_KEY` for downstream processors.
///
/// If no source yields a token, the behavior depends on
/// `trust_upstream_principal`:
/// - `true`: falls back to an already-populated principal in the exchange
///   (e.g. set by an upstream authentication filter). **Spoofable** unless
///   the route topology guarantees property integrity.
/// - `false` (default): returns `Unauthenticated` — fail-closed. Use this
///   unless the deployment explicitly trusts an upstream producer to
///   authenticate and stamp the principal property.
async fn authenticate(
    exchange: &mut Exchange,
    authenticator: &dyn TokenAuthenticator,
    trust_upstream_principal: bool,
    sources: &[CredentialSource],
) -> Result<Principal, CamelError> {
    // Extraction returns an owned token, so the borrow on `exchange` ends before the mut borrow.
    let token = extract_token_from_exchange(exchange, sources).map(|extracted| extracted.token);

    if let Some(token) = token {
        let principal = authenticator.authenticate_bearer(&token).await?;
        // Store for downstream processors
        store_principal_properties(exchange, &principal);
        return Ok(principal);
    }

    if trust_upstream_principal {
        extract_principal_from_exchange(exchange)
    } else {
        Err(CamelError::Unauthenticated(
            "no Bearer token and trust_upstream_principal is false".into(),
        ))
    }
}

/// Extract a `Principal` from exchange properties, returning `Unauthenticated` if absent.
///
/// Delegates to the canonical `principal_from_exchange` reader so the trust
/// branch consumes the same JSON-string format that `store_principal_properties`
/// writes (a `from_value` read of a `to_string`-stored value silently missed the
/// principal and turned every `trust_upstream_principal` grant into a 500).
fn extract_principal_from_exchange(exchange: &Exchange) -> Result<Principal, CamelError> {
    principal_from_exchange(exchange)
        .ok_or_else(|| CamelError::Unauthenticated("no principal in exchange".into()))
}

/// Role-based access control policy.
///
/// Validates the incoming request via a token authenticator (Bearer token) and evaluates whether
/// the principal holds the required roles.
/// When `all_required` is true, every listed role must be present.
/// When `all_required` is false, at least one listed role must be present.
pub struct RolePolicy {
    required_roles: Vec<String>,
    all_required: bool,
    /// When `true`, fall back to the `camel.auth.principal` exchange property
    /// if no Bearer token is present. Default `false` (fail-closed) — see
    /// H1 in `docs/superpowers/specs/v1-sec-stabilization-spec.md`.
    trust_upstream_principal: bool,
    authenticator: Arc<dyn TokenAuthenticator>,
    credential_sources: Vec<CredentialSource>,
}

impl RolePolicy {
    pub fn new(
        required_roles: Vec<String>,
        all_required: bool,
        trust_upstream_principal: bool,
        authenticator: Arc<dyn TokenAuthenticator>,
        credential_sources: Vec<CredentialSource>,
    ) -> Self {
        Self {
            required_roles,
            all_required,
            trust_upstream_principal,
            authenticator,
            credential_sources,
        }
    }

    /// Credential sources the policy extracts tokens from, in declared order.
    pub fn credential_sources(&self) -> &[CredentialSource] {
        &self.credential_sources
    }
}

#[async_trait]
impl SecurityPolicy for RolePolicy {
    async fn evaluate(&self, exchange: &mut Exchange) -> Result<AuthorizationDecision, CamelError> {
        let principal = authenticate(
            exchange,
            &*self.authenticator,
            self.trust_upstream_principal,
            &self.credential_sources,
        )
        .await?;

        let missing: Vec<String> = self
            .required_roles
            .iter()
            .filter(|r| !principal.has_role(r))
            .cloned()
            .collect();

        let granted = if self.all_required {
            missing.is_empty()
        } else {
            self.required_roles.is_empty() || missing.len() < self.required_roles.len()
        };

        if granted {
            Ok(AuthorizationDecision::Granted { principal })
        } else {
            let actual = principal.roles.clone();
            Ok(AuthorizationDecision::Denied {
                reason: format!("missing required role(s): {}", missing.join(", ")), // allow-secret
                required: self.required_roles.clone(),
                actual,
            })
        }
    }
}

/// Scope-based access control policy.
///
/// Validates the incoming request via a token authenticator (Bearer token) and evaluates whether
/// the principal holds the required scopes.
/// When `all_required` is true, every listed scope must be present.
/// When `all_required` is false, at least one listed scope must be present.
pub struct ScopePolicy {
    required_scopes: Vec<String>,
    all_required: bool,
    /// When `true`, fall back to the `camel.auth.principal` exchange property
    /// if no Bearer token is present. Default `false` (fail-closed) — see
    /// H1 in `docs/superpowers/specs/v1-sec-stabilization-spec.md`.
    trust_upstream_principal: bool,
    authenticator: Arc<dyn TokenAuthenticator>,
    credential_sources: Vec<CredentialSource>,
}

impl ScopePolicy {
    pub fn new(
        required_scopes: Vec<String>,
        all_required: bool,
        trust_upstream_principal: bool,
        authenticator: Arc<dyn TokenAuthenticator>,
        credential_sources: Vec<CredentialSource>,
    ) -> Self {
        Self {
            required_scopes,
            all_required,
            trust_upstream_principal,
            authenticator,
            credential_sources,
        }
    }

    /// Credential sources the policy extracts tokens from, in declared order.
    pub fn credential_sources(&self) -> &[CredentialSource] {
        &self.credential_sources
    }
}

#[async_trait]
impl SecurityPolicy for ScopePolicy {
    async fn evaluate(&self, exchange: &mut Exchange) -> Result<AuthorizationDecision, CamelError> {
        let principal = authenticate(
            exchange,
            &*self.authenticator,
            self.trust_upstream_principal,
            &self.credential_sources,
        )
        .await?;

        let missing: Vec<String> = self
            .required_scopes
            .iter()
            .filter(|s| !principal.has_scope(s))
            .cloned()
            .collect();

        let granted = if self.all_required {
            missing.is_empty()
        } else {
            self.required_scopes.is_empty() || missing.len() < self.required_scopes.len()
        };

        if granted {
            Ok(AuthorizationDecision::Granted { principal })
        } else {
            let actual = principal.scopes.clone();
            Ok(AuthorizationDecision::Denied {
                reason: format!("missing required scope(s): {}", missing.join(", ")),
                required: self.required_scopes.clone(),
                actual,
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::jwt::JwtValidator;
    use crate::native_auth::{
        NativeCredential, NativeCredentialSecret, NativeCredentialStore, StaticTokenAuthenticator,
    };
    use crate::types::AuthError;
    use camel_api::Message;

    fn test_principal(roles: Vec<&str>, scopes: Vec<&str>) -> Principal {
        Principal {
            subject: "test-user".into(),
            issuer: "test".into(),
            audience: vec![],
            roles: roles.iter().map(|s| s.to_string()).collect(),
            scopes: scopes.iter().map(|s| s.to_string()).collect(),
            claims: serde_json::Value::Null,
        }
    }

    /// Mock validator that returns a fixed principal regardless of token content.
    struct MockJwtValidator {
        principal: Principal,
    }

    #[async_trait]
    impl JwtValidator for MockJwtValidator {
        async fn validate(&self, _token: &str) -> Result<Principal, AuthError> {
            Ok(self.principal.clone())
        }
    }

    fn mock_validator(principal: Principal) -> Arc<dyn TokenAuthenticator> {
        Arc::new(MockJwtValidator { principal })
    }

    /// Build a static authenticator over a native store seeded with `credential`.
    fn store_seeded_authenticator(
        credential: &str,
        principal: Principal,
    ) -> Arc<dyn TokenAuthenticator> {
        let store = NativeCredentialStore::try_new(vec![NativeCredential {
            secret: NativeCredentialSecret::Plaintext {
                value: zeroize::Zeroizing::new(credential.to_string()),
            },
            principal,
        }])
        .unwrap();
        Arc::new(StaticTokenAuthenticator::new(store))
    }

    /// Build an exchange with a Bearer token in the Authorization header.
    fn exchange_with_bearer(principal: Principal) -> Exchange {
        let mut msg = Message::default();
        msg.set_header(
            "Authorization",
            serde_json::Value::String("Bearer mock-token".into()),
        );
        // Also embed principal in exchange so fallback path is testable if needed.
        let mut ex = Exchange::new(msg);
        store_principal_properties(&mut ex, &principal);
        ex
    }

    /// Build an exchange with the principal in the exchange property (no Bearer header).
    ///
    /// Uses the canonical writer so the property format matches what the WS
    /// component produces and what the trust branch reads.
    fn exchange_with_principal(principal: Principal) -> Exchange {
        let mut ex = Exchange::new(Message::default());
        store_principal_properties(&mut ex, &principal);
        ex
    }

    #[tokio::test]
    async fn role_policy_grants_when_role_present() {
        let principal = test_principal(vec!["admin"], vec![]);
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            mock_validator(principal.clone()),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = exchange_with_bearer(principal);
        let decision = policy.evaluate(&mut ex).await.unwrap();
        assert!(matches!(decision, AuthorizationDecision::Granted { .. }));
    }

    #[tokio::test]
    async fn role_policy_denies_when_role_missing() {
        let principal = test_principal(vec!["user"], vec![]);
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            mock_validator(principal.clone()),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = exchange_with_bearer(principal);
        let decision = policy.evaluate(&mut ex).await.unwrap();
        assert!(matches!(decision, AuthorizationDecision::Denied { .. }));
    }

    #[tokio::test]
    async fn role_policy_any_required() {
        let principal = test_principal(vec!["user"], vec![]);
        let policy = RolePolicy::new(
            vec!["admin".into(), "user".into()],
            false,
            false,
            mock_validator(principal.clone()),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = exchange_with_bearer(principal);
        let decision = policy.evaluate(&mut ex).await.unwrap();
        assert!(matches!(decision, AuthorizationDecision::Granted { .. }));
    }

    #[tokio::test]
    async fn scope_policy_grants() {
        let principal = test_principal(vec![], vec!["read"]);
        let policy = ScopePolicy::new(
            vec!["read".into()],
            true,
            false,
            mock_validator(principal.clone()),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = exchange_with_bearer(principal);
        let decision = policy.evaluate(&mut ex).await.unwrap();
        assert!(matches!(decision, AuthorizationDecision::Granted { .. }));
    }

    #[tokio::test]
    async fn unauthenticated_when_no_principal_and_no_header() {
        // No Bearer header, no exchange property — validator never called
        struct FailValidator;
        #[async_trait]
        impl JwtValidator for FailValidator {
            async fn validate(&self, _token: &str) -> Result<Principal, AuthError> {
                panic!("should not be called")
            }
        }
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            Arc::new(FailValidator),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = Exchange::new(Message::default());
        let result = policy.evaluate(&mut ex).await;
        assert!(matches!(result, Err(CamelError::Unauthenticated(_))));
    }

    #[tokio::test]
    async fn principal_fallback_denied_by_default() {
        // No Bearer header, but principal pre-populated (upstream filter scenario).
        // Without `trust_upstream_principal` opt-in, MUST be denied.
        let principal = test_principal(vec!["admin"], vec![]);
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false, // trust_upstream_principal
            mock_validator(principal.clone()),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = exchange_with_principal(principal); // no Authorization header
        let result = policy.evaluate(&mut ex).await;
        assert!(matches!(result, Err(CamelError::Unauthenticated(_))));
    }

    #[tokio::test]
    async fn principal_fallback_allowed_with_opt_in() {
        // Same setup but `trust_upstream_principal=true` allows upstream-set principal.
        let principal = test_principal(vec!["admin"], vec![]);
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            true, // trust_upstream_principal
            mock_validator(principal.clone()),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = exchange_with_principal(principal); // no Authorization header
        let decision = policy.evaluate(&mut ex).await.unwrap();
        assert!(matches!(decision, AuthorizationDecision::Granted { .. }));
    }

    #[tokio::test]
    async fn scope_policy_fallback_denied_by_default() {
        // No Bearer header, principal pre-populated — Scopes policy also gates.
        let principal = test_principal(vec![], vec!["read"]);
        let policy = ScopePolicy::new(
            vec!["read".into()],
            true,
            false, // trust_upstream_principal
            mock_validator(principal.clone()),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = exchange_with_principal(principal);
        let result = policy.evaluate(&mut ex).await;
        assert!(matches!(result, Err(CamelError::Unauthenticated(_))));
    }

    #[test]
    fn role_policy_constructor_accepts_sources() {
        let authenticator = mock_validator(test_principal(vec![], vec![]));
        let sources = vec![CredentialSource::Cookie { name: "s".into() }];
        let policy = RolePolicy::new(
            vec!["r".into()],
            true,
            false,
            authenticator,
            sources.clone(),
        );
        assert_eq!(policy.credential_sources(), sources.as_slice());
    }

    // --- Task 1.2: multi-source extraction over the Exchange ---

    #[tokio::test]
    async fn authenticate_default_equals_bearer_prefix_strip() {
        // The pre-change path stripped "Bearer " from the Authorization header.
        // A real store seeded with the bare token proves the strip: a no-strip
        // or double-strip regression would miss the lookup and fail here.
        let principal = test_principal(vec!["admin"], vec![]);
        let store = NativeCredentialStore::try_new(vec![NativeCredential {
            secret: NativeCredentialSecret::Plaintext {
                value: zeroize::Zeroizing::new("mock-token".to_string()),
            },
            principal: principal.clone(),
        }])
        .unwrap();
        let authenticator: Arc<dyn TokenAuthenticator> =
            Arc::new(StaticTokenAuthenticator::new(store));
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            authenticator,
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = exchange_with_bearer(principal.clone());
        let decision = policy.evaluate(&mut ex).await.unwrap();
        match decision {
            AuthorizationDecision::Granted { principal: granted } => {
                assert_eq!(granted.subject, principal.subject);
            }
            other => panic!("expected Granted, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn authenticate_header_source_reads_authorization() {
        let principal = test_principal(vec!["admin"], vec![]);

        // Header present -> authenticates.
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            mock_validator(principal.clone()),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = exchange_with_bearer(principal.clone());
        let decision = policy.evaluate(&mut ex).await.unwrap();
        assert!(matches!(decision, AuthorizationDecision::Granted { .. }));

        // Header absent, no other source -> Unauthenticated.
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            mock_validator(principal),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = Exchange::new(Message::default());
        let result = policy.evaluate(&mut ex).await;
        assert!(matches!(result, Err(CamelError::Unauthenticated(_))));
    }

    #[tokio::test]
    async fn cookie_parse_malformed_is_absent_not_error() {
        let principal = test_principal(vec!["admin"], vec![]);
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            mock_validator(principal),
            vec![CredentialSource::Cookie {
                name: "session".into(),
            }],
        );
        let mut ex = Exchange::new(Message::default());
        ex.input.set_header(
            "Cookie",
            serde_json::Value::String("garbage-no-equals".into()),
        );
        // Absent source -> Unauthenticated. Never a parse panic, never another error.
        let result = policy.evaluate(&mut ex).await;
        assert!(matches!(result, Err(CamelError::Unauthenticated(_))));
    }

    #[tokio::test]
    async fn query_source_reads_camel_http_query_header() {
        let principal = test_principal(vec!["admin"], vec![]);
        let store = NativeCredentialStore::try_new(vec![NativeCredential {
            secret: NativeCredentialSecret::Plaintext {
                value: zeroize::Zeroizing::new("TOK".to_string()),
            },
            principal: principal.clone(),
        }])
        .unwrap();
        let authenticator: Arc<dyn TokenAuthenticator> =
            Arc::new(StaticTokenAuthenticator::new(store));
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            authenticator,
            vec![CredentialSource::QueryParam {
                param: "token".into(),
            }],
        );
        let mut ex = Exchange::new(Message::default());
        ex.input.set_header(
            "CamelHttpQuery",
            serde_json::Value::String("token=TOK".into()),
        );
        let decision = policy.evaluate(&mut ex).await.unwrap();
        assert!(matches!(decision, AuthorizationDecision::Granted { .. }));
    }

    #[tokio::test]
    async fn trust_false_preloaded_principal_unauthenticated() {
        let principal = test_principal(vec!["admin"], vec![]);
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false, // trust_upstream_principal
            mock_validator(principal.clone()),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = exchange_with_principal(principal); // preloaded principal, no credential
        let result = policy.evaluate(&mut ex).await;
        assert!(matches!(result, Err(CamelError::Unauthenticated(_))));
    }

    #[tokio::test]
    async fn trust_true_preloaded_principal_fallback() {
        let principal = test_principal(vec!["admin"], vec![]);
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            true, // trust_upstream_principal
            mock_validator(principal.clone()),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = exchange_with_principal(principal); // preloaded principal, no credential
        let decision = policy.evaluate(&mut ex).await.unwrap();
        assert!(matches!(decision, AuthorizationDecision::Granted { .. }));
    }

    #[tokio::test]
    async fn prefix_credential_unauthenticated() {
        // Store holds the full credential. A truncated prefix must never match
        // (constant-time exact compare in the shared store lookup).
        let principal = test_principal(vec!["admin"], vec![]);
        let store = NativeCredentialStore::try_new(vec![NativeCredential {
            secret: NativeCredentialSecret::Plaintext {
                value: zeroize::Zeroizing::new("SENTINEL_FULL_9kq2".to_string()),
            },
            principal: principal.clone(),
        }])
        .unwrap();
        let authenticator: Arc<dyn TokenAuthenticator> =
            Arc::new(StaticTokenAuthenticator::new(store));

        // Authorization header.
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            authenticator.clone(),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = Exchange::new(Message::default());
        ex.input.set_header(
            "Authorization",
            serde_json::Value::String("Bearer SENTINEL_FULL".into()),
        );
        let result = policy.evaluate(&mut ex).await;
        assert!(matches!(result, Err(CamelError::Unauthenticated(_))));

        // Cookie via exchange headers.
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            authenticator.clone(),
            vec![CredentialSource::Cookie {
                name: "session".into(),
            }],
        );
        let mut ex = Exchange::new(Message::default());
        ex.input.set_header(
            "Cookie",
            serde_json::Value::String("session=SENTINEL_FULL".into()),
        );
        let result = policy.evaluate(&mut ex).await;
        assert!(matches!(result, Err(CamelError::Unauthenticated(_))));

        // Query via the CamelHttpQuery header.
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            authenticator,
            vec![CredentialSource::QueryParam {
                param: "token".into(),
            }],
        );
        let mut ex = Exchange::new(Message::default());
        ex.input.set_header(
            "CamelHttpQuery",
            serde_json::Value::String("token=SENTINEL_FULL".into()),
        );
        let result = policy.evaluate(&mut ex).await;
        assert!(matches!(result, Err(CamelError::Unauthenticated(_))));
    }

    // --- Task 4.1: Header source ---

    #[tokio::test]
    async fn header_source_authenticates_api_key() {
        let principal = test_principal(vec!["admin"], vec![]);
        let store = NativeCredentialStore::try_new(vec![NativeCredential {
            secret: NativeCredentialSecret::Plaintext {
                value: zeroize::Zeroizing::new("SENTINEL_KEY_1".to_string()),
            },
            principal: principal.clone(),
        }])
        .unwrap();
        let authenticator: Arc<dyn TokenAuthenticator> =
            Arc::new(StaticTokenAuthenticator::new(store));
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            authenticator,
            vec![CredentialSource::Header {
                name: "x-api-key".into(),
            }],
        );
        let mut ex = Exchange::new(Message::default());
        ex.input.set_header(
            "X-API-Key",
            serde_json::Value::String("SENTINEL_KEY_1".into()),
        );
        let decision = policy.evaluate(&mut ex).await.unwrap();
        match decision {
            AuthorizationDecision::Granted { principal: granted } => {
                assert_eq!(granted.subject, principal.subject);
            }
            other => panic!("expected Granted, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn header_source_miss_maps_401() {
        let principal = test_principal(vec!["admin"], vec![]);
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            mock_validator(principal),
            vec![CredentialSource::Header {
                name: "x-api-key".into(),
            }],
        );
        // No X-API-Key header, no other source -> Unauthenticated.
        let mut ex = Exchange::new(Message::default());
        let result = policy.evaluate(&mut ex).await;
        assert!(matches!(result, Err(CamelError::Unauthenticated(_))));
    }

    #[tokio::test]
    async fn header_lookup_case_insensitive() {
        let principal = test_principal(vec!["admin"], vec![]);
        let store = NativeCredentialStore::try_new(vec![NativeCredential {
            secret: NativeCredentialSecret::Plaintext {
                value: zeroize::Zeroizing::new("SENTINEL_KEY_1".to_string()),
            },
            principal: principal.clone(),
        }])
        .unwrap();
        let authenticator: Arc<dyn TokenAuthenticator> =
            Arc::new(StaticTokenAuthenticator::new(store));
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            authenticator,
            vec![CredentialSource::Header {
                name: "x-api-key".into(),
            }],
        );
        let mut ex = Exchange::new(Message::default());
        // Header key casing differs from the declared source name.
        ex.input.set_header(
            "X-API-KEY",
            serde_json::Value::String("SENTINEL_KEY_1".into()),
        );
        let decision = policy.evaluate(&mut ex).await.unwrap();
        assert!(matches!(decision, AuthorizationDecision::Granted { .. }));
    }

    // --- RFC 9110/7235 bearer scheme semantics on the unified default path ---

    #[tokio::test]
    async fn bearer_scheme_lowercase_grants() {
        let principal = test_principal(vec!["admin"], vec![]);
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            store_seeded_authenticator("TOK", principal),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = Exchange::new(Message::default());
        ex.input.set_header(
            "Authorization",
            serde_json::Value::String("bearer TOK".into()),
        );
        let decision = policy.evaluate(&mut ex).await.unwrap();
        assert!(matches!(decision, AuthorizationDecision::Granted { .. }));
    }

    #[tokio::test]
    async fn bearer_scheme_uppercase_grants() {
        let principal = test_principal(vec!["admin"], vec![]);
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            store_seeded_authenticator("TOK", principal),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = Exchange::new(Message::default());
        ex.input.set_header(
            "Authorization",
            serde_json::Value::String("BEARER TOK".into()),
        );
        let decision = policy.evaluate(&mut ex).await.unwrap();
        assert!(matches!(decision, AuthorizationDecision::Granted { .. }));
    }

    #[tokio::test]
    async fn bearer_leading_whitespace_grants() {
        let principal = test_principal(vec!["admin"], vec![]);
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            store_seeded_authenticator("TOK", principal),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = Exchange::new(Message::default());
        ex.input.set_header(
            "Authorization",
            serde_json::Value::String(" Bearer TOK".into()),
        );
        let decision = policy.evaluate(&mut ex).await.unwrap();
        assert!(matches!(decision, AuthorizationDecision::Granted { .. }));
    }

    #[tokio::test]
    async fn bearer_double_space_yields_trimmed_token() {
        let principal = test_principal(vec!["admin"], vec![]);
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            store_seeded_authenticator("TOK", principal),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = Exchange::new(Message::default());
        ex.input.set_header(
            "Authorization",
            serde_json::Value::String("Bearer  TOK".into()),
        );
        let decision = policy.evaluate(&mut ex).await.unwrap();
        assert!(matches!(decision, AuthorizationDecision::Granted { .. }));
    }

    #[tokio::test]
    async fn bearer_empty_token_falls_to_trust_branch() {
        let principal = test_principal(vec!["admin"], vec![]);

        // trust=false: empty token yields no extraction -> Unauthenticated.
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            false,
            mock_validator(principal.clone()),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = exchange_with_principal(principal.clone());
        ex.input
            .set_header("Authorization", serde_json::Value::String("Bearer ".into()));
        let result = policy.evaluate(&mut ex).await;
        assert!(matches!(result, Err(CamelError::Unauthenticated(_))));

        // trust=true: empty token yields no extraction -> preloaded principal grants.
        let policy = RolePolicy::new(
            vec!["admin".into()],
            true,
            true,
            mock_validator(principal.clone()),
            vec![CredentialSource::AuthorizationHeader],
        );
        let mut ex = exchange_with_principal(principal);
        ex.input
            .set_header("Authorization", serde_json::Value::String("Bearer ".into()));
        let decision = policy.evaluate(&mut ex).await.unwrap();
        assert!(matches!(decision, AuthorizationDecision::Granted { .. }));
    }
}