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

use exp/ttl in remember_me plug #649

Merged
merged 3 commits into from
May 17, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 11 additions & 4 deletions lib/guardian/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if Code.ensure_loaded?(Plug) do
end

@default_key "default"
@default_cookie_options [max_age: 60 * 60 * 24 * 7 * 4]
@default_cookie_max_age [max_age: 60 * 60 * 24 * 7 * 4]

import Guardian, only: [returning_tuple: 1]
import Guardian.Plug.Keys
Expand Down Expand Up @@ -255,6 +255,13 @@ if Code.ensure_loaded?(Plug) do
put_resp_cookie(conn, key, token, opts)
end

@doc """
Sets a token of type refresh directly on a cookie
The max_age of the cookie till be the expire time of the Token, if available
If the token does not have an exp,t the default will be 30 days.
The max age can be overridden by setting the cookie option config.
"""

@spec remember_me(Plug.Conn.t(), module, any, Guardian.Token.claims(), Guardian.options()) :: Plug.Conn.t()
def remember_me(conn, mod, resource, claims \\ %{}, opts \\ []) do
opts = Keyword.put_new(opts, :token_type, "refresh")
Expand Down Expand Up @@ -317,11 +324,11 @@ if Code.ensure_loaded?(Plug) do

defp cookie_options(mod, %{"exp" => timestamp}) do
max_age = timestamp - Guardian.timestamp()
[max_age: max_age] ++ cookie_options(mod, %{})
Keyword.merge([max_age: max_age], mod.config(:cookie_options, []))
end

defp cookie_options(mod, _claims) do
Keyword.merge(@default_cookie_options, mod.config(:cookie_options, []))
defp cookie_options(mod, _) do
Keyword.merge(@default_cookie_max_age, mod.config(:cookie_options, []))
end

defp add_data_to_conn(conn, resource, token, claims, opts) do
Expand Down
26 changes: 26 additions & 0 deletions test/guardian/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,32 @@ defmodule Guardian.PlugTest do
assert gather_function_calls() == expected
end

test "it creates a cookie with the default token and key and a exp in claimss ", ctx do
conn = ctx.conn

exp = Guardian.timestamp() + 10000

assert %Plug.Conn{} = xconn = Guardian.Plug.remember_me(conn, ctx.impl, @resource, %{exp: exp}, [])

assert Map.has_key?(xconn.resp_cookies, "guardian_default_token")
%{value: token, max_age: max_age} = Map.get(xconn.resp_cookies, "guardian_default_token")

# default max age
assert max_age <= 10000
assert token

claims = %{"sub" => @resource.id, "typ" => "refresh", "exp" => exp}
ops = [token_type: "refresh"]

expected = [
{ctx.impl, :subject_for_token, [@resource, %{"exp" => exp}]},
{Guardian.Support.TokenModule, :build_claims, [ctx.impl, @resource, "bobby", %{"exp" => exp}, ops]},
{Guardian.Support.TokenModule, :create_token, [ctx.impl, claims, ops]}
]

assert gather_function_calls() == expected
end

test "it creates a cookie with a default token and custom key", ctx do
conn = ctx.conn

Expand Down