Skip to content

Commit

Permalink
Refactor plugs to use existing config
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Sep 13, 2018
1 parent 6842194 commit 0e04f71
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 68 deletions.
6 changes: 2 additions & 4 deletions lib/extensions/email_confirmation/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ defmodule PowEmailConfirmation.Plug do
end

defp maybe_renew_conn(conn, %{id: user_id} = user, config) do
mod = config[:mod]

case Plug.current_user(conn) do
%{id: ^user_id} -> mod.do_create(conn, user)
case Plug.current_user(conn, config) do
%{id: ^user_id} -> Plug.get_mod(config).do_create(conn, user, config)
_any -> conn
end
end
Expand Down
5 changes: 2 additions & 3 deletions lib/extensions/persistent_session/plug/cookie.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ defmodule PowPersistentSession.Plug.Cookie do
"""
@spec authenticate(Conn.t(), Config.t()) :: Conn.t()
def authenticate(conn, config) do
user = Plug.current_user(conn)
user = Plug.current_user(conn, config)

conn
|> Conn.fetch_cookies()
Expand All @@ -117,7 +117,6 @@ defmodule PowPersistentSession.Plug.Cookie do

defp do_authenticate(conn, key_id, config) do
{store, store_config} = store(config)
mod = config[:mod]

store_config
|> store.get(key_id)
Expand All @@ -130,7 +129,7 @@ defmodule PowPersistentSession.Plug.Cookie do
conn
|> delete(config)
|> create(user, config)
|> mod.do_create(user)
|> Plug.get_mod(config).do_create(user, config)
end
end

Expand Down
45 changes: 20 additions & 25 deletions lib/extensions/reset_password/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,16 @@ defmodule PowResetPassword.Plug do
Plug helper methods.
"""
alias Plug.Conn
alias Pow.{Config, Store.Backend.EtsCache, UUID}
alias Pow.{Config, Store.Backend.EtsCache, Plug, UUID}
alias PowResetPassword.{Ecto.Context, Store.ResetTokenCache}

@doc """
Creates a changeset from the user fetched in the connection.
"""
@spec change_user(Conn.t(), map()) :: map()
def change_user(conn, params \\ %{}) do
user =
conn
|> reset_password_user()
|> case do
nil ->
conn
|> Pow.Plug.fetch_config()
|> Context.user_schema_mod()
|> struct()

user ->
user
end
config = Plug.fetch_config(conn)
user = reset_password_user(conn) || user_struct(config)

user.__struct__.pow_password_changeset(user, params)
end
Expand All @@ -36,14 +25,6 @@ defmodule PowResetPassword.Plug do
Conn.assign(conn, :reset_password_user, user)
end

defp reset_password_user(conn) do
conn.assigns[:reset_password_user]
end

defp put_reset_password_token(conn, token) do
Conn.put_private(conn, :reset_password_token, token)
end

@doc """
Fetches the assigned `:reset_password_token` in the connection.
"""
Expand All @@ -58,7 +39,7 @@ defmodule PowResetPassword.Plug do
"""
@spec create_reset_token(Conn.t(), map()) :: {:ok, map(), Conn.t()} | {:error, map(), Conn.t()}
def create_reset_token(conn, params) do
config = Pow.Plug.fetch_config(conn)
config = Plug.fetch_config(conn)
user =
params
|> Map.get("email")
Expand Down Expand Up @@ -88,7 +69,7 @@ defmodule PowResetPassword.Plug do
def user_from_token(conn, token) do
{store, store_config} =
conn
|> Pow.Plug.fetch_config()
|> Plug.fetch_config()
|> store()

store_config
Expand All @@ -104,7 +85,7 @@ defmodule PowResetPassword.Plug do
"""
@spec update_user_password(Conn.t(), map()) :: {:ok, map(), Conn.t()} | {:error, map(), Conn.t()}
def update_user_password(conn, params) do
config = Pow.Plug.fetch_config(conn)
config = Plug.fetch_config(conn)
token = conn.params["id"]

