Skip to main content

zaun_community_protocol/signed/
member_reauth.rs

1//! `MemberReauth` (ctx `zaun/v1/member-reauth`, migration.md "Members re-authenticating at
2//! the new host"): a member proves its existing member key to a host that took the community
3//! over, so the new connection binds to the same `MemberId`.
4
5use 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/// Signed by the member key. Binds the challenge to the community, the new host and the
14/// delegation epoch it operates under, so a proof made for one host or epoch is useless at
15/// another.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct MemberReauth {
18    /// 2: the community.
19    pub community_id: CommunityId,
20    /// 3: the host this proof is made to.
21    pub host_id: HostId,
22    /// 4: the delegation epoch of that host (from its welcome's proof).
23    pub delegation_epoch: u64,
24    /// 5: from this connection's latest `session.welcome`.
25    pub host_challenge: Challenge,
26    /// 6: the member key; the signer.
27    pub member_public_key: PublicKey,
28    /// 7: ms since the epoch.
29    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/// What the host expects of a reauthentication: the envelope's community, this host, the
64/// epoch it operates under and the connection's current challenge.
65#[derive(Debug, Clone, PartialEq, Eq)]
66pub struct ReauthExpectation {
67    /// The envelope's community.
68    pub community_id: CommunityId,
69    /// This host.
70    pub host_id: HostId,
71    /// The delegation epoch this host operates the community under.
72    pub delegation_epoch: u64,
73    /// The connection's current, unused challenge.
74    pub host_challenge: Challenge,
75}
76
77impl MemberReauth {
78    /// The host's check: verifies under the command's `member_public_key` (never a key read
79    /// from the bytes), then requires field 6 to be that key and fields 2–5 to match.
80    /// Consuming the challenge is the caller's job.
81    ///
82    /// # Errors
83    /// [`RegistrationError`] for a bad signature, encoding or mismatch (an epoch mismatch is
84    /// [`RegistrationError::Host`]: the proof was made for another delegation of a host).
85    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}