stateknot-core 0.1.0-alpha.1

Core domain types for the StateKnot agent runtime
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
// Copyright 2026 StateKnot contributors
// SPDX-License-Identifier: Apache-2.0

//! Exact external issuer/subject identities for authenticated principals.

use std::{borrow::Borrow, fmt, str::FromStr};

use fluent_uri::Uri;
use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
use thiserror::Error;

const ISSUER_ID_PATTERN: &str = "^[Hh][Tt][Tt][Pp][Ss]://[^/?#@]+(?:/[^?#]*)?$";
const SUBJECT_ID_PATTERN: &str = r"^[\u0020-\u007E]{1,255}$";

/// An exact, case-sensitive OIDC/OAuth issuer identifier.
///
/// The value is an absolute HTTPS URI with a host and optional port/path. It
/// contains no user information, query, or fragment. Equality deliberately
/// performs no URI normalization: OIDC requires the configured issuer,
/// discovery metadata, and token `iss` claim to match exactly.
///
/// This type is identity data only. Constructing or deserializing it does not
/// authenticate a token and never performs discovery or network access.
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct IssuerId(Box<str>);

impl IssuerId {
    /// Maximum encoded length in bytes.
    pub const MAX_LEN: usize = 512;

    /// Validates and constructs an exact issuer identifier.
    ///
    /// # Errors
    ///
    /// Returns [`IssuerIdError`] when `value` is not a bounded HTTPS issuer
    /// URI with the required OIDC/OAuth components.
    pub fn new(value: impl Into<String>) -> Result<Self, IssuerIdError> {
        let value = value.into();
        validate_issuer_id(&value)?;
        Ok(Self(value.into_boxed_str()))
    }

    /// Returns the exact issuer text used for security comparison.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl AsRef<str> for IssuerId {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl Borrow<str> for IssuerId {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Debug for IssuerId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_tuple("IssuerId")
            .field(&self.as_str())
            .finish()
    }
}

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

impl FromStr for IssuerId {
    type Err = IssuerIdError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::new(value)
    }
}

impl TryFrom<String> for IssuerId {
    type Error = IssuerIdError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl TryFrom<&str> for IssuerId {
    type Error = IssuerIdError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl From<IssuerId> for String {
    fn from(value: IssuerId) -> Self {
        value.0.into()
    }
}

impl Serialize for IssuerId {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for IssuerId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_string(IssuerIdVisitor)
    }
}

struct IssuerIdVisitor;

impl de::Visitor<'_> for IssuerIdVisitor {
    type Value = IssuerId;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("an exact HTTPS OIDC/OAuth issuer identifier")
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        IssuerId::try_from(value).map_err(E::custom)
    }

    fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        IssuerId::try_from(value).map_err(E::custom)
    }
}

impl JsonSchema for IssuerId {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "IssuerId".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::IssuerId").into()
    }

    fn json_schema(_: &mut SchemaGenerator) -> Schema {
        json_schema!({
            "type": "string",
            "format": "uri",
            "minLength": 9,
            "maxLength": 512,
            "pattern": ISSUER_ID_PATTERN
        })
    }

    fn inline_schema() -> bool {
        true
    }
}

/// Validation failure for an [`IssuerId`].
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum IssuerIdError {
    /// The identifier contained no bytes.
    #[error("issuer identifier must not be empty")]
    Empty,

    /// The identifier exceeded [`IssuerId::MAX_LEN`].
    #[error("issuer identifier is {actual} bytes; maximum is {max}")]
    TooLong {
        /// Maximum accepted byte length.
        max: usize,
        /// Observed byte length.
        actual: usize,
    },

    /// The text was not an RFC 3986 URI.
    #[error("issuer identifier must be an RFC 3986 URI")]
    InvalidUri,

    /// The URI did not use the HTTPS scheme.
    #[error("issuer identifier must use HTTPS")]
    UnsupportedScheme,

    /// The URI did not contain a non-empty authority host.
    #[error("issuer identifier must contain an HTTPS authority host")]
    MissingAuthority,

    /// A port was empty or outside the `u16` range used by HTTPS transports.
    #[error("issuer identifier port must be an integer from 0 through 65535")]
    InvalidPort,

    /// URI user information was present.
    #[error("issuer identifier must not contain user information")]
    UserInfoNotAllowed,

    /// A query component was present, including an empty query.
    #[error("issuer identifier must not contain a query")]
    QueryNotAllowed,

    /// A fragment component was present, including an empty fragment.
    #[error("issuer identifier must not contain a fragment")]
    FragmentNotAllowed,
}

