plexus-auth-core 0.3.4

Sealed-type primitives for the Plexus auth framework: AuthContext, VerifiedUser, Principal.
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
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
//! Role + `ScopeRegistry` + inheritance — the hub-level roles→scopes source
//! of truth (R-0; revives AUTHZ-CORE-4 + AUTHZ-CORE-6 verbatim).
//!
//! Default-deny dispatch (AUTHZ-CORE-1/5, wired in R-5) and schema projection
//! (AUTHZ-PRIVACY-3) need a single source of truth that maps roles to scopes,
//! identifies which methods are public, and resolves a method's required
//! scope set. Per AUTHZ-S01-output §4: "When a hub routes to a child
//! activation, the child's required scopes are reported by the macro. The
//! hub-level `ScopeRegistry` is the **single source of truth** for which
//! roles map to which scopes." This separation lets a backend assemble
//! scope-aware activations from third-party crates without those crates
//! knowing the deployment's role taxonomy.
//!
//! # The role model (R-S01 §b, user-ratified)
//!
//! - **Scopes are atomic** capability identifiers (`facet.write`-style):
//!   dotted lowercase segments, segment-bounded trailing `.*` wildcard, bare
//!   `*` for "all scopes".
//! - **Roles are named scope-sets** with transitive inheritance,
//!   cycle-rejected at [`ScopeRegistryBuilder::build`] time.
//! - **Tokens carry roles** (the `roles` claim, minted by plexus-idp today);
//!   each backend's hub builds a `ScopeRegistry` mapping its deployment's
//!   role taxonomy to its scopes; the gate computes
//!   [`ScopeRegistry::effective_scopes`]`(AuthContext.roles)` and checks
//!   [`Scope::matches`]`(required)`.
//!
//! # Deferred / out of scope here
//!
//! - **Negative scopes** ("admin minus delete_message") are NOT implemented
//!   in v1 per AUTHZ-S01-output §4's Tier B resolution. A deployment wanting
//!   subtractive grants must model them as multiple roles. A future ticket
//!   can add the form if a real use case surfaces.
//! - **Gate/dispatch wiring** (`DynamicHub::with_scope_registry`, the
//!   default-deny consult) is R-5 territory (revives AUTHZ-CORE-1 + CORE-5);
//!   this module is pure types + algorithm.
//!
//! # Deployment responsibility
//!
//! A crate exposing a scope-decorated activation declares its required
//! scopes on its methods; the deploying hub's `ScopeRegistry` decides who
//! has those scopes. If the deploying hub forgets to include a role granting
//! the third-party scopes, those methods are unreachable under default-deny.
//! Auditing the registry against the composed activation set is a
//! deployment responsibility (see AUTHZ-FLOWS-3).

use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fmt;

use serde::{Deserialize, Serialize};

use crate::audit::RoleName;
use crate::capabilities::{is_valid_path_segment, MethodPath};
use crate::credential::Scope;

// ---------------------------------------------------------------------------
// Scope grammar + matching (AUTHZ-CORE-4; truth table pinned by AUTHZ-CORE-6)
// ---------------------------------------------------------------------------

/// Why a [`Scope::try_new`] rejected its input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScopeError {
    /// The input was empty.
    Empty,
    /// The input did not match the scope grammar (carries the offending
    /// input).
    InvalidFormat(String),
}

impl fmt::Display for ScopeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ScopeError::Empty => write!(f, "scope must not be empty"),
            ScopeError::InvalidFormat(s) => write!(
                f,
                "invalid scope {s:?}: expected bare `*`, dot-separated lowercase \
                 identifiers ([a-z][a-z0-9_]*), or such a path with a trailing `.*` \
                 segment wildcard"
            ),
        }
    }
}

impl std::error::Error for ScopeError {}

/// Is `s` a valid scope per the AUTHZ-S01-output §1/§4 grammar?
///
/// Grammar: bare `*`, OR dot-separated segments each matching
/// `[a-z][a-z0-9_]*`, OR such a path with one trailing `.*` wildcard
/// segment. Wildcards anywhere else are rejected.
fn is_valid_scope(s: &str) -> bool {
    if s == "*" {
        return true;
    }
    // Optional trailing `.*` — the body before it must be a non-empty valid
    // dotted path. `.*` alone has an empty body and is rejected.
    let body = s.strip_suffix(".*").unwrap_or(s);
    if body.is_empty() {
        return false;
    }
    body.split('.').all(is_valid_path_segment)
}

