Skip to main content

ic_auth_protocol_types/
claims.rs

1//! Passive portable claims fields. Construction does not establish canonicality or trust.
2
3use crate::{AuthRole, Principal, ScopeLabel};
4
5///
6/// CanonicalId
7///
8/// Exactly 32 identity bytes; every byte pattern is permitted.
9///
10
11#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
12#[cfg_attr(
13    feature = "stable-records",
14    derive(serde::Serialize, serde::Deserialize)
15)]
16pub struct CanonicalId([u8; 32]);
17
18impl CanonicalId {
19    /// Preserve an identity's bytes without assigning authority to their source.
20    #[must_use]
21    pub const fn from_bytes(bytes: [u8; 32]) -> Self {
22        Self(bytes)
23    }
24
25    /// Borrow the exact signed representation.
26    #[must_use]
27    pub const fn as_bytes(&self) -> &[u8; 32] {
28        &self.0
29    }
30}
31
32///
33/// AudienceId
34///
35/// Claimed network/application context, compared later with protected enrollment.
36///
37
38#[derive(Clone, Copy, Debug, Eq, PartialEq)]
39#[cfg_attr(
40    feature = "stable-records",
41    derive(serde::Serialize, serde::Deserialize)
42)]
43pub struct AudienceId {
44    /// Claimed network identity.
45    pub canonical_network_id: CanonicalId,
46    /// Claimed application identity, retaining the existing protocol field name.
47    pub fleet_id: CanonicalId,
48}
49
50///
51/// PortableMechanism
52///
53/// A claimed mechanism, never permission to select a verifier or enroll a key.
54///
55
56#[derive(Clone, Copy, Debug, Eq, PartialEq)]
57#[cfg_attr(
58    feature = "stable-records",
59    derive(serde::Serialize, serde::Deserialize)
60)]
61pub enum PortableMechanism {
62    /// Root-delegated certified issuer proof (signed tag 0).
63    Certified,
64    /// Independently enrolled Ed25519 issuer key (signed tag 1).
65    Ed25519,
66}
67
68///
69/// DelegatedRoleGrant
70///
71/// Syntactically valid labels whose counts, order and authority remain unchecked.
72///
73
74#[derive(Clone, Debug, Eq, PartialEq)]
75#[cfg_attr(
76    feature = "stable-records",
77    derive(serde::Serialize, serde::Deserialize)
78)]
79pub struct DelegatedRoleGrant {
80    /// Role to which these scopes apply; roles must not be combined implicitly.
81    pub target: AuthRole,
82    /// Claimed scope labels in their supplied order.
83    pub scopes: Vec<ScopeLabel>,
84}
85
86/// Maximum signed application extension length in bytes.
87pub const MAX_TOKEN_EXT_BYTES: usize = 4096;
88
89///
90/// PortableClaims
91///
92/// Unauthenticated portable credential fields.
93/// The codec checks structure, not signature validity, time relationships,
94/// enrollment, caller binding, revocation or permission. Even a successfully
95/// decoded value cannot authorize a session.
96///
97
98#[derive(Clone, Debug, Eq, PartialEq)]
99pub struct PortableClaims {
100    /// Mechanism committed by the signed bytes.
101    pub mechanism: PortableMechanism,
102    /// Claimed receiving network and application.
103    pub audience: AudienceId,
104    /// Claimed issuer; verification must match protected enrollment.
105    pub issuer: Principal,
106    /// Claimed key-binding identity, not key material or authority.
107    pub key_binding_id: [u8; 32],
108    /// Raw browser Ed25519 key; possession and caller binding are not checked here.
109    pub browser_public_key: [u8; 32],
110    /// Issuer-qualified subject; canonical claims exclude the anonymous principal.
111    pub account_subject: Principal,
112    /// Opaque issuer-attested application bytes, up to 4096 bytes.
113    pub ext: Option<Vec<u8>>,
114    /// Issuer-assigned slot, canonically bounded to 0–7.
115    pub device_slot: u8,
116    /// Nonzero slot generation, unchanged by renewal.
117    pub device_generation: u64,
118    /// Original login start, in nanoseconds.
119    pub login_started_at_ns: u64,
120    /// Original absolute login deadline, in nanoseconds.
121    pub login_deadline_ns: u64,
122    /// Credential issuance time, in nanoseconds.
123    pub issued_at_ns: u64,
124    /// Exclusive fresh-admission expiry, distinct from the session lease.
125    pub admission_expires_at_ns: u64,
126    /// Credential nonce; all-zero bytes remain valid structural input.
127    pub nonce: [u8; 16],
128    /// Signed grants in strict role/scope order; never normalize them on decode.
129    pub grants: Vec<DelegatedRoleGrant>,
130}