Packages
phauxth
0.10.2
2.5.1
retired
2.5.0
2.4.0
2.3.2
2.3.1
2.3.0
2.2.0
2.1.1
2.1.0
2.0.1
2.0.0
2.0.0-rc.2
2.0.0-rc.1
2.0.0-rc.0
2.0.0-beta.1
2.0.0-beta.0
2.0.0-alpha.5
2.0.0-alpha.4
retired
2.0.0-alpha.3
retired
2.0.0-alpha.2
retired
2.0.0-alpha.1
retired
2.0.0-alpha.0
retired
1.2.9
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.1
1.2.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
0.17.1
0.17.0
0.16.0
0.15.1
0.15.0
0.14.0
0.13.0-rc.1
0.13.0-rc.0
0.12.1-rc.1
0.12.1-rc.0
0.12.0-rc
0.11.0
0.10.3
0.10.2
0.10.0
0.9.0
0.8.2
0.8.1
0.8.0
Authentication library for Phoenix, and other Plug-based, web applications
Current section
Files
Jump to
Current section
Files
lib/phauxth/authenticate/base.ex
defmodule Phauxth.Authenticate.Base do
@moduledoc """
Base module for authentication.
This is used by Phauxth.Authenticate and Phauxth.Remember.
It can also be used to produce a custom authentication module,
as outlined below.
## Custom authentication modules
One example of a custom authentication module is provided by the
Phauxth.Remember module, which uses this base module to provide the
'remember me' functionality.
### Graphql authentication
The following module is another example of how this Base module can
be extended, this time to provide authentication for absinthe-elixir:
defmodule AbsintheAuthenticate do
use Phauxth.Authenticate.Base
import Plug.Conn
def set_user(user, conn) do
put_private(conn, :absinthe, %{context: %{current_user: user}})
end
end
And in the `router.ex` file, call this plug in the pipeline you
want to authenticate (setting the context to the app's endpoint).
plug :api do
plug AbsintheAuthenticate, context: MyApp.Web.Endpoint
end
"""
@doc false
defmacro __using__(_) do
quote do
import unquote(__MODULE__)
@behaviour Plug
@doc false
def init(opts) do
{Keyword.get(opts, :context),
Keyword.get(opts, :max_age, 7 * 24 * 60 * 60)}
end
@doc false
def call(conn, {nil, _}) do
check_session(conn) |> log_user |> set_user(conn)
end
def call(%Plug.Conn{req_headers: headers} = conn, {context, max_age}) do
check_headers(headers, context, max_age) |> log_user |> set_user(conn)
end
@doc """
Set the `current_user` variable.
"""
def set_user(user, conn) do
Plug.Conn.assign(conn, :current_user, user)
end
defoverridable [init: 1, call: 2, set_user: 2]
end
end
import Plug.Conn
alias Phoenix.Token
alias Phauxth.{Config, Log}
@doc """
Check the conn to see if the user is registered in the current
session.
This function also calls the database to get user information.
"""
def check_session(conn) do
with id when not is_nil(id) <- get_session(conn, :user_id),
do: Config.repo.get(Config.user_mod, id)
end
@doc """
Check the headers for an authorization token.
This function also calls the database to get user information.
"""
def check_headers(headers, context, max_age) do
with {_, token} <- List.keyfind(headers, "authorization", 0),
do: check_token(token, context, max_age)
end
@doc """
Check the authorization token.
This function also calls the database to get user information.
"""
def check_token(token, context, max_age) do
with {:ok, user_id} <- Token.verify(context, "user auth", token, max_age: max_age),
do: Config.repo.get(Config.user_mod, user_id)
end
@doc """
Log the result of the authentication and return the user struct or nil.
"""
def log_user(nil) do
Log.info(%Log{}) && nil
end
def log_user({:error, msg}) do
Log.info(%Log{message: "#{msg} token"}) && nil
end
def log_user(user) do
Log.info(%Log{user: user.id, message: "User authenticated"})
Map.drop(user, Config.drop_user_keys)
end
end