impl Scope {
    /// Construct a `Scope`, validating the canonical grammar.
    ///
    /// Accepts: bare `*`, dot-separated lowercase identifiers (each segment
    /// `[a-z][a-z0-9_]*`), and an optional trailing `.*` segment wildcard.
    /// Rejects everything else with a [`ScopeError`] carrying the offending
    /// input.
    ///
    /// Per AUTHZ-CORE-4. The pre-existing unvalidated [`Scope::new`]
    /// constructor remains for wire-decoded values; registry construction
    /// goes through this validating path.
    pub fn try_new(s: impl Into<String>) -> Result<Self, ScopeError> {
        let s = s.into();
        if s.is_empty() {
            return Err(ScopeError::Empty);
        }
        if !is_valid_scope(&s) {
            return Err(ScopeError::InvalidFormat(s));
        }
        Ok(Self::new(s))
    }

    /// True for the bare `*` and for any `<prefix>.*` form.
    pub fn is_wildcard(&self) -> bool {
        self.as_str() == "*" || self.as_str().ends_with(".*")
    }

    /// Does the holder of `self` satisfy the requirement `required`?
    ///
    /// Returns true iff:
    /// - `self` is the bare `*` (matches everything), OR
    /// - `self == required` (exact match), OR
    /// - `self` ends with `.*` AND its prefix-without-`.*` is a
    ///   **segment-prefix** of `required`.
    ///
    /// Segment-prefix matching is the subtle case: `cone.*` matches
    /// `cone.send_message` AND `cone.threads.list` (multiple downstream
    /// segments) but does NOT match `cone_extended.thing` — the `.` boundary
    /// is mandatory. A plain string-prefix check would silently match
    /// `cone_extended.thing`; that is the exemplar trap AUTHZ-CORE-6's
    /// boundary test pins against.
    ///
    /// The required side never carries wildcards in practice (a method's
    /// required scope is concrete); the holder side may. A required `*` is
    /// only satisfied by a held `*` (via the bare-`*` arm).
    pub fn matches(&self, required: &Scope) -> bool {
        let held = self.as_str();
        if held == "*" {
            return true;
        }
        if self == required {
            return true;
        }
        if let Some(prefix) = held.strip_suffix(".*") {
            let req = required.as_str();
            // Segment-bounded: the required scope must continue with a `.`
            // exactly at the prefix boundary.
            return req.len() > prefix.len()
                && req.starts_with(prefix)
                && req.as_bytes()[prefix.len()] == b'.';
        }
        false
    }
}

// ---------------------------------------------------------------------------
// RoleName grammar (AUTHZ-CORE-4)
// ---------------------------------------------------------------------------

/// Why a [`RoleName::try_new`] rejected its input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RoleNameError {
    /// The input was empty.
    Empty,
    /// The input did not match `[a-z][a-z0-9_]*` (carries the offending
    /// input).
    InvalidFormat(String),
}

impl fmt::Display for RoleNameError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RoleNameError::Empty => write!(f, "role name must not be empty"),
            RoleNameError::InvalidFormat(s) => {
                write!(f, "invalid role name {s:?}: expected [a-z][a-z0-9_]*")
            }
        }
    }
}

impl std::error::Error for RoleNameError {}

impl RoleName {
    /// Construct a `RoleName`, validating the `[a-z][a-z0-9_]*` grammar.
    ///
    /// Per AUTHZ-CORE-4. The pre-existing unvalidated [`RoleName::new`]
    /// constructor remains for wire-decoded values (token claims); registry
    /// construction goes through this validating path.
    pub fn try_new(s: impl Into<String>) -> Result<Self, RoleNameError> {
        let s = s.into();
        if s.is_empty() {
            return Err(RoleNameError::Empty);
        }
        if !is_valid_path_segment(&s) {
            return Err(RoleNameError::InvalidFormat(s));
        }
        Ok(Self::new(s))
    }
}

// ---------------------------------------------------------------------------
// Role
// ---------------------------------------------------------------------------

/// A named scope-set with transitive inheritance.
///
/// A `Role` carries a name, a list of granted scopes, an `inherits` list of
/// upstream roles whose scopes are unioned transitively at
/// [`ScopeRegistry::expand_role`] time, and a free-text description.
///
/// Construction performs no validation beyond the field types themselves
/// (per AUTHZ-CORE-4 "no validation beyond field-level"); referential
/// integrity of `inherits` and cycle-freedom are enforced when the role is
/// installed into a registry via [`ScopeRegistryBuilder::build`].
///
/// `Role` is intentionally NOT sealed — the registry's *contents* are
/// deployment-controlled; only the *types* are framework-owned (AUTHZ-0
/// §"Crate-level isolation amplifies the seal").
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Role {
    name: RoleName,
    scopes: Vec<Scope>,
    inherits: Vec<RoleName>,
    description: String,
}

impl Role {
    /// Construct a `Role` from already-typed fields.
    pub fn new(
        name: RoleName,
        scopes: Vec<Scope>,
        inherits: Vec<RoleName>,
        description: impl Into<String>,
    ) -> Self {
        Self {
            name,
            scopes,
            inherits,
            description: description.into(),
        }
    }

