@@ -0,0 +1,185 @@ |
| 1 | +// Code generated by sqlc. DO NOT EDIT. |
| 2 | +// versions: |
| 3 | +// sqlc v1.31.1 |
| 4 | +// source: device_authorizations.sql |
| 5 | + |
| 6 | +package usersdb |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + |
| 11 | + "github.com/jackc/pgx/v5/pgtype" |
| 12 | +) |
| 13 | + |
| 14 | +const approveDeviceAuthorization = `-- name: ApproveDeviceAuthorization :exec |
| 15 | +UPDATE device_authorizations |
| 16 | +SET user_id = $2, |
| 17 | + approved_at = now(), |
| 18 | + issued_token_id = $3 |
| 19 | +WHERE id = $1 |
| 20 | + AND approved_at IS NULL |
| 21 | + AND denied_at IS NULL |
| 22 | + AND expires_at > now() |
| 23 | +` |
| 24 | + |
| 25 | +type ApproveDeviceAuthorizationParams struct { |
| 26 | + ID int64 |
| 27 | + UserID pgtype.Int8 |
| 28 | + IssuedTokenID pgtype.Int8 |
| 29 | +} |
| 30 | + |
| 31 | +// Records the user's approval and links the freshly minted PAT. |
| 32 | +// Idempotency is preserved by the caller — the orchestrator only |
| 33 | +// calls this once per row. |
| 34 | +func (q *Queries) ApproveDeviceAuthorization(ctx context.Context, db DBTX, arg ApproveDeviceAuthorizationParams) error { |
| 35 | + _, err := db.Exec(ctx, approveDeviceAuthorization, arg.ID, arg.UserID, arg.IssuedTokenID) |
| 36 | + return err |
| 37 | +} |
| 38 | + |
| 39 | +const deleteExpiredDeviceAuthorizations = `-- name: DeleteExpiredDeviceAuthorizations :exec |
| 40 | +DELETE FROM device_authorizations |
| 41 | +WHERE expires_at < now() - interval '24 hours' |
| 42 | +` |
| 43 | + |
| 44 | +// Janitor invocation: a small forensics window past expiry is fine, |
| 45 | +// but eventually drop the row so the user_code index stays small. |
| 46 | +func (q *Queries) DeleteExpiredDeviceAuthorizations(ctx context.Context, db DBTX) error { |
| 47 | + _, err := db.Exec(ctx, deleteExpiredDeviceAuthorizations) |
| 48 | + return err |
| 49 | +} |
| 50 | + |
| 51 | +const denyDeviceAuthorization = `-- name: DenyDeviceAuthorization :exec |
| 52 | +UPDATE device_authorizations |
| 53 | +SET denied_at = now() |
| 54 | +WHERE id = $1 |
| 55 | + AND approved_at IS NULL |
| 56 | + AND denied_at IS NULL |
| 57 | +` |
| 58 | + |
| 59 | +func (q *Queries) DenyDeviceAuthorization(ctx context.Context, db DBTX, id int64) error { |
| 60 | + _, err := db.Exec(ctx, denyDeviceAuthorization, id) |
| 61 | + return err |
| 62 | +} |
| 63 | + |
| 64 | +const getDeviceAuthorizationByCodeHash = `-- name: GetDeviceAuthorizationByCodeHash :one |
| 65 | +SELECT id, device_code_hash, user_code, client_id, scopes, user_id, |
| 66 | + approved_at, denied_at, issued_token_id, interval_seconds, |
| 67 | + expires_at, last_polled_at, created_at |
| 68 | +FROM device_authorizations |
| 69 | +WHERE device_code_hash = $1 |
| 70 | +` |
| 71 | + |
| 72 | +// Hot path for the polling /access_token endpoint. The middleware |
| 73 | +// enforces interval_seconds via last_polled_at downstream. |
| 74 | +func (q *Queries) GetDeviceAuthorizationByCodeHash(ctx context.Context, db DBTX, deviceCodeHash []byte) (DeviceAuthorization, error) { |
| 75 | + row := db.QueryRow(ctx, getDeviceAuthorizationByCodeHash, deviceCodeHash) |
| 76 | + var i DeviceAuthorization |
| 77 | + err := row.Scan( |
| 78 | + &i.ID, |
| 79 | + &i.DeviceCodeHash, |
| 80 | + &i.UserCode, |
| 81 | + &i.ClientID, |
| 82 | + &i.Scopes, |
| 83 | + &i.UserID, |
| 84 | + &i.ApprovedAt, |
| 85 | + &i.DeniedAt, |
| 86 | + &i.IssuedTokenID, |
| 87 | + &i.IntervalSeconds, |
| 88 | + &i.ExpiresAt, |
| 89 | + &i.LastPolledAt, |
| 90 | + &i.CreatedAt, |
| 91 | + ) |
| 92 | + return i, err |
| 93 | +} |
| 94 | + |
| 95 | +const getDeviceAuthorizationByUserCode = `-- name: GetDeviceAuthorizationByUserCode :one |
| 96 | +SELECT id, device_code_hash, user_code, client_id, scopes, user_id, |
| 97 | + approved_at, denied_at, issued_token_id, interval_seconds, |
| 98 | + expires_at, last_polled_at, created_at |
| 99 | +FROM device_authorizations |
| 100 | +WHERE user_code = $1 |
| 101 | +` |
| 102 | + |
| 103 | +// Lookup path for the verification page. Returns even non-pending rows |
| 104 | +// so the handler can render a clean "already approved" / "expired" page |
| 105 | +// instead of a generic 404. |
| 106 | +func (q *Queries) GetDeviceAuthorizationByUserCode(ctx context.Context, db DBTX, userCode string) (DeviceAuthorization, error) { |
| 107 | + row := db.QueryRow(ctx, getDeviceAuthorizationByUserCode, userCode) |
| 108 | + var i DeviceAuthorization |
| 109 | + err := row.Scan( |
| 110 | + &i.ID, |
| 111 | + &i.DeviceCodeHash, |
| 112 | + &i.UserCode, |
| 113 | + &i.ClientID, |
| 114 | + &i.Scopes, |
| 115 | + &i.UserID, |
| 116 | + &i.ApprovedAt, |
| 117 | + &i.DeniedAt, |
| 118 | + &i.IssuedTokenID, |
| 119 | + &i.IntervalSeconds, |
| 120 | + &i.ExpiresAt, |
| 121 | + &i.LastPolledAt, |
| 122 | + &i.CreatedAt, |
| 123 | + ) |
| 124 | + return i, err |
| 125 | +} |
| 126 | + |
| 127 | +const insertDeviceAuthorization = `-- name: InsertDeviceAuthorization :one |
| 128 | + |
| 129 | +INSERT INTO device_authorizations ( |
| 130 | + device_code_hash, user_code, client_id, scopes, |
| 131 | + interval_seconds, expires_at |
| 132 | +) VALUES ($1, $2, $3, $4, $5, $6) |
| 133 | +RETURNING id, device_code_hash, user_code, client_id, scopes, user_id, |
| 134 | + approved_at, denied_at, issued_token_id, interval_seconds, |
| 135 | + expires_at, last_polled_at, created_at |
| 136 | +` |
| 137 | + |
| 138 | +type InsertDeviceAuthorizationParams struct { |
| 139 | + DeviceCodeHash []byte |
| 140 | + UserCode string |
| 141 | + ClientID string |
| 142 | + Scopes []string |
| 143 | + IntervalSeconds int32 |
| 144 | + ExpiresAt pgtype.Timestamptz |
| 145 | +} |
| 146 | + |
| 147 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 148 | +func (q *Queries) InsertDeviceAuthorization(ctx context.Context, db DBTX, arg InsertDeviceAuthorizationParams) (DeviceAuthorization, error) { |
| 149 | + row := db.QueryRow(ctx, insertDeviceAuthorization, |
| 150 | + arg.DeviceCodeHash, |
| 151 | + arg.UserCode, |
| 152 | + arg.ClientID, |
| 153 | + arg.Scopes, |
| 154 | + arg.IntervalSeconds, |
| 155 | + arg.ExpiresAt, |
| 156 | + ) |
| 157 | + var i DeviceAuthorization |
| 158 | + err := row.Scan( |
| 159 | + &i.ID, |
| 160 | + &i.DeviceCodeHash, |
| 161 | + &i.UserCode, |
| 162 | + &i.ClientID, |
| 163 | + &i.Scopes, |
| 164 | + &i.UserID, |
| 165 | + &i.ApprovedAt, |
| 166 | + &i.DeniedAt, |
| 167 | + &i.IssuedTokenID, |
| 168 | + &i.IntervalSeconds, |
| 169 | + &i.ExpiresAt, |
| 170 | + &i.LastPolledAt, |
| 171 | + &i.CreatedAt, |
| 172 | + ) |
| 173 | + return i, err |
| 174 | +} |
| 175 | + |
| 176 | +const touchDeviceAuthorizationPoll = `-- name: TouchDeviceAuthorizationPoll :exec |
| 177 | +UPDATE device_authorizations |
| 178 | +SET last_polled_at = now() |
| 179 | +WHERE id = $1 |
| 180 | +` |
| 181 | + |
| 182 | +func (q *Queries) TouchDeviceAuthorizationPoll(ctx context.Context, db DBTX, id int64) error { |
| 183 | + _, err := db.Exec(ctx, touchDeviceAuthorizationPoll, id) |
| 184 | + return err |
| 185 | +} |