plexus-auth-core 0.2.1

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
//! [`Principal`] — an issuer-namespaced subject.
//!
//! This is the **subject-name** half of the two-`Principal` split documented
//! in [`crate::identity`]. It answers *which* identity, and *who vouches for
//! it*; [`crate::principal::Principal`] answers *what kind of actor* is on
//! the other end of the call.
//!
//! # Relationship to `plexus_core::identity::Principal`
//!
//! They are the **same type**. PLX-75 originally landed a type of the same
//! name, grammar, and wire form in `plexus-core`; PLX-82 discovered that
//! `plexus-core` depends on `plexus-auth-core` and never the reverse, so
//! `AuthContext` (which lives here) could not name it, and wrote a mirror
//! here kept in sync by an equivalence test. PLX-87 collapsed the two:
//! **this is the one definition**, and `plexus_core::identity` is a
//! `pub use` of this module's items, so `plexus_core::identity::Principal`
//! keeps working for every consumer PLX-75 wrote for. There is nothing left
//! to keep in sync, so the equivalence test
//! (`plexus-idp/tests/principal_equivalence.rs`) was deleted with the
//! duplication it guarded.

use std::fmt;
use std::str::FromStr;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// The separator between issuer and subject in a principal's string form.
const SEP: char = ':';

/// Who vouches for a [`Principal`]'s subject.
///
/// The set is deliberately **open for extension but closed for guessing**:
///
/// - `#[non_exhaustive]` lets a later build add an authenticator (NIP-05,
///   WebAuthn, mTLS, …) without it being a breaking change for downstream
///   `match` arms; but
/// - [`Issuer::parse`] has an explicit arm per known issuer and *rejects*
///   anything else. An unknown prefix is a [`PrincipalParseError::UnknownIssuer`],
///   never a silently-accepted opaque string. Adding an issuer therefore
///   requires adding its validation rule in the same commit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Issuer {
    /// plexus-idp: a server-minted UUID v4 (`plexus-idp/src/core.rs`).
    Idp,
    /// A self-sovereign nostr identity: a 32-byte secp256k1 x-only pubkey.
    Nostr,
    /// A machine credential minted as an API key.
    ApiKey,
    /// A subject an OIDC provider other than plexus-idp vouches for, behind a
    /// provider adapter: `oidc:<provider>/<sub>` (IDENTITY-13). The provider
    /// is the adapter's configured name (`auth0`), never the issuer URL.
    Oidc,
    /// An Ed25519 public key an agent holds (IDENTITY-41): `key:<64 hex>`,
    /// the 32-byte key in lowercase hex. A key proof (`plexus-key.…`,
    /// `adapters::KeyProofAdapter`) proves it.
    Key,
}

