1pub 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
13pub const BROWSER_LOGOUT_RESPONSE_BYTES: usize = 1024;
15
16pub const BROWSER_DEFAULT_CREDENTIAL_BYTES: usize = 131_072;
18pub const BROWSER_MAX_CONFIGURED_CREDENTIAL_BYTES: usize = 1_048_576;
20
21#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, ic_wire::Wire)]
28pub struct SessionInput {
29 pub credential: model::PortableCredential,
31 pub scopes: List<Text<64>, 16>,
33 pub ttl_secs: Option<u64>,
35}
36
37const CREDENTIAL_LIMITS: Limits = Limits {
38 bytes: 524_288,
39 allocation: 2_097_152,
40 nodes: 65_536,
41 depth: 32,
42};
43const EMPTY_LIMITS: Limits = Limits {
44 bytes: 12,
45 allocation: 8192,
46 nodes: 32,
47 depth: 8,
48};
49const REPLY_LIMITS: Limits = Limits {
50 bytes: 4096,
51 allocation: 16384,
52 nodes: 128,
53 depth: 8,
54};
55
56const SESSION_REPLY_LIMITS: Limits = Limits {
57 bytes: 8192,
58 allocation: 131072,
59 ..REPLY_LIMITS
60};
61
62const CREDENTIAL_WRITE_LIMITS: Limits = Limits {
65 allocation: 3 * 1024 * 1024,
66 ..CREDENTIAL_LIMITS
67};
68
69macro_rules! contract {
72 (@writer CREDENTIAL_LIMITS) => { CREDENTIAL_WRITE_LIMITS };
73 (@writer $limits:ident) => { $limits };
74 (@encode direct, $value:expr, $encoding:expr, $limits:expr,) => { ic_wire::encode($value, $encoding, $limits) };
75 (@decode direct, $bytes:expr, $encoding:expr, $limits:expr,) => { ic_wire::decode($bytes, $encoding, $limits) };
76 (@encode wrapped, $value:expr, $encoding:expr, $limits:expr,) => { ic_wire::encode(&ic_wire::Message($value.clone()), $encoding, $limits) };
77 (@decode wrapped, $bytes:expr, $encoding:expr, $limits:expr,) => { ic_wire::decode::<ic_wire::Message<_>>($bytes, $encoding, $limits).map(|m| m.0) };
78
79 ($contract:ident, $id:expr, $name:literal, $request:ty, $reqroot:ident, $reply:ty, $reproot:ident, $request_limits:ident, $reply_limits:ident) => {
80 impl $contract {
81 pub const ID: u64 = $id;
83 pub const NAME: &'static str = $name;
85 pub const REQUEST_LIMITS: Limits = $request_limits;
87 pub const RESPONSE_LIMITS: Limits = $reply_limits;
89
90 pub const REQUEST_WRITE_LIMITS: Limits = contract!(@writer $request_limits);
92 pub const RESPONSE_WRITE_LIMITS: Limits = contract!(@writer $reply_limits);
94
95 pub fn encode_request(value: &$request) -> Result<Vec<u8>, ic_wire::Error> {
100 contract!(@encode $reqroot, value,
101 Self::encoding(Direction::Request),
102 Self::REQUEST_WRITE_LIMITS,
103 )
104 }
105
106 pub fn decode_request(bytes: &[u8]) -> Result<$request, ic_wire::Error> {
111 contract!(@decode $reqroot, bytes,
112 Self::encoding(Direction::Request),
113 Self::REQUEST_LIMITS,
114 )
115 }
116
117 pub fn encode_response(value: &$reply) -> Result<Vec<u8>, ic_wire::Error> {
122 contract!(@encode $reproot, value,
123 Self::encoding(Direction::Response),
124 Self::RESPONSE_WRITE_LIMITS,
125 )
126 }
127
128 pub fn decode_response(bytes: &[u8]) -> Result<$reply, ic_wire::Error> {
133 contract!(@decode $reproot, bytes,
134 Self::encoding(Direction::Response),
135 Self::RESPONSE_LIMITS,
136 )
137 }
138
139 const fn encoding(direction: Direction) -> Encoding {
140 Encoding::Frame {
141 method: Self::ID,
142 direction,
143 }
144 }
145 }
146 };
147}
148
149pub struct ReadSession;
156
157contract!(
158 ReadSession,
159 0x415554480113,
160 "portable_session_read",
161 (), wrapped,
162 Result<SessionView, model::SessionReadRefusal>, wrapped,
163 EMPTY_LIMITS,
164 SESSION_REPLY_LIMITS
165);
166
167pub struct Logout;
174
175contract!(
176 Logout,
177 0x415554480114,
178 "logout_portable_session",
179 (),
180 wrapped,
181 model::LogoutResult,
182 wrapped,
183 EMPTY_LIMITS,
184 REPLY_LIMITS
185);
186
187pub struct CredentialEnvelope;
194
195contract!(
196 CredentialEnvelope,
197 0x415554480101,
198 "credential",
199 model::PortableCredential,
200 direct,
201 model::PortableCredential,
202 direct,
203 CREDENTIAL_LIMITS,
204 CREDENTIAL_LIMITS
205);
206
207pub struct EstablishSession;
214
215contract!(
216 EstablishSession,
217 0x415554480112,
218 "establish_portable_session",
219 SessionInput, direct,
220 Result<SessionView, model::AdmissionRefusal>, wrapped,
221 CREDENTIAL_LIMITS,
222 SESSION_REPLY_LIMITS
223);