    /// The role's name.
    pub fn name(&self) -> &RoleName {
        &self.name
    }

    /// The scopes granted directly by this role (excluding inherited).
    pub fn scopes(&self) -> &[Scope] {
        &self.scopes
    }

    /// The upstream roles whose scopes this role unions transitively.
    pub fn inherits(&self) -> &[RoleName] {
        &self.inherits
    }

    /// Free-text description of the role.
    pub fn description(&self) -> &str {
        &self.description
    }
}

// ---------------------------------------------------------------------------
// ScopeRegistryError
// ---------------------------------------------------------------------------

/// Why [`ScopeRegistryBuilder::build`] rejected the declared taxonomy.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScopeRegistryError {
    /// A role name (declared or referenced in `inherits`) failed the
    /// `[a-z][a-z0-9_]*` grammar.
    InvalidRoleName(RoleNameError),
    /// A granted scope failed the scope grammar.
    InvalidScope(ScopeError),
    /// The same role name was declared twice. Silent override in an auth
    /// taxonomy is a footgun; redeclaration is rejected explicitly.
    DuplicateRole(RoleName),
    /// A role's `inherits` references a role that was never declared.
    UnknownInheritedRole {
        /// The role whose `inherits` list is dangling.
        role: RoleName,
        /// The undeclared role it references.
        inherits: RoleName,
    },
    /// The inheritance graph contains a cycle. Carries the cycle path
    /// (first element repeated at the end, e.g. `[a, b, a]`).
    InheritanceCycle {
        /// The roles forming the cycle, in walk order, closing on the first.
        path: Vec<RoleName>,
    },
    /// [`ScopeRegistryBuilder::inherits`] (or `description`) was called
    /// before any [`ScopeRegistryBuilder::role`] — there is no
    /// most-recently-added role to modify.
    InheritsBeforeRole,
}

impl fmt::Display for ScopeRegistryError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ScopeRegistryError::InvalidRoleName(e) => write!(f, "{e}"),
            ScopeRegistryError::InvalidScope(e) => write!(f, "{e}"),
            ScopeRegistryError::DuplicateRole(r) => {
                write!(f, "role {r} declared more than once")
            }
            ScopeRegistryError::UnknownInheritedRole { role, inherits } => write!(
                f,
                "role {role} inherits undeclared role {inherits}"
            ),
            ScopeRegistryError::InheritanceCycle { path } => {
                let rendered: Vec<&str> = path.iter().map(|r| r.as_str()).collect();
                write!(f, "role inheritance cycle: {}", rendered.join(" -> "))
            }
            ScopeRegistryError::InheritsBeforeRole => write!(
                f,
                "inherits()/description() called before any role() — nothing to modify"
            ),
        }
    }
}

impl std::error::Error for ScopeRegistryError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            ScopeRegistryError::InvalidRoleName(e) => Some(e),
            ScopeRegistryError::InvalidScope(e) => Some(e),
            _ => None,
        }
    }
}

impl From<RoleNameError> for ScopeRegistryError {
    fn from(e: RoleNameError) -> Self {
        ScopeRegistryError::InvalidRoleName(e)
    }
}

impl From<ScopeError> for ScopeRegistryError {
    fn from(e: ScopeError) -> Self {
        ScopeRegistryError::InvalidScope(e)
    }
}

// ---------------------------------------------------------------------------
// ScopeRegistry
// ---------------------------------------------------------------------------

/// Hub-level roles→scopes source of truth. Built once at hub construction
/// via [`ScopeRegistry::builder`], immutable thereafter.
///
/// Holds the role table, the set of public methods, and a per-method
/// explicit-scope overlay (used when the macro emits explicit `scope = ...`
/// literals; otherwise [`ScopeRegistry::required_scopes_for`] resolves the
/// implicit `<activation>.<method>` requirement at lookup time).
///
/// The lookups dispatch (AUTHZ-CORE-1/5, R-5) and schema projection
/// (AUTHZ-PRIVACY-3) consume:
/// - "what scopes are required for this method?" —
///   [`ScopeRegistry::required_scopes_for`]
/// - "is this method public?" — [`ScopeRegistry::is_public`]
/// - "expand this role to its transitive scope set" —
///   [`ScopeRegistry::expand_role`] / [`ScopeRegistry::effective_scopes`]
///
/// `Default` is the empty registry: no roles, no public methods, no
/// overlays. A hub without a registry is treated as this empty registry —
/// with default-deny OFF that is the harmless no-op; with default-deny ON
/// every gated method denies.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ScopeRegistry {
    roles: BTreeMap<RoleName, Role>,
    public_methods: BTreeSet<MethodPath>,
    method_overlays: BTreeMap<MethodPath, Vec<Scope>>,
}

