| 1 | // Code generated by sqlc. DO NOT EDIT. |
| 2 | // versions: |
| 3 | // sqlc v1.31.1 |
| 4 | // source: user_tokens.sql |
| 5 | |
| 6 | package usersdb |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "net/netip" |
| 11 | |
| 12 | "github.com/jackc/pgx/v5/pgtype" |
| 13 | ) |
| 14 | |
| 15 | const countActiveUserTokens = `-- name: CountActiveUserTokens :one |
| 16 | SELECT count(*) FROM user_tokens |
| 17 | WHERE user_id = $1 AND revoked_at IS NULL |
| 18 | ` |
| 19 | |
| 20 | func (q *Queries) CountActiveUserTokens(ctx context.Context, db DBTX, userID int64) (int64, error) { |
| 21 | row := db.QueryRow(ctx, countActiveUserTokens, userID) |
| 22 | var count int64 |
| 23 | err := row.Scan(&count) |
| 24 | return count, err |
| 25 | } |
| 26 | |
| 27 | const getUserTokenByHash = `-- name: GetUserTokenByHash :one |
| 28 | SELECT id, user_id, name, token_hash, token_prefix, scopes, |
| 29 | expires_at, last_used_at, last_used_ip, revoked_at, created_at |
| 30 | FROM user_tokens |
| 31 | WHERE token_hash = $1 |
| 32 | ` |
| 33 | |
| 34 | // Hot path for the auth middleware. token_hash is UNIQUE; returns at |
| 35 | // most one row. Caller MUST also check revoked_at IS NULL and |
| 36 | // expires_at handling. |
| 37 | func (q *Queries) GetUserTokenByHash(ctx context.Context, db DBTX, tokenHash []byte) (UserToken, error) { |
| 38 | row := db.QueryRow(ctx, getUserTokenByHash, tokenHash) |
| 39 | var i UserToken |
| 40 | err := row.Scan( |
| 41 | &i.ID, |
| 42 | &i.UserID, |
| 43 | &i.Name, |
| 44 | &i.TokenHash, |
| 45 | &i.TokenPrefix, |
| 46 | &i.Scopes, |
| 47 | &i.ExpiresAt, |
| 48 | &i.LastUsedAt, |
| 49 | &i.LastUsedIp, |
| 50 | &i.RevokedAt, |
| 51 | &i.CreatedAt, |
| 52 | ) |
| 53 | return i, err |
| 54 | } |
| 55 | |
| 56 | const insertUserToken = `-- name: InsertUserToken :one |
| 57 | |
| 58 | INSERT INTO user_tokens (user_id, name, token_hash, token_prefix, scopes, expires_at) |
| 59 | VALUES ($1, $2, $3, $4, $5, $6) |
| 60 | RETURNING id, user_id, name, token_hash, token_prefix, scopes, |
| 61 | expires_at, last_used_at, last_used_ip, revoked_at, created_at |
| 62 | ` |
| 63 | |
| 64 | type InsertUserTokenParams struct { |
| 65 | UserID int64 |
| 66 | Name string |
| 67 | TokenHash []byte |
| 68 | TokenPrefix string |
| 69 | Scopes []string |
| 70 | ExpiresAt pgtype.Timestamptz |
| 71 | } |
| 72 | |
| 73 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 74 | func (q *Queries) InsertUserToken(ctx context.Context, db DBTX, arg InsertUserTokenParams) (UserToken, error) { |
| 75 | row := db.QueryRow(ctx, insertUserToken, |
| 76 | arg.UserID, |
| 77 | arg.Name, |
| 78 | arg.TokenHash, |
| 79 | arg.TokenPrefix, |
| 80 | arg.Scopes, |
| 81 | arg.ExpiresAt, |
| 82 | ) |
| 83 | var i UserToken |
| 84 | err := row.Scan( |
| 85 | &i.ID, |
| 86 | &i.UserID, |
| 87 | &i.Name, |
| 88 | &i.TokenHash, |
| 89 | &i.TokenPrefix, |
| 90 | &i.Scopes, |
| 91 | &i.ExpiresAt, |
| 92 | &i.LastUsedAt, |
| 93 | &i.LastUsedIp, |
| 94 | &i.RevokedAt, |
| 95 | &i.CreatedAt, |
| 96 | ) |
| 97 | return i, err |
| 98 | } |
| 99 | |
| 100 | const listUserTokens = `-- name: ListUserTokens :many |
| 101 | SELECT id, user_id, name, token_hash, token_prefix, scopes, |
| 102 | expires_at, last_used_at, last_used_ip, revoked_at, created_at |
| 103 | FROM user_tokens |
| 104 | WHERE user_id = $1 |
| 105 | ORDER BY revoked_at IS NOT NULL, created_at DESC |
| 106 | ` |
| 107 | |
| 108 | func (q *Queries) ListUserTokens(ctx context.Context, db DBTX, userID int64) ([]UserToken, error) { |
| 109 | rows, err := db.Query(ctx, listUserTokens, userID) |
| 110 | if err != nil { |
| 111 | return nil, err |
| 112 | } |
| 113 | defer rows.Close() |
| 114 | items := []UserToken{} |
| 115 | for rows.Next() { |
| 116 | var i UserToken |
| 117 | if err := rows.Scan( |
| 118 | &i.ID, |
| 119 | &i.UserID, |
| 120 | &i.Name, |
| 121 | &i.TokenHash, |
| 122 | &i.TokenPrefix, |
| 123 | &i.Scopes, |
| 124 | &i.ExpiresAt, |
| 125 | &i.LastUsedAt, |
| 126 | &i.LastUsedIp, |
| 127 | &i.RevokedAt, |
| 128 | &i.CreatedAt, |
| 129 | ); err != nil { |
| 130 | return nil, err |
| 131 | } |
| 132 | items = append(items, i) |
| 133 | } |
| 134 | if err := rows.Err(); err != nil { |
| 135 | return nil, err |
| 136 | } |
| 137 | return items, nil |
| 138 | } |
| 139 | |
| 140 | const revokeAllUserTokens = `-- name: RevokeAllUserTokens :exec |
| 141 | UPDATE user_tokens |
| 142 | SET revoked_at = now() |
| 143 | WHERE user_id = $1 AND revoked_at IS NULL |
| 144 | ` |
| 145 | |
| 146 | // Used by user suspension to revoke every active token in one statement. |
| 147 | func (q *Queries) RevokeAllUserTokens(ctx context.Context, db DBTX, userID int64) error { |
| 148 | _, err := db.Exec(ctx, revokeAllUserTokens, userID) |
| 149 | return err |
| 150 | } |
| 151 | |
| 152 | const revokeUserToken = `-- name: RevokeUserToken :execrows |
| 153 | UPDATE user_tokens |
| 154 | SET revoked_at = now() |
| 155 | WHERE id = $1 AND user_id = $2 AND revoked_at IS NULL |
| 156 | ` |
| 157 | |
| 158 | type RevokeUserTokenParams struct { |
| 159 | ID int64 |
| 160 | UserID int64 |
| 161 | } |
| 162 | |
| 163 | // Scoped revoke: caller must pass owning user_id so a hijacked handler |
| 164 | // can never revoke tokens it doesn't own. No-op on already-revoked rows. |
| 165 | func (q *Queries) RevokeUserToken(ctx context.Context, db DBTX, arg RevokeUserTokenParams) (int64, error) { |
| 166 | result, err := db.Exec(ctx, revokeUserToken, arg.ID, arg.UserID) |
| 167 | if err != nil { |
| 168 | return 0, err |
| 169 | } |
| 170 | return result.RowsAffected(), nil |
| 171 | } |
| 172 | |
| 173 | const touchUserTokenLastUsed = `-- name: TouchUserTokenLastUsed :exec |
| 174 | UPDATE user_tokens |
| 175 | SET last_used_at = now(), |
| 176 | last_used_ip = $2 |
| 177 | WHERE id = $1 |
| 178 | ` |
| 179 | |
| 180 | type TouchUserTokenLastUsedParams struct { |
| 181 | ID int64 |
| 182 | LastUsedIp *netip.Addr |
| 183 | } |
| 184 | |
| 185 | func (q *Queries) TouchUserTokenLastUsed(ctx context.Context, db DBTX, arg TouchUserTokenLastUsedParams) error { |
| 186 | _, err := db.Exec(ctx, touchUserTokenLastUsed, arg.ID, arg.LastUsedIp) |
| 187 | return err |
| 188 | } |
| 189 |