conn
Expand All @@ -127,6 +108,20 @@ defmodule PowResetPassword.Plug do
store.delete(store_config, token)
end

defp user_struct(config) do
config
|> Context.user_schema_mod()
|> struct()
end

defp reset_password_user(conn) do
conn.assigns[:reset_password_user]
end

defp put_reset_password_token(conn, token) do
Conn.put_private(conn, :reset_password_token, token)
end

defp store(config) do
backend = Config.get(config, :cache_store_backend, EtsCache)

Expand Down
8 changes: 4 additions & 4 deletions lib/pow/phoenix/mailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule Pow.Phoenix.Mailer do
Remember to update configuration with `mailer_backend: MyAppWeb.Pow.Mailer`
"""
alias Plug.Conn
alias Pow.Phoenix.Mailer.Mail
alias Pow.{Config, Phoenix.Mailer.Mail, Plug}

@callback cast(Mail.t()) :: any()
@callback process(any()) :: any()
Expand All @@ -40,8 +40,8 @@ defmodule Pow.Phoenix.Mailer do

@spec deliver(Conn.t(), Mail.t()) :: any()
def deliver(conn, email) do
config = Pow.Plug.fetch_config(conn)
mailer = Pow.Config.get(config, :mailer_backend) || raise_no_mailer_backend_set()
config = Plug.fetch_config(conn)
mailer = Config.get(config, :mailer_backend) || raise_no_mailer_backend_set()

email
|> mailer.cast()
Expand All @@ -50,6 +50,6 @@ defmodule Pow.Phoenix.Mailer do

@spec raise_no_mailer_backend_set :: no_return
defp raise_no_mailer_backend_set do
Pow.Config.raise_error("No :mailer_backend configuration option found for plug.")
Config.raise_error("No :mailer_backend configuration option found for plug.")
end
end
17 changes: 9 additions & 8 deletions lib/pow/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule Pow.Plug do
|> Operations.authenticate(config)
|> case do
nil -> {:error, conn}
user -> {:ok, get_mod(config).do_create(conn, user)}
user -> {:ok, get_mod(config).do_create(conn, user, config)}
end
end

Expand All @@ -91,7 +91,7 @@ defmodule Pow.Plug do
def clear_authenticated_user(conn) do
config = fetch_config(conn)

{:ok, get_mod(config).do_delete(conn)}
{:ok, get_mod(config).do_delete(conn, config)}
end

@doc """
Expand All @@ -101,7 +101,7 @@ defmodule Pow.Plug do
def change_user(conn, params \\ %{}) do
config = fetch_config(conn)

case current_user(conn) do
case current_user(conn, config) do
nil -> Operations.changeset(params, config)
user -> Operations.changeset(user, params, config)
end
Expand Down Expand Up @@ -131,7 +131,7 @@ defmodule Pow.Plug do
config = fetch_config(conn)

conn
|> current_user()
|> current_user(config)
|> Operations.update(params, config)
|> maybe_create_auth(conn, config)
end
Expand All @@ -146,22 +146,23 @@ defmodule Pow.Plug do
config = fetch_config(conn)

conn
|> current_user()
|> current_user(config)
|> Operations.delete(config)
|> case do
{:ok, user} -> {:ok, user, get_mod(config).do_delete(conn)}
{:ok, user} -> {:ok, user, get_mod(config).do_delete(conn, config)}
{:error, changeset} -> {:error, changeset, conn}
end
end

defp maybe_create_auth({:ok, user}, conn, config) do
{:ok, user, get_mod(config).do_create(conn, user)}
{:ok, user, get_mod(config).do_create(conn, user, config)}
end
defp maybe_create_auth({:error, changeset}, conn, _config) do
{:error, changeset, conn}
end

defp get_mod(config), do: config[:mod]
@spec get_mod(Config.t()) :: atom() | nil
def get_mod(config), do: config[:mod]

