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>
103 lines
3.2 KiB
Go
103 lines
3.2 KiB
Go
package users
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"time"
|
|
|
|
"supermarket/internal/db"
|
|
)
|
|
|
|
// Purpose is what a one-time link is for.
|
|
type Purpose string
|
|
|
|
const (
|
|
SetupLink Purpose = "setup" // creates the first owner
|
|
ResetLink Purpose = "reset" // chooses a new password
|
|
InviteLink Purpose = "invite" // joins the team
|
|
CodeLink Purpose = "code" // a sign-in waiting for its two-factor code
|
|
)
|
|
|
|
// How long each kind of link lasts.
|
|
var linkTTL = map[Purpose]time.Duration{
|
|
SetupLink: 24 * time.Hour,
|
|
ResetLink: time.Hour,
|
|
InviteLink: 7 * 24 * time.Hour,
|
|
CodeLink: 5 * time.Minute,
|
|
}
|
|
|
|
// ErrBadLink is a link that's unknown, used, replaced or out of date.
|
|
var ErrBadLink = errors.New("this link has expired or was already used")
|
|
|
|
// NewLink makes a one-time link's token. It replaces the user's earlier
|
|
// link for the same purpose (the setup link has no user: one at a time).
|
|
// Only the token's hash is kept.
|
|
func NewLink(d *db.DB, p Purpose, user int64, now time.Time) (string, error) {
|
|
ttl, ok := linkTTL[p]
|
|
if !ok {
|
|
return "", errors.New("no such link")
|
|
}
|
|
token, err := NewToken()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
tx, err := d.Begin()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer tx.Rollback()
|
|
if _, err := tx.Exec(`DELETE FROM links WHERE purpose = ? AND user_id = ?`, string(p), user); err != nil {
|
|
return "", err
|
|
}
|
|
if _, err := tx.Exec(`INSERT INTO links (id, purpose, user_id, created_at, expires_at) VALUES (?, ?, ?, ?, ?)`,
|
|
hashToken(token), string(p), user, ts(now), ts(now.Add(ttl))); err != nil {
|
|
return "", err
|
|
}
|
|
return token, tx.Commit()
|
|
}
|
|
|
|
// PeekLink says whose a link is, without using it: to show its page.
|
|
func PeekLink(d *db.DB, token string, p Purpose, now time.Time) (int64, error) {
|
|
var user int64
|
|
err := d.QueryRow(`SELECT user_id FROM links WHERE id = ? AND purpose = ? AND expires_at > ?`, hashToken(token), string(p), ts(now)).Scan(&user)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return 0, ErrBadLink
|
|
}
|
|
return user, err
|
|
}
|
|
|
|
// UseLink uses a link up, once: of two people using it together, one
|
|
// gets ErrBadLink.
|
|
func UseLink(d *db.DB, token string, p Purpose, now time.Time) (int64, error) {
|
|
var user int64
|
|
err := d.QueryRow(`DELETE FROM links WHERE id = ? AND purpose = ? AND expires_at > ? RETURNING user_id`, hashToken(token), string(p), ts(now)).Scan(&user)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return 0, ErrBadLink
|
|
}
|
|
return user, err
|
|
}
|
|
|
|
// DropLinks withdraws a user's links for a purpose (a withdrawn
|
|
// invitation; a reset no longer needed).
|
|
func DropLinks(d *db.DB, p Purpose, user int64) error {
|
|
_, err := d.Exec(`DELETE FROM links WHERE purpose = ? AND user_id = ?`, string(p), user)
|
|
return err
|
|
}
|
|
|
|
// LinkExpiry is when a user's link for a purpose runs out; zero if they
|
|
// have none that works.
|
|
func LinkExpiry(d *db.DB, p Purpose, user int64, now time.Time) (time.Time, error) {
|
|
var exp string
|
|
err := d.QueryRow(`SELECT expires_at FROM links WHERE purpose = ? AND user_id = ? AND expires_at > ?`, string(p), user, ts(now)).Scan(&exp)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return time.Time{}, nil
|
|
}
|
|
return parseTS(exp), err
|
|
}
|
|
|
|
// PruneLinks deletes links that have run out.
|
|
func PruneLinks(d *db.DB, now time.Time) error {
|
|
_, err := d.Exec(`DELETE FROM links WHERE expires_at <= ?`, ts(now))
|
|
return err
|
|
}
|