fix(panel): share TOTP skew tolerance with VerifyTwoFactorCode

Move the +/-1 window helper to internal/util/totp so both 2FA
acceptance points use it: login (CheckUser) and disable/rebind plus
username/password changes (VerifyTwoFactorCode). Also shrink comments
to the 2-line house rule and anchor the unit test mid-window to avoid
a step-boundary flake.

Addresses review on #6546 (MEDIUM + 2 LOWs).
This commit is contained in:
sdhfsl
2026-09-15 18:56:09 +08:00
parent 959e6fd62b
commit f3096bb3c1
6 changed files with 64 additions and 57 deletions
@@ -4,6 +4,7 @@ import (
"path/filepath"
"regexp"
"testing"
"time"
"github.com/xlzd/gotp"
@@ -230,6 +231,9 @@ func TestVerifyTwoFactorCode(t *testing.T) {
if err := s.VerifyTwoFactorCode(gotp.NewDefaultTOTP(token).Now()); err != nil {
t.Fatalf("valid code rejected: %v", err)
}
if err := s.VerifyTwoFactorCode(gotp.NewDefaultTOTP(token).AtTime(time.Now().Add(-30 * time.Second))); err != nil {
t.Fatalf("previous window code rejected: %v", err)
}
if err := s.VerifyTwoFactorCode("000000"); err == nil {
t.Fatal("invalid code accepted")
}