zaun_community_protocol/signed/
member_reauth.rs1use super::error::{RegistrationError, SignedError};
6use super::object::{FORMAT_V1, Signed, SignedObject, read_format};
7use crate::crypto::Context;
8use crate::encoding::{Decode, DecodeError, Encode, EncodeError, Reader, Writer};
9use crate::ids::{Challenge, CommunityId, HostId};
10use crate::json::SignedJson;
11use crate::keys::PublicKey;
12
13#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct MemberReauth {
18 pub community_id: CommunityId,
20 pub host_id: HostId,
22 pub delegation_epoch: u64,
24 pub host_challenge: Challenge,
26 pub member_public_key: PublicKey,
28 pub issued_at: u64,
30}
31
32impl Encode for MemberReauth {
33 fn encode(&self, w: &mut Writer) -> Result<(), EncodeError> {
34 w.u8(FORMAT_V1);
35 w.put(&self.community_id)?;
36 w.put(&self.host_id)?;
37 w.u64(self.delegation_epoch);
38 w.put(&self.host_challenge)?;
39 w.put(&self.member_public_key)?;
40 w.u64(self.issued_at);
41 Ok(())
42 }
43}
44
45impl Decode for MemberReauth {
46 fn decode(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
47 read_format(r)?;
48 Ok(Self {
49 community_id: r.get()?,
50 host_id: r.get()?,
51 delegation_epoch: r.u64()?,
52 host_challenge: r.get()?,
53 member_public_key: r.get()?,
54 issued_at: r.u64()?,
55 })
56 }
57}
58
59impl SignedObject for MemberReauth {
60 const CONTEXT: Context = Context::MemberReauth;
61}
62
63#[derive(Debug, Clone, PartialEq, Eq)]
66pub struct ReauthExpectation {
67 pub community_id: CommunityId,
69 pub host_id: HostId,
71 pub delegation_epoch: u64,
73 pub host_challenge: Challenge,
75}
76
77impl MemberReauth {
78 pub fn verify_for_host(
86 signed: &SignedJson,
87 member_public_key: &PublicKey,
88 expected: &ReauthExpectation,
89 min_suite: u8,
90 ) -> Result<Signed<Self>, RegistrationError> {
91 let signed = Signed::<Self>::from_json(signed, member_public_key, min_suite)
92 .map_err(|e: SignedError| RegistrationError::Signed(e))?;
93 let r = signed.object();
94 let checks = [
95 (
96 r.member_public_key == *member_public_key,
97 RegistrationError::MemberKey,
98 ),
99 (
100 r.community_id == expected.community_id,
101 RegistrationError::Community,
102 ),
103 (
104 r.host_id == expected.host_id && r.delegation_epoch == expected.delegation_epoch,
105 RegistrationError::Host,
106 ),
107 (
108 r.host_challenge == expected.host_challenge,
109 RegistrationError::Challenge,
110 ),
111 ];
112 match checks.into_iter().find(|(ok, _)| !ok) {
113 Some((_, error)) => Err(error),
114 None => Ok(signed),
115 }
116 }
117}