Skip to content

Commit 9baef5b

Browse files
authored
fix: try refreshing cookie on multiple cases (#683)
* fix: try refreshing cookie on multiple cases * chore: add test cases and update changelog * chore: update changelog with missing feature
1 parent 4600c78 commit 9baef5b

File tree

5 files changed

+73
-35
lines changed

5 files changed

+73
-35
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
# Changelog
22

3+
## v2.2.1
4+
5+
### Enhancement
6+
7+
* `Guardian.Plug.VerifyHeader` and `Guardian.Plug.VerifySession` `:refresh_from_cookie` option will try refreshing
8+
when access token not found, invalid or expired if cookie present [#683](https://github.com/ueberauth/guardian/pull/683)
9+
310
## v2.2.0
411

512
### Enhancement
613

714
* Add `:scheme` option to `Guardian.Plug.VerifyHeader` [#680](https://github.com/ueberauth/guardian/pull/680)
15+
* Add `:refresh_from_cookie` option to `Guardian.Plug.VerifyHeader` and `Guardian.Plug.VerifySession`
16+
to replace `Guardian.Plug.VerifyCookie` plug [#675](https://github.com/ueberauth/guardian/pull/675)
817

918
### Deprecation
1019

11-
* `:realm` option configuration of `Guardian.Plug.VerifyHeader` is deprecated
20+
* `:realm` option configuration of `Guardian.Plug.VerifyHeader` is deprecated
1221
please use `:scheme` instead.
22+
* `Guardian.Plug.VerifyCookie` is deprecated in favor of `:refresh_from_cookie` option in
23+
`Guardian.Plug.VerifyHeader` and `Guardian.Plug.VerifySession`
1324

1425
## v2.1.2
1526
### Enhancement

lib/guardian/plug/verify_header.ex

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,8 @@ if Code.ensure_loaded?(Plug) do
9494
|> Guardian.Plug.put_current_token(token, key: key)
9595
|> Guardian.Plug.put_current_claims(claims, key: key)
9696
else
97-
:no_token_found ->
98-
conn
99-
100-
{:error, reason} ->
101-
handle_error(conn, reason, opts)
102-
103-
_ ->
104-
conn
97+
error ->
98+
handle_error(conn, error, opts)
10599
end
106100
end
107101

@@ -127,25 +121,25 @@ if Code.ensure_loaded?(Plug) do
127121
end
128122
end
129123

130-
defp handle_error(conn, :token_expired = reason, opts) do
124+
defp handle_error(conn, error, opts) do
131125
if refresh_from_cookie_opts = fetch_refresh_from_cookie_options(opts) do
132126
Guardian.Plug.VerifyCookie.refresh_from_cookie(conn, refresh_from_cookie_opts)
133127
else
134-
apply_error(conn, reason, opts)
128+
apply_error(conn, error, opts)
135129
end
136130
end
137131

138-
defp handle_error(conn, reason, opts) do
139-
apply_error(conn, reason, opts)
140-
end
141-
142-
defp apply_error(conn, reason, opts) do
132+
defp apply_error(conn, {:error, reason}, opts) do
143133
conn
144134
|> Pipeline.fetch_error_handler!(opts)
145135
|> apply(:auth_error, [conn, {:invalid_token, reason}, opts])
146136
|> Guardian.Plug.maybe_halt(opts)
147137
end
148138

139+
defp apply_error(conn, _, _) do
140+
conn
141+
end
142+
149143
defp fetch_refresh_from_cookie_options(opts) do
150144
case Keyword.get(opts, :refresh_from_cookie) do
151145
value when is_list(value) -> value

lib/guardian/plug/verify_session.ex

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,36 +73,30 @@ if Code.ensure_loaded?(Plug) do
7373
|> Guardian.Plug.put_current_token(token, key: key)
7474
|> Guardian.Plug.put_current_claims(claims, key: key)
7575
else
76-
:no_token_found ->
77-
conn
78-
79-
{:error, reason} ->
80-
handle_error(conn, reason, opts)
81-
82-
_ ->
83-
conn
76+
error ->
77+
handle_error(conn, error, opts)
8478
end
8579
end
8680

87-
defp handle_error(conn, :token_expired = reason, opts) do
81+
defp handle_error(conn, error, opts) do
8882
if refresh_from_cookie_opts = fetch_refresh_from_cookie_options(opts) do
8983
Guardian.Plug.VerifyCookie.refresh_from_cookie(conn, refresh_from_cookie_opts)
9084
else
91-
apply_error(conn, reason, opts)
85+
apply_error(conn, error, opts)
9286
end
9387
end
9488

95-
defp handle_error(conn, reason, opts) do
96-
apply_error(conn, reason, opts)
97-
end
98-
99-
defp apply_error(conn, reason, opts) do
89+
defp apply_error(conn, {:error, reason}, opts) do
10090
conn
10191
|> Pipeline.fetch_error_handler!(opts)
10292
|> apply(:auth_error, [conn, {:invalid_token, reason}, opts])
10393
|> Guardian.Plug.maybe_halt(opts)
10494
end
10595

96+
defp apply_error(conn, _, _) do
97+
conn
98+
end
99+
106100
defp fetch_refresh_from_cookie_options(opts) do
107101
case Keyword.get(opts, :refresh_from_cookie) do
108102
value when is_list(value) -> value

test/guardian/plug/verify_header_test.exs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,27 @@ defmodule Guardian.Plug.VerifyHeaderTest do
305305
|> Pipeline.put_error_handler(ctx.handler)
306306
|> VerifyHeader.call(refresh_from_cookie: [module: ctx.impl])
307307

308-
assert conn.status == 401
309-
assert conn.halted
308+
refute conn.halted
309+
assert new_access_token = Guardian.Plug.current_token(conn)
310+
assert {:ok, _} = apply(ctx.impl, :decode_and_verify, [new_access_token])
311+
assert %{"sub" => "User:jane", "typ" => "access"} = Guardian.Plug.current_claims(conn)
312+
end
313+
314+
test "when no header found", ctx do
315+
{:ok, refresh_token, _} = apply(ctx.impl, :encode_and_sign, [%{id: "jane"}, %{}, [token_type: "refresh"]])
316+
317+
conn =
318+
:get
319+
|> conn("/")
320+
|> put_req_cookie("guardian_default_token", refresh_token)
321+
|> Pipeline.put_module(ctx.impl)
322+
|> Pipeline.put_error_handler(ctx.handler)
323+
|> VerifyHeader.call(refresh_from_cookie: [module: ctx.impl])
324+
325+
refute conn.halted
326+
assert new_access_token = Guardian.Plug.current_token(conn)
327+
assert {:ok, _} = apply(ctx.impl, :decode_and_verify, [new_access_token])
328+
assert %{"sub" => "User:jane", "typ" => "access"} = Guardian.Plug.current_claims(conn)
310329
end
311330
end
312331
end

test/guardian/plug/verify_session_test.exs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,28 @@ defmodule Guardian.Plug.VerifySessionTest do
312312
|> Pipeline.put_error_handler(ctx.handler)
313313
|> VerifySession.call(refresh_from_cookie: [])
314314

315-
assert conn.status == 401
316-
assert conn.halted
315+
refute conn.halted
316+
assert new_access_token = Guardian.Plug.current_token(conn)
317+
assert {:ok, _} = apply(ctx.impl, :decode_and_verify, [new_access_token])
318+
assert %{"sub" => "User:jane", "typ" => "access"} = Guardian.Plug.current_claims(conn)
319+
end
320+
321+
test "when no session found", ctx do
322+
{:ok, refresh_token, _} = apply(ctx.impl, :encode_and_sign, [%{id: "jane"}, %{}, [token_type: "refresh"]])
323+
324+
conn =
325+
:get
326+
|> conn("/")
327+
|> put_req_cookie("guardian_default_token", refresh_token)
328+
|> init_test_session(%{})
329+
|> Pipeline.put_module(ctx.impl)
330+
|> Pipeline.put_error_handler(ctx.handler)
331+
|> VerifySession.call(refresh_from_cookie: [])
332+
333+
refute conn.halted
334+
assert new_access_token = Guardian.Plug.current_token(conn)
335+
assert {:ok, _} = apply(ctx.impl, :decode_and_verify, [new_access_token])
336+
assert %{"sub" => "User:jane", "typ" => "access"} = Guardian.Plug.current_claims(conn)
317337
end
318338
end
319339
end

0 commit comments

Comments
 (0)