diff --git a/lib/joken/error.ex b/lib/joken/error.ex index 9d14b2f..e26ef49 100644 --- a/lib/joken/error.ex +++ b/lib/joken/error.ex @@ -42,6 +42,13 @@ defmodule Joken.Error do Joken.Config.default_claims/1. """ + + def message(%__MODULE__{reason: :algorithm_needs_key}), + do: """ + A map was expected for the key parameter in the signer creation. + This is mandatory for: #{inspect(Signer.map_key_algorithms())}. + """ + def message(%__MODULE__{reason: :unrecognized_algorithm}), do: """ Couldn't recognize the signer algorithm. diff --git a/lib/joken/signer.ex b/lib/joken/signer.ex index aab862c..fd362a8 100644 --- a/lib/joken/signer.ex +++ b/lib/joken/signer.ex @@ -40,6 +40,13 @@ defmodule Joken.Signer do @spec algorithms() :: [binary()] def algorithms, do: @algorithms + + @doc """ + Map key algorithms. + """ + @spec map_key_algorithms() :: [binary()] + def map_key_algorithms, do: @map_key_algorithms + @doc """ Creates a new Joken.Signer struct. Can accept either a binary for HS*** algorithms or a map with arguments for the other kinds of keys. Also, accepts an optional map @@ -90,6 +97,8 @@ defmodule Joken.Signer do ) end + def create(alg, _key, _headers) when alg in @map_key_algorithms, do: raise(Joken.Error, :algorithm_needs_key) + def create(_, _, _), do: raise(Joken.Error, :unrecognized_algorithm) defp raw_create(alg, jws, jwk) do diff --git a/test/joken_signer_test.exs b/test/joken_signer_test.exs index d133798..2502b83 100644 --- a/test/joken_signer_test.exs +++ b/test/joken_signer_test.exs @@ -54,6 +54,12 @@ defmodule Joken.Signer.Test do } = signer end + test "raise with invalid parameter" do + assert_raise Error, Error.message(%Error{reason: :algorithm_needs_key}), fn -> + Signer.create("RS256", "Not a map") + end + end + test "raise with invalid algorithm" do assert_raise Error, Error.message(%Error{reason: :unrecognized_algorithm}), fn -> Signer.create("any algorithm", %{})