@spec no_config_error :: no_return
defp no_config_error do
Expand Down
30 changes: 11 additions & 19 deletions lib/pow/plug/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule Pow.Plug.Base do
@doc """
Initializes the connection for Pow, and assigns current user.
If a user is not already assigned, `do_fetch/1` will be called. `:mod` is
If a user is not already assigned, `do_fetch/2` will be called. `:mod` is
added to the private pow configuration key, so it can be used in
subsequent calls to create, update and delete user credentials from the
connection.
Expand All @@ -58,17 +58,15 @@ defmodule Pow.Plug.Base do
conn = Plug.put_config(conn, config)

conn
|> Plug.current_user()
|> maybe_fetch_user(conn)
|> Plug.current_user(config)
|> maybe_fetch_user(conn, config)
end

@doc """
Calls `fetch/2` and assigns the current user.
"""
@spec do_fetch(Conn.t()) :: Conn.t()
def do_fetch(conn) do
config = fetch_config(conn)

@spec do_fetch(Conn.t(), Config.t()) :: Conn.t()
def do_fetch(conn, config) do
conn
|> fetch(config)
|> assign_current_user(config)
Expand All @@ -77,10 +75,8 @@ defmodule Pow.Plug.Base do
@doc """
Calls `create/3` and assigns the current user.
"""
@spec do_create(Conn.t(), map()) :: Conn.t()
def do_create(conn, user) do
config = fetch_config(conn)

@spec do_create(Conn.t(), map(), Config.t()) :: Conn.t()
def do_create(conn, user, config) do
conn
|> create(user, config)
|> assign_current_user(config)
Expand All @@ -89,19 +85,15 @@ defmodule Pow.Plug.Base do
@doc """
Calls `delete/2` and removes the current user assign.
"""
@spec do_delete(Conn.t()) :: Conn.t()
def do_delete(conn) do
config = fetch_config(conn)

@spec do_delete(Conn.t(), Config.t()) :: Conn.t()
def do_delete(conn, config) do
conn
|> delete(config)
|> remove_current_user(config)
end

defp maybe_fetch_user(nil, conn), do: do_fetch(conn)
defp maybe_fetch_user(_user, conn), do: conn

defp fetch_config(conn), do: Plug.fetch_config(conn)
defp maybe_fetch_user(nil, conn, config), do: do_fetch(conn, config)
defp maybe_fetch_user(_user, conn, _config), do: conn

defp assign_current_user({conn, user}, config), do: Plug.assign_current_user(conn, user, config)

Expand Down
10 changes: 5 additions & 5 deletions test/pow/plug/session_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ defmodule Pow.Plug.SessionTest do
conn =
conn
|> Session.call(opts)
|> Session.do_create(user)
|> Session.do_create(user, opts)

session_id = get_session_id(conn)
{etc_user, _inserted_at} = ets.get(nil, session_id)
Expand All @@ -128,7 +128,7 @@ defmodule Pow.Plug.SessionTest do
assert etc_user == user
assert Plug.current_user(conn) == user

conn = Session.do_create(conn, user)
conn = Session.do_create(conn, user, opts)
new_session_id = get_session_id(conn)
{etc_user, _inserted_at} = ets.get(nil, new_session_id)

Expand All @@ -148,7 +148,7 @@ defmodule Pow.Plug.SessionTest do
conn =
conn
|> Session.call(opts)
|> Session.do_create(%{id: 1})
|> Session.do_create(%{id: 1}, opts)

refute get_session_id(conn)

Expand All @@ -162,7 +162,7 @@ defmodule Pow.Plug.SessionTest do
conn =
conn
|> Session.call(opts)
|> Session.do_create(user)
|> Session.do_create(user, opts)

session_id = get_session_id(conn)
{etc_user, _inserted_at} = ets.get(nil, session_id)
Expand All @@ -171,7 +171,7 @@ defmodule Pow.Plug.SessionTest do
assert etc_user == user
assert Plug.current_user(conn) == user

conn = Session.do_delete(conn)
conn = Session.do_delete(conn, opts)

refute new_session_id = get_session_id(conn)
assert is_nil(new_session_id)
Expand Down

0 comments on commit 0e04f71

Please sign in to comment.