Skip to content

Commit 8073331

Browse files
committed
Make layer generic over Request body type
Nothing in the layer implementation actually depends on the Request's body type. So generalise over the body type, allowing the service implementation not longer be tied to axum specifically.
1 parent d87b7ef commit 8073331

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

jwt-authorizer/src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ where
222222
self
223223
}
224224

225-
/// Build axum layer
225+
/// Build layer
226226
#[deprecated(since = "0.10.0", note = "please use `IntoLayer::into_layer()` instead")]
227227
pub async fn layer(self) -> Result<AuthorizationLayer<C>, InitError> {
228228
let val = self.validation.unwrap_or_default();

jwt-authorizer/src/layer.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use axum::extract::Request;
21
use futures_core::ready;
32
use futures_util::future::{self, BoxFuture};
3+
use http::Request;
44
use jsonwebtoken::TokenData;
55
use pin_project::pin_project;
66
use serde::de::DeserializeOwned;
@@ -15,25 +15,26 @@ use crate::authorizer::Authorizer;
1515
use crate::AuthError;
1616

1717
/// Trait for authorizing requests.
18-
pub trait Authorize {
19-
type Future: Future<Output = Result<Request, AuthError>>;
18+
pub trait Authorize<B> {
19+
type Future: Future<Output = Result<Request<B>, AuthError>>;
2020

2121
/// Authorize the request.
2222
///
2323
/// If the future resolves to `Ok(request)` then the request is allowed through, otherwise not.
24-
fn authorize(&self, request: Request) -> Self::Future;
24+
fn authorize(&self, request: Request<B>) -> Self::Future;
2525
}
2626

27-
impl<S, C> Authorize for AuthorizationService<S, C>
27+
impl<S, B, C> Authorize<B> for AuthorizationService<S, C>
2828
where
29+
B: Send + 'static,
2930
C: Clone + DeserializeOwned + Send + Sync + 'static,
3031
{
31-
type Future = BoxFuture<'static, Result<Request, AuthError>>;
32+
type Future = BoxFuture<'static, Result<Request<B>, AuthError>>;
3233

3334
/// The authorizers are sequentially applied (check_auth) until one of them validates the token.
3435
/// If no authorizer validates the token the request is rejected.
3536
///
36-
fn authorize(&self, mut request: Request) -> Self::Future {
37+
fn authorize(&self, mut request: Request<B>) -> Self::Future {
3738
let tkns_auths: Vec<(String, Arc<Authorizer<C>>)> = self
3839
.auths
3940
.iter()
@@ -154,21 +155,22 @@ where
154155
}
155156
}
156157

157-
impl<S, C> Service<Request> for AuthorizationService<S, C>
158+
impl<S, C, B> Service<Request<B>> for AuthorizationService<S, C>
158159
where
159-
S: Service<Request> + Clone,
160+
B: Send + 'static,
161+
S: Service<Request<B>> + Clone,
160162
S::Response: From<AuthError>,
161163
C: Clone + DeserializeOwned + Send + Sync + 'static,
162164
{
163165
type Response = S::Response;
164166
type Error = S::Error;
165-
type Future = ResponseFuture<S, C>;
167+
type Future = ResponseFuture<S, C, B>;
166168

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

171-
fn call(&mut self, req: Request) -> Self::Future {
173+
fn call(&mut self, req: Request<B>) -> Self::Future {
172174
let inner = self.inner.clone();
173175
// take the service that was ready
174176
let inner = std::mem::replace(&mut self.inner, inner);
@@ -184,13 +186,14 @@ where
184186

185187
#[pin_project]
186188
/// Response future for [`AuthorizationService`].
187-
pub struct ResponseFuture<S, C>
189+
pub struct ResponseFuture<S, C, B>
188190
where
189-
S: Service<Request>,
191+
B: Send + 'static,
192+
S: Service<Request<B>>,
190193
C: Clone + DeserializeOwned + Send + Sync + 'static,
191194
{
192195
#[pin]
193-
state: State<<AuthorizationService<S, C> as Authorize>::Future, S::Future>,
196+
state: State<<AuthorizationService<S, C> as Authorize<B>>::Future, S::Future>,
194197
service: S,
195198
}
196199

@@ -206,9 +209,10 @@ enum State<A, SFut> {
206209
},
207210
}
208211

209-
impl<S, C> Future for ResponseFuture<S, C>
212+
impl<S, C, B> Future for ResponseFuture<S, C, B>
210213
where
211-
S: Service<Request>,
214+
B: Send,
215+
S: Service<Request<B>>,
212216
S::Response: From<AuthError>,
213217
C: Clone + DeserializeOwned + Send + Sync,
214218
{

0 commit comments

Comments
 (0)