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
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{}}
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
Formats a TOTP secret into a human-readable format.
Examples
iex> Bonfire.Me.Accounts.SecondFactors.format_secret("JBSWY3DPEHPK3PXP")
"JBSW Y3DP EHPK 3PXP"
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{}
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}
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>"
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"
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