minos 0.5.0

Authorization library
Documentation
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use crate::errors::{ErrorKind, MinosError};
use crate::group::GroupId;
use crate::resources::{Owner, ResourceType};
use crate::user::UserAttributes;
use crate::Status;
use chrono::{Duration, NaiveDateTime, Utc};

#[derive(Debug, PartialEq, Clone, PartialOrd)]
/// Users permissions, defines what a user is allowed to do.
pub enum Permission {
    /// The user can create the source
    Create,
    /// The user can read the source
    Read,
    /// The user can edit the source, but can't delete the source
    Update,
    /// The user can delete the source
    Delete,

    /// The user can perform a specific action
    #[cfg(feature = "custom_permission")]
    Custom(String),
}

#[cfg(not(feature = "custom_permission"))]
impl From<u8> for Permission {
    fn from(n: u8) -> Self {
        match n {
            3 => Permission::Delete,
            2 => Permission::Update,
            1 => Permission::Create,
            _ => Permission::Read,
        }
    }
}

impl ToString for Permission {
    fn to_string(&self) -> String {
        format!("{:?}", self).to_lowercase()
    }
}

#[cfg(not(feature = "custom_permission"))]
impl From<&str> for Permission {
    fn from(str: &str) -> Self {
        if str == Permission::Create.to_string() {
            Permission::Create
        } else if str == Permission::Update.to_string() {
            Permission::Update
        } else if str == Permission::Delete.to_string() {
            Permission::Delete
        } else {
            Permission::Read
        }
    }
}

#[cfg(feature = "custom_permission")]
impl From<&str> for Permission {
    fn from(str: &str) -> Self {
        if str == Permission::Create.to_string() {
            Permission::Create
        } else if str == Permission::Update.to_string() {
            Permission::Update
        } else if str == Permission::Delete.to_string() {
            Permission::Delete
        } else if str == Permission::Read.to_string() {
            Permission::Read
        } else {
            Permission::Custom(str.to_string())
        }
    }
}

impl Permission {
    #[cfg(not(feature = "custom_permission"))]
    pub fn as_u8(&self) -> u8 {
        self.clone() as u8
    }

    /// Return simple explanation for permission required
    ///
    ///# Examples
    ///```
    ///     use minos::errors::{ErrorKind, MinosError};
    ///     use minos::authorization::Permission;
    ///
    ///     fn check_permission(permission: Permission) -> Result<(), MinosError> {
    ///         if permission != Permission::Update {
    ///             return Err(MinosError::new(
    ///                     ErrorKind::Authorization,
    ///                     &Permission::Update.required_msg(),
    ///             ));
    ///         }
    ///
    ///         Ok(())
    ///     }
    /// ```
    ///```
    ///     use minos::authorization::Permission;
    ///     assert_eq!(Permission::Update.required_msg(), "Update permission is required.");
    /// ```
    pub fn required_msg(&self) -> String {
        format!("{:?} permission is required.", self)
    }

    /// Returns a vector with Create, Read, Update, and Delete permissions
    /// # Example
    /// ```
    ///     use minos::authorization::Permission;
    ///     use minos::authorization::Permission::{Create, Read, Update, Delete};
    ///
    ///     assert_eq!(vec![Create, Read, Update, Delete], Permission::crud())
    /// ```
    pub fn crud() -> Vec<Permission> {
        vec![
            Permission::Create,
            Permission::Read,
            Permission::Update,
            Permission::Delete,
        ]
    }

    /// Like crud, but within Create
    pub fn rud() -> Vec<Permission> {
        vec![Permission::Read, Permission::Update, Permission::Delete]
    }
}

/// Users authorizations
#[derive(Debug, PartialEq, Clone, PartialOrd)]
pub struct Authorization {
    pub(crate) permissions: Vec<Permission>,
    pub(crate) user_id: String,
    pub(crate) resource_id: String,
    pub(crate) resource_type: String,
    pub(crate) expiration: NaiveDateTime,
}

impl Authorization {
    pub fn permissions(&self) -> &Vec<Permission> {
        &self.permissions
    }

    pub fn user_id(&self) -> &str {
        &self.user_id
    }

