Skip to content

Commit

Permalink
Merge pull request #41 from sirihansen/dont-use-deprecated-crypto-fun…
Browse files Browse the repository at this point in the history
…ction

Don't use deprecated crypto:hmac/3
  • Loading branch information
artemeff authored Oct 28, 2020
2 parents 6b3c413 + d445dcc commit b77cbb7
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/jwt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
-define(HOUR, 3600).
-define(DAY, (?HOUR * 24)).

%% Handle version compatibility for crypto
-ifdef(OTP_RELEASE).
-if(?OTP_RELEASE >= 23).
-define(HMAC(Type, Key, Data), crypto:mac(hmac, Type, Key, Data)).
-else.
-define(HMAC(Type, Key, Data), crypto:hmac(Type, Key, Data)).
-endif.
-else.
-define(HMAC(Type, Key, Data), crypto:hmac(Type, Key, Data)).
-endif.

-type expiration() :: {hourly, non_neg_integer()} | {daily, non_neg_integer()} | non_neg_integer().
-type context() :: map().

Expand Down Expand Up @@ -271,7 +282,7 @@ jwt_sign(Alg, Payload, Key) ->
jwt_sign_with_crypto(algorithm_to_crypto(Alg), Payload, Key).

jwt_sign_with_crypto({hmac, Crypto}, Payload, Key) ->
base64url:encode(crypto:hmac(Crypto, Key, Payload));
base64url:encode(?HMAC(Crypto, Key, Payload));

jwt_sign_with_crypto({Algo, Crypto}, Payload, Pem)
when (Algo =:= rsa orelse Algo =:= ecdsa) andalso is_binary(Pem) ->
Expand Down

0 comments on commit b77cbb7

Please sign in to comment.