From 6adce03c1738e3154405a337ffc69f97b1a7aabb Mon Sep 17 00:00:00 2001 From: cduvray Date: Tue, 5 Sep 2023 07:43:25 +0200 Subject: [PATCH] feat: implement into_layer for Arc<...> --- jwt-authorizer/src/authorizer.rs | 27 +++++++++++++++++++ jwt-authorizer/tests/tests.rs | 46 +++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/jwt-authorizer/src/authorizer.rs b/jwt-authorizer/src/authorizer.rs index 5f4ac70..abb0d92 100644 --- a/jwt-authorizer/src/authorizer.rs +++ b/jwt-authorizer/src/authorizer.rs @@ -249,6 +249,15 @@ where } } +impl IntoLayer for Vec>> +where + C: Clone + DeserializeOwned + Send, +{ + fn into_layer(self) -> AsyncAuthorizationLayer { + AsyncAuthorizationLayer::new(self.into_iter().collect()) + } +} + impl IntoLayer for [Authorizer; N] where C: Clone + DeserializeOwned + Send, @@ -258,6 +267,15 @@ where } } +impl IntoLayer for [Arc>; N] +where + C: Clone + DeserializeOwned + Send, +{ + fn into_layer(self) -> AsyncAuthorizationLayer { + AsyncAuthorizationLayer::new(self.into_iter().collect()) + } +} + impl IntoLayer for Authorizer where C: Clone + DeserializeOwned + Send, @@ -267,6 +285,15 @@ where } } +impl IntoLayer for Arc> +where + C: Clone + DeserializeOwned + Send, +{ + fn into_layer(self) -> AsyncAuthorizationLayer { + AsyncAuthorizationLayer::new(vec![self]) + } +} + #[cfg(test)] mod tests { diff --git a/jwt-authorizer/tests/tests.rs b/jwt-authorizer/tests/tests.rs index 3f99917..9ef8ea0 100644 --- a/jwt-authorizer/tests/tests.rs +++ b/jwt-authorizer/tests/tests.rs @@ -2,7 +2,7 @@ mod common; #[cfg(test)] mod tests { - use std::convert::Infallible; + use std::{convert::Infallible, sync::Arc}; use axum::{ body::Body, @@ -370,6 +370,7 @@ mod tests { // -------------------------- #[tokio::test] async fn multiple_authorizers() { + // 1) Vec let auths: Vec> = vec![ JwtAuthorizer::from_ec_pem("../config/ecdsa-public1.pem") .build() @@ -391,6 +392,7 @@ mod tests { .await; assert_eq!(response.status(), StatusCode::OK); + // 2) Slice let auths: [Authorizer; 2] = [ JwtAuthorizer::from_ec_pem("../config/ecdsa-public1.pem") .build() @@ -412,5 +414,47 @@ mod tests { .await; assert_eq!(response.status(), StatusCode::UNAUTHORIZED); assert_eq!(response.headers().get(header::WWW_AUTHENTICATE).unwrap(), &"Bearer"); + + // 3) Arc + let auth1 = Arc::new( + JwtAuthorizer::from_ec_pem("../config/ecdsa-public1.pem") + .build() + .await + .unwrap(), + ); + let auth2 = Arc::new( + JwtAuthorizer::from_rsa_pem("../config/rsa-public1.pem") + .jwt_source(JwtSource::Cookie("ccc".to_owned())) + .build() + .await + .unwrap(), + ); + + // Slice/OK + let response = proteced_request_with_header_and_layer( + [auth1.clone(), auth2.clone()].into_layer(), + header::COOKIE.as_str(), + &format!("ccc={}", common::JWT_RSA1_OK), + ) + .await; + assert_eq!(response.status(), StatusCode::OK); + + // Vec/OK + let response = proteced_request_with_header_and_layer( + vec![auth1, auth2.clone()].into_layer(), + header::COOKIE.as_str(), + &format!("ccc={}", common::JWT_RSA1_OK), + ) + .await; + assert_eq!(response.status(), StatusCode::OK); + + // Arc/OK + let response = proteced_request_with_header_and_layer( + auth2.into_layer(), + header::COOKIE.as_str(), + &format!("ccc={}", common::JWT_RSA1_OK), + ) + .await; + assert_eq!(response.status(), StatusCode::OK); } }