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