zaun_community_protocol/client/
welcome.rs1mod chain;
4
5use serde_json::{Map, Value};
6
7use super::{ExpectedHost, WelcomeError};
8use crate::command::object::Binding;
9use crate::command::session::{HostAttachments, Welcome};
10use crate::crypto;
11use crate::envelope::VersionRange;
12use crate::ids::{Challenge, CommunityId, HostId, Nonce};
13use crate::json::SignedJson;
14use crate::keys::PublicKey;
15use crate::signed::{
16 AcceptedState, HostSessionProof, Scopes, SignedRootRotation, ValidPair, validate_pair,
17};
18
19#[derive(Debug, Clone)]
22pub struct VerifiedWelcome {
23 pub version: u16,
25 pub host_id: HostId,
27 pub host_public_key: PublicKey,
29 pub host_challenge: Challenge,
31 pub capabilities: Vec<String>,
33 pub limits: Map<String, Value>,
35 pub binding: Option<Binding>,
37 pub community: Option<ValidPair>,
40 pub attachments: Option<HostAttachments>,
42}
43
44#[derive(Debug)]
46pub(super) struct Expectation<'a> {
47 pub(super) held: Option<&'a (SignedJson, SignedJson)>,
49 pub(super) nonce: &'a Nonce,
50 pub(super) offered: VersionRange,
51 pub(super) host: &'a ExpectedHost,
52 pub(super) community: Option<CommunityId>,
53 pub(super) accepted: Option<&'a AcceptedState>,
54 pub(super) rotations: &'a [SignedRootRotation],
55 pub(super) now_ms: u64,
56}
57
58pub(super) fn verify(
65 welcome: Welcome,
66 exp: &Expectation<'_>,
67) -> Result<VerifiedWelcome, WelcomeError> {
68 if !exp.offered.contains(welcome.version) {
69 return Err(WelcomeError::Version {
70 version: welcome.version,
71 offered: exp.offered,
72 });
73 }
74 let key = &welcome.host_public_key;
75 if crypto::host_id(key) != welcome.host_id {
76 return Err(WelcomeError::HostId);
77 }
78 if !exp.host.matches(key) {
79 return Err(WelcomeError::Host);
80 }
81 let bound = welcome.binding.map(|b| b.community_id);
82 if let (Some(expected), Some(bound)) = (exp.community, bound)
83 && expected != bound
84 {
85 return Err(WelcomeError::Binding);
86 }
87 let target = exp.community.or(bound);
88 let pair = match target {
89 Some(community) => Some(verify_pair(&welcome, community, exp)?),
90 None => None,
91 };
92 let proof = HostSessionProof::verify_for_client(&welcome.proof, key, exp.nonce, pair.as_ref())?;
93 let proof = proof.object();
94 if target.is_some() && proof.community_id != target {
95 return Err(WelcomeError::MissingCommunity);
96 }
97 if proof.host_challenge != welcome.host_challenge {
98 return Err(WelcomeError::Challenge);
99 }
100 Ok(VerifiedWelcome {
101 version: welcome.version,
102 host_id: welcome.host_id,
103 host_challenge: proof.host_challenge,
104 host_public_key: welcome.host_public_key,
105 capabilities: welcome.capabilities,
106 limits: welcome.limits,
107 binding: welcome.binding,
108 community: pair,
109 attachments: welcome.attachments,
110 })
111}
112
113fn verify_pair(
114 welcome: &Welcome,
115 community: CommunityId,
116 exp: &Expectation<'_>,
117) -> Result<ValidPair, WelcomeError> {
118 let (descriptor, delegation) = match (&welcome.descriptor, &welcome.delegation, exp.held) {
122 (Some(d), Some(g), _) => (d, g),
123 (None, None, Some((d, g))) => (d, g),
124 _ => return Err(WelcomeError::MissingCommunity),
125 };
126 let rotations = chain::complete(&descriptor.bytes, exp.rotations)?;
127 let pair = validate_pair(
128 &community,
129 descriptor,
130 delegation,
131 &rotations,
132 exp.accepted,
133 exp.now_ms,
134 )?;
135 let d = pair.delegation().object();
136 let authorised = d.host_id == welcome.host_id
137 && d.host_public_key == welcome.host_public_key
138 && d.grants(Scopes::OPERATE);
139 if authorised {
140 Ok(pair)
141 } else {
142 Err(WelcomeError::Delegation)
143 }
144}