| 1 | // Code generated by sqlc. DO NOT EDIT. |
| 2 | // versions: |
| 3 | // sqlc v1.31.1 |
| 4 | // source: user_gpg_subkeys.sql |
| 5 | |
| 6 | package usersdb |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | |
| 11 | "github.com/jackc/pgx/v5/pgtype" |
| 12 | ) |
| 13 | |
| 14 | const getUserGPGSubkeyByFingerprint = `-- name: GetUserGPGSubkeyByFingerprint :one |
| 15 | SELECT id, gpg_key_id, fingerprint, key_id, |
| 16 | can_sign, can_encrypt_comms, can_encrypt_storage, can_certify, |
| 17 | expires_at, revoked_at, created_at |
| 18 | FROM user_gpg_subkeys |
| 19 | WHERE fingerprint = $1 AND revoked_at IS NULL |
| 20 | ` |
| 21 | |
| 22 | // Hot path for commit/tag signature verification. The signature |
| 23 | // packet carries the signing subkey's fingerprint; this query |
| 24 | // resolves it back to the primary key (and via FK to the user). |
| 25 | // Index lookup via the partial unique index. |
| 26 | func (q *Queries) GetUserGPGSubkeyByFingerprint(ctx context.Context, db DBTX, fingerprint string) (UserGpgSubkey, error) { |
| 27 | row := db.QueryRow(ctx, getUserGPGSubkeyByFingerprint, fingerprint) |
| 28 | var i UserGpgSubkey |
| 29 | err := row.Scan( |
| 30 | &i.ID, |
| 31 | &i.GpgKeyID, |
| 32 | &i.Fingerprint, |
| 33 | &i.KeyID, |
| 34 | &i.CanSign, |
| 35 | &i.CanEncryptComms, |
| 36 | &i.CanEncryptStorage, |
| 37 | &i.CanCertify, |
| 38 | &i.ExpiresAt, |
| 39 | &i.RevokedAt, |
| 40 | &i.CreatedAt, |
| 41 | ) |
| 42 | return i, err |
| 43 | } |
| 44 | |
| 45 | const insertUserGPGSubkey = `-- name: InsertUserGPGSubkey :one |
| 46 | |
| 47 | INSERT INTO user_gpg_subkeys ( |
| 48 | gpg_key_id, fingerprint, key_id, |
| 49 | can_sign, can_encrypt_comms, can_encrypt_storage, can_certify, |
| 50 | expires_at |
| 51 | ) |
| 52 | VALUES ( |
| 53 | $1, $2, $3, |
| 54 | $4, $5, $6, $7, |
| 55 | $8 |
| 56 | ) |
| 57 | RETURNING id, gpg_key_id, fingerprint, key_id, |
| 58 | can_sign, can_encrypt_comms, can_encrypt_storage, can_certify, |
| 59 | expires_at, revoked_at, created_at |
| 60 | ` |
| 61 | |
| 62 | type InsertUserGPGSubkeyParams struct { |
| 63 | GpgKeyID int64 |
| 64 | Fingerprint string |
| 65 | KeyID string |
| 66 | CanSign bool |
| 67 | CanEncryptComms bool |
| 68 | CanEncryptStorage bool |
| 69 | CanCertify bool |
| 70 | ExpiresAt pgtype.Timestamptz |
| 71 | } |
| 72 | |
| 73 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 74 | // One row per subkey of a primary key. Always inserted in the same |
| 75 | // transaction as the parent InsertUserGPGKey so the verification |
| 76 | // hot path's fingerprint lookup is consistent with the REST nested |
| 77 | // shape. |
| 78 | func (q *Queries) InsertUserGPGSubkey(ctx context.Context, db DBTX, arg InsertUserGPGSubkeyParams) (UserGpgSubkey, error) { |
| 79 | row := db.QueryRow(ctx, insertUserGPGSubkey, |
| 80 | arg.GpgKeyID, |
| 81 | arg.Fingerprint, |
| 82 | arg.KeyID, |
| 83 | arg.CanSign, |
| 84 | arg.CanEncryptComms, |
| 85 | arg.CanEncryptStorage, |
| 86 | arg.CanCertify, |
| 87 | arg.ExpiresAt, |
| 88 | ) |
| 89 | var i UserGpgSubkey |
| 90 | err := row.Scan( |
| 91 | &i.ID, |
| 92 | &i.GpgKeyID, |
| 93 | &i.Fingerprint, |
| 94 | &i.KeyID, |
| 95 | &i.CanSign, |
| 96 | &i.CanEncryptComms, |
| 97 | &i.CanEncryptStorage, |
| 98 | &i.CanCertify, |
| 99 | &i.ExpiresAt, |
| 100 | &i.RevokedAt, |
| 101 | &i.CreatedAt, |
| 102 | ) |
| 103 | return i, err |
| 104 | } |
| 105 | |
| 106 | const listSubkeysForGPGKey = `-- name: ListSubkeysForGPGKey :many |
| 107 | SELECT id, gpg_key_id, fingerprint, key_id, |
| 108 | can_sign, can_encrypt_comms, can_encrypt_storage, can_certify, |
| 109 | expires_at, revoked_at, created_at |
| 110 | FROM user_gpg_subkeys |
| 111 | WHERE gpg_key_id = $1 |
| 112 | ORDER BY id |
| 113 | ` |
| 114 | |
| 115 | // Reads all live subkeys for one primary; used when invalidating the |
| 116 | // verification cache on primary soft-delete (every dependent subkey |
| 117 | // needs its cache rows stamped invalidated too). |
| 118 | func (q *Queries) ListSubkeysForGPGKey(ctx context.Context, db DBTX, gpgKeyID int64) ([]UserGpgSubkey, error) { |
| 119 | rows, err := db.Query(ctx, listSubkeysForGPGKey, gpgKeyID) |
| 120 | if err != nil { |
| 121 | return nil, err |
| 122 | } |
| 123 | defer rows.Close() |
| 124 | items := []UserGpgSubkey{} |
| 125 | for rows.Next() { |
| 126 | var i UserGpgSubkey |
| 127 | if err := rows.Scan( |
| 128 | &i.ID, |
| 129 | &i.GpgKeyID, |
| 130 | &i.Fingerprint, |
| 131 | &i.KeyID, |
| 132 | &i.CanSign, |
| 133 | &i.CanEncryptComms, |
| 134 | &i.CanEncryptStorage, |
| 135 | &i.CanCertify, |
| 136 | &i.ExpiresAt, |
| 137 | &i.RevokedAt, |
| 138 | &i.CreatedAt, |
| 139 | ); err != nil { |
| 140 | return nil, err |
| 141 | } |
| 142 | items = append(items, i) |
| 143 | } |
| 144 | if err := rows.Err(); err != nil { |
| 145 | return nil, err |
| 146 | } |
| 147 | return items, nil |
| 148 | } |
| 149 | |
| 150 | const softDeleteSubkeysForGPGKey = `-- name: SoftDeleteSubkeysForGPGKey :exec |
| 151 | UPDATE user_gpg_subkeys |
| 152 | SET revoked_at = now() |
| 153 | WHERE gpg_key_id = $1 AND revoked_at IS NULL |
| 154 | ` |
| 155 | |
| 156 | // Stamps revoked_at on every live subkey of a primary. Called in the |
| 157 | // same transaction as SoftDeleteUserGPGKey so the partial unique index |
| 158 | // frees up the fingerprint for re-upload if the user rotates. |
| 159 | func (q *Queries) SoftDeleteSubkeysForGPGKey(ctx context.Context, db DBTX, gpgKeyID int64) error { |
| 160 | _, err := db.Exec(ctx, softDeleteSubkeysForGPGKey, gpgKeyID) |
| 161 | return err |
| 162 | } |
| 163 |