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
use std::ops::Deref;

use crate::principal::Principal;

use super::Usecase;

pub struct Authorized<T> {
    principal: T,
}

impl Authorized<Principal> {
    fn new(principal: Principal) -> Self {
        Self { principal }
    }
}

impl<T> Deref for Authorized<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.principal
    }
}

#[derive(Debug)]
pub struct Unauthorized;

pub struct Authorizer {}

impl Authorizer {
    pub fn new() -> Self {
        Self {}
    }

    pub async fn authorize<U: Usecase>(
        &self,
        principal: Principal,
        usecase: &U,
        input: &U::Input,
    ) -> Result<Authorized<Principal>, Unauthorized> {
        usecase
            .authorize(principal, input)
            .await
            .map(Authorized::new)
    }
}