View Source Bonfire.Me.Accounts.SecondFactors (Bonfire v0.9.11-social-beta.6)

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

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

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{}}

delete_account_totp(account_totp)

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")

enabled?()

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

Examples

iex> Bonfire.Me.Accounts.SecondFactors.enabled?()
true

format_secret(secret \\ new())

Formats a TOTP secret into a human-readable format.

Examples

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

get_account_totp(account)

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{}

maybe_authenticate(account, params)

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}

maybe_cast_totp_changeset(changeset, params, opts)

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{}}

new()

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"

new_qrcode(opts \\ [])

Generates a QR code for the TOTP URI.

Examples

iex> Bonfire.Me.Accounts.SecondFactors.new_qrcode()
"<svg>...</svg>"

new_struct(secret \\ nil)

new_uri(secret \\ nil)

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"

validate_account_totp(totp, code)

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