impl Issuer {
    /// The issuer's wire prefix.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Idp => "idp",
            Self::Nostr => "nostr",
            Self::ApiKey => "apikey",
            Self::Oidc => "oidc",
            Self::Key => "key",
        }
    }

    /// Parse an issuer prefix. Unknown prefixes are an error, not a new issuer.
    pub fn parse(s: &str) -> Result<Self, PrincipalParseError> {
        match s {
            "idp" => Ok(Self::Idp),
            "nostr" => Ok(Self::Nostr),
            "apikey" => Ok(Self::ApiKey),
            "oidc" => Ok(Self::Oidc),
            "key" => Ok(Self::Key),
            other => Err(PrincipalParseError::UnknownIssuer(other.to_string())),
        }
    }

    /// Validate a subject against this issuer's rules.
    fn validate(self, subject: &str) -> Result<(), PrincipalParseError> {
        if subject.is_empty() {
            return Err(PrincipalParseError::EmptySubject(self));
        }
        if subject.contains(SEP) {
            return Err(PrincipalParseError::SubjectContainsSeparator(self));
        }

        match self {
            // A canonical lowercase hyphenated UUID, exactly as idp mints it
            // (`Uuid::new_v4().to_string()`). Braced/URN/simple forms are
            // rejected so that one identity has exactly one rendering and
            // `Display`/`FromStr` round-trip byte-for-byte.
            Self::Idp => {
                let parsed = uuid::Uuid::parse_str(subject)
                    .map_err(|_| PrincipalParseError::InvalidUuid(subject.to_string()))?;
                if parsed.hyphenated().to_string() != subject {
                    return Err(PrincipalParseError::InvalidUuid(subject.to_string()));
                }
                Ok(())
            }

            // 64 lowercase hex characters — the x-only pubkey encoding every
            // nostr implementation uses. Uppercase is rejected rather than
            // normalized: normalizing would break the exact round-trip and
            // give one key two principals.
            Self::Nostr => lower_hex_64(subject).map_err(|reason| {
                PrincipalParseError::InvalidNostrPubkey { subject: subject.to_string(), reason }
            }),

            // IDENTITY-41: the Ed25519 public key, 64 lowercase hex
            // characters — uppercase refused for the same reason as nostr's.
            Self::Key => lower_hex_64(subject).map_err(|reason| {
                PrincipalParseError::InvalidKeyPubkey { subject: subject.to_string(), reason }
            }),

            // An opaque key id. Constrained to printable, non-whitespace ASCII
            // so a principal is always safely renderable in a log line, a URL
            // segment, or a header value.
            Self::ApiKey => {
                if subject
                    .bytes()
                    .any(|b| !b.is_ascii_graphic() || b == b'"' || b == b'\\')
                {
                    return Err(PrincipalParseError::InvalidApiKeyId(subject.to_string()));
                }
                Ok(())
            }

            // `<provider>/<sub>`: a provider name of 1–32 lowercase letters,
            // digits and hyphens, then the provider's subject as
            // `Principal::oidc` encoded it — printable ASCII with `:`, `%`,
            // quotes and backslashes percent-encoded, so every subject a
            // provider can mint has exactly one rendering.
            Self::Oidc => {
                let invalid = |reason: &'static str| PrincipalParseError::InvalidOidcSubject {
                    subject: subject.to_string(),
                    reason,
                };
                let (provider, sub) = subject
                    .split_once('/')
                    .ok_or_else(|| invalid("expected `<provider>/<sub>`"))?;
                if !valid_provider_name(provider) {
                    return Err(invalid("the provider name must be 1-32 of a-z, 0-9 and -"));
                }
                if sub.is_empty() {
                    return Err(invalid("the provider's subject is empty"));
                }
                if sub.bytes().any(|b| !b.is_ascii_graphic() || b == b'"' || b == b'\\') {
                    return Err(invalid("the provider's subject is not percent-encoded"));
                }
                Ok(())
            }
        }
    }
}

/// 64 lowercase hex characters (a 32-byte key), or which rule broke.
fn lower_hex_64(subject: &str) -> Result<(), &'static str> {
    if subject.len() != 64 {
        return Err("expected exactly 64 hex characters");
    }
    if !subject.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) {
        return Err("expected lowercase hex only");
    }
    Ok(())
}

/// An OIDC adapter's provider name: 1–32 of `a-z`, `0-9` and `-`.
pub(crate) fn valid_provider_name(name: &str) -> bool {
    !name.is_empty()
        && name.len() <= 32
        && name.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
}

impl fmt::Display for Issuer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Why a string is not a valid [`Principal`].
///
/// Intentionally **not** `#[non_exhaustive]`: `plexus_core::identity::`
/// `PrincipalParseError` re-exports this type (PLX-87), and PLX-75's original
/// was exhaustive, so downstream `match` arms written against the plexus-core
/// path keep compiling. The `Display` strings are byte-identical to PLX-75's
/// for the same reason.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PrincipalParseError {
    /// No `:` separator at all.
    MissingSeparator(String),

    /// The prefix is not a known issuer.
    UnknownIssuer(String),

    /// The subject was empty.
    EmptySubject(Issuer),

    /// The subject contained a second `:`, making the rendering ambiguous.
    SubjectContainsSeparator(Issuer),

    /// `idp:` subject was not a canonical hyphenated UUID.
    InvalidUuid(String),

    /// `nostr:` subject was not a 64-character lowercase hex pubkey.
    InvalidNostrPubkey {
        /// The rejected subject.
        subject: String,
        /// Which rule it broke.
        reason: &'static str,
    },

    /// `apikey:` subject contained non-printable or quoting characters.
    InvalidApiKeyId(String),

    /// `oidc:` subject was not `<provider>/<sub>` (IDENTITY-13).
    ///
    /// Added after the enum was pinned exhaustive for PLX-87's re-export: a
    /// downstream `match` with no wildcard arm needs this arm. No crate in
    /// the workspace has one.
    InvalidOidcSubject {
        /// The rejected subject.
        subject: String,
        /// Which rule it broke.
        reason: &'static str,
    },

    /// `key:` subject was not a 64-character lowercase hex Ed25519 public
    /// key (IDENTITY-41). Added after the enum was pinned exhaustive, as
    /// `InvalidOidcSubject` was.
    InvalidKeyPubkey {
        /// The rejected subject.
        subject: String,
        /// Which rule it broke.
        reason: &'static str,
    },
}

