-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathauthorizer.rs
335 lines (294 loc) · 10.3 KB
/
authorizer.rs
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use std::{collections::HashSet, future::Future};
use tracing::debug;
use tracing::Level;
use crate::roles::BuiltinRoleSet;
pub type UserIdentity = String;
pub type UserName = String;
#[derive(Debug, Clone)]
pub struct UserInfo {
pub identity: UserIdentity,
pub name: UserName,
}
#[derive(Clone)]
pub struct Authorizer<S: StorageDriver> {
user: UserInfo,
user_id: i64,
user_roles: HashSet<S::BuiltinRole>,
#[allow(unused)] // will be used soon
storage: S,
}
pub trait StorageDriver: Clone {
type BuiltinRole: BuiltinRoleSet + std::fmt::Debug;
type Error: std::error::Error;
fn get_user_id(
&self,
user_info: &UserInfo,
) -> impl Future<Output = Result<Option<i64>, Self::Error>> + Send;
fn get_user_info(
&self,
user_id: i64,
) -> impl Future<Output = Result<Option<UserInfo>, Self::Error>> + Send;
fn ensure_user(&self, user: &UserInfo)
-> impl Future<Output = Result<i64, Self::Error>> + Send;
fn fetch_subject_roles(
&self,
subject_id: i64,
) -> impl Future<Output = Result<HashSet<Self::BuiltinRole>, Self::Error>> + Send;
fn ensure_subject_roles(
&self,
subject_id: i64,
roles: HashSet<Self::BuiltinRole>,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn remove_subject_roles(
&self,
subject_id: i64,
roles: HashSet<Self::BuiltinRole>,
) -> impl Future<Output = Result<HashSet<Self::BuiltinRole>, Self::Error>> + Send;
}
impl<S: StorageDriver> Authorizer<S> {
#[tracing::instrument(skip_all, fields(%user), err)]
pub async fn try_initialize(user: UserInfo, storage_driver: S) -> Result<Self, S::Error> {
let user_id = storage_driver.ensure_user(&user).await?;
debug!(%user, %user_id, "user authenticated");
let user_roles = storage_driver.fetch_subject_roles(user_id).await?;
Ok(Self {
user,
user_id,
user_roles,
storage: storage_driver,
})
}
pub fn new_superuser(storage_driver: S) -> Self {
Self {
user: UserInfo {
identity: "superuser".to_string(),
name: "Super User".to_string(),
},
user_id: -1,
user_roles: HashSet::from([S::BuiltinRole::superuser()]),
storage: storage_driver,
}
}
pub fn user_id(&self) -> i64 {
self.user_id
}
pub fn is_superuser(&self) -> bool {
self.user_roles.contains(&S::BuiltinRole::superuser())
}
/// Returns whether a user with some id exists
#[tracing::instrument(skip_all, fields(user_id = %user_id), ret(level = Level::DEBUG), err)]
pub async fn user_exists(&self, user_id: i64) -> Result<bool, S::Error> {
self.storage
.get_user_info(user_id)
.await
.map(|x| x.is_some())
}
/// Check that the user has all the required builting roles
#[tracing::instrument(skip_all, fields(user = %self.user, user_roles = ?self.user_roles, ?required_roles), ret(level = Level::DEBUG), err)]
pub async fn check_roles(
&self,
required_roles: HashSet<S::BuiltinRole>,
) -> Result<bool, S::Error> {
if self.is_superuser() {
tracing::debug!("role checking skipped for superuser");
return Ok(true);
}
Ok(required_roles.is_subset(&self.user_roles))
}
#[tracing::instrument(skip_all, fields(user_id, auth_user = %self.user, user_roles = ?self.user_roles), ret(level = Level::DEBUG), err)]
pub async fn user_builtin_roles(
&self,
user_id: i64,
) -> Result<HashSet<S::BuiltinRole>, S::Error> {
let user_roles = self.storage.fetch_subject_roles(user_id).await?;
Ok(user_roles.clone())
}
#[tracing::instrument(skip_all, fields(user_id, auth_user = %self.user, ?roles), ret(level = Level::DEBUG), err)]
pub async fn grant_roles(
&mut self,
user_id: i64,
roles: HashSet<S::BuiltinRole>,
) -> Result<(), S::Error> {
self.storage
.ensure_subject_roles(user_id, roles.clone())
.await?;
self.user_roles.extend(roles);
Ok(())
}
#[tracing::instrument(skip_all, fields(user_id, auth_user = %self.user, ?roles), ret(level = Level::DEBUG), err)]
pub async fn strip_roles(
&mut self,
user_id: i64,
roles: HashSet<S::BuiltinRole>,
) -> Result<(), S::Error> {
let removed_roles = self
.storage
.remove_subject_roles(user_id, roles.clone())
.await?;
tracing::debug!(?removed_roles, "removed roles");
self.user_roles.retain(|r| !roles.contains(r));
Ok(())
}
}
impl<S: StorageDriver> std::fmt::Debug for Authorizer<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Authorizer")
.field("user", &self.user)
.field("user_id", &self.user_id)
.field("user_roles", &self.user_roles)
.finish()
}
}
impl std::fmt::Display for UserInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({})", self.identity, self.name)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::fixtures::*;
use pretty_assertions::assert_eq;
use std::{
collections::HashMap,
convert::Infallible,
sync::{Arc, Mutex},
};
#[derive(Debug, Clone, Default)]
struct MockStorageDriver {
users: Arc<Mutex<HashMap<UserIdentity, i64>>>,
user_roles: Arc<Mutex<HashMap<i64, HashSet<TestBuiltinRole>>>>,
}
#[tokio::test]
async fn superuser() {
let storage = MockStorageDriver::default();
let authorizer = Authorizer::new_superuser(storage);
assert!(authorizer.is_superuser());
// Check that the superuser has any role even if not explicitely granted
assert_eq!(
authorizer
.check_roles(HashSet::from([TestBuiltinRole::UserBan]))
.await,
Ok(true)
);
}
#[tokio::test]
async fn check_roles() {
let storage = MockStorageDriver::default();
// insert some mocked roles
{
let mut user_roles = storage.user_roles.lock().unwrap();
user_roles.insert(
0,
HashSet::from([
TestBuiltinRole::DocRead,
TestBuiltinRole::UserAdd,
TestBuiltinRole::UserBan,
]),
);
}
let authorizer = Authorizer::try_initialize(
UserInfo {
identity: "toto".to_owned(),
name: "Sir Toto, the One and Only".to_owned(),
},
storage,
)
.await
.unwrap();
{
let users = authorizer.storage.users.lock().unwrap();
assert_eq!(
users.iter().next(),
Some((&"toto".to_owned(), &0)),
"new user should have been created"
);
}
assert!(authorizer
.check_roles(HashSet::from([TestBuiltinRole::DocRead]))
.await
.unwrap());
assert!(authorizer
.check_roles(HashSet::from([
TestBuiltinRole::UserAdd,
TestBuiltinRole::UserBan
]))
.await
.unwrap());
assert!(authorizer
.check_roles(HashSet::from([
TestBuiltinRole::DocRead,
TestBuiltinRole::UserAdd,
TestBuiltinRole::UserBan
]))
.await
.unwrap());
assert!(!authorizer
.check_roles(HashSet::from([TestBuiltinRole::DocEdit]))
.await
.unwrap());
assert!(!authorizer
.check_roles(HashSet::from([
TestBuiltinRole::DocRead,
TestBuiltinRole::DocDelete,
]))
.await
.unwrap());
assert!(authorizer.check_roles(HashSet::from([])).await.unwrap());
}
impl StorageDriver for MockStorageDriver {
type BuiltinRole = TestBuiltinRole;
type Error = Infallible;
async fn ensure_user(&self, user: &UserInfo) -> Result<i64, Self::Error> {
let mut users = self.users.lock().unwrap();
let next_id = users.len() as i64;
let user_id = users.entry(user.identity.clone()).or_insert(next_id);
Ok(*user_id)
}
async fn fetch_subject_roles(
&self,
subject_id: i64,
) -> Result<HashSet<Self::BuiltinRole>, Self::Error> {
let user_roles = self.user_roles.lock().unwrap();
let roles = user_roles.get(&subject_id).cloned().expect("no user");
Ok(roles)
}
async fn ensure_subject_roles(
&self,
subject_id: i64,
roles: HashSet<Self::BuiltinRole>,
) -> Result<(), Self::Error> {
let mut user_roles = self.user_roles.lock().unwrap();
user_roles.entry(subject_id).or_default().extend(roles);
Ok(())
}
async fn remove_subject_roles(
&self,
subject_id: i64,
roles: HashSet<Self::BuiltinRole>,
) -> Result<HashSet<Self::BuiltinRole>, Self::Error> {
let mut user_roles = self.user_roles.lock().unwrap();
let user_roles = user_roles.entry(subject_id).or_default();
let removed_roles = roles
.iter()
.filter(|role| user_roles.remove(*role))
.cloned()
.collect();
Ok(removed_roles)
}
async fn get_user_id(&self, user_info: &UserInfo) -> Result<Option<i64>, Self::Error> {
Ok(self.users.lock().unwrap().get(&user_info.identity).copied())
}
async fn get_user_info(&self, user_id: i64) -> Result<Option<UserInfo>, Self::Error> {
let users = self.users.lock().unwrap();
let user_info = users
.iter()
.find(|(_, id)| **id == user_id)
.map(|(identity, _)| UserInfo {
identity: identity.clone(),
name: "Mocked User".to_owned(),
});
Ok(user_info)
}
}
}