Skip to main content

ic_auth_wire/
lib.rs

1//! Controlled portable-session transport. No endpoint installation, authority,
2//! cryptography, session storage or delivery/retry policy is supplied here.
3pub mod model;
4use ic_auth_protocol_types::limits::{MAX_LABEL_BYTES, MAX_SESSION_SCOPES};
5pub use ic_wire as wire;
6use ic_wire::{Limits, List, Text};
7use model::{LogoutResult, PortableCredential};
8
9/// Default browser retention cap for a complete credential frame. Larger
10/// certified envelopes require an explicitly selected client cap; Wire's own
11/// frame/field budgets continue to apply. Refusal is client `invalid_material`.
12pub const BROWSER_DEFAULT_CREDENTIAL_BYTES: usize = 131_072;
13/// Maximum configurable browser retention cap, not an override of Wire limits.
14/// Preserves the client option range; actual frames remain bounded by the codec.
15pub const BROWSER_MAX_CONFIGURED_CREDENTIAL_BYTES: usize = 1_048_576;
16/// Browser logout response frame cap. All current reply variants fit; extensions
17/// must review this intentionally narrower policy than the shared reply budget.
18pub const BROWSER_LOGOUT_RESPONSE_BYTES: usize = 1024;
19
20/// Admission narrowing accompanies the exact opaque signed credential.
21#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
22#[cfg_attr(feature = "typescript", derive(ic_wire::Wire))]
23pub struct SessionInput {
24    /// Exact claims and proof envelope, requiring full receiver verification.
25    pub credential: PortableCredential,
26    /// Requested narrowing scopes; transport bounds do not grant permissions.
27    pub scopes: List<Text<MAX_LABEL_BYTES>, MAX_SESSION_SCOPES>,
28    /// Requested lease in seconds; absence selects protected host default policy.
29    pub ttl_secs: Option<u64>,
30}
31/// A receiver's authenticated local reply, never a transferable authorization.
32#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
33#[cfg_attr(feature = "typescript", derive(ic_wire::Wire))]
34pub struct SessionView {
35    /// Browser ingress identity to which this local session belongs.
36    pub caller: ic_auth_protocol_types::Principal,
37    /// Issuer qualifying the account subject.
38    pub issuer: ic_auth_protocol_types::Principal,
39    /// Account subject asserted by that issuer.
40    pub subject: ic_auth_protocol_types::Principal,
41    /// Exclusive local session lease deadline in nanoseconds, not the login deadline.
42    pub expires_at_ns: u64,
43}
44const CREDENTIAL_LIMITS: Limits = Limits {
45    bytes: 524288,
46    allocation: 2097152,
47    nodes: 65536,
48    depth: 32,
49};
50const REPLY_LIMITS: Limits = Limits {
51    bytes: 4096,
52    allocation: 16384,
53    nodes: 128,
54    depth: 8,
55};
56const EMPTY_LIMITS: Limits = Limits {
57    bytes: 11,
58    allocation: 128,
59    nodes: 8,
60    depth: 4,
61};
62
63// Keep method identities and limits explicit; only the four native codec
64// forwarders are mechanical. This macro is private and emits the existing API.
65macro_rules! codecs {
66    ($request:ty, $response:ty) => {
67        /// Encode a request with this contract's identity and complete request budget.
68        pub fn encode_request(value: &$request) -> Result<Vec<u8>, ic_wire::Error> {
69            ic_wire::native::encode_request(Self::ID, value, Self::REQUEST_LIMITS)
70        }
71        /// Decode a request, rejecting a different identity or exceeded request budget.
72        pub fn decode_request(bytes: &[u8]) -> Result<$request, ic_wire::Error> {
73            ic_wire::native::decode_request(Self::ID, bytes, Self::REQUEST_LIMITS)
74        }
75        /// Encode a response with this contract's identity and complete response budget.
76        pub fn encode_response(value: &$response) -> Result<Vec<u8>, ic_wire::Error> {
77            ic_wire::native::encode_response(Self::ID, value, Self::RESPONSE_LIMITS)
78        }
79        /// Decode a response, rejecting a different identity or exceeded response budget.
80        pub fn decode_response(bytes: &[u8]) -> Result<$response, ic_wire::Error> {
81            ic_wire::native::decode_response(Self::ID, bytes, Self::RESPONSE_LIMITS)
82        }
83    };
84}
85
86/// Data-envelope identity, not a callable issuer method or signing format.
87pub struct CredentialEnvelope;
88impl CredentialEnvelope {
89    /// Stable IC Wire envelope identity; not a cryptographic domain separator.
90    pub const ID: u64 = 0x415554480001;
91    /// Transport name for adapters exposing this contract.
92    pub const NAME: &'static str = "credential";
93    /// Complete encoding and decoding budget for the request envelope.
94    pub const REQUEST_LIMITS: Limits = CREDENTIAL_LIMITS;
95    /// Complete encoding and decoding budget for the response envelope.
96    pub const RESPONSE_LIMITS: Limits = CREDENTIAL_LIMITS;
97    codecs!(PortableCredential, PortableCredential);
98}
99/// Controlled admission contract; consumers bind it to their own update export.
100pub struct EstablishSession;
101impl EstablishSession {
102    /// Stable IC Wire envelope identity; not a cryptographic domain separator.
103    pub const ID: u64 = 0x415554480012;
104    /// Transport name for adapters exposing this contract.
105    pub const NAME: &'static str = "establish_portable_session";
106    /// Complete encoding and decoding budget for the request envelope.
107    pub const REQUEST_LIMITS: Limits = CREDENTIAL_LIMITS;
108    /// Complete encoding and decoding budget for the response envelope.
109    pub const RESPONSE_LIMITS: Limits = REPLY_LIMITS;
110    codecs!(SessionInput, Result<SessionView, model::AdmissionRefusal>);
111}
112/// Proof-free local read contract; the runtime caller selects the session.
113pub struct ReadSession;
114impl ReadSession {
115    /// Stable IC Wire envelope identity; not a cryptographic domain separator.
116    pub const ID: u64 = 0x415554480013;
117    /// Transport name for adapters exposing this contract.
118    pub const NAME: &'static str = "portable_session_read";
119    /// Complete encoding and decoding budget for the request envelope.
120    pub const REQUEST_LIMITS: Limits = EMPTY_LIMITS;
121    /// Complete encoding and decoding budget for the response envelope.
122    pub const RESPONSE_LIMITS: Limits = REPLY_LIMITS;
123    codecs!((), Result<SessionView, model::SessionReadRefusal>);
124}
125/// Caller-authenticated logout; no submitted proof or caller identity is trusted.
126pub struct Logout;
127impl Logout {
128    /// Stable IC Wire envelope identity; not a cryptographic domain separator.
129    pub const ID: u64 = 0x415554480014;
130    /// Transport name for adapters exposing this contract.
131    pub const NAME: &'static str = "logout_portable_session";
132    /// Complete encoding and decoding budget for the request envelope.
133    pub const REQUEST_LIMITS: Limits = EMPTY_LIMITS;
134    /// Complete encoding and decoding budget for the response envelope.
135    pub const RESPONSE_LIMITS: Limits = REPLY_LIMITS;
136    codecs!((), LogoutResult);
137}