diff --git a/jwt-authorizer/src/authorizer.rs b/jwt-authorizer/src/authorizer.rs index d80963c..8b7b202 100644 --- a/jwt-authorizer/src/authorizer.rs +++ b/jwt-authorizer/src/authorizer.rs @@ -265,7 +265,7 @@ where C: Clone + DeserializeOwned + Send, { fn into_layer(self) -> AuthorizationLayer { - 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, { fn into_layer(self) -> AuthorizationLayer { - AuthorizationLayer::new(self.into_iter().collect()) + AuthorizationLayer::new(self.into_iter().collect(), false) } } @@ -283,7 +283,7 @@ where C: Clone + DeserializeOwned + Send, { fn into_layer(self) -> AuthorizationLayer { - 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, { fn into_layer(self) -> AuthorizationLayer { - AuthorizationLayer::new(self.into_iter().collect()) + AuthorizationLayer::new(self.into_iter().collect(), false) } } @@ -301,7 +301,7 @@ where C: Clone + DeserializeOwned + Send, { fn into_layer(self) -> AuthorizationLayer { - AuthorizationLayer::new(vec![Arc::new(self)]) + AuthorizationLayer::new(vec![Arc::new(self)], false) } } @@ -310,7 +310,7 @@ where C: Clone + DeserializeOwned + Send, { fn into_layer(self) -> AuthorizationLayer { - AuthorizationLayer::new(vec![self]) + AuthorizationLayer::new(vec![self], false) } } diff --git a/jwt-authorizer/src/builder.rs b/jwt-authorizer/src/builder.rs index 5f8799d..7d40160 100644 --- a/jwt-authorizer/src/builder.rs +++ b/jwt-authorizer/src/builder.rs @@ -237,7 +237,7 @@ where ) .await?, ); - Ok(AuthorizationLayer::new(vec![auth])) + Ok(AuthorizationLayer::new(vec![auth], false)) } pub async fn build(self) -> Result, InitError> { diff --git a/jwt-authorizer/src/layer.rs b/jwt-authorizer/src/layer.rs index 7ccde38..4681630 100644 --- a/jwt-authorizer/src/layer.rs +++ b/jwt-authorizer/src/layer.rs @@ -42,7 +42,11 @@ where .collect(); if tkns_auths.is_empty() { - return Box::pin(future::ready(Err(AuthError::MissingToken()))); + if self.token_optional { + return Box::pin(future::ready(Ok(request))); + } else { + return Box::pin(future::ready(Err(AuthError::MissingToken()))); + } } Box::pin(async move { @@ -76,14 +80,15 @@ where C: Clone + DeserializeOwned + Send, { auths: Vec>>, + token_optional: bool, } impl AuthorizationLayer where C: Clone + DeserializeOwned + Send, { - pub fn new(auths: Vec>>) -> AuthorizationLayer { - Self { auths } + pub fn new(auths: Vec>>, token_optional: bool) -> AuthorizationLayer { + Self { auths, token_optional } } } @@ -94,7 +99,7 @@ where type Service = AuthorizationService; 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 auths: Vec>>, + pub token_optional: bool, } impl AuthorizationService @@ -150,8 +156,8 @@ where /// Authorize requests using a custom scheme. /// /// The `Authorization` header is required to have the value provided. - pub fn new(inner: S, auths: Vec>>) -> AuthorizationService { - Self { inner, auths } + pub fn new(inner: S, auths: Vec>>, token_optional: bool) -> AuthorizationService { + Self { inner, auths, token_optional } } }