    pub fn resource_id(&self) -> &str {
        &self.resource_id
    }

    pub fn resource_type(&self) -> &str {
        &self.resource_type
    }
    pub fn expiration(&self) -> NaiveDateTime {
        self.expiration
    }

    fn basic_check(&self, resource_id: &str, user: &UserAttributes) -> Result<(), MinosError> {
        if &self.resource_id != resource_id {
            return Err(MinosError::new(
                ErrorKind::Authorization,
                "Authorization created for another resource",
            ));
        }

        if self.expiration <= Utc::now().naive_utc() {
            return Err(MinosError::new(
                ErrorKind::Authorization,
                "The Authorization is expired",
            ));
        }

        if &user.id != &self.user_id {
            return Err(MinosError::new(
                ErrorKind::Authorization,
                &format!("This Authorization is not for the user {}", &user.id),
            ));
        }

        Ok(())
    }

    pub fn check(
        &self,
        resource_id: &str,
        user: &UserAttributes,
        required_permission: &Permission,
    ) -> Result<(), MinosError> {
        let _ = self.basic_check(resource_id, user)?;

        if !&self.permissions.contains(&required_permission) {
            return Err(MinosError::new(
                ErrorKind::Authorization,
                &required_permission.required_msg(),
            ));
        }

        Ok(())
    }

    pub fn multi_permissions_check(
        &self,
        resource_id: &str,
        user: &UserAttributes,
        required_permissions: &Vec<Permission>,
    ) -> Result<(), MinosError> {
        let _ = self.basic_check(resource_id, user)?;

        for permission in required_permissions {
            if !&self.permissions.contains(permission) {
                return Err(MinosError::new(
                    ErrorKind::Authorization,
                    &permission.required_msg(),
                ));
            }
        }

        Ok(())
    }
}

/// Defines the access and modification rules for a resource. It has two types of
/// authorization policies: by owner and by roles; the use of the first excludes
/// the other and vice versa.
///
/// Care must be taken to use the authorization policies correctly, because when building the
/// Authorization with the AuthorizationBuilder, it will return an error.
///
#[derive(PartialEq, Debug, Clone, PartialOrd, Default)]
pub struct Policy {
    /// authorization duration, in seconds (recommended max duration: 65535 ~ 1092 min ~ 18 hours)
    pub(crate) duration: u16,

    /// Use only for objects with real owner. If you want set only Permission::Create,
    /// use other authorization policy.
    pub(crate) by_owner: bool,

    /// Restricts the authorization to only users with specific groups
    pub(crate) groups_ids: Option<Vec<GroupId>>,

    /// permissions granted
    pub(crate) permissions: Vec<Permission>,
}

impl Policy {
    pub fn new(
        duration: u16,
        by_owner: bool,
        groups_ids: Option<Vec<GroupId>>,
        permissions: Vec<Permission>,
    ) -> Self {
        Self {
            duration,
            by_owner,
            groups_ids,
            permissions,
        }
    }
    pub fn duration(&self) -> u16 {
        self.duration
    }
    pub fn by_owner(&self) -> bool {
        self.by_owner
    }
    pub fn groups_ids(&self) -> &Option<Vec<GroupId>> {
        &self.groups_ids
    }
    pub fn permissions(&self) -> &Vec<Permission> {
        &self.permissions
    }
}

pub struct AuthorizationBuilder<'b> {
    resource_type: &'b ResourceType,
}

impl<'b> AuthorizationBuilder<'b> {
    pub fn new(resource_type: &'b ResourceType) -> Self {
        Self { resource_type }
    }

    fn check_groups(&self, user: &UserAttributes, policy: &Policy) -> Result<(), MinosError> {
        if let Some(possible_ids) = &policy.groups_ids {
            for id in possible_ids {
                if user.groups.contains(&id) {
                    return Ok(());
                }
            }

            return Err(MinosError::new(
                ErrorKind::Authorization,
                "The user is not in the correct group",
            ));
        }

        Ok(())
    }

    fn same_group_check(group_id: GroupId, user: &UserAttributes) -> Result<(), MinosError> {
        if !&user.groups.contains(&group_id) {
            return Err(MinosError::new(
                ErrorKind::Authorization,
                "The user is not in the owning group",
            ));
        }

        Ok(())
    }

