Skip to content

Commit

Permalink
Permit atom keys when verifying claims with EnsureAuthenticated (#696)
Browse files Browse the repository at this point in the history
* Fix spec function name typo

* Ensure that claim keys and stringified in EnsureAuthenticated

* Satisfy Credo

* Bump version and update changelog

Co-authored-by: Paul Dann <pgdann@gmail.com>
  • Loading branch information
giddie and giddie committed Mar 21, 2022
1 parent 882c90b commit 49702bc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog

## v2.2.2

### Enhancement

* `Guardian.Plug.EnsureAuthenticated` will now accept atom keys in the map passed to the `claims` option.

## v2.2.1

### Enhancement
Expand Down
2 changes: 1 addition & 1 deletion lib/guardian/plug/ensure_authenticated.ex
Expand Up @@ -71,7 +71,7 @@ if Code.ensure_loaded?(Plug) do

@spec verify_claims(Guardian.Token.claims(), Keyword.t()) :: {:ok, Guardian.Token.claims()} | {:error, any}
defp verify_claims(claims, opts) do
to_check = Keyword.get(opts, :claims)
to_check = opts |> Keyword.get(:claims) |> Guardian.stringify_keys()
Guardian.Token.Verify.verify_literal_claims(claims, to_check, opts)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/guardian/token/verify.ex
Expand Up @@ -88,7 +88,7 @@ defmodule Guardian.Token.Verify do
end
end

@spec verify_literal_claims(map(), binary(), [binary()] | binary()) ::
@spec verify_literal_claim(map(), binary(), [binary()] | binary()) ::
{:ok, [binary()] | binary()} | {:error, binary()}
defp verify_literal_claim(claims, key, value) do
claim_value = Map.get(claims, key)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Expand Up @@ -2,7 +2,7 @@ defmodule Guardian.Mixfile do
@moduledoc false
use Mix.Project

@version "2.2.1"
@version "2.2.2"
@url "https://github.com/ueberauth/guardian"
@maintainers [
"Daniel Neighman",
Expand Down
30 changes: 28 additions & 2 deletions test/guardian/plug/ensure_authenticated_test.exs
Expand Up @@ -37,7 +37,7 @@ defmodule Guardian.Plug.EnsureAuthenticatedTest do
setup do
impl = Impl
handler = Handler
{:ok, token, claims} = Impl.encode_and_sign(@resource)
{:ok, token, claims} = Impl.encode_and_sign(@resource, %{custom: true})
{:ok, %{claims: claims, conn: conn(:get, "/"), token: token, impl: impl, handler: handler}}
end

Expand Down Expand Up @@ -82,7 +82,33 @@ defmodule Guardian.Plug.EnsureAuthenticatedTest do

assert conn.halted
assert conn.status == 401
assert {401, _, "{:unauthenticated, :no}"} = sent_resp(conn)
assert {401, _, "{:unauthenticated, \"no\"}"} = sent_resp(conn)
end

test "allows the plug to continue if the claims do match, with atom keys", ctx do
conn =
EnsureAuthenticated.call(
ctx.conn,
module: ctx.impl,
error_handler: ctx.handler,
claims: %{custom: true}
)

refute conn.halted
refute conn.status == 401
end

test "allows the plug to continue if the claims do match, with string keys", ctx do
conn =
EnsureAuthenticated.call(
ctx.conn,
module: ctx.impl,
error_handler: ctx.handler,
claims: %{"custom" => true}
)

refute conn.halted
refute conn.status == 401
end
end
end

0 comments on commit 49702bc

Please sign in to comment.