impl ScopeRegistry {
    /// Start declaring a deployment's role taxonomy.
    pub fn builder() -> ScopeRegistryBuilder {
        ScopeRegistryBuilder::default()
    }

    /// Look up a declared role by name.
    pub fn role(&self, name: &RoleName) -> Option<&Role> {
        self.roles.get(name)
    }

    /// True iff the method was declared public via
    /// [`ScopeRegistryBuilder::public_method`].
    pub fn is_public(&self, method: &MethodPath) -> bool {
        self.public_methods.contains(method)
    }

    /// The scopes required to invoke `method`.
    ///
    /// Returns the explicit overlay if [`ScopeRegistryBuilder::method_scopes`]
    /// declared one; otherwise the implicit requirement derived from the
    /// method path itself.
    ///
    /// Implicit-scope note: AUTHZ-CORE-4 phrases the implicit rule as
    /// `<activation>.<method>`; for the canonical two-segment paths the full
    /// dotted path IS `<activation>.<method>`. For deeper paths
    /// (e.g. `solar.earth.luna.info`) this implementation uses the **full
    /// dotted path** as the implicit scope rather than
    /// first-segment + last-segment, because collapsing the middle segments
    /// would alias distinct methods (`solar.earth.luna.info` and
    /// `solar.mars.info` would both require `solar.info` — granting one
    /// would grant the other, an authorization hazard). Wildcard role grants
    /// (`solar.*`) still cover the whole subtree.
    pub fn required_scopes_for(&self, method: &MethodPath) -> Vec<Scope> {
        if let Some(explicit) = self.method_overlays.get(method) {
            return explicit.clone();
        }
        vec![Scope::new(method.as_str())]
    }

    /// The transitive union of scopes granted by `role`: its own scopes,
    /// plus its `inherits` roles' scopes, plus theirs, deduplicated.
    ///
    /// A role unknown to this registry expands to the empty set — token
    /// claims are external input, and a role the deployment never declared
    /// grants nothing (default-deny posture).
    ///
    /// Defense in depth: the walk carries a visited set, so it terminates
    /// even on a cyclic role graph. Cycles cannot arise through
    /// [`ScopeRegistryBuilder::build`] (which rejects them); the visited set
    /// is the layered second defense AUTHZ-CORE-6 risk 3 calls for.
    pub fn expand_role(&self, role: &RoleName) -> BTreeSet<Scope> {
        let mut out = BTreeSet::new();
        let mut visited: HashSet<&RoleName> = HashSet::new();
        let mut frontier: Vec<&RoleName> = vec![role];
        while let Some(name) = frontier.pop() {
            if !visited.insert(name) {
                continue;
            }
            if let Some(def) = self.roles.get(name) {
                out.extend(def.scopes.iter().cloned());
                frontier.extend(def.inherits.iter());
            }
        }
        out
    }

    /// The union of [`ScopeRegistry::expand_role`] over each held role —
    /// the holder's full effective scope set.
    ///
    /// Wildcard scopes (`*`, `<prefix>.*`) are preserved as held scopes in
    /// the result; the wildcard math happens at the use-site via
    /// [`Scope::matches`].
    pub fn effective_scopes(&self, roles: &[RoleName]) -> BTreeSet<Scope> {
        roles.iter().flat_map(|r| self.expand_role(r)).collect()
    }
}

// ---------------------------------------------------------------------------
// ScopeRegistryBuilder
// ---------------------------------------------------------------------------

/// One declared-but-not-yet-validated role inside the builder.
#[derive(Debug, Clone)]
struct RoleDraft {
    name: String,
    scopes: Vec<String>,
    inherits: Vec<String>,
    description: String,
}

/// Chainable declaration of a deployment's role taxonomy
/// (AUTHZ-S01-output §4's registration syntax).
///
/// Role names, scope strings, and inherits references are accepted as bare
/// strings for chaining ergonomics and validated together at
/// [`ScopeRegistryBuilder::build`] time — grammar violations, duplicate
/// roles, dangling `inherits`, and inheritance cycles all surface as
/// [`ScopeRegistryError`]s from `build()`, never as panics.
///
/// # Example
///
/// ```
/// use plexus_auth_core::scope_registry::ScopeRegistry;
/// use plexus_auth_core::MethodPath;
///
/// let registry = ScopeRegistry::builder()
///     .role("viewer", &["forms.list"])
///     .role("editor", &["forms.write"])
///     .inherits(&["viewer"])
///     .role("tenant_owner", &[])
///     .inherits(&["editor"])
///     .role("admin", &["*"])
///     .public_method(MethodPath::try_new("auth.login").unwrap())
///     .build()
///     .unwrap();
/// assert!(registry.is_public(&MethodPath::try_new("auth.login").unwrap()));
/// ```
///
/// # Wildcard grants are a development posture
///
/// `admin = {*}` (the bare-`*` grant) is implemented per AUTHZ-CORE-6 and is
/// useful in development. **Whether wildcard role grants are permitted in
/// production deployments is an open human question (R-S01 open question
/// 5); treat `*` grants as dev-posture until that call is made.** Production
/// taxonomies should prefer enumerated scopes or segment-bounded prefix
/// wildcards (`facet.*`) per activation.
#[derive(Debug, Clone, Default)]
pub struct ScopeRegistryBuilder {
    roles: Vec<RoleDraft>,
    public_methods: BTreeSet<MethodPath>,
    method_overlays: BTreeMap<MethodPath, Vec<Scope>>,
    modifier_before_role: bool,
}

