ic_auth_wire/
projection.rs1use crate::model as wire;
9use ic_auth_protocol_types as domain;
10use std::{error::Error, fmt};
11
12#[derive(Clone, Copy, Debug, Eq, PartialEq)]
14pub enum ProjectionError {
15 Role(domain::RoleError),
17 Scope(domain::ScopeLabelError),
19}
20impl fmt::Display for ProjectionError {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 match self {
23 Self::Role(e) => e.fmt(f),
24 Self::Scope(e) => e.fmt(f),
25 }
26 }
27}
28impl Error for ProjectionError {}
29
30impl TryFrom<wire::PortableCredential> for domain::Credential {
31 type Error = ProjectionError;
32 fn try_from(value: wire::PortableCredential) -> Result<Self, Self::Error> {
33 Ok(Self {
34 claims: value.claims.into_vec(),
35 proof: match value.proof {
36 wire::PortableProof::Ed25519(signature) => {
37 domain::CredentialProof::Ed25519(signature.into_vec())
38 }
39 wire::PortableProof::Certified(proof) => {
40 domain::CredentialProof::Certified(Box::new(certified(*proof)?))
41 }
42 },
43 })
44 }
45}
46
47fn audience(value: wire::DelegationAudience) -> domain::AudienceId {
48 let wire::DelegationAudience::Fleet(value) = value;
49 domain::AudienceId {
50 canonical_network_id: domain::CanonicalId::from_bytes(value.canonical_network_id),
51 fleet_id: domain::CanonicalId::from_bytes(value.fleet_id),
52 }
53}
54fn grants(
55 value: ic_wire::List<wire::DelegatedRoleGrant, 16>,
56) -> Result<Vec<domain::DelegatedRoleGrant>, ProjectionError> {
57 value
58 .into_vec()
59 .into_iter()
60 .map(|grant| {
61 Ok(domain::DelegatedRoleGrant {
62 target: domain::AuthRole::try_from(grant.target.into_string())
63 .map_err(ProjectionError::Role)?,
64 scopes: grant
65 .scopes
66 .into_vec()
67 .into_iter()
68 .map(|scope| {
69 domain::ScopeLabel::try_from(scope.into_string())
70 .map_err(ProjectionError::Scope)
71 })
72 .collect::<Result<_, _>>()?,
73 })
74 })
75 .collect()
76}
77fn certified(
78 value: wire::CertifiedProof,
79) -> Result<domain::CertifiedProofMaterial, ProjectionError> {
80 let cert = value.delegation.cert;
81 let wire::IssuerProofAlgorithm::IcCanisterSignatureV1 = cert.issuer_proof_alg;
82 let wire::IssuerProofBinding::IcCanisterSignatureV1(seed_hash) = cert.issuer_proof_binding;
83 let certificate = domain::DelegationCertificate {
84 issued_at_ns: cert.issued_at_ns,
85 delegation: domain::IssuerDelegation {
86 root: cert.root_pid,
87 issuer: cert.issuer_pid,
88 binding_hash: cert.issuer_proof_binding_hash,
89 seed_hash,
90 max_token_ttl_ns: cert.max_token_ttl_ns,
91 audience: audience(cert.aud),
92 grants: grants(cert.grants)?,
93 not_before_ns: cert.not_before_ns,
94 expires_at_ns: cert.expires_at_ns,
95 },
96 };
97 let wire::RootProof::IcChainKeyBatchSignatureV1(root) = value.delegation.root_proof;
98 let h = root.header;
99 let wire::ChainKeyAlgorithm::EcdsaSecp256k1 = h.algorithm;
100 let header = domain::RootBatchHeader {
101 schema_version: h.schema_version,
102 root: h.root_canister_id,
103 batch_id: h.batch_id,
104 proof_epoch: h.proof_epoch,
105 registry_epoch: h.registry_epoch,
106 registry_hash: h.registry_hash,
107 tree_root: h.tree_root,
108 not_before_ns: h.not_before_ns,
109 expires_at_ns: h.expires_at_ns,
110 key_name: h.key_id.name.into_string(),
111 derivation_path_hash: h.derivation_path_hash,
112 key_version: h.key_version,
113 };
114 let leaf = root.delegation_cert;
115 let wire::IssuerProofAlgorithm::IcCanisterSignatureV1 = leaf.issuer_proof_algorithm;
116 let wire::IssuerProofBinding::IcCanisterSignatureV1(seed_hash) = leaf.issuer_proof_binding;
117 let leaf = domain::DelegationLeaf {
118 proof_epoch: leaf.proof_epoch,
119 registry_epoch: leaf.registry_epoch,
120 registry_hash: leaf.registry_hash,
121 delegation: domain::IssuerDelegation {
122 root: leaf.root_canister_id,
123 issuer: leaf.issuer_canister_id,
124 binding_hash: leaf.issuer_proof_binding_hash,
125 seed_hash,
126 max_token_ttl_ns: leaf.max_token_ttl_ns,
127 audience: audience(leaf.audience),
128 grants: grants(leaf.grants)?,
129 not_before_ns: leaf.not_before_ns,
130 expires_at_ns: leaf.expires_at_ns,
131 },
132 };
133 let signature = root.signature;
134 let wire::ChainKeyAlgorithm::EcdsaSecp256k1 = signature.algorithm;
135 Ok(domain::CertifiedProofMaterial {
136 delegation: domain::RootDelegation {
137 certificate,
138 header,
139 leaf,
140 witness: root
141 .issuer_witness
142 .steps
143 .into_vec()
144 .into_iter()
145 .map(|step| match step {
146 wire::ChainKeyBatchWitnessStepV1::LeftSibling(hash) => {
147 domain::MerkleStep::Left(hash)
148 }
149 wire::ChainKeyBatchWitnessStepV1::RightSibling(hash) => {
150 domain::MerkleStep::Right(hash)
151 }
152 })
153 .collect(),
154 signature: domain::RootSignatureMaterial {
155 key_name: signature.key_id.name.into_string(),
156 derivation_path: signature
157 .derivation_path
158 .into_vec()
159 .into_iter()
160 .map(ic_wire::Bytes::into_vec)
161 .collect(),
162 public_key: signature.public_key.into_vec(),
163 signature: signature.signature.into_vec(),
164 },
165 },
166 issuer_signature_cbor: value.issuer_signature.signature_cbor.into_vec(),
167 issuer_public_key_der: value.issuer_signature.public_key_der.into_vec(),
168 })
169}