From a31c08618c2616d83c0b32910d0bdfe563760a88 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Fri, 8 May 2020 16:02:28 +0800 Subject: [PATCH] Fix `Opts` type for sign/4 --- README.md | 7 ------- overview.edoc | 12 ++++++++++++ src/jwerl.erl | 45 +++++++++++++++++++++++--------------------- test/jwerl_tests.erl | 1 - 4 files changed, 36 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 430aa4a..bf00b44 100644 --- a/README.md +++ b/README.md @@ -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}). ``` @@ -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) - ``` diff --git a/overview.edoc b/overview.edoc index c4e8169..54202db 100644 --- a/overview.edoc +++ b/overview.edoc @@ -53,6 +53,12 @@ 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}).

Elixir

@@ -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])

Licence

diff --git a/src/jwerl.erl b/src/jwerl.erl index 276e486..f0f4a74 100644 --- a/src/jwerl.erl +++ b/src/jwerl.erl @@ -15,18 +15,18 @@ es256 | es384 | es512 | none. -% @equiv sign(Data, hs256, <<"">>, #{}) +% @equiv sign(Data, hs256, <<"">>, []) -spec sign(Data :: map()) -> binary(). sign(Data) -> - sign(Data, hs256, <<"">>, #{}). -% @equiv sign(Data, Algorithm, <<"">>, #{}) + sign(Data, hs256, <<"">>, []). +% @equiv sign(Data, Algorithm, <<"">>, []) -spec sign(Data :: map(), Algorithm :: algorithm()) -> binary(). sign(Data, Algorithm) -> - sign(Data, Algorithm, <<"">>, #{}). -% @equiv sign(Data, Algorithm, KeyOrPem, #{}) + sign(Data, Algorithm, <<"">>, []). +% @equiv sign(Data, Algorithm, KeyOrPem, []) -spec sign(Data :: map(), Algorithm :: algorithm(), KeyOrPem :: binary()) -> binary(). sign(Data, Algorithm, KeyOrPem) -> - sign(Data, Algorithm, KeyOrPem, #{}). + sign(Data, Algorithm, KeyOrPem, []). % @doc % Sign Data with the given Algorithm and KeyOrPem. @@ -47,22 +47,24 @@ sign(Data, Algorithm, KeyOrPem) -> % Token = jwerl:sign(#{key => <<"Hello World">>}, hs256, <<"s3cr3t k3y">>). % % @end --spec sign(Data :: map() | list(), Algorithm :: algorithm(), KeyOrPem :: binary(), Opts :: map()) -> binary(). -sign(Data, Algorithm, KeyOrPem, Opts) when (is_map(Data) orelse is_list(Data)), is_atom(Algorithm), is_binary(KeyOrPem), is_map(Opts) -> +-spec sign(Data :: map() | list(), Algorithm :: algorithm(), KeyOrPem :: binary(), Opts :: map() | list()) -> binary(). +sign(Data, Algorithm, KeyOrPem, Opts) when is_map(Opts) -> + sign(Data, Algorithm, KeyOrPem, maps:to_list(Opts)); +sign(Data, Algorithm, KeyOrPem, Opts) when (is_map(Data) orelse is_list(Data)), is_atom(Algorithm), is_binary(KeyOrPem), is_list(Opts) -> encode(jsx:encode(Data), config_headers(#{alg => algorithm_to_binary(Algorithm)}), KeyOrPem, Opts). -% @equiv verify(Data, <<"">>, hs256, #{}, #{}) +% @equiv verify(Data, <<"">>, hs256, #{}, []) verify(Data) -> - verify(Data, hs256, <<"">>, #{}, #{}). -% @equiv verify(Data, Algorithm, <<"">>, #{}, #{}) + verify(Data, hs256, <<"">>, #{}, []). +% @equiv verify(Data, Algorithm, <<"">>, #{}, []) verify(Data, Algorithm) -> - verify(Data, Algorithm, <<"">>, #{}, #{}). -% @equiv verify(Data, Algorithm, KeyOrPem, #{}, #{}) + verify(Data, Algorithm, <<"">>, #{}, []). +% @equiv verify(Data, Algorithm, KeyOrPem, #{}, []) verify(Data, Algorithm, KeyOrPem) -> - verify(Data, Algorithm, KeyOrPem, #{}, #{}). -% @equiv verify(Data, Algorithm, KeyOrPem, Claims, #{}) + verify(Data, Algorithm, KeyOrPem, #{}, []). +% @equiv verify(Data, Algorithm, KeyOrPem, Claims, []) verify(Data, Algorithm, KeyOrPem, Claims) -> - verify(Data, Algorithm, KeyOrPem, Claims, #{}). + verify(Data, Algorithm, KeyOrPem, Claims, []). % @doc % Verify a JWToken according to the given Algorithm, KeyOrPem and Claims. @@ -86,7 +88,10 @@ verify(Data, Algorithm, KeyOrPem, Claims) -> % @end -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) -> + +verify(Data, Algorithm, KeyOrPem, Claims, Opts) when is_map(Opts) -> + verify(Data, Algorithm, KeyOrPem, Claims, maps:to_list(Opts)); +verify(Data, Algorithm, KeyOrPem, Claims, Opts) when is_list(Opts) -> case decode(Data, KeyOrPem, Algorithm, Opts) of {ok, TokenData} when is_map(Claims) orelse is_list(Claims) -> case check_claims(TokenData, Claims, Opts) of @@ -112,8 +117,6 @@ verify(Data, Algorithm, KeyOrPem, Claims, Opts) -> header(Data) -> decode_header(Data). -check_claims(TokenData, Claims, Opts) when is_map(Opts) -> - check_claims(TokenData, Claims, maps:to_list(Opts)); check_claims(TokenData, Claims, Opts) when is_list(Opts) -> Now = os:system_time(seconds), claims_errors( @@ -244,7 +247,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 proplists:get_value(raw, Opts, false) of true -> raw_to_der(base64_decode(Signature0)); _ -> base64_decode(Signature0) end, @@ -264,7 +267,7 @@ 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 proplists:get_value(raw, Opts, false) of true -> der_to_raw(Signature0); _ -> diff --git a/test/jwerl_tests.erl b/test/jwerl_tests.erl index ab09b67..ded6d92 100644 --- a/test/jwerl_tests.erl +++ b/test/jwerl_tests.erl @@ -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,