### Generate otpauth URI for QR code Source: https://hexdocs.pm/nimble_totp Creates an otpauth URI string and demonstrates how to convert it into an SVG QR code using EQRCode. ```elixir uri = NimbleTOTP.otpauth_uri("Acme:alice", secret, issuer: "Acme") #=> "otpauth://totp/Acme:alice?secret=MFRGGZA&issuer=Acme" uri |> EQRCode.encode() |> EQRCode.svg() #=> "\n NimbleTOTP.otpauth_uri("Acme:alice", "abcd", issuer: "Acme") "otpauth://totp/Acme:alice?secret=MFRGGZA&issuer=Acme" ``` -------------------------------- ### Generate and validate verification codes Source: https://hexdocs.pm/nimble_totp Computes the current verification code and demonstrates how to validate it against a secret. ```elixir NimbleTOTP.verification_code(secret) #=> "569777" ``` ```elixir NimbleTOTP.valid?(secret, "569777") #=> true NimbleTOTP.valid?(secret, "012345") #=> false ``` -------------------------------- ### NimbleTOTP Functions Source: https://hexdocs.pm/nimble_totp Core functions for managing TOTP lifecycle including secret generation, URI creation, code generation, and validation. ```APIDOC ## otpauth_uri(label, secret, uri_params) ### Description Generate the URI to be encoded in the QR code. ### Parameters - **label** (String.t) - Required - The label for the TOTP entry. - **secret** (String.t) - Required - The secret key. - **uri_params** (keyword) - Optional - Additional parameters for the URI. ## secret(size) ### Description Generate a binary composed of random bytes. ### Parameters - **size** (non_neg_integer) - Optional - The number of bytes (default 20). ## valid?(secret, otp, opts) ### Description Checks if the given otp code matches the given secret. ### Parameters - **secret** (binary) - Required - The secret key. - **otp** (String.t) - Required - The code to validate. - **opts** (list) - Optional - Options including :time, :since, and :period. ## verification_code(secret, opts) ### Description Generate Time-Based One-Time Password (TOTP). ### Parameters - **secret** (binary) - Required - The secret key. - **opts** (list) - Optional - Options including :time and :period. ``` -------------------------------- ### Generate Random Secret Source: https://hexdocs.pm/nimble_totp Generates a binary composed of random bytes for TOTP secrets. Defaults to 20 bytes as per RFC. ```elixir NimbleTOTP.secret() #=> <<178, 117, 46, 7, 172, 202, 108, 127, 186, 180, ...>> ``` -------------------------------- ### Generate a random secret Source: https://hexdocs.pm/nimble_totp Generates a binary secret of 20 random bytes for use in TOTP. ```elixir secret = NimbleTOTP.secret() #=> <<178, 117, 46, 7, 172, 202, 108, 127, 186, 180, ...>> ``` -------------------------------- ### Validate TOTP Code with Grace Period Source: https://hexdocs.pm/nimble_totp Checks if a given OTP code is valid against a secret, allowing for a grace period by checking against the current time and 30 seconds prior. This helps ensure the previous code is still valid. ```elixir def valid_code?(secret, otp) do time = System.os_time(:second) NimbleTOTP.valid?(secret, otp, time: time) or NimbleTOTP.valid?(secret, otp, time: time - 30) end ``` -------------------------------- ### Generate TOTP Verification Code Source: https://hexdocs.pm/nimble_totp Generates a Time-Based One-Time Password (TOTP) code from a secret. The code is valid for a default period of 30 seconds. ```elixir secret = Base.decode32!("PTEPUGZ7DUWTBGMW4WLKB6U63MGKKMCA") NimbleTOTP.verification_code(secret) #=> "569777" ``` -------------------------------- ### Prevent code reuse Source: https://hexdocs.pm/nimble_totp Validates a TOTP code while ensuring it has not been reused by checking against the last successful usage timestamp. ```elixir NimbleTOTP.valid?(user.totp_secret, code, since: user.last_totp_at) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.