impl fmt::Display for PrincipalParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::MissingSeparator(s) => write!(
                f,
                "principal `{s}` has no `{SEP}` separator; expected `<issuer>{SEP}<subject>`"
            ),
            Self::UnknownIssuer(s) => write!(
                f,
                "unknown principal issuer `{s}`; known issuers: idp, nostr, apikey, oidc, key"
            ),
            Self::EmptySubject(i) => write!(f, "principal issuer `{i}` has an empty subject"),
            Self::SubjectContainsSeparator(i) => write!(
                f,
                "principal subject for issuer `{i}` contains a `{SEP}`, which is ambiguous"
            ),
            Self::InvalidUuid(s) => {
                write!(f, "principal subject `{s}` is not a canonical hyphenated UUID")
            }
            Self::InvalidNostrPubkey { subject, reason } => write!(
                f,
                "principal subject `{subject}` is not a valid nostr pubkey: {reason}"
            ),
            Self::InvalidApiKeyId(s) => {
                write!(f, "principal subject `{s}` is not a valid api key id")
            }
            Self::InvalidOidcSubject { subject, reason } => write!(
                f,
                "principal subject `{subject}` is not a valid oidc subject: {reason}"
            ),
            Self::InvalidKeyPubkey { subject, reason } => write!(
                f,
                "principal subject `{subject}` is not a valid Ed25519 public key: {reason}"
            ),
        }
    }
}

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

/// An issuer-namespaced subject: `idp:<uuid>`, `nostr:<64-hex>`, `apikey:<id>`.
///
/// # Invariants
///
/// The fields are private and both constructors ([`Principal::new`] and
/// [`FromStr`]) validate, so *every* `Principal` value in existence satisfies
/// its issuer's rules. `Deserialize` goes through `FromStr`, so a JSON document
/// cannot smuggle in an invalid one either.
///
/// [`Display`](fmt::Display) and [`FromStr`] round-trip **exactly** — no
/// normalization is performed anywhere, because normalizing would let one
/// subject have two renderings and therefore two unequal `Principal`s.
///
/// # Wire form
///
/// A plain JSON string (`"idp:6ba7b810-9dad-11d1-80b4-00c04fd430c8"`), not an
/// object — implemented by hand rather than with `#[serde(transparent)]` so
/// that deserialization can enforce validation.
///
/// # This is a name, not a proof
///
/// Holding a `Principal` proves nothing about authentication: it is data,
/// parsed from a string. The *proof* is [`Authenticated`](super::Authenticated),
/// which only an [`Authenticator`](super::Authenticator) produces, and whose
/// principal field was populated from verified material.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Principal {
    issuer: Issuer,
    subject: String,
}

impl Principal {
    /// Build a principal, validating the subject against the issuer's rules.
    ///
    /// # Errors
    ///
    /// See [`PrincipalParseError`].
    pub fn new(issuer: Issuer, subject: impl Into<String>) -> Result<Self, PrincipalParseError> {
        let subject = subject.into();
        issuer.validate(&subject)?;
        Ok(Self { issuer, subject })
    }

    /// The principal an OIDC provider adapter mints: `oidc:<provider>/<sub>`.
    ///
    /// `provider` is the adapter's configured name (`auth0`). `sub` is the
    /// provider's subject exactly as its token carries it; any `:`, `%`,
    /// quote, backslash or non-printable byte is percent-encoded, so
    /// `auth0|64f1` stays `auth0|64f1` and a colon-bearing subject still
    /// has one rendering that round-trips through `Display` and `FromStr`.
    pub fn oidc(provider: &str, sub: &str) -> Result<Self, PrincipalParseError> {
        let mut encoded = String::with_capacity(sub.len());
        for b in sub.bytes() {
            if b.is_ascii_graphic() && !matches!(b, b':' | b'%' | b'"' | b'\\') {
                encoded.push(b as char);
            } else {
                encoded.push_str(&format!("%{b:02X}"));
            }
        }
        Self::new(Issuer::Oidc, format!("{provider}/{encoded}"))
    }

