Skip to content

Commit

Permalink
Allow to use PEM-s with params
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Nikitin committed Apr 4, 2019
1 parent fc6684b commit 31db8ef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/jwt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -318,5 +318,10 @@ append_claim(ClaimsSet, Key, Val) when is_map(ClaimsSet) ->
append_claim(ClaimsSet, Key, Val) -> [{ Key, Val } | ClaimsSet].

pem_to_key(Pem) ->
[Decoded] = public_key:pem_decode(Pem),
Decoded = case public_key:pem_decode(Pem) of
[_, Key] ->
Key;
[Key] ->
Key
end,
public_key:pem_entry_decode(Decoded).
8 changes: 8 additions & 0 deletions test/jwt_tests.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-module(jwt_tests).
-include_lib("eunit/include/eunit.hrl").
-include_lib("public_key/include/public_key.hrl").

-define(SECRET, <<"supas3cri7">>).

Expand Down Expand Up @@ -193,6 +194,10 @@ expired_token_test_() ->
[?_assertEqual({error, expired}, jwt:decode(ExpiredToken, ?SECRET)),
?_assertMatch({ok, #{<<"exp">> := _Exp }}, jwt:decode(UnexpiredToken, ?SECRET))].

pem_to_key_test() ->
Pem = ecdsa_private_with_params(),
Key = jwt:pem_to_key(Pem),
?assert(is_record(Key, 'ECPrivateKey')).

%%
%% Helpers
Expand All @@ -210,6 +215,9 @@ rsa_secret() ->
ecdsa_private_key() ->
from_pem_file("./test/pem/ecdsa_private.pem").

ecdsa_private_with_params() ->
from_pem_file("./test/pem/ecdsa_private_with_params.pem").

ecdsa_public_key() ->
from_pem_file("./test/pem/ecdsa_public.pem").

Expand Down
8 changes: 8 additions & 0 deletions test/pem/ecdsa_private_with_params.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-----BEGIN EC PARAMETERS-----
BggqhkjOPQMBBw==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIFilDgQ4JI27amkUh2DhgtsDCn0qrs4Ll9WRv1EstNX3oAoGCCqGSM49
AwEHoUQDQgAEC7TQXHee2nwrRH/heAXKwvKqev83xFnRP8iImwxO5RsYOe1kT5mW
FywRgeEStB6eVxahfDJCS35jDBF46Akm6Q==
-----END EC PRIVATE KEY-----

0 comments on commit 31db8ef

Please sign in to comment.