NimbleTOTP

repository·master·Indexed 19 days ago

https://github.com/dashbitco/nimble_totp

A lightweight Elixir library for implementing Time-based One-Time Passwords (TOTP) for two-factor authentication (2FA). It provides functionality to generate random secrets, create otpauth URIs for QR codes, generate verification codes, and validate user-provided codes.

Tokens
470
Snippets
4
Records
4
Agent score
17%

What's inside nimble_totp

  1. Generate and validate TOTP codes

    master

    NimbleTOTP provides functions to generate the current code and verify a user-provided code against a secret.

    • NimbleTOTP.verification_code(secret): Returns the current Time-Based One-Time Password as a string.
    • NimbleTOTP.valid?(secret, code): Returns true if the provided code is valid for the given secret, otherwise false.
    # Generate the current code
    NimbleTOTP.verification_code(secret)
    #=> "569777"
    
    # Validate a code
    NimbleTOTP.valid?(secret, "569777")
    #=> true
  2. Generate an otpauth URI for QR codes

    master

    Use NimbleTOTP.otpauth_uri/3 to generate a URI that can be encoded into a QR code for users to scan with authenticator apps.

    Arguments:

    • label: A string representing the user/account identifier (e.g., "Acme:alice").
    • secret: The random byte secret generated via NimbleTOTP.secret/0.
    • opts: A keyword list containing configuration, such as issuer: "Acme".
    NimbleTOTP.otpauth_uri("Acme:alice", secret, issuer: "Acme")
    #=> "otpauth://totp/Acme:alice?secret=MFRGGZA&issuer=Acme"
  3. Generate a TOTP secret

    master

    Use NimbleTOTP.secret/0 to generate a secret composed of random bytes. This secret is used for both generating URIs and validating subsequent OTP codes.

    secret = NimbleTOTP.secret()
    #=> <<63, 24, 42, 30, 95, 116, 80, 121, 106, 102>>