Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jwt): support non-integer timestamp claims #8867

Merged
merged 3 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Bug fixes

* Fix exhook `client.authorize` never being execauted. [#8780](https://github.com/emqx/emqx/pull/8780)
* Fix JWT plugin don't support non-integer timestamp claims. [#8867](https://github.com/emqx/emqx/pull/8867)

## Enhancements

Expand Down
41 changes: 27 additions & 14 deletions apps/emqx_authn/src/simple_authn/emqx_authn_jwt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ do_verify(JWT, [JWK | More], VerifyClaims) ->
try jose_jws:verify(JWK, JWT) of
{true, Payload, _JWT} ->
Claims0 = emqx_json:decode(Payload, [return_maps]),
Claims = try_convert_to_int(Claims0, [<<"exp">>, <<"iat">>, <<"nbf">>]),
Claims = try_convert_to_num(Claims0, [<<"exp">>, <<"iat">>, <<"nbf">>]),
case verify_claims(Claims, VerifyClaims) of
ok ->
{ok, Claims};
Expand All @@ -403,37 +403,37 @@ verify_claims(Claims, VerifyClaims0) ->
VerifyClaims =
[
{<<"exp">>, fun(ExpireTime) ->
is_integer(ExpireTime) andalso Now < ExpireTime
is_number(ExpireTime) andalso Now < ExpireTime
end},
{<<"iat">>, fun(IssueAt) ->
is_integer(IssueAt) andalso IssueAt =< Now
is_number(IssueAt) andalso IssueAt =< Now
end},
{<<"nbf">>, fun(NotBefore) ->
is_integer(NotBefore) andalso NotBefore =< Now
is_number(NotBefore) andalso NotBefore =< Now
end}
] ++ VerifyClaims0,
do_verify_claims(Claims, VerifyClaims).

try_convert_to_int(Claims, [Name | Names]) ->
try_convert_to_num(Claims, [Name | Names]) ->
case Claims of
#{Name := Value} ->
case Value of
Int when is_integer(Int) ->
try_convert_to_int(Claims#{Name => Int}, Names);
Int when is_number(Int) ->
try_convert_to_num(Claims#{Name => Int}, Names);
Bin when is_binary(Bin) ->
case string:to_integer(Bin) of
{Int, <<>>} ->
try_convert_to_int(Claims#{Name => Int}, Names);
case binary_to_number(Bin) of
{ok, Num} ->
try_convert_to_num(Claims#{Name => Num}, Names);
_ ->
try_convert_to_int(Claims, Names)
try_convert_to_num(Claims, Names)
end;
_ ->
try_convert_to_int(Claims, Names)
try_convert_to_num(Claims, Names)
end;
_ ->
try_convert_to_int(Claims, Names)
try_convert_to_num(Claims, Names)
end;
try_convert_to_int(Claims, []) ->
try_convert_to_num(Claims, []) ->
Claims.

do_verify_claims(_Claims, []) ->
Expand Down Expand Up @@ -519,3 +519,16 @@ to_binary(B) when is_binary(B) ->
B.

sc(Type, Meta) -> hoconsc:mk(Type, Meta).

binary_to_number(Bin) ->
try
{ok, erlang:binary_to_integer(Bin)}
catch
_:_ ->
try
{ok, erlang:binary_to_float(Bin)}
catch
_:_ ->
false
end
end.
14 changes: 13 additions & 1 deletion apps/emqx_authn/test/emqx_authn_jwt_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,19 @@ t_verify_claims(_) ->
},
?assertMatch(
{ok, #{is_superuser := false}}, emqx_authn_jwt:authenticate(Credential4, State1)
).
),

Payload5 = #{
<<"username">> => <<"myuser">>,
<<"foo">> => <<"myuser">>,
<<"exp">> => erlang:system_time(second) + 10.5
},
JWS5 = generate_jws('hmac-based', Payload5, Secret),
Credential5 = #{
username => <<"myuser">>,
password => JWS5
},
?assertMatch({ok, #{is_superuser := false}}, emqx_authn_jwt:authenticate(Credential5, State1)).

t_jwt_not_allow_empty_claim_name(_) ->
Request = #{
Expand Down