Ex2FA (ex_2fa v0.1.0)

Copy Markdown View Source

A zero-dependency Elixir Two-Factor Authentication (2FA) library with full API compatibility for nio_google_authenticator.

Features

  • TOTP (RFC 6238) - generate and validate time-based one-time passwords with configurable digits, intervals, hash algorithms (:sha, :sha256, :sha512), clock-skew windows, and replay protection (:since / :last_used_at / :last_step).
  • HOTP (RFC 4226) - counter-based one-time passwords with look-ahead window resynchronization.
  • Provisioning URIs - standard otpauth://totp/... and otpauth://hotp/... URIs via otpauth_uri/3 and hotp_uri/4, plus generate_url/3 for nio_google_authenticator compatibility.
  • Recovery Codes - generate, hash, and verify single-use backup codes via Ex2FA.RecoveryCodes.
  • Ecto Integration - optional Ex2FA.Changeset helpers matching NioGoogleAuthenticator.Changeset.

Configuration

You can configure a default issuer globally:

config :ex_2fa, issuer: "MyApp"

For seamless migration, if :ex_2fa does not have :issuer configured, Ex2FA automatically falls back to Application.get_env(:nio_google_authenticator, :issuer).

Summary

Functions

Generates an HOTP token for secret and counter.

Generates a list of single-use recovery codes. See Ex2FA.RecoveryCodes.generate/1.

Creates a cryptographically secure random Base32-encoded secret string.

Generates a valid TOTP token string based on secret.

Creates a Google Charts QR code URL that includes secret, label, and issuer.

Generates a standard otpauth://hotp/... provisioning URI.

Generates a standard otpauth://totp/... provisioning URI suitable for QR code generation in modern authenticator apps.

Validates an HOTP token against secret and counter, returning {:ok, :pass} or {:error, :invalid_token}.

Validates a recovery code against a list of plaintext or hashed recovery codes and returns {:ok, remaining_codes} or {:error, :invalid_code}.

Validates a given TOTP token based on secret.

Verifies an HOTP token with optional look-ahead :window and returns {:ok, matched_counter} on success so the caller can advance the stored counter to matched_counter + 1.

Validates a TOTP token and returns {:ok, matched_step} on success, allowing callers to persist matched_step and pass it back via :last_step to prevent replay attacks.

Functions

generate_hotp(secret, counter, options \\ [])

@spec generate_hotp(binary(), non_neg_integer(), [Ex2FA.HOTP.option()]) :: binary()

Generates an HOTP token for secret and counter.

generate_recovery_codes(options \\ [])

@spec generate_recovery_codes([Ex2FA.RecoveryCodes.option()]) :: [binary()]

Generates a list of single-use recovery codes. See Ex2FA.RecoveryCodes.generate/1.

generate_secret(byte_length \\ 10)

@spec generate_secret(pos_integer()) :: binary()

Creates a cryptographically secure random Base32-encoded secret string.

Defaults to 10 random bytes (80 bits / 16 Base32 characters) for exact compatibility with nio_google_authenticator. Pass byte_length (e.g., 20 for 160-bit RFC 4226 recommended secrets) to customize secret strength.

Examples

secret = Ex2FA.generate_secret()
strong_secret = Ex2FA.generate_secret(20)

generate_token(secret, options \\ [])

@spec generate_token(binary(), [Ex2FA.TOTP.option()]) :: binary()

Generates a valid TOTP token string based on secret.

Compatible with NioGoogleAuthenticator.generate_token/1. Accepts optional keyword options supported by Ex2FA.TOTP.generate/2.

generate_url(secret, label, issuer \\ default_issuer())

@spec generate_url(binary(), binary(), binary() | nil) :: binary()

Creates a Google Charts QR code URL that includes secret, label, and issuer.

Preserves exact compatibility with NioGoogleAuthenticator.generate_url/2,3. For new applications, prefer otpauth_uri/3 with a local QR code renderer.

hotp_uri(secret, label, counter, options \\ [])

@spec hotp_uri(binary(), binary(), non_neg_integer(), [Ex2FA.URI.hotp_option()]) ::
  binary()

Generates a standard otpauth://hotp/... provisioning URI.

otpauth_uri(secret, label, options \\ [])

@spec otpauth_uri(binary(), binary(), [Ex2FA.URI.totp_option()]) :: binary()

Generates a standard otpauth://totp/... provisioning URI suitable for QR code generation in modern authenticator apps.

If :issuer is not specified in options, falls back to the configured default issuer (if any).

validate_hotp(secret, token, counter, options \\ [])

@spec validate_hotp(term(), term(), term(), [Ex2FA.HOTP.option()]) ::
  {:ok, :pass} | {:error, :invalid_token} | {:error, binary()}

Validates an HOTP token against secret and counter, returning {:ok, :pass} or {:error, :invalid_token}.

validate_recovery_code(code, codes, options \\ [])

@spec validate_recovery_code(binary(), [binary()], [
  Ex2FA.RecoveryCodes.validate_option()
]) ::
  {:ok, [binary()]} | {:error, :invalid_code}

Validates a recovery code against a list of plaintext or hashed recovery codes and returns {:ok, remaining_codes} or {:error, :invalid_code}.

validate_token(secret, token, options \\ [])

@spec validate_token(term(), term(), [Ex2FA.TOTP.option()]) ::
  {:ok, :pass} | {:error, :invalid_token} | {:error, binary()}

Validates a given TOTP token based on secret.

Compatible with NioGoogleAuthenticator.validate_token/2,3.

Options

  • :token_length (alias :digits) - default 6
  • :interval_length (alias :period) - default 30
  • :window - clock-skew tolerance in steps (-window..+window), default 0
  • :algorithm (alias :digest_method) - :sha (:sha1), :sha256, or :sha512, default :sha
  • :time (alias :timestamp) - Unix timestamp, DateTime, or Erlang timestamp tuple
  • :since (alias :last_used_at) - replay protection timestamp
  • :last_step - replay protection last accepted time step

Returns {:ok, :pass} if the token is valid, {:error, :invalid_token} if invalid, or {:error, "Both secret and token must be strings"} if either argument is not a binary.

verify_hotp(secret, token, counter, options \\ [])

@spec verify_hotp(binary(), binary(), non_neg_integer(), [Ex2FA.HOTP.option()]) ::
  {:ok, non_neg_integer()} | {:error, :invalid_token}

Verifies an HOTP token with optional look-ahead :window and returns {:ok, matched_counter} on success so the caller can advance the stored counter to matched_counter + 1.

verify_token(secret, token, options \\ [])

@spec verify_token(binary(), binary(), [Ex2FA.TOTP.option()]) ::
  {:ok, non_neg_integer()} | {:error, :invalid_token}

Validates a TOTP token and returns {:ok, matched_step} on success, allowing callers to persist matched_step and pass it back via :last_step to prevent replay attacks.