chore: add param to make auth optional (to handle more granularly)

This commit is contained in:
technofab 2025-01-05 20:57:45 +01:00
parent 10a926c25b
commit d9597882bf
3 changed files with 19 additions and 13 deletions

View file

@ -265,7 +265,7 @@ where
C: Clone + DeserializeOwned + Send, C: Clone + DeserializeOwned + Send,
{ {
fn into_layer(self) -> AuthorizationLayer<C> { fn into_layer(self) -> AuthorizationLayer<C> {
AuthorizationLayer::new(self.into_iter().map(Arc::new).collect()) AuthorizationLayer::new(self.into_iter().map(Arc::new).collect(), false)
} }
} }
@ -274,7 +274,7 @@ where
C: Clone + DeserializeOwned + Send, C: Clone + DeserializeOwned + Send,
{ {
fn into_layer(self) -> AuthorizationLayer<C> { fn into_layer(self) -> AuthorizationLayer<C> {
AuthorizationLayer::new(self.into_iter().collect()) AuthorizationLayer::new(self.into_iter().collect(), false)
} }
} }
@ -283,7 +283,7 @@ where
C: Clone + DeserializeOwned + Send, C: Clone + DeserializeOwned + Send,
{ {
fn into_layer(self) -> AuthorizationLayer<C> { fn into_layer(self) -> AuthorizationLayer<C> {
AuthorizationLayer::new(self.into_iter().map(Arc::new).collect()) AuthorizationLayer::new(self.into_iter().map(Arc::new).collect(), false)
} }
} }
@ -292,7 +292,7 @@ where
C: Clone + DeserializeOwned + Send, C: Clone + DeserializeOwned + Send,
{ {
fn into_layer(self) -> AuthorizationLayer<C> { fn into_layer(self) -> AuthorizationLayer<C> {
AuthorizationLayer::new(self.into_iter().collect()) AuthorizationLayer::new(self.into_iter().collect(), false)
} }
} }
@ -301,7 +301,7 @@ where
C: Clone + DeserializeOwned + Send, C: Clone + DeserializeOwned + Send,
{ {
fn into_layer(self) -> AuthorizationLayer<C> { fn into_layer(self) -> AuthorizationLayer<C> {
AuthorizationLayer::new(vec![Arc::new(self)]) AuthorizationLayer::new(vec![Arc::new(self)], false)
} }
} }
@ -310,7 +310,7 @@ where
C: Clone + DeserializeOwned + Send, C: Clone + DeserializeOwned + Send,
{ {
fn into_layer(self) -> AuthorizationLayer<C> { fn into_layer(self) -> AuthorizationLayer<C> {
AuthorizationLayer::new(vec![self]) AuthorizationLayer::new(vec![self], false)
} }
} }

View file

@ -237,7 +237,7 @@ where
) )
.await?, .await?,
); );
Ok(AuthorizationLayer::new(vec![auth])) Ok(AuthorizationLayer::new(vec![auth], false))
} }
pub async fn build(self) -> Result<Authorizer<C>, InitError> { pub async fn build(self) -> Result<Authorizer<C>, InitError> {

View file

@ -42,8 +42,12 @@ where
.collect(); .collect();
if tkns_auths.is_empty() { if tkns_auths.is_empty() {
if self.token_optional {
return Box::pin(future::ready(Ok(request)));
} else {
return Box::pin(future::ready(Err(AuthError::MissingToken()))); return Box::pin(future::ready(Err(AuthError::MissingToken())));
} }
}
Box::pin(async move { Box::pin(async move {
let mut token_data: Result<TokenData<C>, AuthError> = Err(AuthError::NoAuthorizer()); let mut token_data: Result<TokenData<C>, AuthError> = Err(AuthError::NoAuthorizer());
@ -76,14 +80,15 @@ where
C: Clone + DeserializeOwned + Send, C: Clone + DeserializeOwned + Send,
{ {
auths: Vec<Arc<Authorizer<C>>>, auths: Vec<Arc<Authorizer<C>>>,
token_optional: bool,
} }
impl<C> AuthorizationLayer<C> impl<C> AuthorizationLayer<C>
where where
C: Clone + DeserializeOwned + Send, C: Clone + DeserializeOwned + Send,
{ {
pub fn new(auths: Vec<Arc<Authorizer<C>>>) -> AuthorizationLayer<C> { pub fn new(auths: Vec<Arc<Authorizer<C>>>, token_optional: bool) -> AuthorizationLayer<C> {
Self { auths } Self { auths, token_optional }
} }
} }
@ -94,7 +99,7 @@ where
type Service = AuthorizationService<S, C>; type Service = AuthorizationService<S, C>;
fn layer(&self, inner: S) -> Self::Service { fn layer(&self, inner: S) -> Self::Service {
AuthorizationService::new(inner, self.auths.clone()) AuthorizationService::new(inner, self.auths.clone(), self.token_optional)
} }
} }
@ -122,6 +127,7 @@ where
{ {
pub inner: S, pub inner: S,
pub auths: Vec<Arc<Authorizer<C>>>, pub auths: Vec<Arc<Authorizer<C>>>,
pub token_optional: bool,
} }
impl<S, C> AuthorizationService<S, C> impl<S, C> AuthorizationService<S, C>
@ -150,8 +156,8 @@ where
/// Authorize requests using a custom scheme. /// Authorize requests using a custom scheme.
/// ///
/// The `Authorization` header is required to have the value provided. /// The `Authorization` header is required to have the value provided.
pub fn new(inner: S, auths: Vec<Arc<Authorizer<C>>>) -> AuthorizationService<S, C> { pub fn new(inner: S, auths: Vec<Arc<Authorizer<C>>>, token_optional: bool) -> AuthorizationService<S, C> {
Self { inner, auths } Self { inner, auths, token_optional }
} }
} }