tower_oauth2_resource_server/
layer.rs

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
use futures_util::{future::BoxFuture, Future};
use http::{Request, Response};
use pin_project::pin_project;
use serde::de::DeserializeOwned;

use std::{
    pin::Pin,
    task::{ready, Context, Poll},
};
use tower::{Layer, Service};

use crate::{error::AuthError, server::OAuth2ResourceServer};

trait Authorize<B> {
    type Future: Future<Output = Result<Request<B>, AuthError>>;

    fn authorize(&mut self, request: Request<B>) -> Self::Future;
}

impl<S, ReqBody, Claims> Authorize<ReqBody> for OAuth2ResourceServerService<S, Claims>
where
    Claims: DeserializeOwned + Clone + Send + Sync + 'static,
    ReqBody: Send + 'static,
{
    type Future = BoxFuture<'static, Result<Request<ReqBody>, AuthError>>;

    fn authorize(&mut self, request: Request<ReqBody>) -> Self::Future {
        let auth = self.auth_manager.clone();
        Box::pin(async move { auth.authorize_request(request).await })
    }
}

#[derive(Clone, Debug)]
pub struct OAuth2ResourceServerLayer<Claims>
where
    Claims: DeserializeOwned,
{
    auth_manager: OAuth2ResourceServer<Claims>,
}

impl<S, Claims> Layer<S> for OAuth2ResourceServerLayer<Claims>
where
    Claims: Clone + DeserializeOwned + Send + 'static,
{
    type Service = OAuth2ResourceServerService<S, Claims>;

    fn layer(&self, inner: S) -> Self::Service {
        OAuth2ResourceServerService::new(inner, self.auth_manager.clone())
    }
}

impl<Claims> OAuth2ResourceServerLayer<Claims>
where
    Claims: DeserializeOwned,
{
    pub(crate) fn new(auth_manager: OAuth2ResourceServer<Claims>) -> Self
    where
        Claims: DeserializeOwned,
    {
        OAuth2ResourceServerLayer { auth_manager }
    }
}

#[derive(Clone, Debug)]
pub struct OAuth2ResourceServerService<S, Claims>
where
    Claims: Clone + DeserializeOwned + Send + 'static,
{
    inner: S,
    auth_manager: OAuth2ResourceServer<Claims>,
}

impl<S, Claims> OAuth2ResourceServerService<S, Claims>
where
    Claims: Clone + DeserializeOwned + Send + 'static,
{
    fn new(inner: S, auth_manager: OAuth2ResourceServer<Claims>) -> Self {
        Self {
            inner,
            auth_manager,
        }
    }
}

impl<S, ReqBody, ResBody, Claims> Service<Request<ReqBody>>
    for OAuth2ResourceServerService<S, Claims>
where
    S: Service<Request<ReqBody>, Response = Response<ResBody>> + Clone,
    ResBody: Default,
    Claims: Clone + DeserializeOwned + Send + Sync + 'static,
    ReqBody: Send + 'static,
{
    type Response = S::Response;
    type Error = S::Error;
    type Future = ResponseFuture<S, ReqBody, Claims>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, request: Request<ReqBody>) -> Self::Future {
        let inner = self.inner.clone();
        let authorize = self.authorize(request);

        ResponseFuture {
            state: State::Authorize { authorize },
            service: inner,
        }
    }
}

#[pin_project]
pub struct ResponseFuture<S, ReqBody, Claims>
where
    S: Service<Request<ReqBody>>,
    ReqBody: Send + 'static,
    Claims: Clone + DeserializeOwned + Send + Sync + 'static,
{
    #[pin]
    state: State<<OAuth2ResourceServerService<S, Claims> as Authorize<ReqBody>>::Future, S::Future>,
    service: S,
}

#[pin_project(project = StateProj)]
enum State<A, SFut> {
    Authorize {
        #[pin]
        authorize: A,
    },
    Authorized {
        #[pin]
        fut: SFut,
    },
}

impl<S, ReqBody, B, Claims> Future for ResponseFuture<S, ReqBody, Claims>
where
    S: Service<Request<ReqBody>, Response = Response<B>>,
    B: Default,
    ReqBody: Send + 'static,
    Claims: Clone + DeserializeOwned + Send + Sync + 'static,
{
    type Output = Result<Response<B>, S::Error>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let mut this = self.project();

        loop {
            match this.state.as_mut().project() {
                StateProj::Authorize { authorize } => {
                    let auth = ready!(authorize.poll(cx));
                    match auth {
                        Ok(req) => {
                            let fut = this.service.call(req);
                            this.state.set(State::Authorized { fut })
                        }
                        Err(res) => {
                            let response = Response::<B>::from(res);
                            return Poll::Ready(Ok(response));
                        }
                    };
                }
                StateProj::Authorized { fut } => {
                    return fut.poll(cx);
                }
            }
        }
    }
}