1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Database models for users and user-related fields

use crate::{
    config::opaque::OpaqueCipherSuite,
    crypto::{Encrypted, StorageKey},
    LockKeeperError,
};

use opaque_ke::ServerRegistration;
use rand::{CryptoRng, Rng, RngCore};
use serde::{Deserialize, Serialize};
use std::{
    array::{IntoIter, TryFromSliceError},
    fmt::{Debug, Display, Formatter},
    str::FromStr,
};
use utilities::crypto::error::CryptoError;

use super::HexBytes;

/// One user with a set of arbitrary secrets and a [`ServerRegistration`] to
/// authenticate with.
#[derive(Deserialize, Serialize)]
pub struct Account {
    pub account_id: AccountId,
    pub user_id: UserId,
    pub account_name: AccountName,
    pub storage_key: Option<Encrypted<StorageKey>>,
    pub server_registration: ServerRegistration<OpaqueCipherSuite>,
}

impl Account {
    pub fn id(&self) -> AccountId {
        self.account_id
    }
}

/// Manual implement Debug to avoid printing storage key or server_registration.
impl Debug for Account {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Account")
            .field("user_id", &self.user_id)
            .field("account_name", &self.account_name)
            .finish()
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub struct AccountId(pub i64);

impl From<i64> for AccountId {
    fn from(n: i64) -> Self {
        Self(n)
    }
}

impl From<AccountId> for i64 {
    fn from(account_id: AccountId) -> Self {
        account_id.0
    }
}

/// Unique ID for a user.
/// Wrapped in a `Box` to avoid stack overflows during heavy traffic.
#[derive(Clone, Serialize, Deserialize, Default, PartialEq, Eq, Hash)]
#[serde(try_from = "HexBytes", into = "HexBytes")]
pub struct UserId(Box<[u8; 16]>);

impl AsRef<[u8; 16]> for UserId {
    fn as_ref(&self) -> &[u8; 16] {
        &self.0
    }
}
impl TryFrom<&[u8]> for UserId {
    type Error = TryFromSliceError;

    fn try_from(id: &[u8]) -> Result<Self, Self::Error> {
        Ok(UserId(Box::new(<[u8; 16]>::try_from(id)?)))
    }
}

impl UserId {
    pub fn new(rng: &mut (impl CryptoRng + RngCore)) -> Result<Self, LockKeeperError> {
        // Generate random bytes
        let mut id = [0_u8; 16];
        rng.try_fill(&mut id)
            .map_err(|_| CryptoError::RandomNumberGeneratorFailed)?;

        Ok(Self(Box::new(id)))
    }

    pub fn as_bytes(&self) -> &[u8] {
        self.0.as_ref()
    }

    pub(crate) fn len(&self) -> usize {
        self.as_bytes().len()
    }
}

impl IntoIterator for UserId {
    type Item = u8;
    type IntoIter = IntoIter<u8, 16>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl Debug for UserId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let hex = hex::encode(*self.0);
        f.debug_tuple("UserId").field(&hex).finish()
    }
}

impl Display for UserId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let hex = hex::encode(*self.0);
        write!(f, "{hex}")
    }
}

impl From<UserId> for HexBytes {
    fn from(key_id: UserId) -> Self {
        (*key_id.0).into()
    }
}

impl TryFrom<HexBytes> for UserId {
    type Error = LockKeeperError;

    fn try_from(bytes: HexBytes) -> Result<Self, Self::Error> {
        Ok(UserId(Box::new(bytes.try_into()?)))
    }
}

/// Account name used as human-memorable identifier for a user during OPAQUE.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AccountName(String);

impl Display for AccountName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

impl From<AccountName> for String {
    fn from(account_name: AccountName) -> Self {
        account_name.0
    }
}

impl AsRef<str> for AccountName {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl FromStr for AccountName {
    type Err = LockKeeperError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(AccountName(s.to_string()))
    }
}

impl From<&str> for AccountName {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

impl AccountName {
    pub fn as_bytes(&self) -> &[u8] {
        self.0.as_bytes()
    }
}