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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
pub use crate::internal::user_api::{
EncryptedPrivateKey, UserCreateResult, UserDevice, UserDeviceListResult, UserId, UserResult,
UserUpdatePrivateKeyResult,
};
use crate::{
internal::{
user_api::{self, DeviceId, DeviceName},
DeviceAddResult, PublicKey, OUR_REQUEST,
},
IronOxide, Result,
};
use recrypt::api::Recrypt;
use std::{collections::HashMap, convert::TryInto};
#[derive(Debug, PartialEq, Clone)]
pub struct DeviceCreateOpts {
device_name: Option<DeviceName>,
}
impl DeviceCreateOpts {
pub fn new(device_name: Option<DeviceName>) -> DeviceCreateOpts {
DeviceCreateOpts { device_name }
}
}
impl Default for DeviceCreateOpts {
fn default() -> Self {
DeviceCreateOpts::new(None)
}
}
pub struct UserCreateOpts {
needs_rotation: bool,
}
impl UserCreateOpts {
pub fn new(needs_rotation: bool) -> UserCreateOpts {
UserCreateOpts { needs_rotation }
}
}
impl Default for UserCreateOpts {
fn default() -> Self {
UserCreateOpts::new(false)
}
}
#[async_trait]
pub trait UserOps {
async fn user_create(
jwt: &str,
password: &str,
user_create_opts: &UserCreateOpts,
) -> Result<UserCreateResult>;
async fn user_list_devices(&self) -> Result<UserDeviceListResult>;
async fn generate_new_device(
jwt: &str,
password: &str,
device_create_options: &DeviceCreateOpts,
) -> Result<DeviceAddResult>;
async fn user_delete_device(&self, device_id: Option<&DeviceId>) -> Result<DeviceId>;
async fn user_verify(jwt: &str) -> Result<Option<UserResult>>;
async fn user_get_public_key(&self, users: &[UserId]) -> Result<HashMap<UserId, PublicKey>>;
async fn user_rotate_private_key(&self, password: &str) -> Result<UserUpdatePrivateKeyResult>;
}
#[async_trait]
impl UserOps for IronOxide {
async fn user_create(
jwt: &str,
password: &str,
user_create_opts: &UserCreateOpts,
) -> Result<UserCreateResult> {
let recrypt = Recrypt::new();
user_api::user_create(
&recrypt,
jwt.try_into()?,
password.try_into()?,
user_create_opts.needs_rotation,
*OUR_REQUEST,
)
.await
}
async fn user_list_devices(&self) -> Result<UserDeviceListResult> {
user_api::device_list(self.device.auth()).await
}
async fn generate_new_device(
jwt: &str,
password: &str,
device_create_options: &DeviceCreateOpts,
) -> Result<DeviceAddResult> {
let recrypt = Recrypt::new();
let device_create_options = device_create_options.clone();
user_api::generate_device_key(
&recrypt,
&jwt.try_into()?,
password.try_into()?,
device_create_options.device_name,
&std::time::SystemTime::now().into(),
&OUR_REQUEST,
)
.await
}
async fn user_delete_device(&self, device_id: Option<&DeviceId>) -> Result<DeviceId> {
user_api::device_delete(self.device.auth(), device_id).await
}
async fn user_verify(jwt: &str) -> Result<Option<UserResult>> {
user_api::user_verify(jwt.try_into()?, *OUR_REQUEST).await
}
async fn user_get_public_key(&self, users: &[UserId]) -> Result<HashMap<UserId, PublicKey>> {
user_api::user_key_list(self.device.auth(), &users.to_vec()).await
}
async fn user_rotate_private_key(&self, password: &str) -> Result<UserUpdatePrivateKeyResult> {
user_api::user_rotate_private_key(&self.recrypt, password.try_into()?, self.device().auth())
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
use galvanic_assert::matchers::*;
#[test]
fn user_create_opts_defaults() {
let opts = UserCreateOpts::default();
assert_that!(
&opts,
has_structure!(UserCreateOpts {
needs_rotation: eq(false)
})
)
}
#[test]
fn user_create_opts_new() {
let opts = UserCreateOpts::new(true);
assert_that!(
&opts,
has_structure!(UserCreateOpts {
needs_rotation: eq(true)
})
)
}
}