@@ -21,6 +21,22 @@ func (q *Queries) CountUserSSHKeys(ctx context.Context, db DBTX, userID int64) ( |
| 21 | 21 | return count, err |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | +const countUserSSHKeysByKind = `-- name: CountUserSSHKeysByKind :one |
| 25 | +SELECT count(*) FROM user_ssh_keys WHERE user_id = $1 AND kind = $2 |
| 26 | +` |
| 27 | + |
| 28 | +type CountUserSSHKeysByKindParams struct { |
| 29 | + UserID int64 |
| 30 | + Kind string |
| 31 | +} |
| 32 | + |
| 33 | +func (q *Queries) CountUserSSHKeysByKind(ctx context.Context, db DBTX, arg CountUserSSHKeysByKindParams) (int64, error) { |
| 34 | + row := db.QueryRow(ctx, countUserSSHKeysByKind, arg.UserID, arg.Kind) |
| 35 | + var count int64 |
| 36 | + err := row.Scan(&count) |
| 37 | + return count, err |
| 38 | +} |
| 39 | + |
| 24 | 40 | const deleteUserSSHKey = `-- name: DeleteUserSSHKey :execrows |
| 25 | 41 | DELETE FROM user_ssh_keys WHERE id = $1 AND user_id = $2 |
| 26 | 42 | ` |
@@ -40,9 +56,42 @@ func (q *Queries) DeleteUserSSHKey(ctx context.Context, db DBTX, arg DeleteUserS |
| 40 | 56 | return result.RowsAffected(), nil |
| 41 | 57 | } |
| 42 | 58 | |
| 59 | +const getUserSSHKey = `-- name: GetUserSSHKey :one |
| 60 | +SELECT id, user_id, title, fingerprint_sha256, key_type, key_bits, public_key, |
| 61 | + last_used_at, last_used_ip, created_at, kind |
| 62 | +FROM user_ssh_keys |
| 63 | +WHERE id = $1 AND user_id = $2 |
| 64 | +` |
| 65 | + |
| 66 | +type GetUserSSHKeyParams struct { |
| 67 | + ID int64 |
| 68 | + UserID int64 |
| 69 | +} |
| 70 | + |
| 71 | +// Single-key lookup for the REST GET-by-id endpoint. user_id filter so |
| 72 | +// one caller can't read another's key by ID. |
| 73 | +func (q *Queries) GetUserSSHKey(ctx context.Context, db DBTX, arg GetUserSSHKeyParams) (UserSshKey, error) { |
| 74 | + row := db.QueryRow(ctx, getUserSSHKey, arg.ID, arg.UserID) |
| 75 | + var i UserSshKey |
| 76 | + err := row.Scan( |
| 77 | + &i.ID, |
| 78 | + &i.UserID, |
| 79 | + &i.Title, |
| 80 | + &i.FingerprintSha256, |
| 81 | + &i.KeyType, |
| 82 | + &i.KeyBits, |
| 83 | + &i.PublicKey, |
| 84 | + &i.LastUsedAt, |
| 85 | + &i.LastUsedIp, |
| 86 | + &i.CreatedAt, |
| 87 | + &i.Kind, |
| 88 | + ) |
| 89 | + return i, err |
| 90 | +} |
| 91 | + |
| 43 | 92 | const getUserSSHKeyByFingerprint = `-- name: GetUserSSHKeyByFingerprint :one |
| 44 | 93 | SELECT id, user_id, title, fingerprint_sha256, key_type, key_bits, public_key, |
| 45 | | - last_used_at, last_used_ip, created_at |
| 94 | + last_used_at, last_used_ip, created_at, kind |
| 46 | 95 | FROM user_ssh_keys |
| 47 | 96 | WHERE fingerprint_sha256 = $1 |
| 48 | 97 | ` |
@@ -63,16 +112,17 @@ func (q *Queries) GetUserSSHKeyByFingerprint(ctx context.Context, db DBTX, finge |
| 63 | 112 | &i.LastUsedAt, |
| 64 | 113 | &i.LastUsedIp, |
| 65 | 114 | &i.CreatedAt, |
| 115 | + &i.Kind, |
| 66 | 116 | ) |
| 67 | 117 | return i, err |
| 68 | 118 | } |
| 69 | 119 | |
| 70 | 120 | const insertUserSSHKey = `-- name: InsertUserSSHKey :one |
| 71 | 121 | |
| 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) |
| 122 | +INSERT INTO user_ssh_keys (user_id, title, fingerprint_sha256, key_type, key_bits, public_key, kind) |
| 123 | +VALUES ($1, $2, $3, $4, $5, $6, $7) |
| 74 | 124 | RETURNING id, user_id, title, fingerprint_sha256, key_type, key_bits, public_key, |
| 75 | | - last_used_at, last_used_ip, created_at |
| 125 | + last_used_at, last_used_ip, created_at, kind |
| 76 | 126 | ` |
| 77 | 127 | |
| 78 | 128 | type InsertUserSSHKeyParams struct { |
@@ -82,6 +132,7 @@ type InsertUserSSHKeyParams struct { |
| 82 | 132 | KeyType string |
| 83 | 133 | KeyBits int32 |
| 84 | 134 | PublicKey string |
| 135 | + Kind string |
| 85 | 136 | } |
| 86 | 137 | |
| 87 | 138 | // SPDX-License-Identifier: AGPL-3.0-or-later |
@@ -93,6 +144,7 @@ func (q *Queries) InsertUserSSHKey(ctx context.Context, db DBTX, arg InsertUserS |
| 93 | 144 | arg.KeyType, |
| 94 | 145 | arg.KeyBits, |
| 95 | 146 | arg.PublicKey, |
| 147 | + arg.Kind, |
| 96 | 148 | ) |
| 97 | 149 | var i UserSshKey |
| 98 | 150 | err := row.Scan( |
@@ -106,13 +158,14 @@ func (q *Queries) InsertUserSSHKey(ctx context.Context, db DBTX, arg InsertUserS |
| 106 | 158 | &i.LastUsedAt, |
| 107 | 159 | &i.LastUsedIp, |
| 108 | 160 | &i.CreatedAt, |
| 161 | + &i.Kind, |
| 109 | 162 | ) |
| 110 | 163 | return i, err |
| 111 | 164 | } |
| 112 | 165 | |
| 113 | 166 | const listUserSSHKeys = `-- name: ListUserSSHKeys :many |
| 114 | 167 | SELECT id, user_id, title, fingerprint_sha256, key_type, key_bits, public_key, |
| 115 | | - last_used_at, last_used_ip, created_at |
| 168 | + last_used_at, last_used_ip, created_at, kind |
| 116 | 169 | FROM user_ssh_keys |
| 117 | 170 | WHERE user_id = $1 |
| 118 | 171 | ORDER BY created_at DESC |
@@ -138,6 +191,63 @@ func (q *Queries) ListUserSSHKeys(ctx context.Context, db DBTX, userID int64) ([ |
| 138 | 191 | &i.LastUsedAt, |
| 139 | 192 | &i.LastUsedIp, |
| 140 | 193 | &i.CreatedAt, |
| 194 | + &i.Kind, |
| 195 | + ); err != nil { |
| 196 | + return nil, err |
| 197 | + } |
| 198 | + items = append(items, i) |
| 199 | + } |
| 200 | + if err := rows.Err(); err != nil { |
| 201 | + return nil, err |
| 202 | + } |
| 203 | + return items, nil |
| 204 | +} |
| 205 | + |
| 206 | +const listUserSSHKeysByKind = `-- name: ListUserSSHKeysByKind :many |
| 207 | +SELECT id, user_id, title, fingerprint_sha256, key_type, key_bits, public_key, |
| 208 | + last_used_at, last_used_ip, created_at, kind |
| 209 | +FROM user_ssh_keys |
| 210 | +WHERE user_id = $1 AND kind = $2 |
| 211 | +ORDER BY created_at DESC |
| 212 | +LIMIT $3 OFFSET $4 |
| 213 | +` |
| 214 | + |
| 215 | +type ListUserSSHKeysByKindParams struct { |
| 216 | + UserID int64 |
| 217 | + Kind string |
| 218 | + Limit int32 |
| 219 | + Offset int32 |
| 220 | +} |
| 221 | + |
| 222 | +// Paginated kind-filtered list used by the REST surface. Order matches |
| 223 | +// ListUserSSHKeys so callers can swap between them without observing a |
| 224 | +// reshuffle. |
| 225 | +func (q *Queries) ListUserSSHKeysByKind(ctx context.Context, db DBTX, arg ListUserSSHKeysByKindParams) ([]UserSshKey, error) { |
| 226 | + rows, err := db.Query(ctx, listUserSSHKeysByKind, |
| 227 | + arg.UserID, |
| 228 | + arg.Kind, |
| 229 | + arg.Limit, |
| 230 | + arg.Offset, |
| 231 | + ) |
| 232 | + if err != nil { |
| 233 | + return nil, err |
| 234 | + } |
| 235 | + defer rows.Close() |
| 236 | + items := []UserSshKey{} |
| 237 | + for rows.Next() { |
| 238 | + var i UserSshKey |
| 239 | + if err := rows.Scan( |
| 240 | + &i.ID, |
| 241 | + &i.UserID, |
| 242 | + &i.Title, |
| 243 | + &i.FingerprintSha256, |
| 244 | + &i.KeyType, |
| 245 | + &i.KeyBits, |
| 246 | + &i.PublicKey, |
| 247 | + &i.LastUsedAt, |
| 248 | + &i.LastUsedIp, |
| 249 | + &i.CreatedAt, |
| 250 | + &i.Kind, |
| 141 | 251 | ); err != nil { |
| 142 | 252 | return nil, err |
| 143 | 253 | } |