fn validate_issuer_id(value: &str) -> Result<(), IssuerIdError> {
    if value.is_empty() {
        return Err(IssuerIdError::Empty);
    }
    if value.len() > IssuerId::MAX_LEN {
        return Err(IssuerIdError::TooLong {
            max: IssuerId::MAX_LEN,
            actual: value.len(),
        });
    }

    let uri = Uri::parse(value).map_err(|_| IssuerIdError::InvalidUri)?;
    if !uri.scheme().as_str().eq_ignore_ascii_case("https") {
        return Err(IssuerIdError::UnsupportedScheme);
    }

    let authority = uri.authority().ok_or(IssuerIdError::MissingAuthority)?;
    if authority.host().is_empty() {
        return Err(IssuerIdError::MissingAuthority);
    }
    if authority.userinfo().is_some() {
        return Err(IssuerIdError::UserInfoNotAllowed);
    }
    if authority
        .port()
        .is_some_and(|port| port.is_empty() || authority.port_to_u16().is_err())
    {
        return Err(IssuerIdError::InvalidPort);
    }
    if uri.query().is_some() {
        return Err(IssuerIdError::QueryNotAllowed);
    }
    if uri.fragment().is_some() {
        return Err(IssuerIdError::FragmentNotAllowed);
    }

    Ok(())
}

/// A bounded, case-sensitive OIDC subject identifier.
///
/// Subject IDs contain 1 to 255 printable ASCII bytes. OIDC allows at most 255
/// ASCII characters; `StateKnot` additionally rejects control bytes so the
/// value is safe to persist across supported database/text boundaries. The
/// value remains opaque and is never normalized or treated as a username.
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct SubjectId(Box<str>);

impl SubjectId {
    /// Maximum encoded length in bytes.
    pub const MAX_LEN: usize = 255;

    /// Validates and constructs an opaque subject identifier.
    ///
    /// # Errors
    ///
    /// Returns [`SubjectIdError`] when `value` is empty, too long, non-ASCII,
    /// or contains an ASCII control byte.
    pub fn new(value: impl Into<String>) -> Result<Self, SubjectIdError> {
        let value = value.into();
        validate_subject_id(&value)?;
        Ok(Self(value.into_boxed_str()))
    }

    /// Returns the exact, case-sensitive subject value.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl AsRef<str> for SubjectId {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl Borrow<str> for SubjectId {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Debug for SubjectId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("SubjectId")
            .field("byte_len", &self.as_str().len())
            .field("value", &"<redacted>")
            .finish()
    }
}

impl FromStr for SubjectId {
    type Err = SubjectIdError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::new(value)
    }
}

impl TryFrom<String> for SubjectId {
    type Error = SubjectIdError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl TryFrom<&str> for SubjectId {
    type Error = SubjectIdError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl From<SubjectId> for String {
    fn from(value: SubjectId) -> Self {
        value.0.into()
    }
}

impl Serialize for SubjectId {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for SubjectId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_string(SubjectIdVisitor)
    }
}

struct SubjectIdVisitor;

impl de::Visitor<'_> for SubjectIdVisitor {
    type Value = SubjectId;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a bounded printable-ASCII OIDC subject identifier")
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        SubjectId::try_from(value).map_err(E::custom)
    }

    fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        SubjectId::try_from(value).map_err(E::custom)
    }
}

impl JsonSchema for SubjectId {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "SubjectId".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::SubjectId").into()
    }

    fn json_schema(_: &mut SchemaGenerator) -> Schema {
        json_schema!({
            "type": "string",
            "minLength": 1,
            "maxLength": 255,
            "pattern": SUBJECT_ID_PATTERN
        })
    }

    fn inline_schema() -> bool {
        true
    }
}

/// Validation failure for a [`SubjectId`].
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum SubjectIdError {
    /// The identifier contained no bytes.
    #[error("subject identifier must not be empty")]
    Empty,

    /// The identifier exceeded [`SubjectId::MAX_LEN`].
    #[error("subject identifier is {actual} bytes; maximum is {max}")]
    TooLong {
        /// Maximum accepted byte length.
        max: usize,
        /// Observed byte length.
        actual: usize,
    },

    /// A byte was not printable ASCII.
    #[error("subject identifier contains a non-printable-ASCII byte at offset {index}")]
    InvalidByte {
        /// Zero-based byte offset of the first invalid byte.
        index: usize,
    },
}

fn validate_subject_id(value: &str) -> Result<(), SubjectIdError> {
    if value.is_empty() {
        return Err(SubjectIdError::Empty);
    }
    if value.len() > SubjectId::MAX_LEN {
        return Err(SubjectIdError::TooLong {
            max: SubjectId::MAX_LEN,
            actual: value.len(),
        });
    }

    if let Some((index, _)) = value
        .bytes()
        .enumerate()
        .find(|(_, byte)| !(0x20..=0x7e).contains(byte))
    {
        return Err(SubjectIdError::InvalidByte { index });
    }

    Ok(())
}

