cassette_plug v1.1.0 Cassette.Plug.AuthenticationHandler behaviour

Behaviour and macro module to define callbacks for the authentication handlers the plug uses.

Most of this works out-of-the-box, but it might be interesting to override Cassette.Plug.AuthenticationHandler.invalid_authentication/2 and present a more friendy error page


defmodule MyErrorHandler do
  use Cassette.Plug.AuthenticationHandler

  def invalid_authentication(conn, _options) do
    render(conn, "error")
  end
end

And while plugging in your router:


plug Cassette.Plug, handler: MyErrorHandler

Check Cassette.Plug.DefaultHandler for the default behaviour.

Summary

Functions

Returns the default implementation for this behaviour

Callbacks

Initializes this handler with the given options

Called when authentication is provided but fails (i.e., ticket is no longer valid or is invalid)

Called to compute the service that must be authenticated against

Called when there is no authentication in the request (i.e., no ticket in the query string)

Called when successfully authenticated the user on conn

Called to extract the current authenticated user and/or the authentication token from conn

Functions

default()

Specs

default :: Cassette.Plug.AuthenticationHandler

Returns the default implementation for this behaviour

Callbacks

init(args)

Specs

init(args :: term) :: term

Initializes this handler with the given options.

They will be forwarded to the other functions.

invalid_authentication(conn, options)

Specs

invalid_authentication(conn :: Plug.Conn.t, options :: term) :: Plug.Conn.t

Called when authentication is provided but fails (i.e., ticket is no longer valid or is invalid).

This might be your Forbidden page.

service(conn, options)

Specs

service(conn :: Plug.Conn.t, options :: term) :: Plug.Conn.t

Called to compute the service that must be authenticated against.

Usually this is the URL of the page the user is trying to access and may be computed using values in conn

unauthenticated(conn, options)

Specs

unauthenticated(conn :: Plug.Conn.t, options :: term) :: Plug.Conn.t

Called when there is no authentication in the request (i.e., no ticket in the query string).

The usual implementation is to redirect to CAS.

user_authenticated(conn, user, options)

Specs

user_authenticated(conn :: Plug.Conn.t, user :: Cassette.User.t, options :: term) :: Plug.Conn.t

Called when successfully authenticated the user on conn

user_or_token(conn, options)

Specs

user_or_token(conn :: Plug.Conn.t, options :: term) :: {Cassette.User.t | nil, {:ok, String.t} | :error}

Called to extract the current authenticated user and/or the authentication token from conn