Skip to content

Commit

Permalink
Make all Jw[<lowercase>] JW[<uppercase>] for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
kivra-pauoli committed Aug 29, 2024
1 parent 7493914 commit f1d62d7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/id_token_jwks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ get_pub_keys(Uri) ->
get_jwks_uri(Uri) ->
case hackney:request(get, Uri, [], <<>>, [with_body]) of
{ok, 200, _Headers, Body} ->
#{<<"jwks_uri">> := JwksUri} = jsx:decode(Body, [return_maps]),
{ok, JwksUri};
#{<<"jwks_uri">> := JWKSUri} = jsx:decode(Body, [return_maps]),
{ok, JWKSUri};
{ok, _, _, _} ->
{error, service_unavailable};
{error, Reason} ->
Expand Down
4 changes: 2 additions & 2 deletions src/id_token_jws.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ generate_key_for(Alg, Options) ->
<<"ES", _S/binary>> -> generate_ec_key(Alg, Options);
_ -> generate_rsa_key(Alg, Options)
end,
Jwk0 = jose_jwk:from_key(Key),
JWK = Jwk0#jose_jwk{fields = #{<<"kid">> => kid(Options),
JWK0 = jose_jwk:from_key(Key),
JWK = JWK0#jose_jwk{fields = #{<<"kid">> => kid(Options),
<<"use">> => <<"sig">>,
<<"iat">> => iat(Options)}},
{_, PublicKeyMap} = jose_jwk:to_public_map(JWK),
Expand Down
4 changes: 2 additions & 2 deletions src/id_token_sign.erl
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ handle_info(_Request, Timers0) ->
%%% Internal functions
%%%===================================================================
put_key_for(Alg, Options) ->
{Jwk, PublicKeyMap} = id_token_jws:generate_key_for(Alg, Options),
SignKeyFun = fun() -> Jwk end,
{JWK, PublicKeyMap} = id_token_jws:generate_key_for(Alg, Options),
SignKeyFun = fun() -> JWK end,
#{<<"kid">> := Kid, <<"iat">> := Iat} = PublicKeyMap,
TTU = maps:get(ttu, Options, ?TTU),
Exp = Iat + TTU,
Expand Down
28 changes: 14 additions & 14 deletions test/id_token_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ init_per_suite(Config) ->
end_per_suite(_Config) -> ok.

