Phase 10, part 2: the first owner, from a one-time setup link

A new server prints a link to create its owner (supermarket setup-link
makes another); until it's used, every page says the setup isn't
finished. One-time links (schema 33) are kept as hashes and used up in
one statement; resets and invitations will use them too. The server
edition opens Postgres.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 08:53:14 -07:00
co-authored by Claude Opus 5.5
parent 96404a3580
commit 72455438c9
16 changed files with 553 additions and 10 deletions
+100
View File
@@ -0,0 +1,100 @@
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
)
// 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,
}
// 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
}