impl ScopeRegistryBuilder {
    /// Declare a role granting the listed scopes. Returns the builder for
    /// chaining; subsequent [`ScopeRegistryBuilder::inherits`] /
    /// [`ScopeRegistryBuilder::description`] calls modify this role until
    /// the next `role()` call.
    pub fn role(mut self, name: impl Into<String>, scopes: &[&str]) -> Self {
        self.roles.push(RoleDraft {
            name: name.into(),
            scopes: scopes.iter().map(|s| (*s).to_string()).collect(),
            inherits: Vec::new(),
            description: String::new(),
        });
        self
    }

    /// Declare that the most-recently-added role inherits the listed roles'
    /// scopes transitively. Calling this before any `role()` is recorded
    /// and surfaces as [`ScopeRegistryError::InheritsBeforeRole`] at
    /// `build()` time.
    pub fn inherits(mut self, parents: &[&str]) -> Self {
        match self.roles.last_mut() {
            Some(draft) => draft
                .inherits
                .extend(parents.iter().map(|s| (*s).to_string())),
            None => self.modifier_before_role = true,
        }
        self
    }

    /// Attach a free-text description to the most-recently-added role.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        match self.roles.last_mut() {
            Some(draft) => draft.description = description.into(),
            None => self.modifier_before_role = true,
        }
        self
    }

    /// Declare a method publicly callable (no scope required).
    pub fn public_method(mut self, method: MethodPath) -> Self {
        self.public_methods.insert(method);
        self
    }

    /// Overlay explicit required scopes for a method (used when the macro
    /// emitted explicit `scope = ...` literals; otherwise the implicit
    /// `<activation>.<method>` rule applies at lookup time).
    pub fn method_scopes(mut self, method: MethodPath, scopes: &[Scope]) -> Self {
        self.method_overlays.insert(method, scopes.to_vec());
        self
    }

    /// Validate the declared taxonomy and freeze it into a
    /// [`ScopeRegistry`].
    ///
    /// Errors (in check order): modifier-before-role, role-name grammar,
    /// scope grammar, duplicate role, dangling `inherits` reference,
    /// inheritance cycle. Cycle detection is iterative (explicit stack) —
    /// no recursion, no stack overflow, no hang, regardless of taxonomy
    /// size (AUTHZ-CORE-4 acceptance 7).
    pub fn build(self) -> Result<ScopeRegistry, ScopeRegistryError> {
        if self.modifier_before_role {
            return Err(ScopeRegistryError::InheritsBeforeRole);
        }

        // Grammar validation + duplicate detection.
        let mut roles: BTreeMap<RoleName, Role> = BTreeMap::new();
        for draft in self.roles {
            let name = RoleName::try_new(draft.name)?;
            let scopes = draft
                .scopes
                .into_iter()
                .map(Scope::try_new)
                .collect::<Result<Vec<_>, _>>()?;
            let inherits = draft
                .inherits
                .into_iter()
                .map(RoleName::try_new)
                .collect::<Result<Vec<_>, _>>()?;
            if roles.contains_key(&name) {
                return Err(ScopeRegistryError::DuplicateRole(name));
            }
            roles.insert(
                name.clone(),
                Role::new(name, scopes, inherits, draft.description),
            );
        }

        // Referential integrity: every inherits target must be declared.
        for role in roles.values() {
            for parent in role.inherits() {
                if !roles.contains_key(parent) {
                    return Err(ScopeRegistryError::UnknownInheritedRole {
                        role: role.name().clone(),
                        inherits: parent.clone(),
                    });
                }
            }
        }

        // Cycle rejection (AUTHZ-CORE-4 acceptance 7).
        if let Some(path) = detect_inheritance_cycle(&roles) {
            return Err(ScopeRegistryError::InheritanceCycle { path });
        }

        Ok(ScopeRegistry {
            roles,
            public_methods: self.public_methods,
            method_overlays: self.method_overlays,
        })
    }
}

