mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
mix format
This commit is contained in:
parent
36cbdff953
commit
11cac54ba0
3 changed files with 35 additions and 24 deletions
|
@ -26,18 +26,21 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlug do
|
||||||
@persistent_cookie_signing_salt Atom.to_string(PowPersistentSession.Plug.Cookie)
|
@persistent_cookie_signing_salt Atom.to_string(PowPersistentSession.Plug.Cookie)
|
||||||
|
|
||||||
def init(:load), do: :load
|
def init(:load), do: :load
|
||||||
|
|
||||||
def init(:pow_session) do
|
def init(:pow_session) do
|
||||||
[
|
[
|
||||||
fetch_token: &__MODULE__.client_store_fetch_session/1,
|
fetch_token: &__MODULE__.client_store_fetch_session/1,
|
||||||
namespace: :session
|
namespace: :session
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
def init(:pow_persistent_session) do
|
def init(:pow_persistent_session) do
|
||||||
[
|
[
|
||||||
fetch_token: &__MODULE__.client_store_fetch_persistent_cookie/1,
|
fetch_token: &__MODULE__.client_store_fetch_persistent_cookie/1,
|
||||||
namespace: :persistent_session
|
namespace: :persistent_session
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
def init({type, opts}) do
|
def init({type, opts}) do
|
||||||
type
|
type
|
||||||
|> init()
|
|> init()
|
||||||
|
@ -49,9 +52,10 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlug do
|
||||||
maybe_load_from_cache(conn, Plug.current_user(conn), opts)
|
maybe_load_from_cache(conn, Plug.current_user(conn), opts)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(conn, opts) do
|
def call(conn, opts) do
|
||||||
fetch_fn = Keyword.fetch!(opts, :fetch_token)
|
fetch_fn = Keyword.fetch!(opts, :fetch_token)
|
||||||
token = fetch_fn.(conn)
|
token = fetch_fn.(conn)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_opts_in_private(opts)
|
|> put_opts_in_private(opts)
|
||||||
|
@ -64,10 +68,11 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlug do
|
||||||
fetch_fn = Keyword.fetch!(opts, :fetch_token)
|
fetch_fn = Keyword.fetch!(opts, :fetch_token)
|
||||||
|
|
||||||
case fetch_fn.(conn) do
|
case fetch_fn.(conn) do
|
||||||
nil -> conn
|
nil -> conn
|
||||||
token -> load_from_cache(conn, token, opts)
|
token -> load_from_cache(conn, token, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp maybe_load_from_cache(conn, _any, _opts), do: conn
|
defp maybe_load_from_cache(conn, _any, _opts), do: conn
|
||||||
|
|
||||||
defp put_opts_in_private(conn, opts) do
|
defp put_opts_in_private(conn, opts) do
|
||||||
|
@ -78,12 +83,13 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlug do
|
||||||
|
|
||||||
defp maybe_put_cache(conn, nil, _old_token, _opts), do: conn
|
defp maybe_put_cache(conn, nil, _old_token, _opts), do: conn
|
||||||
defp maybe_put_cache(conn, _user, nil, _opts), do: conn
|
defp maybe_put_cache(conn, _user, nil, _opts), do: conn
|
||||||
|
|
||||||
defp maybe_put_cache(conn, user, old_token, opts) do
|
defp maybe_put_cache(conn, user, old_token, opts) do
|
||||||
fetch_fn = Keyword.fetch!(opts, :fetch_token)
|
fetch_fn = Keyword.fetch!(opts, :fetch_token)
|
||||||
|
|
||||||
case fetch_fn.(conn) do
|
case fetch_fn.(conn) do
|
||||||
^old_token -> conn
|
^old_token -> conn
|
||||||
_token -> put_cache(conn, user, old_token, opts)
|
_token -> put_cache(conn, user, old_token, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -96,12 +102,12 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlug do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp load_from_cache(conn, token, opts) do
|
defp load_from_cache(conn, token, opts) do
|
||||||
config = Plug.fetch_config(conn)
|
config = Plug.fetch_config(conn)
|
||||||
{store, store_config} = invalidated_cache(conn, opts)
|
{store, store_config} = invalidated_cache(conn, opts)
|
||||||
|
|
||||||
case store.get(store_config, token) do
|
case store.get(store_config, token) do
|
||||||
:not_found -> conn
|
:not_found -> conn
|
||||||
user -> Plug.assign_current_user(conn, user, config)
|
user -> Plug.assign_current_user(conn, user, config)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -113,7 +119,7 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlug do
|
||||||
|> Conn.fetch_session()
|
|> Conn.fetch_session()
|
||||||
|
|
||||||
with session_id when is_binary(session_id) <- Conn.get_session(conn, @session_key),
|
with session_id when is_binary(session_id) <- Conn.get_session(conn, @session_key),
|
||||||
{:ok, session_id} <- Plug.verify_token(conn, @session_signing_salt, session_id) do
|
{:ok, session_id} <- Plug.verify_token(conn, @session_signing_salt, session_id) do
|
||||||
session_id
|
session_id
|
||||||
else
|
else
|
||||||
_any -> nil
|
_any -> nil
|
||||||
|
@ -128,29 +134,28 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlug do
|
||||||
|> Conn.fetch_cookies()
|
|> Conn.fetch_cookies()
|
||||||
|
|
||||||
with token when is_binary(token) <- conn.cookies[@persistent_cookie_key],
|
with token when is_binary(token) <- conn.cookies[@persistent_cookie_key],
|
||||||
{:ok, token} <- Plug.verify_token(conn, @persistent_cookie_signing_salt, token) do
|
{:ok, token} <- Plug.verify_token(conn, @persistent_cookie_signing_salt, token) do
|
||||||
token
|
token
|
||||||
else
|
else
|
||||||
_any -> nil
|
_any -> nil
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp invalidated_cache(conn, opts) do
|
defp invalidated_cache(conn, opts) do
|
||||||
store_config = store_config(opts)
|
store_config = store_config(opts)
|
||||||
config = Plug.fetch_config(conn)
|
config = Plug.fetch_config(conn)
|
||||||
store = Config.get(config, :cache_store_backend, EtsCache)
|
store = Config.get(config, :cache_store_backend, EtsCache)
|
||||||
|
|
||||||
{store, store_config}
|
{store, store_config}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp store_config(opts) do
|
defp store_config(opts) do
|
||||||
namespace = Keyword.fetch!(opts, :namespace)
|
namespace = Keyword.fetch!(opts, :namespace)
|
||||||
ttl = Keyword.get(opts, :ttl, @store_ttl)
|
ttl = Keyword.get(opts, :ttl, @store_ttl)
|
||||||
|
|
||||||
[
|
[
|
||||||
ttl: ttl,
|
ttl: ttl,
|
||||||
namespace: "invalidated_#{namespace}",
|
namespace: "invalidated_#{namespace}"
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -103,9 +103,8 @@ defmodule Search.DateParser do
|
||||||
{[[lower, upper]], context}
|
{[[lower, upper]], context}
|
||||||
|
|
||||||
_false ->
|
_false ->
|
||||||
{:error, "invalid date format in input; requested time #{
|
{:error,
|
||||||
count*scale
|
"invalid date format in input; requested time #{count * scale} seconds is over a millenium ago"}
|
||||||
} seconds is over a millenium ago"}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -19,21 +19,25 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlugTest do
|
||||||
setup do
|
setup do
|
||||||
user =
|
user =
|
||||||
%User{authentication_token: "token", name: "John Doe", slug: "john-doe"}
|
%User{authentication_token: "token", name: "John Doe", slug: "john-doe"}
|
||||||
|> User.changeset(%{"email" => "test@example.com", "password" => "password", "password_confirmation" => "password"})
|
|> User.changeset(%{
|
||||||
|
"email" => "test@example.com",
|
||||||
|
"password" => "password",
|
||||||
|
"password_confirmation" => "password"
|
||||||
|
})
|
||||||
|> Repo.insert!()
|
|> Repo.insert!()
|
||||||
|
|
||||||
{:ok, user: user}
|
{:ok, user: user}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "call/2 session id is reusable for short amount of time", %{conn: init_conn, user: user} do
|
test "call/2 session id is reusable for short amount of time", %{conn: init_conn, user: user} do
|
||||||
config = Keyword.put(@config, :session_ttl_renewal, 0)
|
config = Keyword.put(@config, :session_ttl_renewal, 0)
|
||||||
init_conn = prepare_session_conn(init_conn, user, config)
|
init_conn = prepare_session_conn(init_conn, user, config)
|
||||||
|
|
||||||
assert session_id =
|
assert session_id =
|
||||||
init_conn
|
init_conn
|
||||||
|> init_session_plug()
|
|> init_session_plug()
|
||||||
|> Conn.fetch_session()
|
|> Conn.fetch_session()
|
||||||
|> Conn.get_session(@session_key)
|
|> Conn.get_session(@session_key)
|
||||||
|
|
||||||
conn = run_plug(init_conn, config)
|
conn = run_plug(init_conn, config)
|
||||||
|
|
||||||
|
@ -84,8 +88,12 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlugTest do
|
||||||
defp init_plug(conn, config) do
|
defp init_plug(conn, config) do
|
||||||
conn
|
conn
|
||||||
|> init_session_plug()
|
|> init_session_plug()
|
||||||
|> PowInvalidatedSessionPlug.call(PowInvalidatedSessionPlug.init({:pow_session, ttl: @invalidated_ttl}))
|
|> PowInvalidatedSessionPlug.call(
|
||||||
|> PowInvalidatedSessionPlug.call(PowInvalidatedSessionPlug.init({:pow_persistent_session, ttl: @invalidated_ttl}))
|
PowInvalidatedSessionPlug.init({:pow_session, ttl: @invalidated_ttl})
|
||||||
|
)
|
||||||
|
|> PowInvalidatedSessionPlug.call(
|
||||||
|
PowInvalidatedSessionPlug.init({:pow_persistent_session, ttl: @invalidated_ttl})
|
||||||
|
)
|
||||||
|> Session.call(Session.init(config))
|
|> Session.call(Session.init(config))
|
||||||
|> Cookie.call(Cookie.init([]))
|
|> Cookie.call(Cookie.init([]))
|
||||||
|> PowInvalidatedSessionPlug.call(PowInvalidatedSessionPlug.init(:load))
|
|> PowInvalidatedSessionPlug.call(PowInvalidatedSessionPlug.init(:load))
|
||||||
|
@ -129,7 +137,6 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlugTest do
|
||||||
|> Conn.send_resp(200, "")
|
|> Conn.send_resp(200, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
defp create_session(conn, user, config) do
|
defp create_session(conn, user, config) do
|
||||||
conn
|
conn
|
||||||
|> init_plug(config)
|
|> init_plug(config)
|
||||||
|
|
Loading…
Reference in a new issue