//! Membership actions signed by the person's own device, over a held database's pinned line. use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; use cotton_client::transport::ServerFetch; use cotton_core::keys::member_actions::{ GrantRights, MembershipAction, MembershipApply, MembershipPlan, MembershipPrepare, }; use serde_json::{json, Value}; use crate::{handle::CottonHandle, methods::MethodError, shell::client::ClientController}; fn rights(request: &Value) -> Result { match request.get("rights") { None | Some(Value::Null) => Ok(GrantRights::Member), Some(value) => serde_json::from_value(value.clone()) .map_err(|_| MethodError::new("bad_request", "Invite rights must be member or admin.")), } } pub fn invite(handle: &CottonHandle, request: &Value) -> Result { act( handle, request, MembershipAction::Invite { rights: rights(request)?, }, ) } pub fn remove(handle: &CottonHandle, request: &Value) -> Result { act( handle, request, MembershipAction::Remove { person_key: super::owner::text(request, "personKey")?, }, ) } pub fn admit(handle: &CottonHandle, request: &Value) -> Result { act( handle, request, MembershipAction::Admit { pairing_id: super::owner::text(request, "pairingId")?, person_id: request .get("personId") .filter(|v| !v.is_null()) .map(|_| super::owner::text(request, "personId")) .transpose()?, }, ) } pub fn claim_person(handle: &CottonHandle, request: &Value) -> Result { act( handle, request, MembershipAction::ClaimPerson { person_id: super::owner::text(request, "personId")?, }, ) } fn act( handle: &CottonHandle, request: &Value, action: MembershipAction, ) -> Result { let database = super::owner::text(request, "database")?; super::database_calls::with_fetch(handle, request, |fetch| { action_over(&handle.client(), fetch, &database, action) }) } fn action_over( client: &ClientController, fetch: &dyn ServerFetch, database: &str, action: MembershipAction, ) -> Result { // Household owner routes already authenticate this device as the owner. // They also work before the first admission has created an owner fact. { match &action { MembershipAction::Invite { rights } => { let body = if *rights == GrantRights::Member { json!({}) } else { json!({"rights":rights}) }; return super::owner::send(fetch, super::owner::post("/v1/invites".into(), &body)); } MembershipAction::Remove { person_key } => { return super::owner::send( fetch, super::owner::post( format!( "/v1/databases/members/{}/remove", super::owner::segment(person_key) ), &json!({}), ), ); } MembershipAction::Admit { .. } | MembershipAction::ClaimPerson { .. } => {} } } over(client, fetch, database, action) } /// Validate the server's public plan before lending the local person key to a /// signature. The key lives in Mine, irrespective of the window's database. fn over( client: &ClientController, fetch: &dyn ServerFetch, database: &str, action: MembershipAction, ) -> Result { let _personal = client .name_database(None) .map_err(|error| MethodError::new("database_not_held", error))?; let person = super::vault::person_public_key(client)?; // The plan is signed by the person key, which a locked vault will not // lend: refused here, before the Mac prepares a plan nobody will sign. super::vault::person_can_sign(client)?; let prepared = super::owner::send( fetch, super::owner::post( "/v1/databases/members/prepare".into(), &json!(MembershipPrepare { action: action.clone() }), ), )?; let refused = |error: String| MethodError::new("membership_plan_refused", error); let plan: MembershipPlan = serde_json::from_value(prepared).map_err(|error| refused(error.to_string()))?; plan.matches_request(database, &person, &action) .map_err(|error| refused(error.to_string()))?; let messages = plan .signing_messages() .map_err(|error| refused(error.to_string()))?; let mut signatures = Vec::with_capacity(messages.len()); for message in messages { let (signed_by, signature) = super::vault::sign_as_person(client, &message)?; if signed_by != person { return Err(refused( "The local person key changed during this action.".into(), )); } signatures.push(URL_SAFE_NO_PAD.encode(signature)); } super::owner::send( fetch, super::owner::post( "/v1/databases/members/apply".into(), &json!(MembershipApply { plan, signatures }), ), ) } #[cfg(test)] #[path = "member_actions_tests.rs"] mod tests;