From f2450782eff0a4ac380cec160f58a198455182b2 Mon Sep 17 00:00:00 2001 From: emmanuel arvanitis Date: Tue, 28 Apr 2020 16:29:16 +0200 Subject: [PATCH 1/6] Adding error handling --- lib/joken/error.ex | 7 +++++++ lib/joken/signer.ex | 11 ++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/joken/error.ex b/lib/joken/error.ex index 9d14b2f..131da60 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: """ + Map key algorithms below need a map to be passed in the key parameter + #{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..68e65be 100644 --- a/lib/joken/signer.ex +++ b/lib/joken/signer.ex @@ -35,11 +35,18 @@ defmodule Joken.Signer do defstruct jwk: nil, jws: nil, alg: nil @doc """ - All supported algorithms. + all supported algorithms. """ @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 From 43bc5d365e159cc653c420aed28c16ba7e2f1378 Mon Sep 17 00:00:00 2001 From: emmanuel arvanitis Date: Tue, 28 Apr 2020 17:55:04 +0200 Subject: [PATCH 2/6] added test --- test/joken_signer_test.exs | 6 ++++++ 1 file changed, 6 insertions(+) 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", %{}) From 5120df12bccbce8b7325020dc0da31430b8eda9a Mon Sep 17 00:00:00 2001 From: emmanuel arvanitis Date: Tue, 28 Apr 2020 17:55:59 +0200 Subject: [PATCH 3/6] Fix accidental change --- lib/joken/signer.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/joken/signer.ex b/lib/joken/signer.ex index 68e65be..d28927e 100644 --- a/lib/joken/signer.ex +++ b/lib/joken/signer.ex @@ -35,14 +35,14 @@ defmodule Joken.Signer do defstruct jwk: nil, jws: nil, alg: nil @doc """ - all supported algorithms. + All supported algorithms. """ @spec algorithms() :: [binary()] def algorithms, do: @algorithms @doc """ - map key algorithms. + Map key algorithms. """ @spec map_key_algorithms() :: [binary()] def map_key_algorithms, do: @map_key_algorithms From 4b97b93b8591987f456ceb77b80ba4897f928a0d Mon Sep 17 00:00:00 2001 From: Emmanuel Arvanitis Date: Tue, 28 Apr 2020 18:05:26 +0200 Subject: [PATCH 4/6] Update lib/joken/signer.ex Prefix unused variables with underscores Co-Authored-By: Victor Oliveira Nascimento <376386+victorolinasc@users.noreply.github.com> --- lib/joken/signer.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/joken/signer.ex b/lib/joken/signer.ex index d28927e..fd362a8 100644 --- a/lib/joken/signer.ex +++ b/lib/joken/signer.ex @@ -97,7 +97,7 @@ 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(alg, _key, _headers) when alg in @map_key_algorithms, do: raise(Joken.Error, :algorithm_needs_key) def create(_, _, _), do: raise(Joken.Error, :unrecognized_algorithm) From 49b1af95d39c5d087bf7ef2c8d9d558e2c7ce98e Mon Sep 17 00:00:00 2001 From: Emmanuel Arvanitis Date: Tue, 28 Apr 2020 18:14:54 +0200 Subject: [PATCH 5/6] Update lib/joken/error.ex Fix phrasing Co-Authored-By: Victor Oliveira Nascimento <376386+victorolinasc@users.noreply.github.com> --- lib/joken/error.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/joken/error.ex b/lib/joken/error.ex index 131da60..730ad56 100644 --- a/lib/joken/error.ex +++ b/lib/joken/error.ex @@ -45,7 +45,8 @@ defmodule Joken.Error do def message(%__MODULE__{reason: :algorithm_needs_key}), do: """ - Map key algorithms below need a map to be passed in the key parameter + A map was expected for the key parameter in the signer creation. + This is mandatory for: #{inspect(Signer.map_key_algorithms())} #{inspect(Signer.map_key_algorithms())} """ From d9e009647137aa28dbe2c29688c13cec7e842261 Mon Sep 17 00:00:00 2001 From: Emmanuel Arvanitis Date: Tue, 28 Apr 2020 18:34:10 +0200 Subject: [PATCH 6/6] Update lib/joken/error.ex Co-Authored-By: Victor Oliveira Nascimento <376386+victorolinasc@users.noreply.github.com> --- lib/joken/error.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/joken/error.ex b/lib/joken/error.ex index 730ad56..e26ef49 100644 --- a/lib/joken/error.ex +++ b/lib/joken/error.ex @@ -46,8 +46,7 @@ defmodule Joken.Error do 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())} - #{inspect(Signer.map_key_algorithms())} + This is mandatory for: #{inspect(Signer.map_key_algorithms())}. """ def message(%__MODULE__{reason: :unrecognized_algorithm}),