/// Find a cycle in the role-inheritance graph, if any, returning its path
/// (closing on the first repeated role, e.g. `[a, b, a]`).
///
/// Iterative three-color DFS with an explicit stack: bounded memory, no
/// recursion, terminates on every finite graph. Precondition: every
/// `inherits` target exists in `roles` (build checks referential integrity
/// first).
fn detect_inheritance_cycle(roles: &BTreeMap<RoleName, Role>) -> Option<Vec<RoleName>> {
    #[derive(Clone, Copy, PartialEq)]
    enum Color {
        /// On the current DFS path.
        Gray,
        /// Fully explored; cannot be part of an undiscovered cycle.
        Black,
    }

    let mut color: HashMap<&RoleName, Color> = HashMap::new();

    for start in roles.keys() {
        if color.contains_key(start) {
            continue;
        }
        // Stack of (role, index of next inherits-edge to explore).
        let mut stack: Vec<(&RoleName, usize)> = vec![(start, 0)];
        color.insert(start, Color::Gray);

        while let Some(&(node, edge_idx)) = stack.last() {
            let inherits = roles
                .get(node)
                .expect("referential integrity checked before cycle detection")
                .inherits();
            if edge_idx < inherits.len() {
                stack.last_mut().expect("stack non-empty inside loop").1 += 1;
                let child = &inherits[edge_idx];
                match color.get(child) {
                    Some(Color::Gray) => {
                        // Cycle: slice the current path from the first
                        // occurrence of `child`, then close on it.
                        let pos = stack
                            .iter()
                            .position(|(n, _)| *n == child)
                            .expect("gray node is on the current path");
                        let mut path: Vec<RoleName> =
                            stack[pos..].iter().map(|(n, _)| (*n).clone()).collect();
                        path.push(child.clone());
                        return Some(path);
                    }
                    Some(Color::Black) => {}
                    None => {
                        color.insert(child, Color::Gray);
                        stack.push((child, 0));
                    }
                }
            } else {
                color.insert(node, Color::Black);
                stack.pop();
            }
        }
    }
    None
}

