Haytni v0.0.1 Haytni.LockablePlugin View Source

This plugin locks an account after a specified number of failed sign-in attempts. User can unlock its account via email or after a specified time period.

Fields:

  • failed_attempts (integer, default: 0): the current count of successive failures to login
  • locked_at (datetime@utc, nullable, default: NULL): when the account was locked (NULL while the account is not locked)
  • unlock_token (string, nullable, unique, default: NULL): the token send to the user to unlock its account

Configuration:

  • maximum_attempts (default: 20): the amount of successive attempts to login before locking the corresponding account
  • unlock_token_length (default: 32): the length of the generated token
  • unlock_keys (default: ~W[email]a): the field(s) to match to accept the unlock request
  • unlock_in (default: {1, :hour}): delay to automatically unlock the account
  • unlock_strategy (default: :both): strategy used to unlock an account. One of:

    • :email: sends an unlock link to the user email
    • :time: re-enables login after a certain amount of time (see :unlock_in below)
    • :both: enables both strategies
    • :none: no unlock strategy. You should handle unlocking by yourself.

Routes:

  • unlock_path (actions: new/create, show)

Link to this section Summary

Functions

Returns true if :email (included in :both) is enabled

Extract (early) the user from the HTTP request (http authentification, cookies/session, …)

Returns true if it’s the last attempt before account locking in case of a new sign-in failure

Returns true if user account is currently locked

This callback is invoked when a user is editing its registration and change its email address. It is a facility (subset) to avoid you to handle it by yourself via validate_update_registration/1

This callback is invoked when a user (manually) log out. Its purpose is mainly to do some cleanup like removing a cookie

Invoked to accomplish a task right after user’s registration (insert). This callback allows you to do some linked changes to the database, send an email or whatever by appending it to multi

Resend, by email, the instructions to unlock an account

Unlock an account from a token

This callback let you do any kind of change or additionnal validation on the changeset when a user is registering

Same as validate_create_registration but registration’s edition as logic between the two may be completely different

Link to this section Functions

Link to this function email_strategy_enabled?() View Source
email_strategy_enabled?() :: boolean()

Returns true if :email (included in :both) is enabled

Extract (early) the user from the HTTP request (http authentification, cookies/session, …).

Returns a tuple of the form {conn, user} with user being nil if no user could be found at this early stage.

Callback implementation for Haytni.Plugin.find_user/1.

Link to this function last_attempt?(user) View Source
last_attempt?(user :: struct()) :: boolean()

Returns true if it’s the last attempt before account locking in case of a new sign-in failure

Link to this function locked?(user) View Source
locked?(user :: struct()) :: boolean()

Returns true if user account is currently locked.

Link to this function maximum_attempts(default \\ 20) View Source
Link to this function on_email_change(multi, changeset) View Source

This callback is invoked when a user is editing its registration and change its email address. It is a facility (subset) to avoid you to handle it by yourself via validate_update_registration/1.

It returns a tuple of {Ecto.Multi, Ecto.Changeset}, same as its arguments, to permit to the callback to add any operation to multi or change to changeset.

This callback is called before updating the user but the actions added to multi will be run after its update.

Callback implementation for Haytni.Plugin.on_email_change/2.

This callback is invoked when a user (manually) log out. Its purpose is mainly to do some cleanup like removing a cookie.

Callback implementation for Haytni.Plugin.on_logout/1.

Invoked to accomplish a task right after user’s registration (insert). This callback allows you to do some linked changes to the database, send an email or whatever by appending it to multi.

Remember to comply to Ecto.Multi functions. In particular Ecto.Multi.run: the function called by it have to return {:ok, your value} or {:error, your value}. Also note that the inserted user will be passed to the function called by Ecto.Multi.run as the :user key to the map received by the last one as its (only) argument.

The following example illustrate how to send a welcome mail:

def on_registration(multi = %Ecto.Multi{}) do
  multi
  |> Ecto.Multi.run(:send_welcome_email, fn %{user: user} ->
    send_welcome_email_to(user)
    {:ok, :success}
  end)
end

Callback implementation for Haytni.Plugin.on_registration/1.

Link to this function resend_unlock_instructions(request) View Source
resend_unlock_instructions(request :: Haytni.Unlockable.Request.t()) ::
  {:ok, struct()} | {:error, :no_match | :not_locked | :email_strategy_disabled}

Resend, by email, the instructions to unlock an account.

Returns:

  • {:error, :email_strategy_disabled} if :email strategy is disabled
  • {:error, :no_match} if there is no such account matching unlock_keys
  • {:error, :not_locked} if the account is not currently locked
  • {:ok, user} if successful
Link to this function unlock(token) View Source
unlock(token :: String.t()) :: struct() | {:error, String.t()} | no_return()

Unlock an account from a token.

Returns the user if the token exists and {:error, message} if not.

Also raises if updating user fails.

Link to this function unlock_in(default \\ {1, :hour}) View Source
Link to this function unlock_keys(default \\ ~W"email"a) View Source
Link to this function unlock_strategy(default \\ :both) View Source
Link to this function unlock_token_length(default \\ 32) View Source
Link to this function validate_create_registration(changeset) View Source

This callback let you do any kind of change or additionnal validation on the changeset when a user is registering.

Callback implementation for Haytni.Plugin.validate_create_registration/1.

Link to this function validate_update_registration(changeset) View Source

Same as validate_create_registration but registration’s edition as logic between the two may be completely different.

Callback implementation for Haytni.Plugin.validate_update_registration/1.