Packages
ash_authentication_phoenix
2.4.8
3.0.0-rc.9
3.0.0-rc.8
3.0.0-rc.7
3.0.0-rc.6
3.0.0-rc.4
3.0.0-rc.3
3.0.0-rc.2
3.0.0-rc.1
3.0.0-rc.0
2.17.2
2.17.1
2.17.0
2.16.0
2.15.0
2.14.1
2.14.0
2.13.1
2.13.0
2.12.2
2.12.1
2.12.0
2.11.0
2.10.5
2.10.4
2.10.3
2.10.2
2.10.1
2.10.0
2.9.0
2.8.0
2.7.0
2.6.3
2.6.2
2.6.1
2.6.0
2.5.4
2.5.3
2.5.2
2.5.1
2.5.0
2.4.8
2.4.7
2.4.6
2.4.5
2.4.4
2.4.3
2.4.2
2.4.1
2.4.0
2.3.0
2.2.1
2.2.0
2.1.11
2.1.10
2.1.9
2.1.8
2.1.7
2.1.6
2.1.5
2.1.4
2.1.3
2.1.2
2.1.1
2.1.0
2.0.2
2.0.1
2.0.0
2.0.0-rc.2
2.0.0-rc.1
2.0.0-rc.0
1.9.4
1.9.3
1.9.2
1.9.1
1.9.0
1.8.7
1.8.6
1.8.5
1.8.4
1.8.3
1.8.2
1.8.1
1.8.0
1.7.3
1.7.2
1.7.1
1.7.0
1.6.6
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.1
1.3.0
1.2.0
1.1.0
1.0.1
1.0.0
Phoenix integration for Ash Authentication
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/ash_authentication_phoenix/controller.ex
defmodule AshAuthentication.Phoenix.Controller do
@moduledoc """
The authentication controller generator.
Since authentication often requires explicit HTTP requests to do things like
set cookies or return Authorization headers, use this module to create an
`AuthController` in your Phoenix application.
## Example
Handling the registration or authentication of a normal web-based user.
```elixir
defmodule MyAppWeb.AuthController do
use MyAppWeb, :controller
use AshAuthentication.Phoenix.Controller
def success(conn, _activity, user, _token) do
conn
|> store_in_session(user)
|> assign(:current_user, user)
|> redirect(to: Routes.page_path(conn, :index))
end
def failure(conn, _activity, _reason) do
conn
|> put_status(401)
|> render("failure.html")
end
def sign_out(conn, _params) do
conn
|> clear_session()
|> render("sign_out.html")
end
end
```
Handling registration or authentication of an API user.
```elixir
defmodule MyAppWeb.ApiAuthController do
use MyAppWeb, :controller
use AshAuthentication.Phoenix.Controller
alias AshAuthentication.TokenRevocation
def success(conn, _activity, _user, token) do
conn
|> put_status(200)
|> json(%{
authentication: %{
status: :success,
bearer: token}
})
end
def failure(conn, _activity, _reason) do
conn
|> put_status(401)
|> json(%{
authentication: %{
status: :failed
}
})
end
def sign_out(conn, _params) do
conn
|> revoke_bearer_tokens()
|> json(%{
status: :ok
})
end
end
```
"""
alias AshAuthentication.Plug.Dispatcher
alias Plug.Conn
@type t :: module
@type activity :: {strategy_name :: atom, phase :: atom}
@type user :: Ash.Resource.record() | nil
@type token :: String.t() | nil
@doc """
Called when authentication (or registration, depending on the provider) has been successful.
"""
@callback success(Conn.t(), activity, user, token) :: Conn.t()
@doc """
Called when authentication fails.
"""
@callback failure(Conn.t(), activity, reason :: any) :: Conn.t()
@doc """
Called when a request to sign out is received.
"""
@callback sign_out(Conn.t(), params :: map) :: Conn.t()
@doc false
@spec __using__(any) :: Macro.t()
defmacro __using__(_opts) do
quote do
@behaviour AshAuthentication.Phoenix.Controller
@behaviour AshAuthentication.Plug
import Phoenix.Controller
import Plug.Conn
import AshAuthentication.Phoenix.Plug
import AshAuthentication.Phoenix.Controller
@doc false
@impl true
@spec success(
Conn.t(),
AshAuthentication.Phoenix.Controller.activity(),
AshAuthentication.Phoenix.Controller.user(),
AshAuthentication.Phoenix.Controller.token()
) ::
Conn.t()
def success(conn, _activity, user, _token) do
conn
|> store_in_session(user)
|> put_status(200)
|> render("success.html")
end
@doc false
@impl true
@spec failure(Conn.t(), AshAuthentication.Phoenix.Controller.activity(), reason :: any) ::
Conn.t()
def failure(conn, _activity, _reason) do
conn
|> put_status(401)
|> render("failure.html")
end
@doc false
@impl true
@spec sign_out(Conn.t(), map) :: Conn.t()
def sign_out(conn, _params) do
conn
|> clear_session()
|> render("sign_out.html")
end
@doc false
@impl true
@spec call(Conn.t(), any) :: Conn.t()
def call(%{private: %{strategy: strategy}} = conn, {_subject_name, _stategy_name, phase}) do
conn
|> Dispatcher.call({phase, strategy, __MODULE__})
end
def call(conn, opts) do
super(conn, opts)
end
@doc false
@impl true
@spec handle_success(
Conn.t(),
AshAuthentication.Phoenix.Controller.activity(),
AshAuthentication.Phoenix.Controller.user(),
AshAuthentication.Phoenix.Controller.token()
) :: Conn.t()
def handle_success(conn, activity, user, token) do
conn
|> put_private(:phoenix_action, :success)
|> put_private(:success_args, [activity, user, token])
|> call(:success)
end
@doc false
@impl true
@spec handle_failure(Conn.t(), AshAuthentication.Phoenix.Controller.activity(), any) ::
Conn.t()
def handle_failure(conn, activity, reason) do
conn
|> put_private(:phoenix_action, :failure)
|> put_private(:failure_args, [activity, reason])
|> call(:failure)
end
@doc false
@spec action(Conn.t(), any) :: Conn.t()
def action(conn, opts) do
conn
|> action_name()
|> case do
:success ->
args = Map.get(conn.private, :success_args, [nil, nil, nil])
apply(__MODULE__, :success, [conn | args])
:failure ->
args = Map.get(conn.private, :failure_args, [nil, nil])
apply(__MODULE__, :failure, [conn | args])
_ ->
super(conn, opts)
end
end
defoverridable success: 4, failure: 3, sign_out: 2
end
end
end