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