// ---------------------------------------------------------------------------
// Tests (AUTHZ-CORE-4's unit matrix; AUTHZ-CORE-6's exhaustive truth table
// lives in tests/scope_registry.rs)
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    fn scope(s: &str) -> Scope {
        Scope::try_new(s).unwrap()
    }

    fn role_name(s: &str) -> RoleName {
        RoleName::try_new(s).unwrap()
    }

    fn method(s: &str) -> MethodPath {
        MethodPath::try_new(s).unwrap()
    }

    // -- Scope grammar: exhaustive accept/reject matrix (CORE-4 acceptance 2)

    #[test]
    fn scope_grammar_accepts_canonical_forms() {
        for ok in [
            "*",
            "forms",
            "forms.list",
            "forms.list.detail",
            "forms.*",
            "forms.list.*",
            "cone.send_message",
            "a1.b_2",
            "a_x.y9.*",
            "x",
        ] {
            assert!(
                Scope::try_new(ok).is_ok(),
                "grammar must accept {ok:?}"
            );
        }
    }

    #[test]
    fn scope_grammar_rejects_violations() {
        assert_eq!(Scope::try_new(""), Err(ScopeError::Empty));
        for bad in [
            ".",
            ".*",
            "*.*",
            "**",
            "*a",
            "a*",
            "a.b*",
            "a.*x",
            "a.*.b",
            "*.thing",
            "a.",
            ".a",
            "a..b",
            "a.b.",
            "A.b",
            "a.B",
            "Forms.List",
            "9a.b",
            "_a.b",
            "a-b.c",
            "a b",
            "a.b ",
            " a.b",
            "café.list",
        ] {
            assert_eq!(
                Scope::try_new(bad),
                Err(ScopeError::InvalidFormat(bad.to_string())),
                "grammar must reject {bad:?}"
            );
        }
    }

    #[test]
    fn scope_is_wildcard() {
        assert!(scope("*").is_wildcard());
        assert!(scope("cone.*").is_wildcard());
        assert!(scope("cone.threads.*").is_wildcard());
        assert!(!scope("cone.send_message").is_wildcard());
        assert!(!scope("forms").is_wildcard());
    }

    // -- Scope matching: the canonical six (CORE-4 acceptance 3; the full
    //    CORE-6 matrix lives in tests/scope_registry.rs)

    #[test]
    fn scope_matches_canonical_six() {
        assert!(scope("*").matches(&scope("cone.send_message")));
        assert!(scope("cone.*").matches(&scope("cone.send_message")));
        assert!(scope("cone.*").matches(&scope("cone.threads.list")));
        assert!(!scope("cone.*").matches(&scope("forms.list")));
        assert!(scope("cone.send_message").matches(&scope("cone.send_message")));
        assert!(!scope("cone.send_message").matches(&scope("cone.read_thread")));
    }

    // -- RoleName grammar (CORE-4 acceptance 4)

    #[test]
    fn role_name_grammar_accepts() {
        for ok in ["admin", "a", "tenant_owner", "r2d2", "x_9"] {
            assert!(
                RoleName::try_new(ok).is_ok(),
                "grammar must accept {ok:?}"
            );
        }
    }

    #[test]
    fn role_name_grammar_rejects() {
        assert_eq!(RoleName::try_new(""), Err(RoleNameError::Empty));
        for bad in ["Admin", "9x", "_x", "a.b", "a-b", "a b", "*", "ADMIN", "café"] {
            assert_eq!(
                RoleName::try_new(bad),
                Err(RoleNameError::InvalidFormat(bad.to_string())),
                "grammar must reject {bad:?}"
            );
        }
    }

    // -- Role construction (CORE-4: no validation beyond field-level)

    #[test]
    fn role_construction_carries_fields() {
        let r = Role::new(
            role_name("editor"),
            vec![scope("forms.write")],
            vec![role_name("viewer")],
            "can edit forms",
        );
        assert_eq!(r.name(), &role_name("editor"));
        assert_eq!(r.scopes(), &[scope("forms.write")]);
        assert_eq!(r.inherits(), &[role_name("viewer")]);
        assert_eq!(r.description(), "can edit forms");
    }

    // -- Builder: the §4 example taxonomy (CORE-4 acceptance 5 + 6)

    fn example_registry() -> ScopeRegistry {
        ScopeRegistry::builder()
            .role("viewer", &["forms.list"])
            .description("read-only access")
            .role("editor", &["forms.write"])
            .inherits(&["viewer"])
            .role("tenant_owner", &[])
            .inherits(&["editor"])
            .role("admin", &["*"])
            .public_method(method("auth.login"))
            .method_scopes(method("forms.export"), &[scope("forms.list")])
            .build()
            .expect("the §4 example taxonomy must build")
    }

    #[test]
    fn builder_example_taxonomy_expands_as_pinned() {
        let reg = example_registry();
        assert_eq!(
            reg.expand_role(&role_name("viewer")),
            BTreeSet::from([scope("forms.list")])
        );
        assert_eq!(
            reg.expand_role(&role_name("editor")),
            BTreeSet::from([scope("forms.write"), scope("forms.list")])
        );
        assert_eq!(
            reg.expand_role(&role_name("tenant_owner")),
            BTreeSet::from([scope("forms.write"), scope("forms.list")])
        );
        assert_eq!(
            reg.expand_role(&role_name("admin")),
            BTreeSet::from([scope("*")])
        );
    }

    #[test]
    fn builder_role_lookup_carries_description() {
        let reg = example_registry();
        let viewer = reg.role(&role_name("viewer")).expect("viewer declared");
        assert_eq!(viewer.description(), "read-only access");
    }

    // -- is_public (CORE-4 acceptance 10)

    #[test]
    fn is_public_true_only_for_declared_methods() {
        let reg = example_registry();
        assert!(reg.is_public(&method("auth.login")));
        assert!(!reg.is_public(&method("forms.export")));
        assert!(!reg.is_public(&method("cone.send_message")));
    }

    // -- required_scopes_for (CORE-4 acceptance 9)

    #[test]
    fn required_scopes_explicit_overlay_wins() {
        let reg = example_registry();
        assert_eq!(
            reg.required_scopes_for(&method("forms.export")),
            vec![scope("forms.list")]
        );
    }

    #[test]
    fn required_scopes_implicit_is_the_method_path() {
        let reg = example_registry();
        assert_eq!(
            reg.required_scopes_for(&method("cone.send_message")),
            vec![Scope::new("cone.send_message")]
        );
    }

    // -- Cycle rejection (CORE-4 acceptance 7)

    #[test]
    fn build_rejects_two_role_cycle_with_path() {
        let err = ScopeRegistry::builder()
            .role("a", &["x.one"])
            .inherits(&["b"])
            .role("b", &["y.one"])
            .inherits(&["a"])
            .build()
            .expect_err("cycle must be rejected at build()");
        match err {
            ScopeRegistryError::InheritanceCycle { path } => {
                // Path closes on its first element and contains both roles.
                assert_eq!(path.first(), path.last());
                assert!(path.contains(&role_name("a")));
                assert!(path.contains(&role_name("b")));
                assert_eq!(path.len(), 3, "a->b->a (closed) has length 3");
            }
            other => panic!("expected InheritanceCycle, got {other:?}"),
        }
    }

    #[test]
    fn build_rejects_self_cycle() {
        let err = ScopeRegistry::builder()
            .role("a", &["x.one"])
            .inherits(&["a"])
            .build()
            .expect_err("self-cycle must be rejected at build()");
        assert!(matches!(
            err,
            ScopeRegistryError::InheritanceCycle { ref path } if path == &vec![role_name("a"), role_name("a")]
        ));
    }

    #[test]
    fn build_rejects_deep_cycle_without_overflow() {
        // A long chain ending in a cycle: r0 -> r1 -> ... -> r9999 -> r0.
        // Iterative DFS must neither overflow nor hang.
        let n = 10_000;
        let names: Vec<String> = (0..n).map(|i| format!("r{i}")).collect();
        let mut b = ScopeRegistry::builder();
        for i in 0..n {
            let parent = names[(i + 1) % n].clone();
            b = b.role(names[i].as_str(), &[]).inherits(&[parent.as_str()]);
        }
        let err = b.build().expect_err("the loop must be rejected");
        assert!(matches!(err, ScopeRegistryError::InheritanceCycle { .. }));
    }

    #[test]
    fn build_accepts_diamond_inheritance() {
        // a -> b, a -> c, b -> d, c -> d: shared ancestor, no cycle.
        let reg = ScopeRegistry::builder()
            .role("d", &["base.read"])
            .role("b", &["b.write"])
            .inherits(&["d"])
            .role("c", &["c.write"])
            .inherits(&["d"])
            .role("a", &[])
            .inherits(&["b", "c"])
            .build()
            .expect("diamond is acyclic");
        assert_eq!(
            reg.expand_role(&role_name("a")),
            BTreeSet::from([scope("base.read"), scope("b.write"), scope("c.write")])
        );
    }

    // -- Builder error surfaces

    #[test]
    fn build_rejects_unknown_inherited_role() {
        let err = ScopeRegistry::builder()
            .role("a", &["x.one"])
            .inherits(&["ghost"])
            .build()
            .expect_err("dangling inherits must be rejected");
        assert_eq!(
            err,
            ScopeRegistryError::UnknownInheritedRole {
                role: role_name("a"),
                inherits: role_name("ghost"),
            }
        );
    }

    #[test]
    fn build_rejects_duplicate_role() {
        let err = ScopeRegistry::builder()
            .role("a", &["x.one"])
            .role("a", &["y.one"])
            .build()
            .expect_err("redeclaration must be rejected");
        assert_eq!(err, ScopeRegistryError::DuplicateRole(role_name("a")));
    }

    #[test]
    fn build_rejects_invalid_role_name() {
        let err = ScopeRegistry::builder()
            .role("Admin", &["x.one"])
            .build()
            .expect_err("bad role name must be rejected");
        assert!(matches!(err, ScopeRegistryError::InvalidRoleName(_)));
    }

    #[test]
    fn build_rejects_invalid_scope() {
        let err = ScopeRegistry::builder()
            .role("a", &["Bad.Scope"])
            .build()
            .expect_err("bad scope must be rejected");
        assert!(matches!(err, ScopeRegistryError::InvalidScope(_)));
    }

    #[test]
    fn build_rejects_inherits_before_role() {
        let err = ScopeRegistry::builder()
            .inherits(&["a"])
            .role("a", &["x.one"])
            .build()
            .expect_err("inherits() before role() must be rejected");
        assert_eq!(err, ScopeRegistryError::InheritsBeforeRole);
    }

    // -- Empty/default registry (CORE-4 "hub without registry" row)

    #[test]
    fn default_registry_is_empty() {
        let reg = ScopeRegistry::default();
        assert!(!reg.is_public(&method("auth.login")));
        assert!(reg.effective_scopes(&[role_name("admin")]).is_empty());
        assert_eq!(
            reg.required_scopes_for(&method("cone.send_message")),
            vec![Scope::new("cone.send_message")]
        );
        assert_eq!(reg, ScopeRegistry::builder().build().unwrap());
    }

    // -- Defense-in-depth smoke test (CORE-6 acceptance 5): a cyclic graph
    //    injected by direct internal manipulation (bypassing build()) must
    //    not hang or overflow expand_role; the visited set bounds the walk.

    #[test]
    fn expand_role_terminates_on_injected_cycle() {
        let mut roles = BTreeMap::new();
        roles.insert(
            role_name("a"),
            Role::new(role_name("a"), vec![scope("x.one")], vec![role_name("b")], ""),
        );
        roles.insert(
            role_name("b"),
            Role::new(role_name("b"), vec![scope("y.one")], vec![role_name("a")], ""),
        );
        // Direct struct construction — possible only inside the crate; the
        // builder (the only public path) rejects this shape. AUTHZ-CORE-4's
        // build()-time rejection is the primary defense; this is layered.
        let reg = ScopeRegistry {
            roles,
            public_methods: BTreeSet::new(),
            method_overlays: BTreeMap::new(),
        };
        assert_eq!(
            reg.expand_role(&role_name("a")),
            BTreeSet::from([scope("x.one"), scope("y.one")])
        );
    }
}