    /// An idp-minted principal from a UUID.
    pub fn idp(uuid: &uuid::Uuid) -> Self {
        // A `Uuid` always renders as a canonical hyphenated string, so this
        // cannot fail — but it still goes through the validating constructor
        // so there is exactly one path that builds a `Principal`.
        Self::new(Issuer::Idp, uuid.hyphenated().to_string())
            .expect("a canonical Uuid is always a valid idp subject")
    }

    /// Interpret a legacy bare `user_id` as an idp principal.
    ///
    /// This is the bridge that makes [`AuthContext::principal`] and
    /// [`AuthContext::user_id`] agree by construction: today's `user_id` is
    /// a plexus-idp-minted UUID v4 reused verbatim as the JWT `sub`, so
    /// `idp:<user_id>` is the same identity under a namespaced name.
    ///
    /// Returns `None` for anything that is not a canonical hyphenated UUID —
    /// notably `"anonymous"`, and any `user_id` minted by a system that is
    /// not plexus-idp. A `None` here means "this context has no principal we
    /// can name", never "this context is authenticated as something else".
    ///
    /// [`AuthContext::principal`]: crate::AuthContext::principal
    /// [`AuthContext::user_id`]: crate::AuthContext#structfield.user_id
    pub fn from_legacy_user_id(user_id: &str) -> Option<Self> {
        Self::new(Issuer::Idp, user_id).ok()
    }

    /// Who vouches for this subject.
    pub fn issuer(&self) -> Issuer {
        self.issuer
    }

    /// The issuer-scoped subject, without the prefix.
    pub fn subject(&self) -> &str {
        &self.subject
    }

    /// The value this principal contributes to the legacy
    /// [`AuthContext::user_id`](crate::AuthContext#structfield.user_id)
    /// field.
    ///
    /// For `idp:` principals this is the bare subject — byte-identical to
    /// what plexus-idp writes today, so nothing that reads `user_id`
    /// observes any change. For every other issuer it is the **full**
    /// namespaced string (`nostr:abc…`), because a nostr pubkey and an idp
    /// UUID share no namespace and must never collide in a column that
    /// callers treat as a unique user key.
    pub fn as_legacy_user_id(&self) -> String {
        match self.issuer {
            Issuer::Idp => self.subject.clone(),
            _ => self.to_string(),
        }
    }
}

impl fmt::Display for Principal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}{}{}", self.issuer.as_str(), SEP, self.subject)
    }
}

impl FromStr for Principal {
    type Err = PrincipalParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // Split on the FIRST separator; `Issuer::validate` then rejects any
        // further `:` in the remainder, so `idp:a:b` is an error rather than a
        // principal with a colon-bearing subject.
        let (prefix, subject) = s
            .split_once(SEP)
            .ok_or_else(|| PrincipalParseError::MissingSeparator(s.to_string()))?;
        let issuer = Issuer::parse(prefix)?;
        issuer.validate(subject)?;
        Ok(Self {
            issuer,
            subject: subject.to_string(),
        })
    }
}

impl Serialize for Principal {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.collect_str(self)
    }
}

