Go · 3536 bytes Raw Blame History
1 // Code generated by sqlc. DO NOT EDIT.
2 // versions:
3 // sqlc v1.31.1
4 // source: user_totp.sql
5
6 package usersdb
7
8 import (
9 "context"
10 )
11
12 const bumpTOTPCounter = `-- name: BumpTOTPCounter :execrows
13 UPDATE user_totp
14 SET last_used_counter = $2
15 WHERE user_id = $1 AND $2::bigint > last_used_counter
16 `
17
18 type BumpTOTPCounterParams struct {
19 UserID int64
20 LastUsedCounter int64
21 }
22
23 // Atomically advances last_used_counter only when the proposed counter is
24 // strictly greater. Returns rows affected — 0 means a replay attempt and
25 // the caller should reject the code.
26 func (q *Queries) BumpTOTPCounter(ctx context.Context, db DBTX, arg BumpTOTPCounterParams) (int64, error) {
27 result, err := db.Exec(ctx, bumpTOTPCounter, arg.UserID, arg.LastUsedCounter)
28 if err != nil {
29 return 0, err
30 }
31 return result.RowsAffected(), nil
32 }
33
34 const confirmUserTOTP = `-- name: ConfirmUserTOTP :execrows
35 UPDATE user_totp
36 SET confirmed_at = now(),
37 last_used_counter = $2
38 WHERE user_id = $1 AND confirmed_at IS NULL
39 `
40
41 type ConfirmUserTOTPParams struct {
42 UserID int64
43 LastUsedCounter int64
44 }
45
46 // Sets confirmed_at on a pending row. Returns the number of rows updated;
47 // callers MUST check this to handle the parallel-enrollment race
48 // (only one of two concurrent confirms wins).
49 func (q *Queries) ConfirmUserTOTP(ctx context.Context, db DBTX, arg ConfirmUserTOTPParams) (int64, error) {
50 result, err := db.Exec(ctx, confirmUserTOTP, arg.UserID, arg.LastUsedCounter)
51 if err != nil {
52 return 0, err
53 }
54 return result.RowsAffected(), nil
55 }
56
57 const deleteUserTOTP = `-- name: DeleteUserTOTP :exec
58 DELETE FROM user_totp WHERE user_id = $1
59 `
60
61 func (q *Queries) DeleteUserTOTP(ctx context.Context, db DBTX, userID int64) error {
62 _, err := db.Exec(ctx, deleteUserTOTP, userID)
63 return err
64 }
65
66 const getUserTOTP = `-- name: GetUserTOTP :one
67 SELECT id, user_id, secret_encrypted, secret_nonce, confirmed_at,
68 last_used_counter, created_at, updated_at
69 FROM user_totp
70 WHERE user_id = $1
71 `
72
73 func (q *Queries) GetUserTOTP(ctx context.Context, db DBTX, userID int64) (UserTotp, error) {
74 row := db.QueryRow(ctx, getUserTOTP, userID)
75 var i UserTotp
76 err := row.Scan(
77 &i.ID,
78 &i.UserID,
79 &i.SecretEncrypted,
80 &i.SecretNonce,
81 &i.ConfirmedAt,
82 &i.LastUsedCounter,
83 &i.CreatedAt,
84 &i.UpdatedAt,
85 )
86 return i, err
87 }
88
89 const upsertUserTOTP = `-- name: UpsertUserTOTP :one
90
91 INSERT INTO user_totp (user_id, secret_encrypted, secret_nonce)
92 VALUES ($1, $2, $3)
93 ON CONFLICT (user_id) DO UPDATE
94 SET secret_encrypted = EXCLUDED.secret_encrypted,
95 secret_nonce = EXCLUDED.secret_nonce,
96 confirmed_at = NULL,
97 last_used_counter = 0
98 WHERE user_totp.confirmed_at IS NULL
99 RETURNING id, user_id, secret_encrypted, secret_nonce, confirmed_at,
100 last_used_counter, created_at, updated_at
101 `
102
103 type UpsertUserTOTPParams struct {
104 UserID int64
105 SecretEncrypted []byte
106 SecretNonce []byte
107 }
108
109 // SPDX-License-Identifier: AGPL-3.0-or-later
110 // Inserts a new pending TOTP row, or replaces an existing pending row for
111 // the same user. Confirmed rows are NOT replaced — disable+regenerate
112 // must go through the dedicated query.
113 func (q *Queries) UpsertUserTOTP(ctx context.Context, db DBTX, arg UpsertUserTOTPParams) (UserTotp, error) {
114 row := db.QueryRow(ctx, upsertUserTOTP, arg.UserID, arg.SecretEncrypted, arg.SecretNonce)
115 var i UserTotp
116 err := row.Scan(
117 &i.ID,
118 &i.UserID,
119 &i.SecretEncrypted,
120 &i.SecretNonce,
121 &i.ConfirmedAt,
122 &i.LastUsedCounter,
123 &i.CreatedAt,
124 &i.UpdatedAt,
125 )
126 return i, err
127 }
128