mirror of
https://github.com/TECHNOFAB11/jwt-authorizer.git
synced 2025-12-11 23:50:07 +01:00
feat: implement into_layer for Arc<...>
This commit is contained in:
parent
44bdc8ad4c
commit
6adce03c17
2 changed files with 72 additions and 1 deletions
|
|
@ -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]
|
impl<C, const N: usize> IntoLayer<C> for [Authorizer<C>; N]
|
||||||
where
|
where
|
||||||
C: Clone + DeserializeOwned + Send,
|
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>
|
impl<C> IntoLayer<C> for Authorizer<C>
|
||||||
where
|
where
|
||||||
C: Clone + DeserializeOwned + Send,
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ mod common;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::convert::Infallible;
|
use std::{convert::Infallible, sync::Arc};
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
body::Body,
|
body::Body,
|
||||||
|
|
@ -370,6 +370,7 @@ mod tests {
|
||||||
// --------------------------
|
// --------------------------
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn multiple_authorizers() {
|
async fn multiple_authorizers() {
|
||||||
|
// 1) Vec
|
||||||
let auths: Vec<Authorizer<User>> = vec![
|
let auths: Vec<Authorizer<User>> = vec![
|
||||||
JwtAuthorizer::from_ec_pem("../config/ecdsa-public1.pem")
|
JwtAuthorizer::from_ec_pem("../config/ecdsa-public1.pem")
|
||||||
.build()
|
.build()
|
||||||
|
|
@ -391,6 +392,7 @@ mod tests {
|
||||||
.await;
|
.await;
|
||||||
assert_eq!(response.status(), StatusCode::OK);
|
assert_eq!(response.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
// 2) Slice
|
||||||
let auths: [Authorizer<User>; 2] = [
|
let auths: [Authorizer<User>; 2] = [
|
||||||
JwtAuthorizer::from_ec_pem("../config/ecdsa-public1.pem")
|
JwtAuthorizer::from_ec_pem("../config/ecdsa-public1.pem")
|
||||||
.build()
|
.build()
|
||||||
|
|
@ -412,5 +414,47 @@ mod tests {
|
||||||
.await;
|
.await;
|
||||||
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
|
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
|
||||||
assert_eq!(response.headers().get(header::WWW_AUTHENTICATE).unwrap(), &"Bearer");
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue