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