coven_storage/
invite_storage.rs1use crate::{CloudProvider, S3Credentials, StorageError, StorageFailure, StorageInvitation};
4use coven_crypto::SecretBytes;
5use coven_format::code_fields::{pack, FieldReader};
6
7#[derive(Clone)]
10pub enum InviteStorage {
11 Account(StorageInvitation),
13 S3 {
15 invitation: StorageInvitation,
17 credentials: S3Credentials,
19 },
20}
21
22impl InviteStorage {
23 pub fn encode(&self) -> Result<SecretBytes, StorageError> {
27 let (invitation, credentials) = match self {
28 Self::Account(invitation) => (invitation, None),
29 Self::S3 { invitation, credentials } => (invitation, Some(credentials)),
30 };
31 if (invitation.location().provider() == CloudProvider::S3) != credentials.is_some() {
32 return Err(StorageFailure::InvalidConfiguration
33 .with_source("an S3 invite carries the member's key; no other invite does"));
34 }
35 let shared = matches!(
36 invitation.location().provider(),
37 CloudProvider::Dropbox | CloudProvider::OneDrive
38 );
39 if shared != invitation.account().is_some() {
40 return Err(StorageFailure::InvalidConfiguration.with_source(
41 "a Dropbox or OneDrive invite names the account it was shared with; no other does",
42 ));
43 }
44 if let Some(credentials) = credentials {
45 credentials.validate()?;
46 }
47 Ok(pack(|out| {
48 invitation.pack(out)?;
49 match credentials {
50 Some(credentials) => credentials.pack(out),
51 None => Ok(()),
52 }
53 })?)
54 }
55 pub fn decode(bytes: &[u8]) -> Result<Self, StorageError> {
58 let mut input = FieldReader::new(bytes)?;
59 let invitation = StorageInvitation::unpack(&mut input)?;
60 let storage = match invitation.location().provider() {
61 CloudProvider::S3 => {
62 Self::S3 { invitation, credentials: S3Credentials::unpack(&mut input)? }
63 }
64 _ => Self::Account(invitation),
65 };
66 input.finish()?;
67 Ok(storage)
68 }
69}
70
71#[cfg(test)]
72#[path = "invite_storage_tests.rs"]
73mod tests;