View Source Bonfire.Me.Accounts.SecondFactors (Bonfire v0.9.10-classic-beta.169)

Support for second-factor authentication. Powered by NimbleTOTP.

Summary

Functions

Creates or updates a TOTP changeset.

Disables the TOTP configuration for the given account.

Checks if second-factor authentication (TOTP) is enabled on the instance.

Formats a TOTP secret into a human-readable format.

Retrieves the second factor record for an account, if any.

Tries to authenticates an account using the provided parameters.

Conditionally casts a TOTP changeset based on parameters and options.

Generates a new TOTP secret if second-factor authentication is enabled.

Generates a QR code for the TOTP URI.

Generates a new TOTP URI for use in an authentication app.

Validates if the given TOTP code is valid.

Functions

Link to this function

changeset(totp \\ %AuthSecondFactor{}, attrs, opts \\ [])

View Source

Creates or updates a TOTP changeset.

The secret will only be updated if the OTP code sent is valid.

Examples

iex> Bonfire.Me.Accounts.SecondFactors.changeset(%AuthSecondFactor{}, %{secret: "new_secret"}, [])
%Ecto.Changeset{data: %AuthSecondFactor{}}
Link to this function

delete_account_totp(account_totp)

View Source

Disables the TOTP configuration for the given account.

Examples

iex> Bonfire.Me.Accounts.SecondFactors.delete_account_totp(%AuthSecondFactor{id: "some_id"})

iex> Bonfire.Me.Accounts.SecondFactors.delete_account_totp("some_id")

Checks if second-factor authentication (TOTP) is enabled on the instance.

Examples

iex> Bonfire.Me.Accounts.SecondFactors.enabled?()
true
Link to this function

format_secret(secret \\ new())

View Source

Formats a TOTP secret into a human-readable format.

Examples

iex> Bonfire.Me.Accounts.SecondFactors.format_secret("JBSWY3DPEHPK3PXP")
"JBSW Y3DP EHPK 3PXP"
Link to this function

get_account_totp(account)

View Source

Retrieves the second factor record for an account, if any.

Examples

iex> Bonfire.Me.Accounts.SecondFactors.get_account_totp(%{auth_second_factor: %AuthSecondFactor{}})
%AuthSecondFactor{}

iex> Bonfire.Me.Accounts.SecondFactors.get_account_totp("some_id")
%AuthSecondFactor{}
Link to this function

maybe_authenticate(account, params)

View Source

Tries to authenticates an account using the provided parameters.

Examples

iex> Bonfire.Me.Accounts.SecondFactors.maybe_authenticate("account_id", %{"auth_second_factor" => %{"code" => "123456"}})
{:ok, :valid_totp}
Link to this function

maybe_cast_totp_changeset(changeset, params, opts)

View Source

Conditionally casts a TOTP changeset based on parameters and options.

Examples

iex> Bonfire.Me.Accounts.SecondFactors.maybe_cast_totp_changeset(changeset, %{auth_second_factor: %{code: "123456"}}, [])
%Ecto.Changeset{data: %AuthSecondFactor{}}

Generates a new TOTP secret if second-factor authentication is enabled.

The secret is a random 20 bytes binary that is used to generate the QR Code to enable 2FA using auth applications.

Examples

iex> Bonfire.Me.Accounts.SecondFactors.new()
"JBSWY3DPEHPK3PXP"

Generates a QR code for the TOTP URI.

Examples

iex> Bonfire.Me.Accounts.SecondFactors.new_qrcode()
"<svg>...</svg>"
Link to this function

new_struct(secret \\ nil)

View Source

Generates a new TOTP URI for use in an authentication app.

Examples

iex> Bonfire.Me.Accounts.SecondFactors.new_uri()
"otpauth://totp/bonfire:AccountName?secret=APYWY3DPEHPK3PXV&issuer=Issuer"

iex> Bonfire.Me.Accounts.SecondFactors.new_uri("JBSWY3DPEHPK3PXP")
"otpauth://totp/bonfire:AccountName?secret=JBSWY3DPEHPK3PXP&issuer=Issuer"
Link to this function

validate_account_totp(totp, code)

View Source

Validates if the given TOTP code is valid.

Examples

iex> Bonfire.Me.Accounts.SecondFactors.validate_account_totp(%AuthSecondFactor{id: "some_id", secret: "JBSWY3DPEHPK3PXP"}, "123456")
:valid_totp

 iex> Bonfire.Me.Accounts.SecondFactors.validate_account_totp("some_id", "123456")
:valid_totp