    fn same_user_check(user_id: &str, user: &UserAttributes) -> Result<(), MinosError> {
        if user_id != &user.id {
            return Err(MinosError::new(
                ErrorKind::Authorization,
                "The user is not the owner",
            ));
        }
        Ok(())
    }

    fn by_owner_check(&self, user: &UserAttributes) -> Result<(), MinosError> {
        match &self.resource_type.owner {
            None => {
                return Err(MinosError::new(
                    ErrorKind::IncompatibleAuthPolicy,
                    "The resource haven't an owner",
                ));
            }
            Some(owner) => match owner {
                Owner::User(id) => Self::same_user_check(id, &user)?,
                Owner::Group(id) => Self::same_group_check(GroupId::from(id.as_str()), &user)?,
            },
        }

        Ok(())
    }

    /// Create an Authorization based in Policy, resource id, and User. This function unlike,
    /// [`build`], checks if the policy is malformed.
    ///
    /// # Errors
    /// This function will return an error in three cases:
    /// * [`InactiveUser`]: The user is not active.
    /// * [`IncompatibleAuthPolicy`]: The policy not corresponds to resource type or the attribute
    ///   `by_owner` is true, but the resource not have an owner.
    /// * [`Authorization`]: The user not have any permissions available.
    ///
    /// [`build`]: AuthorizationBuilder::build
    /// [`InactiveUser`]: ErrorKind::InactiveUser
    /// [`IncompatibleAuthPolicy`]: ErrorKind::IncompatibleAuthPolicy
    /// [`Authorization`]: ErrorKind::Authorization
    pub fn build_by_policy(
        &self,
        policy: &Policy,
        resource_id: &str,
        user: &UserAttributes,
    ) -> Result<Authorization, MinosError> {
        if user.status != Status::Active {
            return Err(MinosError::new(
                ErrorKind::InactiveUser,
                "The user is not active",
            ));
        }

        if !&self.resource_type.policies.contains(&policy) {
            return Err(MinosError::new(
                ErrorKind::IncompatibleAuthPolicy,
                "The policy not corresponds to resource type",
            ));
        }

        if policy.by_owner {
            let _ = self.by_owner_check(&user)?;
        } else {
            let _ = self.check_groups(&user, &policy)?;
        }

        Ok(Authorization {
            permissions: policy.permissions.clone(),
            user_id: user.id.clone(),
            resource_id: resource_id.to_string(),
            resource_type: self.resource_type.label.clone(),
            expiration: Utc::now().naive_utc() + Duration::seconds(policy.duration.clone() as i64),
        })
    }

    /// Create an Authorization based in resource id and User. Check all policies and assign all
    ///  permissions available to the user, but assign the shortest duration found.
    ///
    /// # Errors
    /// This function will return an error only in two cases:
    /// * [`InactiveUser`]: The user is not active.
    /// * [`Authorization`]: The user not have any permissions available.
    ///
    /// [`InactiveUser`]: ErrorKind::InactiveUser
    /// [`Authorization`]: ErrorKind::Authorization
    pub fn build(
        &self,
        resource_id: &str,
        user: &UserAttributes,
    ) -> Result<Authorization, MinosError> {
        if user.status != Status::Active {
            return Err(MinosError::new(
                ErrorKind::Authorization,
                "The user is not active",
            ));
        }

        let mut permissions = vec![];
        let mut durations = vec![];
        for policy in &self.resource_type.policies {
            match self.build_by_policy(&policy, &resource_id, &user) {
                Ok(mut auth) => {
                    permissions.append(&mut auth.permissions);
                    durations.push(&policy.duration);
                }
                Err(_) => continue,
            }
        }

        durations.sort();
        let seconds = **durations
            .get(0)
            .ok_or(MinosError::new(ErrorKind::Authorization, "Not authorized"))?;

        Ok(Authorization {
            permissions,
            user_id: user.id.clone(),
            resource_id: resource_id.to_string(),
            resource_type: self.resource_type.label.clone(),
            expiration: Utc::now().naive_utc() + Duration::seconds(seconds as i64),
        })
    }
}