impl<'de> Deserialize<'de> for Principal {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let raw = String::deserialize(d)?;
        raw.parse().map_err(serde::de::Error::custom)
    }
}

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

    const UUID: &str = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
    const PUBKEY: &str = "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d";

    fn samples() -> Vec<&'static str> {
        vec![
            "idp:6ba7b810-9dad-11d1-80b4-00c04fd430c8",
            "nostr:3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d",
            "apikey:ak_live_01HQ8ZQK",
        ]
    }

    #[test]
    fn display_fromstr_round_trip_exactly() {
        for s in samples() {
            let p: Principal = s.parse().expect(s);
            assert_eq!(p.to_string(), s);
            let again: Principal = p.to_string().parse().unwrap();
            assert_eq!(p, again);
        }
    }

    #[test]
    fn serde_round_trips_as_a_plain_string() {
        for s in samples() {
            let p: Principal = s.parse().unwrap();
            let json = serde_json::to_string(&p).unwrap();
            assert_eq!(json, format!("\"{s}\""));
            let back: Principal = serde_json::from_str(&json).unwrap();
            assert_eq!(p, back);
        }
    }

    #[test]
    fn deserialization_enforces_validation() {
        assert!(serde_json::from_str::<Principal>("\"ldap:someone\"").is_err());
        assert!(serde_json::from_str::<Principal>("\"nostr:NOTHEX\"").is_err());
    }

    #[test]
    fn rejects_unknown_issuer_and_missing_separator() {
        assert!(matches!(
            "ldap:someone".parse::<Principal>(),
            Err(PrincipalParseError::UnknownIssuer(i)) if i == "ldap"
        ));
        assert!(matches!(
            "idp".parse::<Principal>(),
            Err(PrincipalParseError::MissingSeparator(_))
        ));
    }

    #[test]
    fn rejects_malformed_subjects_per_issuer() {
        assert!(format!("idp:{}", "not-a-uuid").parse::<Principal>().is_err());
        assert!(format!("nostr:{}", PUBKEY.to_uppercase())
            .parse::<Principal>()
            .is_err());
        assert!("apikey:ak live".parse::<Principal>().is_err());
        assert!("idp:".parse::<Principal>().is_err());
        assert!(format!("idp:{UUID}:extra").parse::<Principal>().is_err());
    }

    #[test]
    fn from_legacy_user_id_round_trips_todays_user_ids() {
        let p = Principal::from_legacy_user_id(UUID).unwrap();
        assert_eq!(p.to_string(), format!("idp:{UUID}"));
        assert_eq!(p.as_legacy_user_id(), UUID);
        // The anonymous sentinel is not a principal.
        assert!(Principal::from_legacy_user_id("anonymous").is_none());
    }

    #[test]
    fn non_idp_principals_keep_their_namespace_in_the_legacy_field() {
        let p: Principal = format!("nostr:{PUBKEY}").parse().unwrap();
        assert_eq!(p.as_legacy_user_id(), format!("nostr:{PUBKEY}"));
        // and that string is not mistakable for an idp user_id
        assert!(Principal::from_legacy_user_id(&p.as_legacy_user_id()).is_none());
    }

    #[test]
    fn principals_from_different_issuers_never_collide() {
        let a = Principal::new(Issuer::ApiKey, UUID).unwrap();
        let b = Principal::new(Issuer::Idp, UUID).unwrap();
        assert_ne!(a, b);
        assert_ne!(a.to_string(), b.to_string());
    }

    #[test]
    fn idp_helper_matches_manual_construction() {
        let u = uuid::Uuid::parse_str(UUID).unwrap();
        assert_eq!(Principal::idp(&u), Principal::new(Issuer::Idp, UUID).unwrap());
        assert_eq!(Principal::idp(&u).to_string(), format!("idp:{UUID}"));
    }

    // ---------------------------------------------------------------------
    // PLX-87: coverage folded in from the two things this type replaced —
    // `plexus_core::identity::principal`'s unit tests (PLX-75), now deleted
    // because plexus-core re-exports this module, and the accepted/rejected
    // corpus from `plexus-idp/tests/principal_equivalence.rs` (PLX-82), also
    // deleted because there is no longer a second implementation to equate.
    // The assertions below are the *union* of what those two files asserted
    // about this grammar; none of that coverage was dropped in the collapse.
    // ---------------------------------------------------------------------

    /// Every form both former implementations were required to accept.
    const ACCEPTED: &[&str] = &[
        "idp:6ba7b810-9dad-11d1-80b4-00c04fd430c8",
        "idp:00000000-0000-4000-8000-000000000001",
        "nostr:3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d",
        "nostr:0000000000000000000000000000000000000000000000000000000000000000",
        "apikey:ak_live_01HQ8ZQK",
        "apikey:a",
        "apikey:!#$%&'()*+,-./",
    ];

    /// Every form both former implementations were required to reject.
    const REJECTED: &[&str] = &[
        // no separator
        "idp",
        "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
        "",
        // unknown issuer
        "ldap:someone",
        "IDP:6ba7b810-9dad-11d1-80b4-00c04fd430c8",
        "Nostr:3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d",
        ":x",
        // empty subject
        "idp:",
        "nostr:",
        "apikey:",
        // second separator
        "idp:6ba7b810-9dad-11d1-80b4-00c04fd430c8:extra",
        "apikey:ak:live",
        // non-canonical uuid
        "idp:not-a-uuid",
        "idp:6ba7b8109dad11d180b400c04fd430c8",
        "idp:{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
        "idp:6BA7B810-9DAD-11D1-80B4-00C04FD430C8",
        // bad nostr pubkeys
        "nostr:3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459",
        "nostr:3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459da",
        "nostr:3BF0C63FCB93463407AF97A5E5EE64FA883D107EF9E558472C4EB9AAAEFA459D",
        "nostr:zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
        // bad api key ids
        "apikey:ak live",
        "apikey:ak\tlive",
        "apikey:ak\"live",
        "apikey:ak\\live",
    ];

    #[test]
    fn accepts_the_whole_accepted_corpus_and_round_trips_it_exactly() {
        for s in ACCEPTED {
            let p: Principal = s
                .parse()
                .unwrap_or_else(|e| panic!("`{s}` should be accepted: {e}"));
            assert_eq!(p.to_string(), *s, "must round-trip `{s}` exactly");
            let json = serde_json::to_string(&p).unwrap();
            assert_eq!(json, format!("\"{s}\""), "wire form must be a bare string");
            assert_eq!(serde_json::from_str::<Principal>(&json).unwrap(), p);
        }
    }

    #[test]
    fn rejects_the_whole_rejected_corpus_at_both_entry_points() {
        for s in REJECTED {
            assert!(s.parse::<Principal>().is_err(), "`{s:?}` must be rejected");
            // Deserialization must not be a back door around FromStr.
            let doc = serde_json::to_string(s).unwrap();
            assert!(
                serde_json::from_str::<Principal>(&doc).is_err(),
                "`{s:?}` must be rejected by Deserialize too"
            );
        }
    }

    #[test]
    fn issuer_and_subject_are_split_correctly() {
        let p: Principal = format!("idp:{UUID}").parse().unwrap();
        assert_eq!(p.issuer(), Issuer::Idp);
        assert_eq!(p.subject(), UUID);

        let p: Principal = format!("nostr:{PUBKEY}").parse().unwrap();
        assert_eq!(p.issuer(), Issuer::Nostr);
        assert_eq!(p.subject(), PUBKEY);

        let p: Principal = "apikey:ak_live_01HQ8ZQK".parse().unwrap();
        assert_eq!(p.issuer(), Issuer::ApiKey);
        assert_eq!(p.subject(), "ak_live_01HQ8ZQK");
    }

    #[test]
    fn deserialization_error_text_names_the_rule_that_was_broken() {
        let err = serde_json::from_str::<Principal>("\"ldap:someone\"").unwrap_err();
        assert!(err.to_string().contains("unknown principal issuer"), "{err}");

        let err = serde_json::from_str::<Principal>("\"nostr:NOTHEX\"").unwrap_err();
        assert!(err.to_string().contains("nostr pubkey"), "{err}");
    }

    #[test]
    fn rejects_unknown_issuer_including_wrong_case() {
        assert!(matches!(
            "ldap:someone".parse::<Principal>(),
            Err(PrincipalParseError::UnknownIssuer(i)) if i == "ldap"
        ));
        // Capitalization of the issuer is not a known issuer either.
        assert!(matches!(
            "IDP:6ba7b810-9dad-11d1-80b4-00c04fd430c8".parse::<Principal>(),
            Err(PrincipalParseError::UnknownIssuer(_))
        ));
    }

    #[test]
    fn rejects_empty_subject_for_every_issuer() {
        for issuer in ["idp", "nostr", "apikey"] {
            let s = format!("{issuer}:");
            assert!(
                matches!(
                    s.parse::<Principal>(),
                    Err(PrincipalParseError::EmptySubject(_))
                ),
                "{s} should be rejected"
            );
        }
    }

    #[test]
    fn rejects_missing_separator() {
        assert!(matches!(
            "idp".parse::<Principal>(),
            Err(PrincipalParseError::MissingSeparator(_))
        ));
        assert!(matches!(
            UUID.parse::<Principal>(),
            Err(PrincipalParseError::MissingSeparator(_))
        ));
    }

    #[test]
    fn rejects_nostr_pubkey_of_wrong_length() {
        let short = &PUBKEY[..63];
        let long = format!("{PUBKEY}a");
        assert_eq!(short.len(), 63);
        assert_eq!(long.len(), 65);

        for bad in [short.to_string(), long] {
            let s = format!("nostr:{bad}");
            match s.parse::<Principal>() {
                Err(PrincipalParseError::InvalidNostrPubkey { reason, .. }) => {
                    assert_eq!(reason, "expected exactly 64 hex characters");
                }
                other => panic!("{s} should be rejected, got {other:?}"),
            }
        }
    }

    #[test]
    fn rejects_uppercase_nostr_hex() {
        let upper = PUBKEY.to_uppercase();
        assert_eq!(upper.len(), 64);
        let s = format!("nostr:{upper}");
        match s.parse::<Principal>() {
            Err(PrincipalParseError::InvalidNostrPubkey { reason, .. }) => {
                assert_eq!(reason, "expected lowercase hex only");
            }
            other => panic!("uppercase hex should be rejected, got {other:?}"),
        }
    }

    #[test]
    fn rejects_embedded_second_colon() {
        for s in [
            format!("idp:{UUID}:extra"),
            format!("nostr:{PUBKEY}:extra"),
            "apikey:ak:live".to_string(),
        ] {
            assert!(
                matches!(
                    s.parse::<Principal>(),
                    Err(PrincipalParseError::SubjectContainsSeparator(_))
                ),
                "{s} should be rejected"
            );
        }
    }

    #[test]
    fn rejects_non_canonical_uuid() {
        for bad in [
            "not-a-uuid",
            "6ba7b8109dad11d180b400c04fd430c8",       // simple form
            "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}", // braced form
            "6BA7B810-9DAD-11D1-80B4-00C04FD430C8",   // uppercase
        ] {
            let s = format!("idp:{bad}");
            assert!(
                matches!(
                    s.parse::<Principal>(),
                    Err(PrincipalParseError::InvalidUuid(_))
                ),
                "{s} should be rejected"
            );
        }
    }

    #[test]
    fn rejects_unprintable_api_key_ids() {
        for bad in ["ak live", "ak\tlive", "ak\"live", "ak\\live"] {
            let s = format!("apikey:{bad}");
            assert!(
                matches!(
                    s.parse::<Principal>(),
                    Err(PrincipalParseError::InvalidApiKeyId(_))
                ),
                "{s:?} should be rejected"
            );
        }
    }

    #[test]
    fn a_key_principal_is_64_lowercase_hex_and_round_trips() {
        let s = format!("key:{PUBKEY}");
        let p: Principal = s.parse().unwrap();
        assert_eq!(p.issuer(), Issuer::Key);
        assert_eq!(p.subject(), PUBKEY);
        assert_eq!(p.to_string(), s);
        assert_eq!(serde_json::to_string(&p).unwrap(), format!("\"{s}\""));
        assert_eq!(serde_json::from_str::<Principal>(&format!("\"{s}\"")).unwrap(), p);
        assert_eq!(p.as_legacy_user_id(), s, "keeps its namespace");
        assert_ne!(p, Principal::new(Issuer::Nostr, PUBKEY).unwrap(), "one key, two issuers: two principals");
        match format!("key:{}", PUBKEY.to_uppercase()).parse::<Principal>() {
            Err(PrincipalParseError::InvalidKeyPubkey { reason, .. }) => assert_eq!(reason, "expected lowercase hex only"),
            other => panic!("uppercase must be refused: {other:?}"),
        }
        for bad in [&PUBKEY[..63], &format!("{PUBKEY}0")[..], "", "zz"] {
            assert!(format!("key:{bad}").parse::<Principal>().is_err(), "{bad}");
        }
        assert!(format!("key:{PUBKEY}:x").parse::<Principal>().is_err());
        assert!(format!("KEY:{PUBKEY}").parse::<Principal>().is_err());
    }

    #[test]
    fn new_validates_like_from_str() {
        assert!(Principal::new(Issuer::Idp, UUID).is_ok());
        assert!(Principal::new(Issuer::Idp, "nope").is_err());
        assert!(Principal::new(Issuer::Nostr, PUBKEY).is_ok());
        assert!(Principal::new(Issuer::Nostr, "").is_err());
    }
}