Ex2FA.Changeset (ex_2fa v0.1.0)

Copy Markdown View Source

Ecto.Changeset helper functions for inserting or regenerating a 2FA secret.

API-compatible with NioGoogleAuthenticator.Changeset. This module is automatically compiled when :ecto is available in your project.

Summary

Functions

Regenerates and replaces the secret on an Ecto.Changeset, regardless of whether a value already exists.

Functions

add_secret_to_changeset(changeset, attribute_name \\ :ga_secret, options \\ [])

@spec add_secret_to_changeset(Ecto.Changeset.t(), atom(), keyword()) ::
  Ecto.Changeset.t()

Adds a secret to an Ecto.Changeset if the target field is currently nil.

Defaults to :ga_secret for compatibility with nio_google_authenticator. You may pass a custom field atom as the second argument, and optional :byte_length in options (default 10).

Examples

user
|> cast(params, [:name, :ga_secret])
|> Ex2FA.Changeset.add_secret_to_changeset()

user
|> cast(params, [:name, :totp_secret])
|> Ex2FA.Changeset.add_secret_to_changeset(:totp_secret, byte_length: 20)

regenerate_secret_in_changeset(changeset, attribute_name \\ :ga_secret, options \\ [])

@spec regenerate_secret_in_changeset(Ecto.Changeset.t(), atom(), keyword()) ::
  Ecto.Changeset.t()

Regenerates and replaces the secret on an Ecto.Changeset, regardless of whether a value already exists.

Defaults to :ga_secret for compatibility with nio_google_authenticator.