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
+19 -4
View File
@@ -83,9 +83,19 @@ type User struct {
SignedIn time.Time // their last sign-in; zero if never
HasPassword bool
passwordHash string
FailedSignIns int // failed in a row since the last sign-in
LockedAt time.Time // zero unless locked after too many failures
TwoFactor bool // signs in with an authenticator app's code too
RecoveryLeft int // unused recovery codes
passwordHash string
totpStep int64
recoveryCodes []string
}
// Locked says whether the account is locked after too many failures.
func (u *User) Locked() bool { return !u.LockedAt.IsZero() }
// Membership is a user's place in a team.
type Membership struct {
Team int64
@@ -127,12 +137,14 @@ func NormalEmail(s string) (string, error) {
return strings.ToLower(s), nil
}
const userCols = `id, email, name, kind, status, password_hash, title, time_zone, photo, emails, created_at, signed_in_at`
const userCols = `id, email, name, kind, status, password_hash, title, time_zone, photo, emails, created_at, signed_in_at, failed_signins, locked_at, two_factor, totp_step, recovery_codes`
func scanUser(row interface{ Scan(...any) error }) (*User, error) {
var u User
var kind, status, emails, created, signedIn string
err := row.Scan(&u.ID, &u.Email, &u.Name, &kind, &status, &u.passwordHash, &u.Title, &u.TimeZone, &u.Photo, &emails, &created, &signedIn)
var kind, status, emails, created, signedIn, locked, codes string
var twoFactor int
err := row.Scan(&u.ID, &u.Email, &u.Name, &kind, &status, &u.passwordHash, &u.Title, &u.TimeZone, &u.Photo, &emails, &created, &signedIn,
&u.FailedSignIns, &locked, &twoFactor, &u.totpStep, &codes)
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotFound
}
@@ -143,6 +155,9 @@ func scanUser(row interface{ Scan(...any) error }) (*User, error) {
_ = json.Unmarshal([]byte(emails), &u.Emails)
u.Created, u.SignedIn = parseTS(created), parseTS(signedIn)
u.HasPassword = u.passwordHash != ""
u.LockedAt, u.TwoFactor = parseTS(locked), twoFactor != 0
_ = json.Unmarshal([]byte(codes), &u.recoveryCodes)
u.RecoveryLeft = len(u.recoveryCodes)
return &u, nil
}