mirror of
https://github.com/TECHNOFAB11/jwt-authorizer.git
synced 2025-12-11 23:50:07 +01:00
unit tests pass, removed generic to expand upon, integration tests freeze
This commit is contained in:
parent
e1e5874347
commit
7970a6c358
3 changed files with 47 additions and 16 deletions
|
|
@ -48,7 +48,7 @@ pub enum KeySourceType {
|
||||||
|
|
||||||
impl<C> Authorizer<C>
|
impl<C> Authorizer<C>
|
||||||
where
|
where
|
||||||
C: DeserializeOwned + Clone + Send + Sync,
|
C: DeserializeOwned + Clone + Send,
|
||||||
{
|
{
|
||||||
pub(crate) async fn build(
|
pub(crate) async fn build(
|
||||||
key_source_type: KeySourceType,
|
key_source_type: KeySourceType,
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use axum::body::Body;
|
||||||
use axum::http::Request;
|
use axum::http::Request;
|
||||||
use futures_core::ready;
|
use futures_core::ready;
|
||||||
use futures_util::future::{self, BoxFuture};
|
use futures_util::future::{self, BoxFuture};
|
||||||
|
|
@ -8,6 +9,7 @@ use std::future::Future;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
use tokio::sync::Mutex;
|
||||||
use tower_layer::Layer;
|
use tower_layer::Layer;
|
||||||
use tower_service::Service;
|
use tower_service::Service;
|
||||||
|
|
||||||
|
|
@ -27,8 +29,8 @@ pub trait Authorize<B> {
|
||||||
|
|
||||||
impl<B, S, C> Authorize<B> for AuthorizationService<S, C>
|
impl<B, S, C> Authorize<B> for AuthorizationService<S, C>
|
||||||
where
|
where
|
||||||
B: Send + Sync + 'static,
|
B: Send + 'static,
|
||||||
C: Clone + DeserializeOwned + Send + Sync + 'static,
|
C: Clone + DeserializeOwned + Send + 'static,
|
||||||
{
|
{
|
||||||
type RequestBody = B;
|
type RequestBody = B;
|
||||||
type Future = BoxFuture<'static, Result<Request<B>, AuthError>>;
|
type Future = BoxFuture<'static, Result<Request<B>, AuthError>>;
|
||||||
|
|
@ -59,7 +61,9 @@ where
|
||||||
Ok(tdata) => {
|
Ok(tdata) => {
|
||||||
// Set `token_data` as a request extension so it can be accessed by other
|
// Set `token_data` as a request extension so it can be accessed by other
|
||||||
// services down the stack.
|
// services down the stack.
|
||||||
request.extensions_mut().insert(tdata);
|
|
||||||
|
let something = Arc::new(Mutex::new(tdata));
|
||||||
|
request.extensions_mut().insert(something);
|
||||||
|
|
||||||
Ok(request)
|
Ok(request)
|
||||||
}
|
}
|
||||||
|
|
@ -119,7 +123,7 @@ pub enum JwtSource {
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AuthorizationService<S, C>
|
pub struct AuthorizationService<S, C>
|
||||||
where
|
where
|
||||||
C: Clone + DeserializeOwned + Send + Sync,
|
C: Clone + DeserializeOwned + Send,
|
||||||
{
|
{
|
||||||
pub inner: S,
|
pub inner: S,
|
||||||
pub auths: Vec<Arc<Authorizer<C>>>,
|
pub auths: Vec<Arc<Authorizer<C>>>,
|
||||||
|
|
@ -127,7 +131,7 @@ where
|
||||||
|
|
||||||
impl<S, C> AuthorizationService<S, C>
|
impl<S, C> AuthorizationService<S, C>
|
||||||
where
|
where
|
||||||
C: Clone + DeserializeOwned + Send + Sync,
|
C: Clone + DeserializeOwned + Send,
|
||||||
{
|
{
|
||||||
pub fn get_ref(&self) -> &S {
|
pub fn get_ref(&self) -> &S {
|
||||||
&self.inner
|
&self.inner
|
||||||
|
|
@ -156,6 +160,34 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<S, C> Service<Request<Body>> for AuthorizationService<S, C>
|
||||||
|
where
|
||||||
|
S: Service<Request<Body>> + Clone,
|
||||||
|
S::Response: From<AuthError>,
|
||||||
|
C: Clone + DeserializeOwned + Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
type Response = S::Response;
|
||||||
|
type Error = S::Error;
|
||||||
|
type Future = ResponseFuture<S, C>;
|
||||||
|
|
||||||
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
|
self.inner.poll_ready(cx)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, req: Request<Body>) -> Self::Future {
|
||||||
|
let inner = self.inner.clone();
|
||||||
|
// take the service that was ready
|
||||||
|
let inner = std::mem::replace(&mut self.inner, inner);
|
||||||
|
|
||||||
|
let auth_fut = self.authorize(req);
|
||||||
|
|
||||||
|
ResponseFuture {
|
||||||
|
state: State::Authorize { auth_fut },
|
||||||
|
service: inner,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
impl<ReqBody, S, C> Service<Request<ReqBody>> for AuthorizationService<S, C>
|
impl<ReqBody, S, C> Service<Request<ReqBody>> for AuthorizationService<S, C>
|
||||||
where
|
where
|
||||||
ReqBody: Send + Sync + 'static,
|
ReqBody: Send + Sync + 'static,
|
||||||
|
|
@ -184,17 +216,17 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
#[pin_project]
|
#[pin_project]
|
||||||
/// Response future for [`AuthorizationService`].
|
/// Response future for [`AuthorizationService`].
|
||||||
pub struct ResponseFuture<S, ReqBody, C>
|
pub struct ResponseFuture<S, C>
|
||||||
where
|
where
|
||||||
S: Service<Request<ReqBody>>,
|
S: Service<Request<Body>>,
|
||||||
ReqBody: Send + Sync + 'static,
|
|
||||||
C: Clone + DeserializeOwned + Send + Sync + 'static,
|
C: Clone + DeserializeOwned + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
#[pin]
|
#[pin]
|
||||||
state: State<<AuthorizationService<S, C> as Authorize<ReqBody>>::Future, S::Future>,
|
state: State<<AuthorizationService<S, C> as Authorize<Body>>::Future, S::Future>,
|
||||||
service: S,
|
service: S,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -210,11 +242,10 @@ enum State<A, SFut> {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, ReqBody, C> Future for ResponseFuture<S, ReqBody, C>
|
impl<S, C> Future for ResponseFuture<S, C>
|
||||||
where
|
where
|
||||||
S: Service<Request<ReqBody>>,
|
S: Service<Request<Body>>,
|
||||||
S::Response: From<AuthError>,
|
S::Response: From<AuthError>,
|
||||||
ReqBody: Send + Sync + 'static,
|
|
||||||
C: Clone + DeserializeOwned + Send + Sync,
|
C: Clone + DeserializeOwned + Send + Sync,
|
||||||
{
|
{
|
||||||
type Output = Result<S::Response, S::Error>;
|
type Output = Result<S::Response, S::Error>;
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,9 @@ use std::{
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use axum::body::Body;
|
||||||
use axum::{response::Response, routing::get, Json, Router};
|
use axum::{response::Response, routing::get, Json, Router};
|
||||||
use http::{header::AUTHORIZATION, Request, StatusCode};
|
use http::{header::AUTHORIZATION, Request, StatusCode};
|
||||||
use hyper::Body;
|
|
||||||
use jwt_authorizer::{IntoLayer, JwtAuthorizer, JwtClaims, Refresh, RefreshStrategy, Validation};
|
use jwt_authorizer::{IntoLayer, JwtAuthorizer, JwtClaims, Refresh, RefreshStrategy, Validation};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
@ -127,7 +127,7 @@ fn init_test() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn make_proteced_request(app: &mut Router, bearer: &str) -> Response {
|
async fn make_proteced_request(app: &mut Router, bearer: &str) -> Response {
|
||||||
app.ready()
|
<Router as tower::ServiceExt<Request<Body>>>::ready(app)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.call(
|
.call(
|
||||||
|
|
@ -142,7 +142,7 @@ async fn make_proteced_request(app: &mut Router, bearer: &str) -> Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn make_public_request(app: &mut Router) -> Response {
|
async fn make_public_request(app: &mut Router) -> Response {
|
||||||
app.ready()
|
<Router as tower::ServiceExt<Request<Body>>>::ready(app)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.call(Request::builder().uri("/public").body(Body::empty()).unwrap())
|
.call(Request::builder().uri("/public").body(Body::empty()).unwrap())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue