relax totp time

This commit is contained in:
Graham McIntire 2026-01-28 15:32:06 -06:00
parent 1ac771c7a6
commit b13de895f7
2 changed files with 4 additions and 3 deletions

View file

@ -196,10 +196,11 @@ defmodule Towerops.Accounts do
Verifies a TOTP code against a secret. Verifies a TOTP code against a secret.
Returns true if the code is valid within the time window, false otherwise. Returns true if the code is valid within the time window, false otherwise.
Allows for +/- 1 time step (30 seconds) to handle clock drift.
""" """
def verify_totp(secret, code) when is_binary(secret) and is_binary(code) do def verify_totp(secret, code) when is_binary(secret) and is_binary(code) do
# Allow 30 second window for time drift # NimbleTOTP.valid? by default checks current time +/- 1 time step (30s)
NimbleTOTP.valid?(secret, code, since: System.system_time(:second) - 30) NimbleTOTP.valid?(secret, code)
end end
@doc """ @doc """

View file

@ -441,7 +441,7 @@ defmodule Towerops.AccountsTest do
# Generate code for current time (should always be valid) # Generate code for current time (should always be valid)
code = NimbleTOTP.verification_code(secret) code = NimbleTOTP.verification_code(secret)
# Verify it works with the time window check (since: -30) # Verify it works (checks current time +/- 1 step)
assert Accounts.verify_totp(secret, code) assert Accounts.verify_totp(secret, code)
end end