Haytni.AuthenticablePlugin (Haytni v0.7.0) View Source

This is a base plugin as it handles basic informations of a user (which are email and hashed password) and their authentication.

Fields:

  • email (string)
  • encrypted_password (string)

Configuration:

  • authentication_keys (default: [:email]): the key(s), in addition to the password, requested to login. You can redefine it to ~W[name]a, for example, to ask the username instead of its email address.
  • hashing_method (no default): a module implementing the behaviour ExPassword.Algorithm to hash and verify passwords
  • hashing_options (a map, no default since they are hash-specific): ExPassword settings for hashing passwords

To support:

  • bcrypt:
    • add {:expassword_bcrypt, "~> 0.2"} in deps/0 to your mix.exs
    • set :hashing_method to ExPassword.Bcrypt on the line stack Haytni.AuthenticablePlugin and also hashing_options: %{cost: 10}
  • argon2:
    • add {:expassword_argon2, "~> 0.2"} in deps/0 to your mix.exs
    • set :hashing_method to ExPassword.Argon2 on the line stack Haytni.AuthenticablePlugin and also hashing_options: %{type: :argon2id, threads: 2, time_cost: 4, memory_cost: 131072}
stack Haytni.AuthenticablePlugin,
  authentication_keys: [:email],
  hashing_method: ExPassword.Bcrypt,
  hashing_options: %{
    cost: 10,
  }

Routes:

  • haytni_<scope>_session_path (actions: new/create, delete): the generated routes can be customized through the following parameters when you call YourAppWeb.Haytni.routes/1:

    • login_path (default: "/session"): custom path assigned to the sign-in route
    • logout_path (default: same value as login_path): the path for th sign out route
    • logout_method (default: :delete): the HTTP method to use for the user to log out, in case where the default DELETE method were not well supported by your clients
    # lib/your_app_web/router.ex
    defmodule YourAppWeb.Router do
      # ...
      scope ... do
        YourAppWeb.Haytni.routes(
          login_path: "/login",
          logout_path: "/logout",
          logout_method: :get
        )
      end
      # ...
    end

Link to this section Summary

Functions

Hashes a password.

The translated string to display when credentials (password and/or email by default) are wrong.

Converts the parameters received for authentication by the controller in a %Ecto.Changeset{} to handle and validate user inputs according to plugin's configuration (authentication_keys).

Returns true if password matches user's current hash (encrypted_password field)

Link to this section Functions

Link to this function

authenticate(conn, module, config, session_params)

View Source

Specs

authenticate(
  conn :: Plug.Conn.t(),
  module :: module(),
  config :: Haytni.AuthenticablePlugin.Config.t(),
  session_params :: Haytni.params()
) :: Haytni.repo_nobang_operation(Plug.Conn.t())

Authentificates a user.

Returns:

  • {:ok, user} if crendentials are correct and user is valid
  • {:error, changeset} if credentials are incorrect or user is invalid (rejected by a Haytni.Plugin.invalid? callback by a plugin in the stack)
Link to this function

find_user(conn, module, config)

View Source

Callback implementation for Haytni.Plugin.find_user/3.

Link to this function

hash_password(password, config)

View Source

Specs

hash_password(
  password :: String.t(),
  config :: Haytni.AuthenticablePlugin.Config.t()
) :: String.t()

Hashes a password.

Returns the hash of the password after having hashed it

Link to this function

invalid?(user, module, config)

View Source

Callback implementation for Haytni.Plugin.invalid?/3.

Link to this function

invalid_credentials_message()

View Source

Specs

invalid_credentials_message() :: String.t()

The translated string to display when credentials (password and/or email by default) are wrong.

Link to this function

on_delete_user(multi, user, module, config)

View Source

Callback implementation for Haytni.Plugin.on_delete_user/4.

Link to this function

on_email_change(multi, changeset, module, config)

View Source

Callback implementation for Haytni.Plugin.on_email_change/4.

Link to this function

on_failed_authentication(user, multi, keywords, module, config)

View Source

Callback implementation for Haytni.Plugin.on_failed_authentication/5.

Link to this function

on_logout(conn, module, config)

View Source

Callback implementation for Haytni.Plugin.on_logout/3.

Link to this function

on_registration(multi, module, config)

View Source

Callback implementation for Haytni.Plugin.on_registration/3.

Link to this function

on_successful_authentication(conn, user, multi, keywords, module, config)

View Source

Callback implementation for Haytni.Plugin.on_successful_authentication/6.

Link to this function

session_changeset(config, session_params \\ %{})

View Source

Specs

session_changeset(
  config :: Haytni.AuthenticablePlugin.Config.t(),
  request_params :: Haytni.params()
) :: Ecto.Changeset.t()

Converts the parameters received for authentication by the controller in a %Ecto.Changeset{} to handle and validate user inputs according to plugin's configuration (authentication_keys).

Link to this function

valid_password?(user, password, config)

View Source

Specs

valid_password?(
  user :: Haytni.nilable(Haytni.user()),
  password :: String.t(),
  config :: Haytni.AuthenticablePlugin.Config.t()
) :: boolean()

Returns true if password matches user's current hash (encrypted_password field)

Link to this function

validate_password(changeset, module, config)

View Source

Callback implementation for Haytni.Plugin.validate_password/3.

Link to this function

validate_update_registration(changeset, module, config)

View Source

Callback implementation for Haytni.Plugin.validate_update_registration/3.