This repository has been archived on 2026-04-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
core/server/store/sqlite/tickets.go
Jeffrey Smith 3d4228f868
All checks were successful
CI/CD / detect-changes (push) Successful in 4s
CI/CD / test-frontend (push) Successful in 6s
CI/CD / test-sqlite (push) Successful in 2m46s
CI/CD / test-go-pg (push) Successful in 2m47s
CI/CD / build-and-deploy (push) Successful in 26s
Feat v0.6.3 dead code sweep (#38)
Co-authored-by: Jeffrey Smith <jasafpro@gmail.com>
Co-committed-by: Jeffrey Smith <jasafpro@gmail.com>
2026-03-31 12:37:47 +00:00

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
}