Skip to main content

ic_auth_wire/
lib.rs

1//! Bounded authentication transport contracts. Decoding establishes no authority.
2//!
3//! Hosts supply authenticated callers and implement the operations. This crate
4//! supplies neither endpoint installation nor session/revocation state.
5
6pub mod model;
7pub mod projection;
8
9pub use ic_wire as wire;
10use ic_wire::{Direction, Encoding, Limits, List, Text};
11pub use model::SessionView;
12
13/// Browser cap for a complete logout reply, independent of native codec limits.
14pub const BROWSER_LOGOUT_RESPONSE_BYTES: usize = 1024;
15
16/// Default browser retention cap for a complete credential frame.
17pub const BROWSER_DEFAULT_CREDENTIAL_BYTES: usize = 131_072;
18/// Maximum configurable browser retention cap; codec limits still apply.
19pub const BROWSER_MAX_CONFIGURED_CREDENTIAL_BYTES: usize = 1_048_576;
20
21///
22/// SessionInput
23///
24/// Unsigned session narrowing accompanying an opaque signed credential.
25///
26
27#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, ic_wire::Wire)]
28pub struct SessionInput {
29    /// Claims and proof requiring complete verification against host authority.
30    pub credential: model::PortableCredential,
31    /// Unnormalized scope text; semantic validation belongs to request admission.
32    pub scopes: List<Text<64>, 16>,
33    /// Requested seconds; absence selects the protected host default.
34    pub ttl_secs: Option<u64>,
35}
36
37const CREDENTIAL_LIMITS: Limits = Limits {
38    bytes: 524_288,
39    allocation: 2_097_152,
40    nodes: 65_536,
41    depth: 32,
42};
43const EMPTY_LIMITS: Limits = Limits {
44    bytes: 12,
45    allocation: 8192,
46    nodes: 32,
47    depth: 8,
48};
49const REPLY_LIMITS: Limits = Limits {
50    bytes: 4096,
51    allocation: 16384,
52    nodes: 128,
53    depth: 8,
54};
55
56const SESSION_REPLY_LIMITS: Limits = Limits {
57    bytes: 8192,
58    allocation: 131072,
59    ..REPLY_LIMITS
60};
61
62// Sparse writers charge staging in addition to traversal. Preserve the tighter
63// reader allowance when receiving credentials from an untrusted peer.
64const CREDENTIAL_WRITE_LIMITS: Limits = Limits {
65    allocation: 3 * 1024 * 1024,
66    ..CREDENTIAL_LIMITS
67};
68
69// Forward framing and admission to Wire; each contract declares its concrete
70// types, identity and both budgets below. No local serializer or runtime policy.
71macro_rules! contract {
72    (@writer CREDENTIAL_LIMITS) => { CREDENTIAL_WRITE_LIMITS };
73    (@writer $limits:ident) => { $limits };
74    (@encode direct, $value:expr, $encoding:expr, $limits:expr,) => { ic_wire::encode($value, $encoding, $limits) };
75    (@decode direct, $bytes:expr, $encoding:expr, $limits:expr,) => { ic_wire::decode($bytes, $encoding, $limits) };
76    (@encode wrapped, $value:expr, $encoding:expr, $limits:expr,) => { ic_wire::encode(&ic_wire::Message($value.clone()), $encoding, $limits) };
77    (@decode wrapped, $bytes:expr, $encoding:expr, $limits:expr,) => { ic_wire::decode::<ic_wire::Message<_>>($bytes, $encoding, $limits).map(|m| m.0) };
78
79    ($contract:ident, $id:expr, $name:literal, $request:ty, $reqroot:ident, $reply:ty, $reproot:ident, $request_limits:ident, $reply_limits:ident) => {
80        impl $contract {
81            /// Fixed routing identity, not an authentication or signing domain.
82            pub const ID: u64 = $id;
83            /// Contract name used by generated clients. The credential envelope has no endpoint.
84            pub const NAME: &'static str = $name;
85            /// Request reader allowance.
86            pub const REQUEST_LIMITS: Limits = $request_limits;
87            /// Response reader allowance.
88            pub const RESPONSE_LIMITS: Limits = $reply_limits;
89
90            /// Request writer allowance, including sparse preparation.
91            pub const REQUEST_WRITE_LIMITS: Limits = contract!(@writer $request_limits);
92            /// Response writer allowance, including sparse preparation.
93            pub const RESPONSE_WRITE_LIMITS: Limits = contract!(@writer $reply_limits);
94
95            /// Encode this request within all four resource allowances.
96            ///
97            /// # Errors
98            /// Returns the supplier's codec error if admission fails.
99            pub fn encode_request(value: &$request) -> Result<Vec<u8>, ic_wire::Error> {
100                contract!(@encode $reqroot, value,
101                    Self::encoding(Direction::Request),
102                    Self::REQUEST_WRITE_LIMITS,
103                )
104            }
105
106            /// Decode only this contract's request frame, without establishing trust.
107            ///
108            /// # Errors
109            /// Rejects malformed, misrouted, over-budget or trailing input.
110            pub fn decode_request(bytes: &[u8]) -> Result<$request, ic_wire::Error> {
111                contract!(@decode $reqroot, bytes,
112                    Self::encoding(Direction::Request),
113                    Self::REQUEST_LIMITS,
114                )
115            }
116
117            /// Encode this method's reply within all four resource allowances.
118            ///
119            /// # Errors
120            /// Returns the supplier's codec error if admission fails.
121            pub fn encode_response(value: &$reply) -> Result<Vec<u8>, ic_wire::Error> {
122                contract!(@encode $reproot, value,
123                    Self::encoding(Direction::Response),
124                    Self::RESPONSE_WRITE_LIMITS,
125                )
126            }
127
128            /// Decode a reply without treating it as proof of authority.
129            ///
130            /// # Errors
131            /// Rejects malformed, misrouted, over-budget or trailing input.
132            pub fn decode_response(bytes: &[u8]) -> Result<$reply, ic_wire::Error> {
133                contract!(@decode $reproot, bytes,
134                    Self::encoding(Direction::Response),
135                    Self::RESPONSE_LIMITS,
136                )
137            }
138
139            const fn encoding(direction: Direction) -> Encoding {
140                Encoding::Frame {
141                    method: Self::ID,
142                    direction,
143                }
144            }
145        }
146    };
147}
148
149///
150/// ReadSession
151///
152/// Read the authenticated caller's local session without a submitted credential.
153///
154
155pub struct ReadSession;
156
157contract!(
158    ReadSession,
159    0x415554480113,
160    "portable_session_read",
161    (), wrapped,
162    Result<SessionView, model::SessionReadRefusal>, wrapped,
163    EMPTY_LIMITS,
164    SESSION_REPLY_LIMITS
165);
166
167///
168/// Logout
169///
170/// Request local logout for the authenticated caller only.
171///
172
173pub struct Logout;
174
175contract!(
176    Logout,
177    0x415554480114,
178    "logout_portable_session",
179    (),
180    wrapped,
181    model::LogoutResult,
182    wrapped,
183    EMPTY_LIMITS,
184    REPLY_LIMITS
185);
186
187///
188/// CredentialEnvelope
189///
190/// Portable credential data envelope, not an issuer endpoint or signing format.
191///
192
193pub struct CredentialEnvelope;
194
195contract!(
196    CredentialEnvelope,
197    0x415554480101,
198    "credential",
199    model::PortableCredential,
200    direct,
201    model::PortableCredential,
202    direct,
203    CREDENTIAL_LIMITS,
204    CREDENTIAL_LIMITS
205);
206
207///
208/// EstablishSession
209///
210/// Session admission transport; the host binds it to its authenticated update.
211///
212
213pub struct EstablishSession;
214
215contract!(
216    EstablishSession,
217    0x415554480112,
218    "establish_portable_session",
219    SessionInput, direct,
220    Result<SessionView, model::AdmissionRefusal>, wrapped,
221    CREDENTIAL_LIMITS,
222    SESSION_REPLY_LIMITS
223);