1package catalog23import (4 "context"5 "database/sql"6 "time"7)89// ConsumeAuth atomically records a signed request identifier. It returns10// false when the identifier has already been consumed, providing replay11// protection for process-level creation endpoints across restarts.12func (c *Catalog) ConsumeAuth(ctx context.Context, id string, expires time.Time) (bool, error) {13 if id == "" {14 return false, nil15 }16 if _, err := c.db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS auth_nonces(id TEXT PRIMARY KEY, expires_at INTEGER NOT NULL)`); err != nil {17 return false, err18 }19 if _, err := c.db.ExecContext(ctx, `DELETE FROM auth_nonces WHERE expires_at < ?`, time.Now().Unix()); err != nil {20 return false, err21 }22 var inserted int6423 err := c.db.QueryRowContext(ctx, `INSERT INTO auth_nonces(id,expires_at) VALUES(?,?) ON CONFLICT(id) DO NOTHING RETURNING 1`, id, expires.Unix()).Scan(&inserted)24 if err == sql.ErrNoRows {25 return false, nil26 }27 return err == nil && inserted == 1, err28}