diff --git a/demo-server/src/main.rs b/demo-server/src/main.rs index 91f7879..a460dad 100644 --- a/demo-server/src/main.rs +++ b/demo-server/src/main.rs @@ -1,7 +1,5 @@ use axum::{routing::get, Router}; -use jwt_authorizer::{ - error::InitError, AuthError, JwtAuthorizer, JwtClaims, Refresh, RefreshStrategy, ToAuthorizationLayer, -}; +use jwt_authorizer::{error::InitError, AuthError, IntoLayer, JwtAuthorizer, JwtClaims, Refresh, RefreshStrategy}; use serde::Deserialize; use std::net::SocketAddr; use tower_http::trace::TraceLayer; @@ -51,7 +49,7 @@ async fn main() -> Result<(), InitError> { let api = Router::new() .route("/protected", get(protected)) // adding the authorizer layer - .layer(jwt_auth.to_layer().await?); + .layer(jwt_auth.into_layer().await?); let app = Router::new() // public endpoint diff --git a/jwt-authorizer/src/layer.rs b/jwt-authorizer/src/layer.rs index e401f73..7e3884a 100644 --- a/jwt-authorizer/src/layer.rs +++ b/jwt-authorizer/src/layer.rs @@ -195,11 +195,11 @@ where } #[async_trait] -impl ToAuthorizationLayer for JwtAuthorizer +impl IntoLayer for JwtAuthorizer where C: Clone + DeserializeOwned + Send + Sync, { - async fn to_layer(self) -> Result, InitError> { + async fn into_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?, @@ -209,11 +209,11 @@ where } #[async_trait] -impl ToAuthorizationLayer for Vec> +impl IntoLayer for Vec> where C: Clone + DeserializeOwned + Send + Sync, { - async fn to_layer(self) -> Result, InitError> { + async fn into_layer(self) -> Result, InitError> { let mut errs = Vec::::new(); let mut auths = Vec::>>::new(); let mut auths_futs: FuturesUnordered<_> = self @@ -330,11 +330,11 @@ where } #[async_trait] -pub trait ToAuthorizationLayer +pub trait IntoLayer where C: Clone + DeserializeOwned + Send, { - async fn to_layer(self) -> Result, InitError>; + async fn into_layer(self) -> Result, InitError>; } // ---------- AsyncAuthorizationService -------- @@ -485,12 +485,12 @@ where #[cfg(test)] mod tests { - use crate::{JwtAuthorizer, ToAuthorizationLayer}; + use crate::{IntoLayer, JwtAuthorizer}; #[tokio::test] async fn jwt_auth_to_layer() { let auth1: JwtAuthorizer = JwtAuthorizer::from_secret("aaa"); - let layer = auth1.to_layer().await; + let layer = auth1.into_layer().await; assert!(layer.is_ok()); } @@ -499,7 +499,7 @@ mod tests { let auth1: JwtAuthorizer = JwtAuthorizer::from_secret("aaa"); let auth2: JwtAuthorizer = JwtAuthorizer::from_secret("bbb"); let av = vec![auth1, auth2]; - let layer = av.to_layer().await; + let layer = av.into_layer().await; assert!(layer.is_ok()); } @@ -508,7 +508,7 @@ mod tests { let auth1: JwtAuthorizer = JwtAuthorizer::from_ec_pem("aaa"); let auth2: JwtAuthorizer = JwtAuthorizer::from_ed_pem("bbb"); let av = vec![auth1, auth2]; - let layer = av.to_layer().await; + let layer = av.into_layer().await; assert!(layer.is_err()); if let Err(err) = layer { assert_eq!(err.to_string(), "No such file or directory (os error 2)"); diff --git a/jwt-authorizer/src/lib.rs b/jwt-authorizer/src/lib.rs index e35bbfb..a5f7243 100644 --- a/jwt-authorizer/src/lib.rs +++ b/jwt-authorizer/src/lib.rs @@ -8,7 +8,7 @@ use serde::de::DeserializeOwned; pub use self::error::AuthError; pub use claims::{NumericDate, OneOrArray, RegisteredClaims}; pub use jwks::key_store_manager::{Refresh, RefreshStrategy}; -pub use layer::{JwtAuthorizer, ToAuthorizationLayer}; +pub use layer::{IntoLayer, JwtAuthorizer}; pub use validation::Validation; pub mod authorizer; diff --git a/jwt-authorizer/tests/integration_tests.rs b/jwt-authorizer/tests/integration_tests.rs index 36b7704..2fbee09 100644 --- a/jwt-authorizer/tests/integration_tests.rs +++ b/jwt-authorizer/tests/integration_tests.rs @@ -11,7 +11,7 @@ use std::{ use axum::{response::Response, routing::get, Json, Router}; use http::{header::AUTHORIZATION, Request, StatusCode}; use hyper::Body; -use jwt_authorizer::{JwtAuthorizer, JwtClaims, Refresh, RefreshStrategy, ToAuthorizationLayer}; +use jwt_authorizer::{IntoLayer, JwtAuthorizer, JwtClaims, Refresh, RefreshStrategy}; use lazy_static::lazy_static; use serde::{Deserialize, Serialize}; use serde_json::Value; @@ -104,7 +104,7 @@ async fn app(jwt_auth: JwtAuthorizer) -> Router { let protected_route: Router = Router::new() .route("/protected", get(protected_handler)) .route("/protected-with-user", get(protected_with_user)) - .layer(jwt_auth.to_layer().await.unwrap()); + .layer(jwt_auth.into_layer().await.unwrap()); Router::new().merge(pub_route).merge(protected_route) } diff --git a/jwt-authorizer/tests/tests.rs b/jwt-authorizer/tests/tests.rs index 0811022..74f7162 100644 --- a/jwt-authorizer/tests/tests.rs +++ b/jwt-authorizer/tests/tests.rs @@ -12,7 +12,7 @@ mod tests { BoxError, Router, }; use http::{header, HeaderValue}; - use jwt_authorizer::{layer::JwtSource, validation::Validation, JwtAuthorizer, JwtClaims, ToAuthorizationLayer}; + use jwt_authorizer::{layer::JwtSource, validation::Validation, IntoLayer, JwtAuthorizer, JwtClaims}; use serde::Deserialize; use tower::{util::MapErrLayer, ServiceExt}; @@ -23,7 +23,7 @@ mod tests { sub: String, } - async fn app(jwt_auth: impl ToAuthorizationLayer) -> Router { + async fn app(jwt_auth: impl IntoLayer) -> Router { Router::new().route("/public", get(|| async { "hello" })).route( "/protected", get(|JwtClaims(user): JwtClaims| async move { format!("hello: {}", user.sub) }).layer( @@ -32,14 +32,14 @@ mod tests { tower::buffer::BufferLayer::new(1), MapErrLayer::new(|e: BoxError| -> Infallible { panic!("{}", e) }), ), - jwt_auth.to_layer().await.unwrap(), + jwt_auth.into_layer().await.unwrap(), ), ), ) } async fn proteced_request_with_header( - jwt_auth: impl ToAuthorizationLayer, + jwt_auth: impl IntoLayer, header_name: &str, header_value: &str, ) -> Response { @@ -56,7 +56,7 @@ mod tests { .unwrap() } - async fn make_proteced_request(jwt_auth: impl ToAuthorizationLayer, bearer: &str) -> Response { + async fn make_proteced_request(jwt_auth: impl IntoLayer, bearer: &str) -> Response { proteced_request_with_header(jwt_auth, "Authorization", &format!("Bearer {bearer}")).await }