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