仰望星辰工作室

miaopanel

miaopanel/ internal/store/qrlogin.go 4.3 KB · 129 行 原始文件
1package store
2
3import (
4 "context"
5 "database/sql"
6 "errors"
7)
8
9// QR login ticket states. A ticket is created by the browser, promoted to
10// "scanned" when the phone reads the code, then to "confirmed" or "denied" by
11// the operator. The browser may exchange it exactly once ("consumed").
12const (
13 QRStatusPending = "pending"
14 QRStatusScanned = "scanned"
15 QRStatusConfirmed = "confirmed"
16 QRStatusDenied = "denied"
17 QRStatusConsumed = "consumed"
18)
19
20// QRTicket is a short lived hand-off between a browser login page and the
21// mobile app. The plain ticket and secret never touch the database: only their
22// SHA-256 digests do, exactly like session tokens.
23type QRTicket struct {
24 ID string // sha256(ticket)
25 SecretHash string // sha256(secret)
26 UserID int64 // the account that approved the request
27 Status string
28 IP string // browser address shown to the phone before approving
29 UA string
30 AppIP string
31 AppUA string
32 CreatedAt int64
33 ExpiresAt int64
34 UpdatedAt int64
35 ConsumedAt int64
36}
37
38const qrTicketColumns = `id, secret_hash, user_id, status, ip, ua, app_ip, app_ua,
39 created_at, expires_at, updated_at, consumed_at`
40
41func scanQRTicket(row interface{ Scan(...any) error }) (*QRTicket, error) {
42 var t QRTicket
43 err := row.Scan(&t.ID, &t.SecretHash, &t.UserID, &t.Status, &t.IP, &t.UA,
44 &t.AppIP, &t.AppUA, &t.CreatedAt, &t.ExpiresAt, &t.UpdatedAt, &t.ConsumedAt)
45 if errors.Is(err, sql.ErrNoRows) {
46 return nil, ErrNotFound
47 }
48 if err != nil {
49 return nil, err
50 }
51 return &t, nil
52}
53
54// CreateQRTicket stores a new pending challenge.
55func (s *Store) CreateQRTicket(ctx context.Context, t *QRTicket) error {
56 _, err := s.db.ExecContext(ctx,
57 `INSERT INTO qr_login_tickets(id, secret_hash, user_id, status, ip, ua,
58 app_ip, app_ua, created_at, expires_at, updated_at, consumed_at)
59 VALUES(?,?,?,?,?,?,?,?,?,?,?,?)`,
60 t.ID, t.SecretHash, t.UserID, t.Status, t.IP, t.UA,
61 t.AppIP, t.AppUA, t.CreatedAt, t.ExpiresAt, t.UpdatedAt, t.ConsumedAt)
62 return err
63}
64
65// QRTicketByID returns a ticket regardless of its state; callers decide what a
66// stale row means so the UI can explain it.
67func (s *Store) QRTicketByID(ctx context.Context, id string) (*QRTicket, error) {
68 return scanQRTicket(s.db.QueryRowContext(ctx,
69 `SELECT `+qrTicketColumns+` FROM qr_login_tickets WHERE id = ?`, id))
70}
71
72// MarkQRTicketScanned records that the phone opened the code. Only a pending
73// ticket moves forward, so a re-scan cannot undo an approval.
74func (s *Store) MarkQRTicketScanned(ctx context.Context, id, ip, ua string, now int64) (bool, error) {
75 res, err := s.db.ExecContext(ctx,
76 `UPDATE qr_login_tickets
77 SET status = ?, app_ip = ?, app_ua = ?, updated_at = ?
78 WHERE id = ? AND status = ? AND expires_at > ?`,
79 QRStatusScanned, ip, ua, now, id, QRStatusPending, now)
80 if err != nil {
81 return false, err
82 }
83 n, err := res.RowsAffected()
84 return n > 0, err
85}
86
87// DecideQRTicket approves or rejects a challenge on behalf of userID. It is a
88// single conditional statement so two phones cannot both claim a ticket.
89func (s *Store) DecideQRTicket(ctx context.Context, id string, userID int64, approve bool, ip, ua string, now int64) (bool, error) {
90 status := QRStatusDenied
91 if approve {
92 status = QRStatusConfirmed
93 }
94 res, err := s.db.ExecContext(ctx,
95 `UPDATE qr_login_tickets
96 SET status = ?, user_id = ?, app_ip = ?, app_ua = ?, updated_at = ?
97 WHERE id = ? AND status IN (?, ?) AND expires_at > ?`,
98 status, userID, ip, ua, now, id, QRStatusPending, QRStatusScanned, now)
99 if err != nil {
100 return false, err
101 }
102 n, err := res.RowsAffected()
103 return n > 0, err
104}
105
106// ConsumeQRTicket burns a confirmed ticket so it can only ever create a single
107// browser session.
108func (s *Store) ConsumeQRTicket(ctx context.Context, id string, now int64) (bool, error) {
109 res, err := s.db.ExecContext(ctx,
110 `UPDATE qr_login_tickets
111 SET status = ?, consumed_at = ?, updated_at = ?
112 WHERE id = ? AND status = ? AND expires_at > ?`,
113 QRStatusConsumed, now, now, id, QRStatusConfirmed, now)
114 if err != nil {
115 return false, err
116 }
117 n, err := res.RowsAffected()
118 return n > 0, err
119}
120
121// PurgeQRTickets drops tickets whose TTL has passed.
122func (s *Store) PurgeQRTickets(ctx context.Context, now int64) (int64, error) {
123 res, err := s.db.ExecContext(ctx,
124 `DELETE FROM qr_login_tickets WHERE expires_at < ?`, now)
125 if err != nil {
126 return 0, err
127 }
128 return res.RowsAffected()
129}