Go · 4112 bytes Raw Blame History
1 // Code generated by sqlc. DO NOT EDIT.
2 // versions:
3 // sqlc v1.31.1
4 // source: user_ssh_keys.sql
5
6 package usersdb
7
8 import (
9 "context"
10 "net/netip"
11 )
12
13 const countUserSSHKeys = `-- name: CountUserSSHKeys :one
14 SELECT count(*) FROM user_ssh_keys WHERE user_id = $1
15 `
16
17 func (q *Queries) CountUserSSHKeys(ctx context.Context, db DBTX, userID int64) (int64, error) {
18 row := db.QueryRow(ctx, countUserSSHKeys, userID)
19 var count int64
20 err := row.Scan(&count)
21 return count, err
22 }
23
24 const deleteUserSSHKey = `-- name: DeleteUserSSHKey :execrows
25 DELETE FROM user_ssh_keys WHERE id = $1 AND user_id = $2
26 `
27
28 type DeleteUserSSHKeyParams struct {
29 ID int64
30 UserID int64
31 }
32
33 // Scoped delete: caller must pass the owning user_id so a hijacked
34 // handler can never delete keys it doesn't own.
35 func (q *Queries) DeleteUserSSHKey(ctx context.Context, db DBTX, arg DeleteUserSSHKeyParams) (int64, error) {
36 result, err := db.Exec(ctx, deleteUserSSHKey, arg.ID, arg.UserID)
37 if err != nil {
38 return 0, err
39 }
40 return result.RowsAffected(), nil
41 }
42
43 const getUserSSHKeyByFingerprint = `-- name: GetUserSSHKeyByFingerprint :one
44 SELECT id, user_id, title, fingerprint_sha256, key_type, key_bits, public_key,
45 last_used_at, last_used_ip, created_at
46 FROM user_ssh_keys
47 WHERE fingerprint_sha256 = $1
48 `
49
50 // Hot path for sshd's AuthorizedKeysCommand. Index lookup via the UNIQUE
51 // index on fingerprint_sha256.
52 func (q *Queries) GetUserSSHKeyByFingerprint(ctx context.Context, db DBTX, fingerprintSha256 string) (UserSshKey, error) {
53 row := db.QueryRow(ctx, getUserSSHKeyByFingerprint, fingerprintSha256)
54 var i UserSshKey
55 err := row.Scan(
56 &i.ID,
57 &i.UserID,
58 &i.Title,
59 &i.FingerprintSha256,
60 &i.KeyType,
61 &i.KeyBits,
62 &i.PublicKey,
63 &i.LastUsedAt,
64 &i.LastUsedIp,
65 &i.CreatedAt,
66 )
67 return i, err
68 }
69
70 const insertUserSSHKey = `-- name: InsertUserSSHKey :one
71
72 INSERT INTO user_ssh_keys (user_id, title, fingerprint_sha256, key_type, key_bits, public_key)
73 VALUES ($1, $2, $3, $4, $5, $6)
74 RETURNING id, user_id, title, fingerprint_sha256, key_type, key_bits, public_key,
75 last_used_at, last_used_ip, created_at
76 `
77
78 type InsertUserSSHKeyParams struct {
79 UserID int64
80 Title string
81 FingerprintSha256 string
82 KeyType string
83 KeyBits int32
84 PublicKey string
85 }
86
87 // SPDX-License-Identifier: AGPL-3.0-or-later
88 func (q *Queries) InsertUserSSHKey(ctx context.Context, db DBTX, arg InsertUserSSHKeyParams) (UserSshKey, error) {
89 row := db.QueryRow(ctx, insertUserSSHKey,
90 arg.UserID,
91 arg.Title,
92 arg.FingerprintSha256,
93 arg.KeyType,
94 arg.KeyBits,
95 arg.PublicKey,
96 )
97 var i UserSshKey
98 err := row.Scan(
99 &i.ID,
100 &i.UserID,
101 &i.Title,
102 &i.FingerprintSha256,
103 &i.KeyType,
104 &i.KeyBits,
105 &i.PublicKey,
106 &i.LastUsedAt,
107 &i.LastUsedIp,
108 &i.CreatedAt,
109 )
110 return i, err
111 }
112
113 const listUserSSHKeys = `-- name: ListUserSSHKeys :many
114 SELECT id, user_id, title, fingerprint_sha256, key_type, key_bits, public_key,
115 last_used_at, last_used_ip, created_at
116 FROM user_ssh_keys
117 WHERE user_id = $1
118 ORDER BY created_at DESC
119 `
120
121 func (q *Queries) ListUserSSHKeys(ctx context.Context, db DBTX, userID int64) ([]UserSshKey, error) {
122 rows, err := db.Query(ctx, listUserSSHKeys, userID)
123 if err != nil {
124 return nil, err
125 }
126 defer rows.Close()
127 items := []UserSshKey{}
128 for rows.Next() {
129 var i UserSshKey
130 if err := rows.Scan(
131 &i.ID,
132 &i.UserID,
133 &i.Title,
134 &i.FingerprintSha256,
135 &i.KeyType,
136 &i.KeyBits,
137 &i.PublicKey,
138 &i.LastUsedAt,
139 &i.LastUsedIp,
140 &i.CreatedAt,
141 ); err != nil {
142 return nil, err
143 }
144 items = append(items, i)
145 }
146 if err := rows.Err(); err != nil {
147 return nil, err
148 }
149 return items, nil
150 }
151
152 const touchSSHKeyLastUsed = `-- name: TouchSSHKeyLastUsed :exec
153 UPDATE user_ssh_keys
154 SET last_used_at = now(),
155 last_used_ip = $2
156 WHERE id = $1
157 `
158
159 type TouchSSHKeyLastUsedParams struct {
160 ID int64
161 LastUsedIp *netip.Addr
162 }
163
164 func (q *Queries) TouchSSHKeyLastUsed(ctx context.Context, db DBTX, arg TouchSSHKeyLastUsedParams) error {
165 _, err := db.Exec(ctx, touchSSHKeyLastUsed, arg.ID, arg.LastUsedIp)
166 return err
167 }
168