1use ic_auth_protocol_types as domain;
3use ic_auth_protocol_types::limits::{
4 MAX_CLAIMS_BYTES, MAX_GRANTS, MAX_LABEL_BYTES, MAX_SCOPES_PER_GRANT,
5};
6use ic_wire::{Bytes, Error, List, Text};
7
8trait Field<D>: Sized {
9 fn from_domain(value: D) -> Result<Self, Error>;
10 fn into_domain(self) -> Result<D, Error>;
11}
12macro_rules! scalar {
13 ($($ty:ty),*) => {$(impl Field<$ty> for $ty {
14 fn from_domain(value: $ty) -> Result<Self, Error> { Ok(value) }
15 fn into_domain(self) -> Result<$ty, Error> { Ok(self) }
16 })*};
17}
18scalar!(u16, u64, [u8; 16], [u8; 32], domain::Principal);
19impl<const N: usize> Field<Vec<u8>> for Bytes<N> {
20 fn from_domain(value: Vec<u8>) -> Result<Self, Error> {
21 Self::new(value)
22 }
23 fn into_domain(self) -> Result<Vec<u8>, Error> {
24 Ok(self.into_vec())
25 }
26}
27impl<const N: usize> Field<String> for Text<N> {
28 fn from_domain(value: String) -> Result<Self, Error> {
29 Self::new(value)
30 }
31 fn into_domain(self) -> Result<String, Error> {
32 Ok(self.into_string())
33 }
34}
35impl<D, W: Field<D>, const N: usize> Field<Vec<D>> for List<W, N> {
36 fn from_domain(value: Vec<D>) -> Result<Self, Error> {
37 if value.len() > N {
38 return Err(Error::Limit);
39 }
40 Self::new(
41 value
42 .into_iter()
43 .map(W::from_domain)
44 .collect::<Result<_, _>>()?,
45 )
46 }
47 fn into_domain(self) -> Result<Vec<D>, Error> {
48 self.into_vec().into_iter().map(W::into_domain).collect()
49 }
50}
51impl Field<domain::CanonicalId> for [u8; 32] {
52 fn from_domain(value: domain::CanonicalId) -> Result<Self, Error> {
53 Ok(*value.as_bytes())
54 }
55 fn into_domain(self) -> Result<domain::CanonicalId, Error> {
56 Ok(domain::CanonicalId::from_bytes(self))
57 }
58}
59impl Field<domain::AuthRole> for Text<MAX_LABEL_BYTES> {
60 fn from_domain(value: domain::AuthRole) -> Result<Self, Error> {
61 Self::new(value.into_string())
62 }
63 fn into_domain(self) -> Result<domain::AuthRole, Error> {
64 domain::AuthRole::try_from(self.into_string()).map_err(|_| Error::Malformed)
65 }
66}
67impl Field<domain::ScopeLabel> for Text<MAX_LABEL_BYTES> {
68 fn from_domain(value: domain::ScopeLabel) -> Result<Self, Error> {
69 Self::new(value.into_string())
70 }
71 fn into_domain(self) -> Result<domain::ScopeLabel, Error> {
72 domain::ScopeLabel::try_from(self.into_string()).map_err(|_| Error::Malformed)
73 }
74}
75macro_rules! record {
78 ($name:ident { $($field:ident: $ty:ty),* $(,)? }) => {
79 #[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
80#[cfg_attr(feature = "typescript", derive(ic_wire::Wire))]
81 pub struct $name { $(pub $field: $ty),* }
82 impl TryFrom<domain::$name> for $name {
83 type Error = Error;
84 fn try_from(value: domain::$name) -> Result<Self, Error> {
85 Ok(Self { $($field: Field::from_domain(value.$field)?),* })
86 }
87 }
88 impl TryFrom<$name> for domain::$name {
89 type Error = Error;
90 fn try_from(value: $name) -> Result<Self, Error> {
91 Ok(Self { $($field: Field::into_domain(value.$field)?),* })
92 }
93 }
94 impl Field<domain::$name> for $name {
95 fn from_domain(value: domain::$name) -> Result<Self, Error> { value.try_into() }
96 fn into_domain(self) -> Result<domain::$name, Error> { self.try_into() }
97 }
98 };
99}
100macro_rules! unit_enum {
101 ($name:ident { $($variant:ident),* $(,)? }) => {
102 #[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
103#[cfg_attr(feature = "typescript", derive(ic_wire::Wire))]
104 pub enum $name { $($variant),* }
105 impl Field<domain::$name> for $name {
106 fn from_domain(value: domain::$name) -> Result<Self, Error> {
107 Ok(value.into())
108 }
109 fn into_domain(self) -> Result<domain::$name, Error> {
110 Ok(self.into())
111 }
112 }
113 impl From<domain::$name> for $name {
114 fn from(value: domain::$name) -> Self {
115 match value { $(domain::$name::$variant => Self::$variant),* }
116 }
117 }
118 impl From<$name> for domain::$name {
119 fn from(value: $name) -> Self {
120 match value { $($name::$variant => Self::$variant),* }
121 }
122 }
123 };
124}
125macro_rules! tuple_enum {
126 ($name:ident { $($variant:ident($ty:ty)),* $(,)? }) => {
127 #[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
128#[cfg_attr(feature = "typescript", derive(ic_wire::Wire))]
129 pub enum $name { $($variant($ty)),* }
130 impl Field<domain::$name> for $name {
131 fn from_domain(value: domain::$name) -> Result<Self, Error> {
132 Ok(match value { $(domain::$name::$variant(v) => Self::$variant(Field::from_domain(v)?)),* })
133 }
134 fn into_domain(self) -> Result<domain::$name, Error> {
135 Ok(match self { $(Self::$variant(v) => domain::$name::$variant(Field::into_domain(v)?)),* })
136 }
137 }
138 };
139}
140record!(AudienceId {
141 canonical_network_id: [u8; 32],
142 fleet_id: [u8; 32]
143});
144record!(DelegatedRoleGrant { target: Text<MAX_LABEL_BYTES>, scopes: List<Text<MAX_LABEL_BYTES>, MAX_SCOPES_PER_GRANT> });
145tuple_enum!(DelegationAudience {
146 Fleet(AudienceId)
147});
148unit_enum!(ChainKeyAlgorithm { EcdsaSecp256k1 });
149record!(ChainKeyKeyId { name: Text<128> });
150unit_enum!(IssuerProofAlgorithm {
151 IcCanisterSignatureV1
152});
153#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
154#[cfg_attr(feature = "typescript", derive(ic_wire::Wire))]
155pub enum IssuerProofBinding {
156 IcCanisterSignatureV1([u8; 32]),
157}
158impl Field<domain::IssuerProofBinding> for IssuerProofBinding {
159 fn from_domain(value: domain::IssuerProofBinding) -> Result<Self, Error> {
160 let domain::IssuerProofBinding::IcCanisterSignatureV1 { seed_hash } = value;
161 Ok(Self::IcCanisterSignatureV1(seed_hash))
162 }
163 fn into_domain(self) -> Result<domain::IssuerProofBinding, Error> {
164 let Self::IcCanisterSignatureV1(seed_hash) = self;
165 Ok(domain::IssuerProofBinding::IcCanisterSignatureV1 { seed_hash })
166 }
167}
168record!(IcCanisterSignatureProofV1 { signature_cbor: Bytes<131072>, public_key_der: Bytes<256> });
169record!(ChainKeyBatchHeaderV1 {
170 schema_version: u16,
171 root_canister_id: domain::Principal,
172 batch_id: [u8; 32],
173 proof_epoch: u64,
174 registry_epoch: u64,
175 registry_hash: [u8; 32],
176 tree_root: [u8; 32],
177 not_before_ns: u64,
178 expires_at_ns: u64,
179 algorithm: ChainKeyAlgorithm,
180 key_id: ChainKeyKeyId,
181 derivation_path_hash: [u8; 32],
182 key_version: u64,
183});
184record!(ChainKeyDelegationCertV1 {
185 root_canister_id: domain::Principal, issuer_canister_id: domain::Principal, proof_epoch: u64,
186 issuer_proof_algorithm: IssuerProofAlgorithm, issuer_proof_binding_hash: [u8; 32],
187 issuer_proof_binding: IssuerProofBinding, max_token_ttl_ns: u64,
188 audience: DelegationAudience, grants: List<DelegatedRoleGrant, MAX_GRANTS>,
189 not_before_ns: u64, expires_at_ns: u64, registry_epoch: u64, registry_hash: [u8; 32],
190});
191record!(ChainKeyRootSignatureV1 {
192 algorithm: ChainKeyAlgorithm, key_id: ChainKeyKeyId,
193 derivation_path: List<Bytes<256>, 32>, public_key: Bytes<65>, signature: Bytes<64>,
194});
195tuple_enum!(ChainKeyBatchWitnessStepV1 {
196 LeftSibling([u8; 32]),
197 RightSibling([u8; 32])
198});
199record!(ChainKeyBatchWitnessV1 { steps: List<ChainKeyBatchWitnessStepV1, 64> });
200record!(IcChainKeyBatchSignatureProofV1 {
201 header: ChainKeyBatchHeaderV1,
202 delegation_cert: ChainKeyDelegationCertV1,
203 issuer_witness: ChainKeyBatchWitnessV1,
204 signature: ChainKeyRootSignatureV1,
205});
206tuple_enum!(RootProof {
207 IcChainKeyBatchSignatureV1(IcChainKeyBatchSignatureProofV1)
208});
209record!(DelegationCert {
210 root_pid: domain::Principal, issuer_pid: domain::Principal, issuer_proof_alg: IssuerProofAlgorithm,
211 issuer_proof_binding_hash: [u8; 32], issuer_proof_binding: IssuerProofBinding,
212 issued_at_ns: u64, not_before_ns: u64, expires_at_ns: u64,
213 max_token_ttl_ns: u64, aud: DelegationAudience, grants: List<DelegatedRoleGrant, MAX_GRANTS>,
214});
215record!(DelegationProof {
216 cert: DelegationCert,
217 root_proof: RootProof
218});
219#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
220#[cfg_attr(feature = "typescript", derive(ic_wire::Wire))]
221pub struct CertifiedProof {
222 pub delegation: DelegationProof,
223 pub issuer_signature: IcCanisterSignatureProofV1,
224}
225#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
226#[cfg_attr(feature = "typescript", derive(ic_wire::Wire))]
227pub enum PortableProof {
228 Certified(Box<CertifiedProof>),
229 Ed25519(Bytes<64>),
230}
231impl Field<domain::PortableProof> for PortableProof {
232 fn from_domain(value: domain::PortableProof) -> Result<Self, Error> {
233 Ok(match value {
234 domain::PortableProof::Certified {
235 delegation,
236 issuer_signature,
237 } => Self::Certified(Box::new(CertifiedProof {
238 delegation: (*delegation).try_into()?,
239 issuer_signature: issuer_signature.try_into()?,
240 })),
241 domain::PortableProof::Ed25519 { signature } => Self::Ed25519(Bytes::new(signature)?),
242 })
243 }
244 fn into_domain(self) -> Result<domain::PortableProof, Error> {
245 Ok(match self {
246 Self::Certified(value) => domain::PortableProof::Certified {
247 delegation: Box::new(value.delegation.try_into()?),
248 issuer_signature: value.issuer_signature.try_into()?,
249 },
250 Self::Ed25519(signature) => domain::PortableProof::Ed25519 {
251 signature: signature.into_vec(),
252 },
253 })
254 }
255}
256record!(PortableCredential { claims: Bytes<MAX_CLAIMS_BYTES>, proof: PortableProof });
257unit_enum!(AdmissionRefusal {
259 NotYetValid,
260 Expired,
261 InvalidCredential,
262 AuthorityUnavailable,
263 BeforeReset,
264 ScopeRejected,
265 ReplayConflict,
266 Revoked,
267 Capacity,
268 Throttled,
269 Unavailable
270});
271unit_enum!(LogoutUnavailable {
272 Capacity,
273 Storage,
274 Clock,
275 ResetPending
276});
277#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
278#[cfg_attr(feature = "typescript", derive(ic_wire::Wire))]
279pub enum LogoutResult {
280 Revoked(u64),
281 Unconfirmed(u64),
282 InvalidCaller,
283 Unavailable(LogoutUnavailable),
284}
285impl From<domain::LogoutResult> for LogoutResult {
286 fn from(value: domain::LogoutResult) -> Self {
287 match value {
288 domain::LogoutResult::Revoked { deny_until_ns } => Self::Revoked(deny_until_ns),
289 domain::LogoutResult::Unconfirmed { deny_until_ns } => Self::Unconfirmed(deny_until_ns),
290 domain::LogoutResult::InvalidCaller => Self::InvalidCaller,
291 domain::LogoutResult::Unavailable(reason) => Self::Unavailable(reason.into()),
292 }
293 }
294}
295unit_enum!(SessionReadRefusal {
296 MissingOrExpired,
297 AuthorityUnavailable,
298 ScopeRejected,
299 Revoked,
300 Unavailable
301});