Skip to main content

zaun_community_protocol/client/
member.rs

1//! Helpers for a member: a fresh community-scoped key (§6.1: never reused), its
2//! registration for `invite.redeem` / `membership.request` (§4.4), invitation links
3//! (§8.6), and following the community to a new host (`migration.reauth`).
4
5mod link;
6mod moved;
7
8pub use link::{INVITE_LINK_PREFIX, InvitePayload, LinkError};
9pub use moved::{Moved, ReauthTarget, moved, reauth_body};
10
11use crate::command::Validate;
12use crate::command::invite::Redeem;
13use crate::command::membership::Request;
14use crate::command::object::NewProfile;
15use crate::crypto::SecretKey;
16use crate::ids::{Challenge, CommunityId, HostId, RngError};
17use crate::signed::{MemberKeyRegistration, Purpose, Signed, SignedObject};
18
19use super::BuildError;
20
21/// Generates a fresh member key. Use one per community (§6.1, ADR-0005).
22///
23/// # Errors
24/// [`RngError`] when the OS CSPRNG fails.
25pub fn generate_member_key() -> Result<SecretKey, RngError> {
26    SecretKey::generate()
27}
28
29/// Where a registration is made: the community, the host and the welcome's challenge.
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub struct RegistrationTarget {
32    /// The community.
33    pub community_id: CommunityId,
34    /// The host the registration is made to.
35    pub host_id: HostId,
36    /// `host_challenge` from this connection's latest verified welcome.
37    pub host_challenge: Challenge,
38}
39
40/// Signs a `MemberKeyRegistration` (§4.4) with the member key itself.
41///
42/// # Errors
43/// Signing errors.
44pub fn registration(
45    member: &SecretKey,
46    purpose: Purpose,
47    target: RegistrationTarget,
48    now_ms: u64,
49) -> Result<Signed<MemberKeyRegistration>, BuildError> {
50    let registration = MemberKeyRegistration {
51        purpose,
52        community_id: target.community_id,
53        host_id: target.host_id,
54        host_challenge: target.host_challenge,
55        member_public_key: member.public_key(),
56        issued_at: now_ms,
57    };
58    Ok(registration.sign(member)?)
59}
60
61/// The `invite.redeem` body (§8.6): the link's secret, the member key and its registration
62/// (purpose 2) for the link's community and host. Send it with the link's community as the
63/// envelope `community_id`.
64///
65/// # Errors
66/// Signing or body errors (display name length).
67pub fn redeem_body(
68    invite: &InvitePayload,
69    member: &SecretKey,
70    host_challenge: Challenge,
71    display_name: impl Into<String>,
72    now_ms: u64,
73) -> Result<Redeem, BuildError> {
74    let target = RegistrationTarget {
75        community_id: invite.community_id,
76        host_id: invite.host_id,
77        host_challenge,
78    };
79    let signed = registration(member, Purpose::JoinByInvitation, target, now_ms)?;
80    let body = Redeem {
81        invite_secret: invite.invite_secret.clone(),
82        member_public_key: member.public_key(),
83        registration: signed.to_json(),
84        profile: NewProfile {
85            display_name: display_name.into(),
86        },
87    };
88    body.validate().map_err(BuildError::Body)?;
89    Ok(body)
90}
91
92/// The `membership.request` body (§8.9) with a purpose-3 registration. Send it with
93/// `target.community_id` as the envelope `community_id` (§7.1).
94///
95/// # Errors
96/// Signing or body errors (display name, note length).
97pub fn request_body(
98    target: RegistrationTarget,
99    member: &SecretKey,
100    display_name: impl Into<String>,
101    note: Option<String>,
102    now_ms: u64,
103) -> Result<Request, BuildError> {
104    let signed = registration(member, Purpose::MembershipRequest, target, now_ms)?;
105    let body = Request {
106        member_public_key: member.public_key(),
107        registration: signed.to_json(),
108        profile: NewProfile {
109            display_name: display_name.into(),
110        },
111        note,
112    };
113    body.validate().map_err(BuildError::Body)?;
114    Ok(body)
115}