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
use primitives::issuer::{Issuer, IssuedToken, RefreshedToken};
use primitives::grant::Grant;

use super::super::actix::{Handler, Message};
use super::super::AsActor;

/// Command issuing a bearer token.
pub struct Issue {
    /// The grant defining client, scope and timeframe of the token.
    pub grant: Grant,
}

impl Message for Issue {
    type Result = Result<IssuedToken, ()>;
}

/// Issue a renewed bearer token.
pub struct Refresh {
    /// The refresh token with which to issue a refreshed bearer token.
    pub token: String,

    /// The grant defining client, scope and timeframe of the token.
    pub grant: Grant,
}

impl Message for Refresh {
    type Result = Result<RefreshedToken, ()>;
}

/// Try to find the grant of a specific token.
pub struct RecoverToken {
    /// Previously returned as `token` in an `IssuedToken`.
    pub token: String,
}

impl Message for RecoverToken {
    /// An `Ok` represents a successful search (which may find nothing).
    type Result = Result<Option<Grant>, ()>;
}

/// Try to find the refresh grant of a specific token.
pub struct RecoverRefresh {
    /// Previously returned as `refresh` in an `IssuedToken`.
    pub token: String,
}

impl Message for RecoverRefresh {
    /// An `Ok` represents a successful search (which may find nothing).
    type Result = Result<Option<Grant>, ()>;
}

impl<I: Issuer + 'static> Handler<Issue> for AsActor<I> {
    type Result = Result<IssuedToken, ()>;

    fn handle(&mut self, msg: Issue, _: &mut Self::Context) -> Self::Result {
        self.0.issue(msg.grant)
    }
}

impl<I: Issuer + 'static> Handler<Refresh> for AsActor<I> {
    type Result = Result<RefreshedToken, ()>;

    fn handle(&mut self, msg: Refresh, _: &mut Self::Context) -> Self::Result {
        self.0.refresh(&msg.token, msg.grant)
    }
}


impl<I: Issuer + 'static> Handler<RecoverToken> for AsActor<I> {
    type Result = Result<Option<Grant>, ()>; 

    fn handle(&mut self, msg: RecoverToken, _: &mut Self::Context) -> Self::Result {
        self.0.recover_token(&msg.token)
    }
}

impl<I: Issuer + 'static> Handler<RecoverRefresh> for AsActor<I> {
    type Result = Result<Option<Grant>, ()>; 

    fn handle(&mut self, msg: RecoverRefresh, _: &mut Self::Context) -> Self::Result {
        self.0.recover_refresh(&msg.token)
    }
}