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)]
28#[cfg_attr(feature = "typescript", derive(ic_wire::typescript::Wire))]
29#[cfg_attr(feature = "typescript", wire(fixed))]
30pub struct SessionInput {
31    /// Claims and proof requiring complete verification against host authority.
32    pub credential: model::PortableCredential,
33    /// Unnormalized scope text; semantic validation belongs to request admission.
34    pub scopes: List<Text<64>, 16>,
35    /// Requested seconds; absence selects the protected host default.
36    pub ttl_secs: Option<u64>,
37}
38
39const CREDENTIAL_LIMITS: Limits = Limits {
40    bytes: 524_288,
41    allocation: 2_097_152,
42    nodes: 65_536,
43    depth: 32,
44};
45const EMPTY_LIMITS: Limits = Limits {
46    bytes: 11,
47    allocation: 128,
48    nodes: 8,
49    depth: 4,
50};
51const REPLY_LIMITS: Limits = Limits {
52    bytes: 4096,
53    allocation: 16384,
54    nodes: 128,
55    depth: 8,
56};
57
58const SESSION_REPLY_LIMITS: Limits = Limits {
59    bytes: 8192,
60    allocation: 65536,
61    ..REPLY_LIMITS
62};
63
64// Forward framing and admission to Wire; each contract declares its concrete
65// types, identity and both budgets below. No local serializer or runtime policy.
66macro_rules! contract {
67    ($contract:ident, $id:expr, $name:literal, $request:ty, $reply:ty, $request_limits:ident, $reply_limits:ident) => {
68        impl $contract {
69            /// Fixed routing identity, not an authentication or signing domain.
70            pub const ID: u64 = $id;
71            /// Contract name used by generated clients. The credential envelope has no endpoint.
72            pub const NAME: &'static str = $name;
73            /// Complete request encoding and decoding allowances.
74            pub const REQUEST_LIMITS: Limits = $request_limits;
75            /// Complete response encoding and decoding allowances.
76            pub const RESPONSE_LIMITS: Limits = $reply_limits;
77
78            /// Encode this request within all four resource allowances.
79            ///
80            /// # Errors
81            /// Returns the supplier's codec error if admission fails.
82            pub fn encode_request(value: &$request) -> Result<Vec<u8>, ic_wire::Error> {
83                ic_wire::encode(
84                    value,
85                    Self::encoding(Direction::Request),
86                    Self::REQUEST_LIMITS,
87                )
88            }
89
90            /// Decode only this contract's request frame, without establishing trust.
91            ///
92            /// # Errors
93            /// Rejects malformed, misrouted, over-budget or trailing input.
94            pub fn decode_request(bytes: &[u8]) -> Result<$request, ic_wire::Error> {
95                ic_wire::decode(
96                    bytes,
97                    Self::encoding(Direction::Request),
98                    Self::REQUEST_LIMITS,
99                )
100            }
101
102            /// Encode this method's reply within all four resource allowances.
103            ///
104            /// # Errors
105            /// Returns the supplier's codec error if admission fails.
106            pub fn encode_response(value: &$reply) -> Result<Vec<u8>, ic_wire::Error> {
107                ic_wire::encode(
108                    value,
109                    Self::encoding(Direction::Response),
110                    Self::RESPONSE_LIMITS,
111                )
112            }
113
114            /// Decode a reply without treating it as proof of authority.
115            ///
116            /// # Errors
117            /// Rejects malformed, misrouted, over-budget or trailing input.
118            pub fn decode_response(bytes: &[u8]) -> Result<$reply, ic_wire::Error> {
119                ic_wire::decode(
120                    bytes,
121                    Self::encoding(Direction::Response),
122                    Self::RESPONSE_LIMITS,
123                )
124            }
125
126            const fn encoding(direction: Direction) -> Encoding {
127                Encoding::Frame {
128                    method: Self::ID,
129                    direction,
130                }
131            }
132        }
133    };
134}
135
136///
137/// ReadSession
138///
139/// Read the authenticated caller's local session without a submitted credential.
140///
141
142pub struct ReadSession;
143
144contract!(
145    ReadSession,
146    0x415554480013,
147    "portable_session_read",
148    (),
149    Result<SessionView, model::SessionReadRefusal>,
150    EMPTY_LIMITS,
151    SESSION_REPLY_LIMITS
152);
153
154///
155/// Logout
156///
157/// Request local logout for the authenticated caller only.
158///
159
160pub struct Logout;
161
162contract!(
163    Logout,
164    0x415554480014,
165    "logout_portable_session",
166    (),
167    model::LogoutResult,
168    EMPTY_LIMITS,
169    REPLY_LIMITS
170);
171
172///
173/// CredentialEnvelope
174///
175/// Portable credential data envelope, not an issuer endpoint or signing format.
176///
177
178pub struct CredentialEnvelope;
179
180contract!(
181    CredentialEnvelope,
182    0x415554480001,
183    "credential",
184    model::PortableCredential,
185    model::PortableCredential,
186    CREDENTIAL_LIMITS,
187    CREDENTIAL_LIMITS
188);
189
190///
191/// EstablishSession
192///
193/// Session admission transport; the host binds it to its authenticated update.
194///
195
196pub struct EstablishSession;
197
198contract!(
199    EstablishSession,
200    0x415554480012,
201    "establish_portable_session",
202    SessionInput,
203    Result<SessionView, model::AdmissionRefusal>,
204    CREDENTIAL_LIMITS,
205    SESSION_REPLY_LIMITS
206);