Skip to content

Commit

Permalink
Merge pull request #374 from alecostard/feat/hs-error-message
Browse files Browse the repository at this point in the history
handle non binary hmac sha key error
  • Loading branch information
victorolinasc authored Jan 29, 2023
2 parents c5abc11 + b7e0fd8 commit 05722ea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/joken/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ defmodule Joken.Error do
See `Joken.Config.validate/2` for more information on Context
"""

def message(%__MODULE__{reason: :algorithm_needs_binary_key}),
do: """
Couldn't create a signer because key is not binary.
HMAC SHA algorithms need a binary key.
"""

def message(%__MODULE__{reason: :wrong_key_parameters}),
do: """
Couldn't create a signer because there are missing parameters.
Expand Down
3 changes: 3 additions & 0 deletions lib/joken/signer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ defmodule Joken.Signer do
)
end

def create(alg, _key, _headers) when alg in @hs_algorithms,
do: raise(Joken.Error, :algorithm_needs_binary_key)

def create(alg, %{"pem" => pem}, headers) when alg in @map_key_algorithms do
raw_create(
alg,
Expand Down
6 changes: 6 additions & 0 deletions test/joken_signer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ defmodule Joken.Signer.Test do
end
end

test "raise when key is invalid" do
assert_raise Error, Error.message(%Error{reason: :algorithm_needs_binary_key}), fn ->
Signer.create("HS256", %{})
end
end

test "raise when parsing invalid algorithm from configuration" do
assert_raise Error, Error.message(%Error{reason: :unrecognized_algorithm}), fn ->
Signer.parse_config(:bad_algorithm)
Expand Down

0 comments on commit 05722ea

Please sign in to comment.