rustium/authentication/
authorization.rs

1use std::any::type_name;
2
3// Actions
4pub trait Action {
5    fn to_str() -> &'static str;
6}
7pub struct Read;
8impl Action for Read {
9    fn to_str() -> &'static str {
10        "read"
11    }
12}
13pub struct Write;
14impl Action for Write {
15    fn to_str() -> &'static str {
16        "write"
17    }
18}
19
20// Scopes
21pub trait Scope {
22    fn to_str() -> &'static str;
23}
24
25impl<R> Scope for R {
26    fn to_str() -> &'static str {
27        type_name::<R>()
28    }
29}
30
31// pub struct Authorization<A, S>(pub ApiToken, PhantomData<(A, S)>);
32
33// impl<'a, 'r, A, S> FromRequest<'a, 'r> for Authorization<A, S>
34// where
35//     A: Action,
36//     S: Scope,
37// {
38//     type Error = ();
39
40//     fn from_request(request: &'a Request<'r>) -> request::Outcome<Authorization<A, S>, ()> {
41//         request
42//             .guard::<ApiToken>()
43//             .map_failure(|_| (Status::Unauthorized, ()))
44//             .and_then(|token| {
45//                 if token.can(A::to_str(), S::to_str()) {
46//                     Outcome::Success(Authorization(token, PhantomData))
47//                 } else {
48//                     Outcome::Failure((Status::Unauthorized, ()))
49//                 }
50//             })
51//     }
52// }