authority v0.1.0 Authority.Locking behaviour

A behaviour for locking and unlocking resources, such as user accounts.

Usage

defmodule MyApp.Accounts.Locking do
  use Authority.Locking

  @impl Authority.Locking
  def get_lock(user) do
    # get the current active lock on the user
  end

  @impl Authority.Locking
  def lock(user, reason) do
    # apply a lock to the user for a given reason
  end

  @impl Authority.Locking
  def unlock(user) do
    # remove any active locks from the user's account
  end
end

Once you have this module, you can use it within your authentication module:

defmodule MyApp.Accounts.Authentication do
  use Authority.Authentication

  alias MyApp.Accounts.Locking

  # Check for active locks on the user's account before
  # validating their credentials
  def before_validate(user, _purpose) do
    case Locking.get_lock(user) do
      {:ok, lock} -> {:error, lock}
      _other -> :ok
    end
  end

  def failed(user) do
    # Lock the user account after too many 
    # failed attempts
  end

  # ...
end

Link to this section Summary

Types

A lock on a resource. Can be any type or struct that makes sense for your application

The reason a lock was applied to a resource

A resource that can be locked/unlocked

Callbacks

Get the currently active lock on a given resource, if any

Lock a given resource for a given reason. The reason should be stored with and returned with the lock

Unlock a given resource

Link to this section Types

Link to this type error()
error() :: {:error, term()}
Link to this type lock()
lock() :: any()

A lock on a resource. Can be any type or struct that makes sense for your application.

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

The reason a lock was applied to a resource.

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

A resource that can be locked/unlocked.

Link to this section Callbacks

Link to this callback get_lock(resource)
get_lock(resource()) :: {:ok, lock()} | error()

Get the currently active lock on a given resource, if any.

Link to this callback lock(resource, reason)
lock(resource(), reason()) :: {:ok, lock()} | error()

Lock a given resource for a given reason. The reason should be stored with and returned with the lock.

Link to this callback unlock(resource)
unlock(resource()) :: :ok | error()

Unlock a given resource.