Removes standalone "// v0.X.X:" comment lines and inline trailing version annotations. Keeps version references in config field docs and compatibility notes where they describe requirements. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package sqlite
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
)
|
|
|
|
// TicketStore manages WS auth tickets in SQLite.
|
|
type TicketStore struct{}
|
|
|
|
func NewTicketStore() *TicketStore { return &TicketStore{} }
|
|
|
|
func (s *TicketStore) Issue(ctx context.Context, userID string) (string, error) {
|
|
b := make([]byte, 16)
|
|
if _, err := rand.Read(b); err != nil {
|
|
return "", err
|
|
}
|
|
id := hex.EncodeToString(b)
|
|
_, err := DB.ExecContext(ctx, `
|
|
INSERT INTO ws_tickets (id, user_id, expires_at)
|
|
VALUES (?, ?, datetime('now', '+30 seconds'))`, id, userID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
func (s *TicketStore) Validate(ctx context.Context, ticketID string) (string, bool) {
|
|
// SQLite has no DELETE ... RETURNING. Two-step: select then delete.
|
|
var userID string
|
|
err := DB.QueryRowContext(ctx, `
|
|
SELECT user_id FROM ws_tickets
|
|
WHERE id = ? AND expires_at > datetime('now')`, ticketID).Scan(&userID)
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return "", false
|
|
}
|
|
return "", false
|
|
}
|
|
DB.ExecContext(ctx, `DELETE FROM ws_tickets WHERE id = ?`, ticketID)
|
|
return userID, true
|
|
}
|
|
|
|
func (s *TicketStore) Reap(ctx context.Context) (int, error) {
|
|
res, err := DB.ExecContext(ctx, `DELETE FROM ws_tickets WHERE expires_at <= datetime('now')`)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
n, _ := res.RowsAffected()
|
|
return int(n), nil
|
|
}
|