Packages
phauxth
2.2.0
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/token.ex
defmodule Phauxth.Authenticate.Token do
@moduledoc """
Base module for token authentication.
This is `use`-d by Phauxth.AuthenticateToken, and it can also be used
to produce a custom token authentication module, as outlined below.
## Custom token authentication modules
The next sections give examples of extending this module to create
custom authentication modules.
### Token authentication with token stored in a cookie
This module will retrieve the token from a cookie, instead of from
the headers:
defmodule Phauxth.AuthenticateTokenCookie do
use Phauxth.Authenticate.Token
@impl true
def authenticate(%Plug.Conn{req_cookies: %{"access_token" => token}}, opts) do
verify_token(token, opts)
end
end
And in the `router.ex` file, call this plug in the pipeline you
want to authenticate.
pipeline :api do
plug :accepts, ["json"]
plug AuthenticateTokenCookie
end
### GraphQL authentication
The following module is an example of how Phauxth.Authenticate.Token
module can be extended, this time to provide authentication for
absinthe-elixir:
defmodule AbsintheAuthenticate do
use Phauxth.Authenticate.Token
@impl true
def set_user(user, conn) do
Absinthe.Plug.put_options(conn, context: %{current_user: user})
end
end
As in the above example, in the `router.ex` file, call this plug
in the pipeline you want to authenticate.
"""
@doc """
Gets the token from the authorization headers.
"""
@callback get_token(list, module, keyword) :: {:ok, map} | {:error, String.t() | atom}
defmacro __using__(_) do
quote do
@behaviour Phauxth.Authenticate.Token
use Phauxth.Authenticate.Base
alias Phauxth.Config
@impl Plug
def init(opts), do: super(opts ++ [max_age: 14400])
@impl Phauxth.Authenticate.Base
def authenticate(conn, user_context, opts) do
conn |> get_req_header("authorization") |> get_token(user_context, opts)
end
@impl Phauxth.Authenticate.Token
def get_token([], _, _), do: {:error, "no token found"}
def get_token(["Bearer " <> token | _], user_context, opts),
do: verify_token(token, user_context, opts)
def get_token([token | _], user_context, opts),
do: verify_token(token, user_context, opts)
defp verify_token(token, user_context, opts) do
token |> Config.token_module().verify(opts) |> get_user(user_context)
end
defoverridable Plug
defoverridable Phauxth.Authenticate.Base
defoverridable Phauxth.Authenticate.Token
end
end
end