From 655c2987f3ce4ba1fc74bd04849a576107e7aeea Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Sun, 15 May 2022 09:24:02 +0200 Subject: [PATCH] fix(acl): deny all ACl when token expired --- apps/emqx_auth_jwt/src/emqx_auth_jwt.erl | 27 +++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/apps/emqx_auth_jwt/src/emqx_auth_jwt.erl b/apps/emqx_auth_jwt/src/emqx_auth_jwt.erl index 46451d1bb..2d7690a42 100644 --- a/apps/emqx_auth_jwt/src/emqx_auth_jwt.erl +++ b/apps/emqx_auth_jwt/src/emqx_auth_jwt.erl @@ -72,14 +72,31 @@ check_acl(ClientInfo = #{jwt_claims := Claims}, Topic, _NoMatchAction, #{acl_claim_name := AclClaimName}) -> - Deadline = erlang:system_time(second), case Claims of - #{AclClaimName := Acl, <<"exp">> := Exp} - when is_integer(Exp) andalso Exp >= Deadline -> - verify_acl(ClientInfo, Acl, PubSub, Topic); - _ -> ignore + #{AclClaimName := Acl, <<"exp">> := Exp} -> + try is_expired(Exp) of + true -> + ?DEBUG("acl_deny_due_to_jwt_expired", []), + deny; + false -> + verify_acl(ClientInfo, Acl, PubSub, Topic) + catch + _:_ -> + ?DEBUG("acl_deny_due_to_invalid_jwt_exp", []), + deny + end; + _ -> + ?DEBUG("no_acl_jwt_claim", []), + ignore end. +is_expired(Exp) when is_binary(Exp) -> + ExpInt = binary_to_integer(Exp), + is_expired(ExpInt); +is_expired(Exp) -> + Now = erlang:system_time(second), + Now > Exp. + description() -> "Authentication with JWT". %%------------------------------------------------------------------------------