Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding error handling #277

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/joken/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
earv1 marked this conversation as resolved.
Show resolved Hide resolved
#{inspect(Signer.map_key_algorithms())}
"""

def message(%__MODULE__{reason: :unrecognized_algorithm}),
do: """
Couldn't recognize the signer algorithm.
Expand Down
9 changes: 9 additions & 0 deletions lib/joken/signer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
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 @@ -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", %{})
Expand Down