feat: implement into_layer for Arc<...>

This commit is contained in:
cduvray 2023-09-05 07:43:25 +02:00
parent 44bdc8ad4c
commit 6adce03c17
2 changed files with 72 additions and 1 deletions

View file

@ -249,6 +249,15 @@ where
}
}
impl<C> IntoLayer<C> for Vec<Arc<Authorizer<C>>>
where
C: Clone + DeserializeOwned + Send,
{
fn into_layer(self) -> AsyncAuthorizationLayer<C> {
AsyncAuthorizationLayer::new(self.into_iter().collect())
}
}
impl<C, const N: usize> IntoLayer<C> for [Authorizer<C>; N]
where
C: Clone + DeserializeOwned + Send,
@ -258,6 +267,15 @@ where
}
}
impl<C, const N: usize> IntoLayer<C> for [Arc<Authorizer<C>>; N]
where
C: Clone + DeserializeOwned + Send,
{
fn into_layer(self) -> AsyncAuthorizationLayer<C> {
AsyncAuthorizationLayer::new(self.into_iter().collect())
}
}
impl<C> IntoLayer<C> for Authorizer<C>
where
C: Clone + DeserializeOwned + Send,
@ -267,6 +285,15 @@ where
}
}
impl<C> IntoLayer<C> for Arc<Authorizer<C>>
where
C: Clone + DeserializeOwned + Send,
{
fn into_layer(self) -> AsyncAuthorizationLayer<C> {
AsyncAuthorizationLayer::new(vec![self])
}
}
#[cfg(test)]
mod tests {

View file

@ -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<Authorizer<User>> = 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<User>; 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);
}
}