package store import ( "context" "database/sql" "errors" ) // QR login ticket states. A ticket is created by the browser, promoted to // "scanned" when the phone reads the code, then to "confirmed" or "denied" by // the operator. The browser may exchange it exactly once ("consumed"). const ( QRStatusPending = "pending" QRStatusScanned = "scanned" QRStatusConfirmed = "confirmed" QRStatusDenied = "denied" QRStatusConsumed = "consumed" ) // QRTicket is a short lived hand-off between a browser login page and the // mobile app. The plain ticket and secret never touch the database: only their // SHA-256 digests do, exactly like session tokens. type QRTicket struct { ID string // sha256(ticket) SecretHash string // sha256(secret) UserID int64 // the account that approved the request Status string IP string // browser address shown to the phone before approving UA string AppIP string AppUA string CreatedAt int64 ExpiresAt int64 UpdatedAt int64 ConsumedAt int64 } const qrTicketColumns = `id, secret_hash, user_id, status, ip, ua, app_ip, app_ua, created_at, expires_at, updated_at, consumed_at` func scanQRTicket(row interface{ Scan(...any) error }) (*QRTicket, error) { var t QRTicket err := row.Scan(&t.ID, &t.SecretHash, &t.UserID, &t.Status, &t.IP, &t.UA, &t.AppIP, &t.AppUA, &t.CreatedAt, &t.ExpiresAt, &t.UpdatedAt, &t.ConsumedAt) if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } if err != nil { return nil, err } return &t, nil } // CreateQRTicket stores a new pending challenge. func (s *Store) CreateQRTicket(ctx context.Context, t *QRTicket) error { _, err := s.db.ExecContext(ctx, `INSERT INTO qr_login_tickets(id, secret_hash, user_id, status, ip, ua, app_ip, app_ua, created_at, expires_at, updated_at, consumed_at) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)`, t.ID, t.SecretHash, t.UserID, t.Status, t.IP, t.UA, t.AppIP, t.AppUA, t.CreatedAt, t.ExpiresAt, t.UpdatedAt, t.ConsumedAt) return err } // QRTicketByID returns a ticket regardless of its state; callers decide what a // stale row means so the UI can explain it. func (s *Store) QRTicketByID(ctx context.Context, id string) (*QRTicket, error) { return scanQRTicket(s.db.QueryRowContext(ctx, `SELECT `+qrTicketColumns+` FROM qr_login_tickets WHERE id = ?`, id)) } // MarkQRTicketScanned records that the phone opened the code. Only a pending // ticket moves forward, so a re-scan cannot undo an approval. func (s *Store) MarkQRTicketScanned(ctx context.Context, id, ip, ua string, now int64) (bool, error) { res, err := s.db.ExecContext(ctx, `UPDATE qr_login_tickets SET status = ?, app_ip = ?, app_ua = ?, updated_at = ? WHERE id = ? AND status = ? AND expires_at > ?`, QRStatusScanned, ip, ua, now, id, QRStatusPending, now) if err != nil { return false, err } n, err := res.RowsAffected() return n > 0, err } // DecideQRTicket approves or rejects a challenge on behalf of userID. It is a // single conditional statement so two phones cannot both claim a ticket. func (s *Store) DecideQRTicket(ctx context.Context, id string, userID int64, approve bool, ip, ua string, now int64) (bool, error) { status := QRStatusDenied if approve { status = QRStatusConfirmed } res, err := s.db.ExecContext(ctx, `UPDATE qr_login_tickets SET status = ?, user_id = ?, app_ip = ?, app_ua = ?, updated_at = ? WHERE id = ? AND status IN (?, ?) AND expires_at > ?`, status, userID, ip, ua, now, id, QRStatusPending, QRStatusScanned, now) if err != nil { return false, err } n, err := res.RowsAffected() return n > 0, err } // ConsumeQRTicket burns a confirmed ticket so it can only ever create a single // browser session. func (s *Store) ConsumeQRTicket(ctx context.Context, id string, now int64) (bool, error) { res, err := s.db.ExecContext(ctx, `UPDATE qr_login_tickets SET status = ?, consumed_at = ?, updated_at = ? WHERE id = ? AND status = ? AND expires_at > ?`, QRStatusConsumed, now, now, id, QRStatusConfirmed, now) if err != nil { return false, err } n, err := res.RowsAffected() return n > 0, err } // PurgeQRTickets drops tickets whose TTL has passed. func (s *Store) PurgeQRTickets(ctx context.Context, now int64) (int64, error) { res, err := s.db.ExecContext(ctx, `DELETE FROM qr_login_tickets WHERE expires_at < ?`, now) if err != nil { return 0, err } return res.RowsAffected() }