mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +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)
|
||||
|
||||
def init(:load), do: :load
|
||||
|
||||
def init(:pow_session) do
|
||||
[
|
||||
fetch_token: &__MODULE__.client_store_fetch_session/1,
|
||||
namespace: :session
|
||||
]
|
||||
end
|
||||
|
||||
def init(:pow_persistent_session) do
|
||||
[
|
||||
fetch_token: &__MODULE__.client_store_fetch_persistent_cookie/1,
|
||||
namespace: :persistent_session
|
||||
]
|
||||
end
|
||||
|
||||
def init({type, opts}) do
|
||||
type
|
||||
|> init()
|
||||
|
@ -49,9 +52,10 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlug do
|
|||
maybe_load_from_cache(conn, Plug.current_user(conn), opts)
|
||||
end)
|
||||
end
|
||||
|
||||
def call(conn, opts) do
|
||||
fetch_fn = Keyword.fetch!(opts, :fetch_token)
|
||||
token = fetch_fn.(conn)
|
||||
token = fetch_fn.(conn)
|
||||
|
||||
conn
|
||||
|> put_opts_in_private(opts)
|
||||
|
@ -64,10 +68,11 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlug do
|
|||
fetch_fn = Keyword.fetch!(opts, :fetch_token)
|
||||
|
||||
case fetch_fn.(conn) do
|
||||
nil -> conn
|
||||
nil -> conn
|
||||
token -> load_from_cache(conn, token, opts)
|
||||
end
|
||||
end
|
||||
|
||||
defp maybe_load_from_cache(conn, _any, _opts), do: conn
|
||||
|
||||
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, _user, nil, _opts), do: conn
|
||||
|
||||
defp maybe_put_cache(conn, user, old_token, opts) do
|
||||
fetch_fn = Keyword.fetch!(opts, :fetch_token)
|
||||
|
||||
case fetch_fn.(conn) do
|
||||
^old_token -> conn
|
||||
_token -> put_cache(conn, user, old_token, opts)
|
||||
_token -> put_cache(conn, user, old_token, opts)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -96,12 +102,12 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlug do
|
|||
end
|
||||
|
||||
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)
|
||||
|
||||
case store.get(store_config, token) do
|
||||
:not_found -> conn
|
||||
user -> Plug.assign_current_user(conn, user, config)
|
||||
user -> Plug.assign_current_user(conn, user, config)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -113,7 +119,7 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlug do
|
|||
|> Conn.fetch_session()
|
||||
|
||||
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
|
||||
else
|
||||
_any -> nil
|
||||
|
@ -128,29 +134,28 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlug do
|
|||
|> Conn.fetch_cookies()
|
||||
|
||||
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
|
||||
else
|
||||
_any -> nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
defp invalidated_cache(conn, opts) do
|
||||
store_config = store_config(opts)
|
||||
config = Plug.fetch_config(conn)
|
||||
store = Config.get(config, :cache_store_backend, EtsCache)
|
||||
config = Plug.fetch_config(conn)
|
||||
store = Config.get(config, :cache_store_backend, EtsCache)
|
||||
|
||||
{store, store_config}
|
||||
end
|
||||
|
||||
defp store_config(opts) do
|
||||
namespace = Keyword.fetch!(opts, :namespace)
|
||||
ttl = Keyword.get(opts, :ttl, @store_ttl)
|
||||
ttl = Keyword.get(opts, :ttl, @store_ttl)
|
||||
|
||||
[
|
||||
ttl: ttl,
|
||||
namespace: "invalidated_#{namespace}",
|
||||
namespace: "invalidated_#{namespace}"
|
||||
]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -103,9 +103,8 @@ defmodule Search.DateParser do
|
|||
{[[lower, upper]], context}
|
||||
|
||||
_false ->
|
||||
{:error, "invalid date format in input; requested time #{
|
||||
count*scale
|
||||
} seconds is over a millenium ago"}
|
||||
{:error,
|
||||
"invalid date format in input; requested time #{count * scale} seconds is over a millenium ago"}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -19,21 +19,25 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlugTest do
|
|||
setup do
|
||||
user =
|
||||
%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!()
|
||||
|
||||
{:ok, user: user}
|
||||
end
|
||||
|
||||
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)
|
||||
|
||||
assert session_id =
|
||||
init_conn
|
||||
|> init_session_plug()
|
||||
|> Conn.fetch_session()
|
||||
|> Conn.get_session(@session_key)
|
||||
init_conn
|
||||
|> init_session_plug()
|
||||
|> Conn.fetch_session()
|
||||
|> Conn.get_session(@session_key)
|
||||
|
||||
conn = run_plug(init_conn, config)
|
||||
|
||||
|
@ -84,8 +88,12 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlugTest do
|
|||
defp init_plug(conn, config) do
|
||||
conn
|
||||
|> init_session_plug()
|
||||
|> PowInvalidatedSessionPlug.call(PowInvalidatedSessionPlug.init({:pow_session, ttl: @invalidated_ttl}))
|
||||
|> PowInvalidatedSessionPlug.call(PowInvalidatedSessionPlug.init({:pow_persistent_session, ttl: @invalidated_ttl}))
|
||||
|> PowInvalidatedSessionPlug.call(
|
||||
PowInvalidatedSessionPlug.init({:pow_session, ttl: @invalidated_ttl})
|
||||
)
|
||||
|> PowInvalidatedSessionPlug.call(
|
||||
PowInvalidatedSessionPlug.init({:pow_persistent_session, ttl: @invalidated_ttl})
|
||||
)
|
||||
|> Session.call(Session.init(config))
|
||||
|> Cookie.call(Cookie.init([]))
|
||||
|> PowInvalidatedSessionPlug.call(PowInvalidatedSessionPlug.init(:load))
|
||||
|
@ -129,7 +137,6 @@ defmodule PhilomenaWeb.PowInvalidatedSessionPlugTest do
|
|||
|> Conn.send_resp(200, "")
|
||||
end
|
||||
|
||||
|
||||
defp create_session(conn, user, config) do
|
||||
conn
|
||||
|> init_plug(config)
|
||||
|
|
Loading…
Reference in a new issue