/// The stable external identity of an authenticated principal.
///
/// OIDC guarantees uniqueness and stability only for the issuer/subject pair.
/// Tenant boundaries remain separate and must still be included in storage
/// keys and authorization decisions.
#[derive(
    Clone, Debug, Deserialize, Eq, Hash, JsonSchema, Ord, PartialEq, PartialOrd, Serialize,
)]
#[serde(deny_unknown_fields)]
pub struct PrincipalIdentity {
    issuer: IssuerId,
    subject: SubjectId,
}

impl PrincipalIdentity {
    /// Constructs an identity from already validated components.
    #[must_use]
    pub const fn new(issuer: IssuerId, subject: SubjectId) -> Self {
        Self { issuer, subject }
    }

    /// Returns the exact authenticated issuer.
    #[must_use]
    pub const fn issuer(&self) -> &IssuerId {
        &self.issuer
    }

    /// Returns the issuer-local subject.
    #[must_use]
    pub const fn subject(&self) -> &SubjectId {
        &self.subject
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::{Value, from_value, json, to_value};

    const ISSUER: &str = "https://issuer.example.com/tenant";

    #[test]
    fn issuer_ids_preserve_exact_oidc_identity_text() {
        for value in [
            "https://issuer.example.com",
            "https://issuer.example.com/",
            ISSUER,
            "https://issuer.example.com:8443/tenant",
            "HTTPS://ISSUER.example.com:443/a/../tenant",
            "https://issuer.example.com/%7Etenant",
        ] {
            let issuer = value.parse::<IssuerId>().unwrap();
            assert_eq!(issuer.as_str(), value);
            assert_eq!(issuer.to_string(), value);
            assert_eq!(to_value(&issuer).unwrap(), Value::from(value));
        }

        let lowercase = "https://issuer.example.com".parse::<IssuerId>().unwrap();
        let uppercase = "HTTPS://ISSUER.example.com".parse::<IssuerId>().unwrap();
        let slash = "https://issuer.example.com/".parse::<IssuerId>().unwrap();
        let default_port = "https://issuer.example.com:443"
            .parse::<IssuerId>()
            .unwrap();
        assert_ne!(lowercase, uppercase);
        assert_ne!(lowercase, slash);
        assert_ne!(lowercase, default_port);

        let prefix = "https://issuer.example.com/";
        let maximum = format!("{prefix}{}", "a".repeat(IssuerId::MAX_LEN - prefix.len()));
        assert_eq!(maximum.len(), IssuerId::MAX_LEN);
        assert_eq!(maximum.parse::<IssuerId>().unwrap().as_str(), maximum);
    }

    #[test]
    fn issuer_ids_reject_non_oidc_urls() {
        for value in [
            "issuer.example.com",
            "http://issuer.example.com",
            "https:/issuer.example.com",
            "https://",
            "https://issuer.example.com:",
            "https://issuer.example.com:65536",
            "https://user@issuer.example.com",
            "https://issuer.example.com?",
            "https://issuer.example.com?tenant=one",
            "https://issuer.example.com#",
            "https://issuer.example.com#fragment",
            "https://例子.example.com",
        ] {
            assert!(value.parse::<IssuerId>().is_err(), "accepted {value:?}");
        }

        let oversized = format!(
            "https://issuer.example.com/{}",
            "a".repeat(IssuerId::MAX_LEN)
        );
        assert_eq!(
            oversized.parse::<IssuerId>(),
            Err(IssuerIdError::TooLong {
                max: IssuerId::MAX_LEN,
                actual: oversized.len(),
            })
        );
    }

    #[test]
    fn issuer_id_serde_and_schema_enforce_the_wire_contract() {
        let issuer = ISSUER.parse::<IssuerId>().unwrap();
        assert_eq!(from_value::<IssuerId>(json!(ISSUER)).unwrap(), issuer);
        assert!(from_value::<IssuerId>(json!(42)).is_err());
        assert!(from_value::<IssuerId>(Value::Null).is_err());

        let schema = to_value(schemars::schema_for!(IssuerId)).unwrap();
        assert_eq!(schema["type"], "string");
        assert_eq!(schema["format"], "uri");
        assert_eq!(schema["minLength"], 9);
        assert_eq!(schema["maxLength"], IssuerId::MAX_LEN);
        assert_eq!(schema["pattern"], ISSUER_ID_PATTERN);
    }

    #[test]
    fn subject_ids_are_opaque_case_sensitive_and_redacted_in_debug() {
        let all_printable_ascii = (0x20_u8..=0x7e).map(char::from).collect::<String>();
        assert_eq!(
            all_printable_ascii.parse::<SubjectId>().unwrap().as_str(),
            all_printable_ascii
        );

        for value in [
            "24400320",
            "AItOawmwtWwcT0k51BayewNvutrJUqsvl6qs7A4",
            "User Name",
            "subject:\"quoted\"\\opaque",
        ] {
            let subject = value.parse::<SubjectId>().unwrap();
            assert_eq!(subject.as_str(), value);
            assert_eq!(to_value(&subject).unwrap(), Value::from(value));
            let debug = format!("{subject:?}");
            assert!(!debug.contains(value));
            assert!(debug.contains("<redacted>"));
        }

        assert_ne!(
            "subject".parse::<SubjectId>().unwrap(),
            "Subject".parse::<SubjectId>().unwrap()
        );

        let maximum = "a".repeat(SubjectId::MAX_LEN);
        assert_eq!(maximum.parse::<SubjectId>().unwrap().as_str(), maximum);
    }

    #[test]
    fn subject_ids_reject_empty_control_unicode_and_oversized_values() {
        assert_eq!("".parse::<SubjectId>(), Err(SubjectIdError::Empty));
        assert_eq!(
            "a".repeat(SubjectId::MAX_LEN + 1).parse::<SubjectId>(),
            Err(SubjectIdError::TooLong {
                max: SubjectId::MAX_LEN,
                actual: SubjectId::MAX_LEN + 1,
            })
        );

        for byte in 0_u8..=0x7f {
            if (0x20..=0x7e).contains(&byte) {
                continue;
            }
            let value = String::from_utf8(vec![byte]).unwrap();
            assert_eq!(
                value.parse::<SubjectId>(),
                Err(SubjectIdError::InvalidByte { index: 0 }),
                "accepted ASCII byte 0x{byte:02x}"
            );
        }
        assert_eq!(
            "用户".parse::<SubjectId>(),
            Err(SubjectIdError::InvalidByte { index: 0 })
        );
    }

    #[test]
    fn subject_id_serde_and_schema_enforce_the_wire_contract() {
        let subject = "subject-42".parse::<SubjectId>().unwrap();
        assert_eq!(
            from_value::<SubjectId>(json!(subject.as_str())).unwrap(),
            subject
        );
        assert!(from_value::<SubjectId>(json!(42)).is_err());
        assert!(from_value::<SubjectId>(Value::Null).is_err());

        let schema = to_value(schemars::schema_for!(SubjectId)).unwrap();
        assert_eq!(schema["type"], "string");
        assert_eq!(schema["minLength"], 1);
        assert_eq!(schema["maxLength"], SubjectId::MAX_LEN);
        assert_eq!(schema["pattern"], SUBJECT_ID_PATTERN);
    }

    #[test]
    fn principal_identity_requires_the_exact_issuer_subject_pair() {
        let issuer = ISSUER.parse::<IssuerId>().unwrap();
        let subject = "subject-42".parse::<SubjectId>().unwrap();
        let identity = PrincipalIdentity::new(issuer.clone(), subject.clone());

        assert_eq!(identity.issuer(), &issuer);
        assert_eq!(identity.subject(), &subject);
        assert_eq!(
            to_value(&identity).unwrap(),
            json!({ "issuer": ISSUER, "subject": "subject-42" })
        );
        let debug = format!("{identity:?}");
        assert!(debug.contains(ISSUER));
        assert!(!debug.contains(subject.as_str()));
        assert!(debug.contains("<redacted>"));
        assert_ne!(
            identity,
            PrincipalIdentity::new(
                "https://other.example.com".parse().unwrap(),
                subject.clone()
            )
        );

        for invalid in [
            json!({ "issuer": ISSUER }),
            json!({ "subject": "subject-42" }),
            json!({ "issuer": ISSUER, "subject": "subject-42", "extra": true }),
            json!({ "issuer": "http://issuer.example.com", "subject": "subject-42" }),
            json!({ "issuer": ISSUER, "subject": "subject\n42" }),
            Value::Null,
        ] {
            assert!(
                from_value::<PrincipalIdentity>(invalid.clone()).is_err(),
                "PrincipalIdentity accepted {invalid}"
            );
        }

        let schema = to_value(schemars::schema_for!(PrincipalIdentity)).unwrap();
        assert_eq!(schema["type"], "object");
        assert_eq!(schema["additionalProperties"], false);
        assert_eq!(schema["required"], json!(["issuer", "subject"]));
        assert_eq!(schema["properties"]["issuer"]["pattern"], ISSUER_ID_PATTERN);
        assert_eq!(
            schema["properties"]["subject"]["pattern"],
            SUBJECT_ID_PATTERN
        );
    }
}