Skip to content

Commit 49702bc

Browse files
authored
Permit atom keys when verifying claims with EnsureAuthenticated (#696)
* 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 <[email protected]>
1 parent 882c90b commit 49702bc

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v2.2.2
4+
5+
### Enhancement
6+
7+
* `Guardian.Plug.EnsureAuthenticated` will now accept atom keys in the map passed to the `claims` option.
8+
39
## v2.2.1
410

511
### Enhancement

lib/guardian/plug/ensure_authenticated.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ if Code.ensure_loaded?(Plug) do
7171

7272
@spec verify_claims(Guardian.Token.claims(), Keyword.t()) :: {:ok, Guardian.Token.claims()} | {:error, any}
7373
defp verify_claims(claims, opts) do
74-
to_check = Keyword.get(opts, :claims)
74+
to_check = opts |> Keyword.get(:claims) |> Guardian.stringify_keys()
7575
Guardian.Token.Verify.verify_literal_claims(claims, to_check, opts)
7676
end
7777
end

lib/guardian/token/verify.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ defmodule Guardian.Token.Verify do
8888
end
8989
end
9090

91-
@spec verify_literal_claims(map(), binary(), [binary()] | binary()) ::
91+
@spec verify_literal_claim(map(), binary(), [binary()] | binary()) ::
9292
{:ok, [binary()] | binary()} | {:error, binary()}
9393
defp verify_literal_claim(claims, key, value) do
9494
claim_value = Map.get(claims, key)

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule Guardian.Mixfile do
22
@moduledoc false
33
use Mix.Project
44

5-
@version "2.2.1"
5+
@version "2.2.2"
66
@url "https://github.com/ueberauth/guardian"
77
@maintainers [
88
"Daniel Neighman",

test/guardian/plug/ensure_authenticated_test.exs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ defmodule Guardian.Plug.EnsureAuthenticatedTest do
3737
setup do
3838
impl = Impl
3939
handler = Handler
40-
{:ok, token, claims} = Impl.encode_and_sign(@resource)
40+
{:ok, token, claims} = Impl.encode_and_sign(@resource, %{custom: true})
4141
{:ok, %{claims: claims, conn: conn(:get, "/"), token: token, impl: impl, handler: handler}}
4242
end
4343

@@ -82,7 +82,33 @@ defmodule Guardian.Plug.EnsureAuthenticatedTest do
8282

8383
assert conn.halted
8484
assert conn.status == 401
85-
assert {401, _, "{:unauthenticated, :no}"} = sent_resp(conn)
85+
assert {401, _, "{:unauthenticated, \"no\"}"} = sent_resp(conn)
86+
end
87+
88+
test "allows the plug to continue if the claims do match, with atom keys", ctx do
89+
conn =
90+
EnsureAuthenticated.call(
91+
ctx.conn,
92+
module: ctx.impl,
93+
error_handler: ctx.handler,
94+
claims: %{custom: true}
95+
)
96+
97+
refute conn.halted
98+
refute conn.status == 401
99+
end
100+
101+
test "allows the plug to continue if the claims do match, with string keys", ctx do
102+
conn =
103+
EnsureAuthenticated.call(
104+
ctx.conn,
105+
module: ctx.impl,
106+
error_handler: ctx.handler,
107+
claims: %{"custom" => true}
108+
)
109+
110+
refute conn.halted
111+
refute conn.status == 401
86112
end
87113
end
88114
end

0 commit comments

Comments
 (0)