Skip to content

Commit

Permalink
Add list() type support for opts paramter
Browse files Browse the repository at this point in the history
  • Loading branch information
HJianBo committed May 8, 2020
1 parent 7cce3a4 commit 3cd56a3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ jwerl:verify(Jwt, rs512, PublcPem).
{ok, PublcPem} = file:read_file("path/to/es_public.pem"),
Jwt = jwerl:sign([{name, <<"bob">>}], es256, PrivtPem).
jwerl:verify(Jwt, es256, PublcPem).

% Compatibility
% - sign/verify the signature with raw format instead of DER
% - it is necessary to compatible with nodejs or other platforms
Jwt = jwerl:sign([{name, <<"bob">>}], es256, PrivtPem, #{raw => true}).
jwerl:verify(Jwt, es256, PublcPem, Claims, #{raw => true}).
```


Expand Down Expand Up @@ -99,7 +93,6 @@ Jwerl.verify(jwt, :rs512, public_pem)
{ok, public_pem} = File.read("path/to/es_public.pem")
jwt = Jwerl.sign([name: "bob"], :es256, private_pem)
Jwerl.verify(jwt, :es256, public_pem)

```


Expand Down
12 changes: 12 additions & 0 deletions overview.edoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ jwerl:verify(Jwt, rs512, PublcPem).
{ok, PublcPem} = file:read_file("path/to/es_public.pem"),
Jwt = jwerl:sign([{name, &lt;&lt;"bob"&gt;&gt;}], es256, PrivtPem).
jwerl:verify(Jwt, es256, PublcPem).

% Compatibility
% - sign/verify the signature with raw format instead of DER
% - it is necessary to compatible with nodejs or other platforms
Jwt = jwerl:sign([{name, <<"bob">>}], es256, PrivtPem, #{raw => true}).
jwerl:verify(Jwt, es256, PublcPem, Claims, #{raw => true}).
</pre>

<h3>Elixir</h3>
Expand Down Expand Up @@ -85,6 +91,12 @@ Jwerl.verify(jwt, :rs512, public_pem)
{ok, public_pem} = File.read("path/to/es_public.pem")
jwt = Jwerl.sign([name: "bob"], :es256, private_pem)
Jwerl.verify(jwt, :es256, public_pem)

% Compatibility
% - sign/verify the signature with raw format instead of DER
% - it is necessary to compatible with nodejs or other platforms
Jwt = Jwerl:sign([name: "bob"], :es256, private_pem, [raw: true])
Jwerl.verify(jwt, :es256, public_pem, claims, [raw: true])
</pre>

<h2>Licence</h2>
Expand Down
17 changes: 11 additions & 6 deletions src/jwerl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ sign(Data, Algorithm, KeyOrPem) ->
% Token = jwerl:sign(#{key =&gt; &lt;&lt;"Hello World"&gt;&gt;}, hs256, &lt;&lt;"s3cr3t k3y"&gt;&gt;).
% </pre>
% @end
-spec sign(Data :: map() | list(), Algorithm :: algorithm(), KeyOrPem :: binary(), Opts :: map()) -> binary().
-spec sign(Data :: map() | list(), Algorithm :: algorithm(), KeyOrPem :: binary(), Opts :: map() | list()) -> binary().
sign(Data, Algorithm, KeyOrPem, Opts) when (is_map(Data) orelse is_list(Data)), is_atom(Algorithm), is_binary(KeyOrPem), is_map(Opts) ->
encode(jsx:encode(Data), config_headers(#{alg => algorithm_to_binary(Algorithm)}), KeyOrPem, Opts).

Expand Down Expand Up @@ -77,7 +77,7 @@ verify(Data, Algorithm, KeyOrPem, Claims) ->
% aud =&gt; [&lt;&lt;"world"&gt;&gt;, &lt;&lt;"aliens"&gt;&gt;]}).
% </pre>
% @end
-spec verify(Data :: binary(), Algorithm :: algorithm(), KeyOrPem :: binary(), CheckClaims :: map() | list() | false, Opts :: map()) ->
-spec verify(Data :: binary(), Algorithm :: algorithm(), KeyOrPem :: binary(), CheckClaims :: map() | list() | false, Opts :: map() | list()) ->
{ok, map()} | {error, term()}.
verify(Data, Algorithm, KeyOrPem, Claims, Opts) ->
case decode(Data, KeyOrPem, Algorithm, Opts) of
Expand Down Expand Up @@ -110,11 +110,11 @@ check_claims(TokenData, Claims, Opts) ->
claims_errors(
[
check_claim(TokenData, exp, false, fun(ExpireTime) ->
ExpLeeway = maps:get(exp_leeway, Opts, 0),
ExpLeeway = get_value(exp_leeway, Opts, 0),
Now < ExpireTime + ExpLeeway
end, exp),
check_claim(TokenData, iat, false, fun(IssuedAt) ->
IatLeeway = maps:get(iat_leeway, Opts, 0),
IatLeeway = get_value(iat_leeway, Opts, 0),
IssuedAt - IatLeeway =< Now
end, iat),
check_claim(TokenData, nbf, false, fun(NotBefore) ->
Expand Down Expand Up @@ -233,7 +233,7 @@ payload(Data, Algorithm, Key, Opts) ->
[Header, Data1, Signature0] = binary:split(Data, <<".">>, [global]),
{AlgMod, ShaBits} = algorithm_to_infos(Algorithm),

Signature = case maps:get(raw, Opts, false) of
Signature = case get_value(raw, Opts, false) of
true -> raw_to_der(base64_decode(Signature0));
_ -> base64_decode(Signature0)
end,
Expand All @@ -253,14 +253,19 @@ encode_input(Data, Options) ->
signature(Algorithm, Key, Data, Opts) ->
{AlgMod, ShaBits} = algorithm_to_infos(Algorithm),
Signature0 = erlang:apply(AlgMod, sign, [ShaBits, Key, Data]),
Signature = case maps:get(raw, Opts, false) of
Signature = case get_value(raw, Opts, false) of
true ->
der_to_raw(Signature0);
_ ->
Signature0
end,
base64_encode(Signature).

get_value(K, T, Default) when is_list(T) ->
proplists:get_value(K, T, Default);
get_value(K, M, Default) when is_map(M) ->
maps:get(K, M, Default).

der_to_raw(<<48,_,_, L1, R:L1/binary, _, L2, S:L2/binary>>) ->
<<(trim_zero_padding(R))/binary, (trim_zero_padding(S))/binary>>.

Expand Down
1 change: 0 additions & 1 deletion test/jwerl_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ t_jwerl_ecdsa() ->
jwerl:sign(Data, es512, ec_private_key()),
es512,
ec_public_key())),

?assertMatch({ok, Data}, jwerl:verify(
jwerl:sign(Data, es512, ec_private_key(), #{raw => true}),
es512,
Expand Down

0 comments on commit 3cd56a3

Please sign in to comment.