diff --git a/jwt-authorizer/src/authorizer.rs b/jwt-authorizer/src/authorizer.rs index b3182b7..6f82e64 100644 --- a/jwt-authorizer/src/authorizer.rs +++ b/jwt-authorizer/src/authorizer.rs @@ -20,14 +20,14 @@ pub trait ClaimsChecker { #[derive(Clone)] pub struct FnClaimsChecker where - C: Clone, + C: Clone + Send + Sync, { - pub checker_fn: fn(&C) -> bool, + pub checker_fn: Arc bool + Send + Sync>>, } impl ClaimsChecker for FnClaimsChecker where - C: Clone, + C: Clone + Send + Sync, { fn check(&self, claims: &C) -> bool { (self.checker_fn)(claims) @@ -36,7 +36,7 @@ where pub struct Authorizer where - C: Clone, + C: Clone + Send, { pub key_source: KeySource, pub claims_checker: Option>, diff --git a/jwt-authorizer/src/builder.rs b/jwt-authorizer/src/builder.rs index 234ffb5..ab3dc34 100644 --- a/jwt-authorizer/src/builder.rs +++ b/jwt-authorizer/src/builder.rs @@ -154,8 +154,13 @@ where /// configures token content check (custom function), if false a 403 will be sent. /// (AuthError::InvalidClaims()) - pub fn check(mut self, checker_fn: fn(&C) -> bool) -> AuthorizerBuilder { - self.claims_checker = Some(FnClaimsChecker { checker_fn }); + pub fn check(mut self, checker_fn: F) -> AuthorizerBuilder + where + F: Fn(&C) -> bool + Send + Sync + 'static, + { + self.claims_checker = Some(FnClaimsChecker { + checker_fn: Arc::new(Box::new(checker_fn)), + }); self } diff --git a/jwt-authorizer/tests/tests.rs b/jwt-authorizer/tests/tests.rs index f2a89b6..f5128e4 100644 --- a/jwt-authorizer/tests/tests.rs +++ b/jwt-authorizer/tests/tests.rs @@ -126,8 +126,9 @@ mod tests { #[tokio::test] async fn protected_with_claims_check() { + let b = true; // to test closures let rsp_ok = make_proteced_request( - JwtAuthorizer::from_rsa_pem("../config/rsa-public2.pem").check(|_| true), + JwtAuthorizer::from_rsa_pem("../config/rsa-public2.pem").check(move |_| b), common::JWT_RSA2_OK, ) .await;