init_per_testcase(_TestCase, Config) ->
{Jwk, PublicKeyMap} =
{JWK, PublicKeyMap} =
id_token_jws:generate_key_for(<<"RS256">>, #{key_size => 1024}),
Claims = #{ <<"exp">> => erlang:system_time(second) + 10},
Jwt = id_token_jws:sign(Claims, Jwk),
JWT = id_token_jws:sign(Claims, JWK),
mock_id_provider(PublicKeyMap, 0),
application:ensure_all_started(id_token),
[{jwt, Jwt}, {pubkeys, [PublicKeyMap]} | Config].
[{jwt, JWT}, {pubkeys, [PublicKeyMap]} | Config].
end_per_testcase(_TestCase, Config) ->
application:stop(id_token),
meck:unload([id_token_jwks, hackney]),
Config.

validate_jwt(Config) ->
Jwt = ?config(jwt, Config),
?assertMatch({ok, _}, id_token:validate(?ID_PROVIDER, Jwt)).
JWT = ?config(jwt, Config),
?assertMatch({ok, _}, id_token:validate(?ID_PROVIDER, JWT)).

keys_are_cached(Config) ->
Jwt = ?config(jwt, Config),
?assertMatch({ok, _}, id_token:validate(?ID_PROVIDER, Jwt)),
?assertMatch({ok, _}, id_token:validate(?ID_PROVIDER, Jwt)),
JWT = ?config(jwt, Config),
?assertMatch({ok, _}, id_token:validate(?ID_PROVIDER, JWT)),
?assertMatch({ok, _}, id_token:validate(?ID_PROVIDER, JWT)),
1 = meck:num_calls(id_token_jwks, get_pub_keys, 1),
ok.

Expand All @@ -49,24 +49,24 @@ keys_are_only_refreshed_once_per_kid(Config) ->
ok = meck:expect(id_token_provider, get_cached_keys, 1, CurrentKeyCache),

%% create JWT with kid that's not yet in the pubkey cache
{Jwk, NewPubkey} = id_token_jws:generate_key_for(<<"RS256">>, #{key_size => 1024}),
{JWK, NewPubkey} = id_token_jws:generate_key_for(<<"RS256">>, #{key_size => 1024}),
Claims = #{ <<"exp">> => erlang:system_time(second) + 10 },
Jwt = id_token_jws:sign(Claims, Jwk),
JWT = id_token_jws:sign(Claims, JWK),

%% set up provider to return new pubkeys with a 50 ms delay
HttpReponseDelay = 50,
mock_id_provider(NewPubkey, HttpReponseDelay),
?assertEqual(0, meck:num_calls(id_token_jwks, get_pub_keys, 1)),

%% try to validate multiple JWTs based on kid that's not yet in the key cache
spawn(fun() -> id_token:validate(?ID_PROVIDER, Jwt) end),
spawn(fun() -> id_token:validate(?ID_PROVIDER, Jwt) end),
spawn(fun() -> id_token:validate(?ID_PROVIDER, Jwt) end),
spawn(fun() -> id_token:validate(?ID_PROVIDER, JWT) end),
spawn(fun() -> id_token:validate(?ID_PROVIDER, JWT) end),
spawn(fun() -> id_token:validate(?ID_PROVIDER, JWT) end),
timer:sleep(10 * HttpReponseDelay),

%% ensure that the pubkey cache was only refreshed once
?assertEqual(1, meck:num_calls(id_token_jwks, get_pub_keys, 1)),
?assertMatch({ok, _}, id_token:validate(?ID_PROVIDER, Jwt)),
?assertMatch({ok, _}, id_token:validate(?ID_PROVIDER, JWT)),

meck:unload([id_token_provider]).

Expand Down
22 changes: 11 additions & 11 deletions test/prop_id_token_jwt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,36 @@ eunit_test_() ->
%%% Properties %%%
%%%%%%%%%%%%%%%%%%
prop_valid_signature() ->
?FORALL({{Jwk, PublicKeyMap}, Claims},
?FORALL({{JWK, PublicKeyMap}, Claims},
{key_pair(), jwt_claims()},
begin
#{<<"exp">> := Exp} = Claims,
Jwt = id_token_jws:sign(Claims, Jwk),
Result = id_token_jws:validate(Jwt, [PublicKeyMap]),
JWT = id_token_jws:sign(Claims, JWK),
Result = id_token_jws:validate(JWT, [PublicKeyMap]),
Exp =< erlang:system_time(second)
andalso {error, expired} =:= Result
orelse {ok, Claims} =:= Result
end).

prop_invalid_signature() ->
?FORALL({{Jwk, PublicKeyMap}, {OtherJwk, OtherPublicKeyMap}, Claims},
?FORALL({{JWK, PublicKeyMap}, {OtherJWK, OtherPublicKeyMap}, Claims},
{key_pair(), key_pair(), jwt_claims()},
begin
#jose_jwk{fields = OtherFields} = OtherJwk,
JwkWithChangedKid = Jwk#jose_jwk{fields = OtherFields},
Jwt = id_token_jws:sign(Claims, JwkWithChangedKid),
#jose_jwk{fields = OtherFields} = OtherJWK,
JWKWithChangedKid = JWK#jose_jwk{fields = OtherFields},
JWT = id_token_jws:sign(Claims, JWKWithChangedKid),
{error, invalid_signature}
=:= id_token_jws:validate(Jwt, [OtherPublicKeyMap])
=:= id_token_jws:validate(JWT, [OtherPublicKeyMap])
end).

prop_no_matching_key() ->
?FORALL({[{Jwk, PublicKeyMap} | OtherKeys], Claims},
?FORALL({[{JWK, PublicKeyMap} | OtherKeys], Claims},
{non_empty(list(key_pair())), jwt_claims()},
begin
Jwt = id_token_jws:sign(Claims, Jwk),
JWT = id_token_jws:sign(Claims, JWK),
PublicKeys = lists:map(fun({_, Key}) -> Key end, OtherKeys),
{error, no_public_key_matches}
=:= id_token_jws:validate(Jwt, PublicKeys)
=:= id_token_jws:validate(JWT, PublicKeys)
end).

%%%%%%%%%%%%%%%%%%
Expand Down

0 comments on commit f1d62d7

Please sign in to comment.