Ex2FA.URI (ex_2fa v0.1.0)

Copy Markdown View Source

Generates standard Key URI Format (otpauth://) provisioning URIs for TOTP and HOTP authenticator apps (Google Authenticator, 1Password, Authy, etc.), as well as the legacy Google Charts QR URL compatible with nio_google_authenticator.

Summary

Functions

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

Generates the Google Charts QR code URL in the exact format produced by nio_google_authenticator.

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

Types

hotp_option()

@type hotp_option() ::
  {:issuer, binary() | nil}
  | {:algorithm, :sha | :sha1 | :sha256 | :sha512}
  | {:digest_method, :sha | :sha1 | :sha256 | :sha512}
  | {:digits, pos_integer()}
  | {:token_length, pos_integer()}

totp_option()

@type totp_option() ::
  {:issuer, binary() | nil}
  | {:algorithm, :sha | :sha1 | :sha256 | :sha512}
  | {:digest_method, :sha | :sha1 | :sha256 | :sha512}
  | {:digits, pos_integer()}
  | {:token_length, pos_integer()}
  | {:period, pos_integer()}
  | {:interval_length, pos_integer()}

Functions

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

@spec hotp(binary(), binary(), non_neg_integer(), [hotp_option()]) :: binary()

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

Options

  • :issuer - provider or service name
  • :algorithm (alias :digest_method) - :sha (:sha1), :sha256, or :sha512
  • :digits (alias :token_length) - number of digits (included when not 6)

legacy_google_qr_url(secret, label, issuer)

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

Generates the Google Charts QR code URL in the exact format produced by nio_google_authenticator.

Note: Google deprecated the chart.googleapis.com QR endpoint. For new applications, prefer Ex2FA.otpauth_uri/3 paired with a local QR code library such as eqrcode.

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

@spec totp(binary(), binary(), [totp_option()]) :: binary()

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

Options

  • :issuer - provider or service name (defaults to configured :issuer if omitted)
  • :algorithm (alias :digest_method) - :sha (:sha1), :sha256, or :sha512 (included in URI when set to a non-default algorithm)
  • :digits (alias :token_length) - number of digits (included when not 6)
  • :period (alias :interval_length) - time step in seconds (included when not 30)

Examples

iex> Ex2FA.URI.totp("JBSWY3DPEHPK3PXP", "alice@example.com", issuer: "MyApp")
"otpauth://totp/MyApp:alice%40example.com?secret=JBSWY3DPEHPK3PXP&issuer=MyApp"