authority v0.3.0 Authority.Tokenization behaviour

A behaviour for creating tokens to represent resources (or credentials).

Most commonly, this will be used to exchange credentials for a temporary token which will be used to authenticate future requests.

Example

defmodule MyApp.Accounts.Tokenization do
  use Authority.Tokenization

  @impl Authority.Tokenization
  def tokenize(credential, purpose \\ :any) do
    with {:ok, user} <- MyApp.Accounts.authenticate(credential, purpose) do
      # create a token associated with the user
    end
  end
end

Link to this section Summary

Types

The purpose for the token

A resource to be converted into a token. Can be any type that makes sense for the application

A token. Can be any type that makes sense for your application

Callbacks

Gets a token by an identifier, such as a string version of it

Creates a token, assuming the :any purpose, representing a given resource

Create a token with a given purpose, representing a given resource. For example, you might convert credentials (email/password) into a token representing a user

Link to this section Types

Link to this type purpose()
purpose() :: atom()

The purpose for the token.

Link to this type resource()
resource() :: any()

A resource to be converted into a token. Can be any type that makes sense for the application.

Link to this type token()
token() :: any()

A token. Can be any type that makes sense for your application.

Link to this section Callbacks

Link to this callback get_token(token) (optional)
get_token(token()) :: {:ok, token()} | {:error, term()}

Gets a token by an identifier, such as a string version of it.

Link to this callback tokenize(resource)
tokenize(resource()) :: {:ok, token()} | {:error, term()}

Creates a token, assuming the :any purpose, representing a given resource.

Link to this callback tokenize(resource, purpose)
tokenize(resource(), purpose()) :: {:ok, token()} | {:error, term()}

Create a token with a given purpose, representing a given resource. For example, you might convert credentials (email/password) into a token representing a user.