Packages
pow
1.0.15
1.0.39
1.0.38
1.0.37
1.0.36
1.0.35
1.0.34
1.0.33
1.0.32
1.0.31
1.0.30
1.0.29
1.0.28
1.0.27
1.0.26
1.0.25
1.0.24
1.0.23
1.0.22
1.0.21
1.0.20
1.0.19
1.0.18
1.0.17
1.0.16
1.0.15
1.0.14
1.0.13
1.0.12
1.0.11
1.0.10
1.0.9
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-rc.4
1.0.0-rc.3
1.0.0-rc.2
1.0.0-rc.1
1.0.0-rc.0
0.1.0-rc.1
0.1.0-alpha.8
0.1.0-alpha.7
retired
0.1.0-alpha.6
0.1.0-alpha.5
0.1.0-alpha.4
0.1.0-alpha.3
0.1.0-alpha.2
0.1.0-alpha.1
0.1.0-alpha
Robust user authentication solution
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/pow/plug/base.ex
defmodule Pow.Plug.Base do
@moduledoc """
This plug macro will set `:pow_config` as private, and attempt to fetch and
assign a user in the connection if it has not already been assigned. The user
will be assigned automatically in any of the operations.
## Example
defmodule MyAppWeb.Pow.CustomPlug do
use Pow.Plug.Base
@impl true
def fetch(conn, _config) do
user = fetch_user_from_cookie(conn)
{conn, user}
end
@impl true
def create(conn, user, _config) do
conn = update_cookie(conn, user)
{conn, user}
end
@impl true
def delete(conn, _config) do
delete_cookie(conn)
end
end
"""
alias Plug.Conn
alias Pow.{Config, Plug}
@callback init(Config.t()) :: Config.t()
@callback call(Conn.t(), Config.t()) :: Conn.t()
@callback fetch(Conn.t(), Config.t()) :: {Conn.t(), map() | nil}
@callback create(Conn.t(), map(), Config.t()) :: {Conn.t(), map()}
@callback delete(Conn.t(), Config.t()) :: Conn.t()
@doc false
defmacro __using__(_opts) do
quote do
@behaviour unquote(__MODULE__)
@doc false
def init(config), do: config
@doc """
Configures the connection for Pow, and fetches user.
`:plug` is appended to the passed configuration, so the current plug will
be used in any subsequent calls to create, update and delete user
credentials from the connection. The configuration is then set for the
conn with `Pow.Plug.put_config/2`.
If a user can't be fetched with `Pow.Plug.current_user/2`, `do_fetch/2`
will be called.
"""
def call(conn, config) do
config = put_plug(config)
conn = Plug.put_config(conn, config)
conn
|> Plug.current_user(config)
|> maybe_fetch_user(conn, config)
end
defp put_plug(config) do
config
|> Config.put(:plug, __MODULE__)
|> Config.put(:mod, __MODULE__) # TODO: Remove by 1.1.0, this is only for backwards compability
end
@doc """
Calls `fetch/2` and assigns the current user to the conn.
The user is assigned to the conn with `Pow.Plug.assign_current_user/3`.
"""
@spec do_fetch(Conn.t(), Config.t()) :: Conn.t()
def do_fetch(conn, config) do
conn
|> fetch(config)
|> assign_current_user(config)
end
@doc """
Calls `create/3` and assigns the current user.
The user is assigned to the conn with `Pow.Plug.assign_current_user/3`.
"""
@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)
end
@doc """
Calls `delete/2` and removes the current user assigned to the conn.
The user assigned is removed from the conn with
`Pow.Plug.assign_current_user/3`.
"""
@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, 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)
defp remove_current_user(conn, config), do: Plug.assign_current_user(conn, nil, config)
defoverridable unquote(__MODULE__)
end
end
end