diff --git a/jwt-authorizer/src/authorizer.rs b/jwt-authorizer/src/authorizer.rs index abb0d92..b3182b7 100644 --- a/jwt-authorizer/src/authorizer.rs +++ b/jwt-authorizer/src/authorizer.rs @@ -9,7 +9,7 @@ use serde::de::DeserializeOwned; use crate::{ error::{AuthError, InitError}, jwks::{key_store_manager::KeyStoreManager, KeyData, KeySource}, - layer::{self, AsyncAuthorizationLayer, JwtSource}, + layer::{self, AuthorizationLayer, JwtSource}, oidc, Refresh, RegisteredClaims, }; @@ -237,15 +237,15 @@ pub trait IntoLayer where C: Clone + DeserializeOwned + Send, { - fn into_layer(self) -> AsyncAuthorizationLayer; + fn into_layer(self) -> AuthorizationLayer; } impl IntoLayer for Vec> where C: Clone + DeserializeOwned + Send, { - fn into_layer(self) -> AsyncAuthorizationLayer { - AsyncAuthorizationLayer::new(self.into_iter().map(Arc::new).collect()) + fn into_layer(self) -> AuthorizationLayer { + AuthorizationLayer::new(self.into_iter().map(Arc::new).collect()) } } @@ -253,8 +253,8 @@ impl IntoLayer for Vec>> where C: Clone + DeserializeOwned + Send, { - fn into_layer(self) -> AsyncAuthorizationLayer { - AsyncAuthorizationLayer::new(self.into_iter().collect()) + fn into_layer(self) -> AuthorizationLayer { + AuthorizationLayer::new(self.into_iter().collect()) } } @@ -262,8 +262,8 @@ impl IntoLayer for [Authorizer; N] where C: Clone + DeserializeOwned + Send, { - fn into_layer(self) -> AsyncAuthorizationLayer { - AsyncAuthorizationLayer::new(self.into_iter().map(Arc::new).collect()) + fn into_layer(self) -> AuthorizationLayer { + AuthorizationLayer::new(self.into_iter().map(Arc::new).collect()) } } @@ -271,8 +271,8 @@ impl IntoLayer for [Arc>; N] where C: Clone + DeserializeOwned + Send, { - fn into_layer(self) -> AsyncAuthorizationLayer { - AsyncAuthorizationLayer::new(self.into_iter().collect()) + fn into_layer(self) -> AuthorizationLayer { + AuthorizationLayer::new(self.into_iter().collect()) } } @@ -280,8 +280,8 @@ impl IntoLayer for Authorizer where C: Clone + DeserializeOwned + Send, { - fn into_layer(self) -> AsyncAuthorizationLayer { - AsyncAuthorizationLayer::new(vec![Arc::new(self)]) + fn into_layer(self) -> AuthorizationLayer { + AuthorizationLayer::new(vec![Arc::new(self)]) } } @@ -289,8 +289,8 @@ impl IntoLayer for Arc> where C: Clone + DeserializeOwned + Send, { - fn into_layer(self) -> AsyncAuthorizationLayer { - AsyncAuthorizationLayer::new(vec![self]) + fn into_layer(self) -> AuthorizationLayer { + AuthorizationLayer::new(vec![self]) } } diff --git a/jwt-authorizer/src/layer.rs b/jwt-authorizer/src/layer.rs index a8b8fb5..1ea2864 100644 --- a/jwt-authorizer/src/layer.rs +++ b/jwt-authorizer/src/layer.rs @@ -183,12 +183,12 @@ where /// Build axum layer #[deprecated(since = "0.10.0", note = "please use `IntoLayer::into_layer()` instead")] - pub async fn layer(self) -> Result, InitError> { + pub async fn layer(self) -> Result, InitError> { let val = self.validation.unwrap_or_default(); let auth = Arc::new( Authorizer::build(self.key_source_type, self.claims_checker, self.refresh, val, self.jwt_source).await?, ); - Ok(AsyncAuthorizationLayer::new(vec![auth])) + Ok(AuthorizationLayer::new(vec![auth])) } pub async fn build(self) -> Result, InitError> { @@ -199,7 +199,7 @@ where } /// Trait for authorizing requests. -pub trait AsyncAuthorizer { +pub trait Authorize { type RequestBody; type Future: Future, AuthError>>; @@ -209,7 +209,7 @@ pub trait AsyncAuthorizer { fn authorize(&self, request: Request) -> Self::Future; } -impl AsyncAuthorizer for AsyncAuthorizationService +impl Authorize for AuthorizationService where B: Send + Sync + 'static, C: Clone + DeserializeOwned + Send + Sync + 'static, @@ -256,34 +256,34 @@ where // -------------- Layer ----------------- #[derive(Clone)] -pub struct AsyncAuthorizationLayer +pub struct AuthorizationLayer where C: Clone + DeserializeOwned + Send, { auths: Vec>>, } -impl AsyncAuthorizationLayer +impl AuthorizationLayer where C: Clone + DeserializeOwned + Send, { - pub fn new(auths: Vec>>) -> AsyncAuthorizationLayer { + pub fn new(auths: Vec>>) -> AuthorizationLayer { Self { auths } } } -impl Layer for AsyncAuthorizationLayer +impl Layer for AuthorizationLayer where C: Clone + DeserializeOwned + Send + Sync, { - type Service = AsyncAuthorizationService; + type Service = AuthorizationService; fn layer(&self, inner: S) -> Self::Service { - AsyncAuthorizationService::new(inner, self.auths.clone()) + AuthorizationService::new(inner, self.auths.clone()) } } -// ---------- AsyncAuthorizationService -------- +// ---------- AuthorizationService -------- /// Source of the bearer token #[derive(Clone)] @@ -301,7 +301,7 @@ pub enum JwtSource { } #[derive(Clone)] -pub struct AsyncAuthorizationService +pub struct AuthorizationService where C: Clone + DeserializeOwned + Send + Sync, { @@ -309,7 +309,7 @@ where pub auths: Vec>>, } -impl AsyncAuthorizationService +impl AuthorizationService where C: Clone + DeserializeOwned + Send + Sync, { @@ -328,19 +328,19 @@ where } } -impl AsyncAuthorizationService +impl AuthorizationService where C: Clone + DeserializeOwned + Send + Sync, { /// Authorize requests using a custom scheme. /// /// The `Authorization` header is required to have the value provided. - pub fn new(inner: S, auths: Vec>>) -> AsyncAuthorizationService { + pub fn new(inner: S, auths: Vec>>) -> AuthorizationService { Self { inner, auths } } } -impl Service> for AsyncAuthorizationService +impl Service> for AuthorizationService where ReqBody: Send + Sync + 'static, S: Service> + Clone, @@ -370,7 +370,7 @@ where } #[pin_project] -/// Response future for [`AsyncAuthorizationService`]. +/// Response future for [`AuthorizationService`]. pub struct ResponseFuture where S: Service>, @@ -378,7 +378,7 @@ where C: Clone + DeserializeOwned + Send + Sync + 'static, { #[pin] - state: State< as AsyncAuthorizer>::Future, S::Future>, + state: State< as Authorize>::Future, S::Future>, service: S, } @@ -433,7 +433,7 @@ where mod tests { use crate::{authorizer::Authorizer, IntoLayer, JwtAuthorizer, RegisteredClaims}; - use super::AsyncAuthorizationLayer; + use super::AuthorizationLayer; #[tokio::test] async fn auth_into_layer() { @@ -447,7 +447,7 @@ mod tests { let auth1 = JwtAuthorizer::from_secret("aaa").build().await.unwrap(); let auth2 = JwtAuthorizer::from_secret("bbb").build().await.unwrap(); - let layer: AsyncAuthorizationLayer = [auth1, auth2].into_layer(); + let layer: AuthorizationLayer = [auth1, auth2].into_layer(); assert_eq!(2, layer.auths.len()); } @@ -456,7 +456,7 @@ mod tests { let auth1 = JwtAuthorizer::from_secret("aaa").build().await.unwrap(); let auth2 = JwtAuthorizer::from_secret("bbb").build().await.unwrap(); - let layer: AsyncAuthorizationLayer = vec![auth1, auth2].into_layer(); + let layer: AuthorizationLayer = vec![auth1, auth2].into_layer(); assert_eq!(2, layer.auths.len()); } diff --git a/jwt-authorizer/tests/tests.rs b/jwt-authorizer/tests/tests.rs index 9ef8ea0..f2a89b6 100644 --- a/jwt-authorizer/tests/tests.rs +++ b/jwt-authorizer/tests/tests.rs @@ -14,7 +14,7 @@ mod tests { use http::{header, HeaderValue}; use jwt_authorizer::{ authorizer::Authorizer, - layer::{AsyncAuthorizationLayer, JwtSource}, + layer::{AuthorizationLayer, JwtSource}, validation::Validation, IntoLayer, JwtAuthorizer, JwtClaims, }; @@ -28,7 +28,7 @@ mod tests { sub: String, } - async fn app(layer: AsyncAuthorizationLayer) -> Router { + async fn app(layer: AuthorizationLayer) -> Router { Router::new().route("/public", get(|| async { "hello" })).route( "/protected", get(|JwtClaims(user): JwtClaims| async move { format!("hello: {}", user.sub) }).layer( @@ -48,7 +48,7 @@ mod tests { } async fn proteced_request_with_header_and_layer( - layer: AsyncAuthorizationLayer, + layer: AuthorizationLayer, header_name: &str, header_value: &str, ) -> Response { diff --git a/jwt-authorizer/tests/tonic.rs b/jwt-authorizer/tests/tonic.rs index da499a8..733eefc 100644 --- a/jwt-authorizer/tests/tonic.rs +++ b/jwt-authorizer/tests/tonic.rs @@ -3,7 +3,7 @@ use std::{sync::Once, task::Poll}; use axum::body::HttpBody; use futures_core::future::BoxFuture; use http::header::AUTHORIZATION; -use jwt_authorizer::{layer::AsyncAuthorizationService, IntoLayer, JwtAuthorizer}; +use jwt_authorizer::{layer::AuthorizationService, IntoLayer, JwtAuthorizer}; use serde::{Deserialize, Serialize}; use tonic::{server::UnaryService, transport::NamedService, IntoRequest, Status}; use tower::{buffer::Buffer, Service}; @@ -82,7 +82,7 @@ impl NamedService for GreeterServer { async fn app( jwt_auth: JwtAuthorizer, expected_sub: String, -) -> AsyncAuthorizationService>, User> { +) -> AuthorizationService>, User> { let layer = jwt_auth.build().await.unwrap().into_layer(); tonic::transport::Server::builder() .layer(layer) @@ -144,7 +144,7 @@ where } async fn make_protected_request( - app: AsyncAuthorizationService, + app: AuthorizationService, bearer: Option<&str>, message: &str, ) -> Result, Status>