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
//! Provides a configurable actor with the functionality of a code grant frontend.
use endpoint::{AuthorizationFlow, AccessTokenFlow, ResourceFlow};
use endpoint::{Endpoint, WebRequest};
use primitives::grant::Grant;

use super::actix::{Actor, Context, Handler};
use super::message::{AccessToken, AuthorizationCode, Resource};
use super::{AsActor, ResourceProtection};

// /// A tag type to signal that no handler for this request type has been configured on the endpoint.
// pub struct NoHandler;

impl<P: 'static> Actor for AsActor<P> {
    type Context = Context<Self>;
}

impl<W, P, E> Handler<AuthorizationCode<W>> for AsActor<P> 
where 
    W: WebRequest<Error=E> + Send + Sync + 'static,
    P: Endpoint<W, Error=E> + 'static,
    W::Response: Send + Sync + 'static,
    E: Send + Sync + 'static,
{
    type Result = Result<W::Response, W::Error>;

    fn handle(&mut self, msg: AuthorizationCode<W>, _: &mut Self::Context) -> Self::Result {
        AuthorizationFlow::prepare(&mut self.0)?.execute(msg.0)
    }
}

impl<W, P, E> Handler<AccessToken<W>> for AsActor<P> 
where 
    W: WebRequest<Error=E> + Send + Sync + 'static,
    P: Endpoint<W, Error=E> + 'static,
    W::Response: Send + Sync + 'static,
    E: Send + Sync + 'static,
{
    type Result = Result<W::Response, W::Error>;

    fn handle(&mut self, msg: AccessToken<W>, _: &mut Self::Context) -> Self::Result {
        AccessTokenFlow::prepare(&mut self.0)?.execute(msg.0)
    }
}

impl<W, P, E> Handler<Resource<W>> for AsActor<P> 
where 
    W: WebRequest<Error=E> + Send + Sync + 'static,
    P: Endpoint<W, Error=E> + 'static,
    W::Response: Send + Sync + 'static,
    E: Send + Sync + 'static,
{
    type Result = Result<Grant, ResourceProtection<W::Response>>;

    fn handle(&mut self, msg: Resource<W>, _: &mut Self::Context) -> Self::Result {
        let result = ResourceFlow::prepare(&mut self.0)
            .map_err(ResourceProtection::Error)?
            .execute(msg.0);

        match result {
            Ok(grant) => Ok(grant),
            Err(Ok(response)) => Err(ResourceProtection::Respond(response)),
            Err(Err(error)) => Err(ResourceProtection::Error(error)),
        }
    }
}