Phase 10, part 3: signing in and out, resets, locks and two-factor

The server edition now starts. Sign-in by email and password, limited per
address and per IP; ten failures in a row lock the account and email its
owner, and a reset by email (or an admin) unlocks it. Settings → Your
account has the profile, the password, where you're signed in, and
two-factor sign-in with an authenticator app (TOTP, from a QR code) and
ten recovery codes. The app's own mail goes through SUPERMARKET_MAIL_URL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 09:01:29 -07:00
co-authored by Claude Opus 5.5
parent 72455438c9
commit 8040512a18
26 changed files with 1991 additions and 12 deletions
+118
View File
@@ -263,3 +263,121 @@ func TestLinks(t *testing.T) {
t.Error("unknown purpose")
}
}
func TestLockAndThrottle(t *testing.T) {
d := openDB(t)
now := time.Date(2026, 11, 25, 10, 0, 0, 0, time.UTC)
u := &User{Email: "pat@example.com", Status: Active}
Create(d, u, now)
for i := 1; i <= LockAfter; i++ {
locked, err := FailedSignIn(d, u, now)
if err != nil {
t.Fatal(err)
}
if locked != (i == LockAfter) {
t.Errorf("failure %d: locked %v", i, locked)
}
}
if locked, _ := FailedSignIn(d, u, now); locked {
t.Error("locked twice")
}
got, _ := ByID(d, u.ID)
if !got.Locked() || got.FailedSignIns != LockAfter+1 {
t.Errorf("after: %+v", got)
}
Unlock(d, u.ID)
if got, _ = ByID(d, u.ID); got.Locked() || got.FailedSignIns != 0 {
t.Error("still locked")
}
key := "signin:ip:203.0.113.5"
for i := 0; i < 3; i++ {
if err := Fail(d, key, ThrottleWindow, now.Add(time.Duration(i)*time.Minute)); err != nil {
t.Fatal(err)
}
}
if n, _ := Fails(d, key, ThrottleWindow, now.Add(5*time.Minute)); n != 3 {
t.Errorf("fails %d", n)
}
if n, _ := Fails(d, key, ThrottleWindow, now.Add(ThrottleWindow)); n != 0 {
t.Errorf("after the window: %d", n)
}
Fail(d, key, ThrottleWindow, now.Add(ThrottleWindow)) // a new window
if n, _ := Fails(d, key, ThrottleWindow, now.Add(ThrottleWindow)); n != 1 {
t.Errorf("new window: %d", n)
}
ClearFails(d, key)
if n, _ := Fails(d, key, ThrottleWindow, now.Add(ThrottleWindow)); n != 0 {
t.Errorf("cleared: %d", n)
}
}
func TestTOTP(t *testing.T) {
// RFC 6238's test secret, at 59 seconds: 94287082, of which apps show
// the last six digits.
secret := "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"
at := time.Unix(59, 0)
if got := TOTPCode(secret, at); got != "287082" {
t.Errorf("code %s", got)
}
step, ok := CheckTOTP(secret, "287 082", 0, at)
if !ok || step != 1 {
t.Errorf("check: %d %v", step, ok)
}
if _, ok := CheckTOTP(secret, "287082", 1, at); ok {
t.Error("a used code worked again")
}
if _, ok := CheckTOTP(secret, "287082", 0, at.Add(30*time.Second)); !ok {
t.Error("a step late: refused")
}
if _, ok := CheckTOTP(secret, "287082", 0, at.Add(90*time.Second)); ok {
t.Error("three steps late: accepted")
}
if _, ok := CheckTOTP(secret, "000000", 0, at); ok {
t.Error("wrong code")
}
s, _ := NewTOTPSecret()
if len(s) != 32 {
t.Errorf("secret %s", s)
}
if u := TOTPURL("Supermarket (acme.com)", "pat@example.com", s); !strings.HasPrefix(u, "otpauth://totp/Supermarket%20%28acme.com%29:pat@example.com?") {
t.Errorf("url %s", u)
}
}
func TestRecoveryCodes(t *testing.T) {
d := openDB(t)
u := &User{Email: "pat@example.com", Status: Active}
Create(d, u, time.Now())
codes, hashes, err := NewRecoveryCodes()
if err != nil || len(codes) != 10 || len(codes[0]) != 14 {
t.Fatalf("%v %v", codes, err)
}
if err := SetTwoFactor(d, u, true, hashes); err != nil {
t.Fatal(err)
}
u, _ = ByID(d, u.ID)
if !u.TwoFactor || u.RecoveryLeft != 10 {
t.Errorf("%+v", u)
}
if ok, _ := UseRecoveryCode(d, u, strings.ToUpper(codes[3])); !ok {
t.Error("code refused")
}
if ok, _ := UseRecoveryCode(d, u, codes[3]); ok {
t.Error("used twice")
}
// A stale copy of the user can't use a code another request used.
stale, _ := ByID(d, u.ID)
UseRecoveryCode(d, u, codes[4])
if ok, _ := UseRecoveryCode(d, stale, codes[4]); ok {
t.Error("used twice, from a stale copy")
}
u, _ = ByID(d, u.ID)
if u.RecoveryLeft != 8 {
t.Errorf("left %d", u.RecoveryLeft)
}
SetTwoFactor(d, u, false, nil)
if u, _ = ByID(d, u.ID); u.TwoFactor || u.RecoveryLeft != 0 {
t.Errorf("off: %+v", u)
}
}