- Track learner sessions and revoke them immediately on every request - Add a device replacement challenge for logins over the limit - Add learner and admin endpoints to list and remove devices - Trim excess sessions when enforcement is enabled - Stop logging refresh tokens and authorization headers Enforcement is off by default through DEVICE_LIMIT_ENFORCED.
592 lines
18 KiB
Go
592 lines
18 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: user_sessions.sql
|
|
|
|
package dbgen
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const AttachRefreshTokenToSession = `-- name: AttachRefreshTokenToSession :exec
|
|
UPDATE refresh_tokens SET session_id = $2 WHERE id = $1 AND session_id IS NULL
|
|
`
|
|
|
|
type AttachRefreshTokenToSessionParams struct {
|
|
ID int64 `json:"id"`
|
|
SessionID uuid.NullUUID `json:"session_id"`
|
|
}
|
|
|
|
func (q *Queries) AttachRefreshTokenToSession(ctx context.Context, arg AttachRefreshTokenToSessionParams) error {
|
|
_, err := q.db.Exec(ctx, AttachRefreshTokenToSession, arg.ID, arg.SessionID)
|
|
return err
|
|
}
|
|
|
|
const CountActiveUserSessions = `-- name: CountActiveUserSessions :one
|
|
SELECT COUNT(*)
|
|
FROM user_sessions
|
|
WHERE user_id = $1 AND revoked_at IS NULL AND expires_at > NOW()
|
|
`
|
|
|
|
func (q *Queries) CountActiveUserSessions(ctx context.Context, userID int64) (int64, error) {
|
|
row := q.db.QueryRow(ctx, CountActiveUserSessions, userID)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const CreateDeviceLoginChallenge = `-- name: CreateDeviceLoginChallenge :exec
|
|
INSERT INTO device_login_challenges
|
|
(user_id, token_hash, device_key, device_label, platform, expires_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
`
|
|
|
|
type CreateDeviceLoginChallengeParams struct {
|
|
UserID int64 `json:"user_id"`
|
|
TokenHash []byte `json:"token_hash"`
|
|
DeviceKey pgtype.Text `json:"device_key"`
|
|
DeviceLabel string `json:"device_label"`
|
|
Platform string `json:"platform"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
}
|
|
|
|
func (q *Queries) CreateDeviceLoginChallenge(ctx context.Context, arg CreateDeviceLoginChallengeParams) error {
|
|
_, err := q.db.Exec(ctx, CreateDeviceLoginChallenge,
|
|
arg.UserID,
|
|
arg.TokenHash,
|
|
arg.DeviceKey,
|
|
arg.DeviceLabel,
|
|
arg.Platform,
|
|
arg.ExpiresAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const CreateRefreshTokenForSession = `-- name: CreateRefreshTokenForSession :exec
|
|
INSERT INTO refresh_tokens (user_id, token, expires_at, created_at, revoked, session_id)
|
|
VALUES ($1, $2, $3, $4, FALSE, $5)
|
|
`
|
|
|
|
type CreateRefreshTokenForSessionParams struct {
|
|
UserID int64 `json:"user_id"`
|
|
Token string `json:"token"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
SessionID uuid.NullUUID `json:"session_id"`
|
|
}
|
|
|
|
func (q *Queries) CreateRefreshTokenForSession(ctx context.Context, arg CreateRefreshTokenForSessionParams) error {
|
|
_, err := q.db.Exec(ctx, CreateRefreshTokenForSession,
|
|
arg.UserID,
|
|
arg.Token,
|
|
arg.ExpiresAt,
|
|
arg.CreatedAt,
|
|
arg.SessionID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const CreateUserSession = `-- name: CreateUserSession :one
|
|
INSERT INTO user_sessions (user_id, device_key, device_label, platform, expires_at)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING id, user_id, device_key, device_label, platform, created_at, last_seen_at, expires_at, revoked_at, revoked_reason
|
|
`
|
|
|
|
type CreateUserSessionParams struct {
|
|
UserID int64 `json:"user_id"`
|
|
DeviceKey pgtype.Text `json:"device_key"`
|
|
DeviceLabel string `json:"device_label"`
|
|
Platform string `json:"platform"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
}
|
|
|
|
func (q *Queries) CreateUserSession(ctx context.Context, arg CreateUserSessionParams) (UserSession, error) {
|
|
row := q.db.QueryRow(ctx, CreateUserSession,
|
|
arg.UserID,
|
|
arg.DeviceKey,
|
|
arg.DeviceLabel,
|
|
arg.Platform,
|
|
arg.ExpiresAt,
|
|
)
|
|
var i UserSession
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.DeviceKey,
|
|
&i.DeviceLabel,
|
|
&i.Platform,
|
|
&i.CreatedAt,
|
|
&i.LastSeenAt,
|
|
&i.ExpiresAt,
|
|
&i.RevokedAt,
|
|
&i.RevokedReason,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const GetActiveUserSession = `-- name: GetActiveUserSession :one
|
|
SELECT id, user_id, device_key, device_label, platform, created_at, last_seen_at, expires_at, revoked_at, revoked_reason
|
|
FROM user_sessions
|
|
WHERE id = $1 AND revoked_at IS NULL AND expires_at > NOW()
|
|
`
|
|
|
|
func (q *Queries) GetActiveUserSession(ctx context.Context, id uuid.UUID) (UserSession, error) {
|
|
row := q.db.QueryRow(ctx, GetActiveUserSession, id)
|
|
var i UserSession
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.DeviceKey,
|
|
&i.DeviceLabel,
|
|
&i.Platform,
|
|
&i.CreatedAt,
|
|
&i.LastSeenAt,
|
|
&i.ExpiresAt,
|
|
&i.RevokedAt,
|
|
&i.RevokedReason,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const GetActiveUserSessionForUser = `-- name: GetActiveUserSessionForUser :one
|
|
SELECT id, user_id, device_key, device_label, platform, created_at, last_seen_at, expires_at, revoked_at, revoked_reason
|
|
FROM user_sessions
|
|
WHERE id = $1 AND user_id = $2 AND revoked_at IS NULL AND expires_at > NOW()
|
|
`
|
|
|
|
type GetActiveUserSessionForUserParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
UserID int64 `json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) GetActiveUserSessionForUser(ctx context.Context, arg GetActiveUserSessionForUserParams) (UserSession, error) {
|
|
row := q.db.QueryRow(ctx, GetActiveUserSessionForUser, arg.ID, arg.UserID)
|
|
var i UserSession
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.DeviceKey,
|
|
&i.DeviceLabel,
|
|
&i.Platform,
|
|
&i.CreatedAt,
|
|
&i.LastSeenAt,
|
|
&i.ExpiresAt,
|
|
&i.RevokedAt,
|
|
&i.RevokedReason,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const GetDeviceLoginChallengeForUpdate = `-- name: GetDeviceLoginChallengeForUpdate :one
|
|
SELECT id, user_id, token_hash, device_key, device_label, platform, expires_at, used_at, created_at
|
|
FROM device_login_challenges
|
|
WHERE token_hash = $1
|
|
FOR UPDATE
|
|
`
|
|
|
|
func (q *Queries) GetDeviceLoginChallengeForUpdate(ctx context.Context, tokenHash []byte) (DeviceLoginChallenge, error) {
|
|
row := q.db.QueryRow(ctx, GetDeviceLoginChallengeForUpdate, tokenHash)
|
|
var i DeviceLoginChallenge
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.TokenHash,
|
|
&i.DeviceKey,
|
|
&i.DeviceLabel,
|
|
&i.Platform,
|
|
&i.ExpiresAt,
|
|
&i.UsedAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const GetRefreshTokenForUpdate = `-- name: GetRefreshTokenForUpdate :one
|
|
SELECT id, user_id, token, expires_at, revoked, created_at, session_id FROM refresh_tokens WHERE token = $1 FOR UPDATE
|
|
`
|
|
|
|
func (q *Queries) GetRefreshTokenForUpdate(ctx context.Context, token string) (RefreshToken, error) {
|
|
row := q.db.QueryRow(ctx, GetRefreshTokenForUpdate, token)
|
|
var i RefreshToken
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.Token,
|
|
&i.ExpiresAt,
|
|
&i.Revoked,
|
|
&i.CreatedAt,
|
|
&i.SessionID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const InvalidateDeviceChallengesForRoleExcludingUser = `-- name: InvalidateDeviceChallengesForRoleExcludingUser :exec
|
|
UPDATE device_login_challenges
|
|
SET used_at = NOW()
|
|
WHERE used_at IS NULL AND user_id IN (
|
|
SELECT u.id FROM users AS u WHERE u.role = $1 AND u.id <> $2
|
|
)
|
|
`
|
|
|
|
type InvalidateDeviceChallengesForRoleExcludingUserParams struct {
|
|
Role string `json:"role"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) InvalidateDeviceChallengesForRoleExcludingUser(ctx context.Context, arg InvalidateDeviceChallengesForRoleExcludingUserParams) error {
|
|
_, err := q.db.Exec(ctx, InvalidateDeviceChallengesForRoleExcludingUser, arg.Role, arg.ID)
|
|
return err
|
|
}
|
|
|
|
const InvalidateUserDeviceChallenges = `-- name: InvalidateUserDeviceChallenges :exec
|
|
UPDATE device_login_challenges
|
|
SET used_at = NOW()
|
|
WHERE user_id = $1 AND used_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) InvalidateUserDeviceChallenges(ctx context.Context, userID int64) error {
|
|
_, err := q.db.Exec(ctx, InvalidateUserDeviceChallenges, userID)
|
|
return err
|
|
}
|
|
|
|
const ListActiveUserSessions = `-- name: ListActiveUserSessions :many
|
|
SELECT id, user_id, device_key, device_label, platform, created_at, last_seen_at, expires_at, revoked_at, revoked_reason
|
|
FROM user_sessions
|
|
WHERE user_id = $1 AND revoked_at IS NULL AND expires_at > NOW()
|
|
ORDER BY last_seen_at DESC, created_at DESC, id
|
|
`
|
|
|
|
func (q *Queries) ListActiveUserSessions(ctx context.Context, userID int64) ([]UserSession, error) {
|
|
rows, err := q.db.Query(ctx, ListActiveUserSessions, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []UserSession
|
|
for rows.Next() {
|
|
var i UserSession
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.DeviceKey,
|
|
&i.DeviceLabel,
|
|
&i.Platform,
|
|
&i.CreatedAt,
|
|
&i.LastSeenAt,
|
|
&i.ExpiresAt,
|
|
&i.RevokedAt,
|
|
&i.RevokedReason,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const ListUserIDsForRoleExcludingUser = `-- name: ListUserIDsForRoleExcludingUser :many
|
|
SELECT id
|
|
FROM users
|
|
WHERE role = $1 AND id <> $2
|
|
ORDER BY id
|
|
`
|
|
|
|
type ListUserIDsForRoleExcludingUserParams struct {
|
|
Role string `json:"role"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) ListUserIDsForRoleExcludingUser(ctx context.Context, arg ListUserIDsForRoleExcludingUserParams) ([]int64, error) {
|
|
rows, err := q.db.Query(ctx, ListUserIDsForRoleExcludingUser, arg.Role, arg.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []int64
|
|
for rows.Next() {
|
|
var id int64
|
|
if err := rows.Scan(&id); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, id)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const LockUserSessions = `-- name: LockUserSessions :exec
|
|
SELECT pg_advisory_xact_lock($1)
|
|
`
|
|
|
|
func (q *Queries) LockUserSessions(ctx context.Context, pgAdvisoryXactLock int64) error {
|
|
_, err := q.db.Exec(ctx, LockUserSessions, pgAdvisoryXactLock)
|
|
return err
|
|
}
|
|
|
|
const ResolveDeviceLoginChallengeUserID = `-- name: ResolveDeviceLoginChallengeUserID :one
|
|
SELECT user_id
|
|
FROM device_login_challenges
|
|
WHERE token_hash = $1
|
|
`
|
|
|
|
func (q *Queries) ResolveDeviceLoginChallengeUserID(ctx context.Context, tokenHash []byte) (int64, error) {
|
|
row := q.db.QueryRow(ctx, ResolveDeviceLoginChallengeUserID, tokenHash)
|
|
var user_id int64
|
|
err := row.Scan(&user_id)
|
|
return user_id, err
|
|
}
|
|
|
|
const ResolveRefreshTokenUserID = `-- name: ResolveRefreshTokenUserID :one
|
|
SELECT user_id
|
|
FROM refresh_tokens
|
|
WHERE token = $1
|
|
`
|
|
|
|
func (q *Queries) ResolveRefreshTokenUserID(ctx context.Context, token string) (int64, error) {
|
|
row := q.db.QueryRow(ctx, ResolveRefreshTokenUserID, token)
|
|
var user_id int64
|
|
err := row.Scan(&user_id)
|
|
return user_id, err
|
|
}
|
|
|
|
const RevokeActiveSessionByDeviceKey = `-- name: RevokeActiveSessionByDeviceKey :exec
|
|
UPDATE user_sessions
|
|
SET revoked_at = NOW(), revoked_reason = 'device_relogin'
|
|
WHERE user_id = $1 AND device_key = $2 AND revoked_at IS NULL
|
|
`
|
|
|
|
type RevokeActiveSessionByDeviceKeyParams struct {
|
|
UserID int64 `json:"user_id"`
|
|
DeviceKey pgtype.Text `json:"device_key"`
|
|
}
|
|
|
|
func (q *Queries) RevokeActiveSessionByDeviceKey(ctx context.Context, arg RevokeActiveSessionByDeviceKeyParams) error {
|
|
_, err := q.db.Exec(ctx, RevokeActiveSessionByDeviceKey, arg.UserID, arg.DeviceKey)
|
|
return err
|
|
}
|
|
|
|
const RevokeAllUserRefreshTokens = `-- name: RevokeAllUserRefreshTokens :exec
|
|
UPDATE refresh_tokens SET revoked = TRUE WHERE user_id = $1 AND revoked = FALSE
|
|
`
|
|
|
|
func (q *Queries) RevokeAllUserRefreshTokens(ctx context.Context, userID int64) error {
|
|
_, err := q.db.Exec(ctx, RevokeAllUserRefreshTokens, userID)
|
|
return err
|
|
}
|
|
|
|
const RevokeAllUserSessions = `-- name: RevokeAllUserSessions :exec
|
|
UPDATE user_sessions
|
|
SET revoked_at = NOW(), revoked_reason = $2
|
|
WHERE user_id = $1 AND revoked_at IS NULL
|
|
`
|
|
|
|
type RevokeAllUserSessionsParams struct {
|
|
UserID int64 `json:"user_id"`
|
|
RevokedReason pgtype.Text `json:"revoked_reason"`
|
|
}
|
|
|
|
func (q *Queries) RevokeAllUserSessions(ctx context.Context, arg RevokeAllUserSessionsParams) error {
|
|
_, err := q.db.Exec(ctx, RevokeAllUserSessions, arg.UserID, arg.RevokedReason)
|
|
return err
|
|
}
|
|
|
|
const RevokeExcessUserSessionRefreshTokens = `-- name: RevokeExcessUserSessionRefreshTokens :exec
|
|
WITH excess AS (
|
|
SELECT us.id
|
|
FROM user_sessions AS us
|
|
WHERE us.user_id = $1
|
|
AND us.id <> $2
|
|
AND us.revoked_at IS NULL
|
|
AND us.expires_at > NOW()
|
|
ORDER BY us.last_seen_at DESC, us.created_at DESC, us.id
|
|
OFFSET GREATEST($3::bigint - 1, 0)
|
|
)
|
|
UPDATE refresh_tokens
|
|
SET revoked = TRUE
|
|
WHERE revoked = FALSE AND session_id IN (SELECT id FROM excess)
|
|
`
|
|
|
|
type RevokeExcessUserSessionRefreshTokensParams struct {
|
|
TargetUserID int64 `json:"target_user_id"`
|
|
KeepSessionID uuid.UUID `json:"keep_session_id"`
|
|
MaxDevices int64 `json:"max_devices"`
|
|
}
|
|
|
|
func (q *Queries) RevokeExcessUserSessionRefreshTokens(ctx context.Context, arg RevokeExcessUserSessionRefreshTokensParams) error {
|
|
_, err := q.db.Exec(ctx, RevokeExcessUserSessionRefreshTokens, arg.TargetUserID, arg.KeepSessionID, arg.MaxDevices)
|
|
return err
|
|
}
|
|
|
|
const RevokeExcessUserSessions = `-- name: RevokeExcessUserSessions :exec
|
|
WITH excess AS (
|
|
SELECT us.id
|
|
FROM user_sessions AS us
|
|
WHERE us.user_id = $1
|
|
AND us.id <> $2
|
|
AND us.revoked_at IS NULL
|
|
AND us.expires_at > NOW()
|
|
ORDER BY us.last_seen_at DESC, us.created_at DESC, us.id
|
|
OFFSET GREATEST($3::bigint - 1, 0)
|
|
)
|
|
UPDATE user_sessions
|
|
SET revoked_at = NOW(), revoked_reason = 'over_device_limit'
|
|
WHERE id IN (SELECT id FROM excess)
|
|
`
|
|
|
|
type RevokeExcessUserSessionsParams struct {
|
|
TargetUserID int64 `json:"target_user_id"`
|
|
KeepSessionID uuid.UUID `json:"keep_session_id"`
|
|
MaxDevices int64 `json:"max_devices"`
|
|
}
|
|
|
|
func (q *Queries) RevokeExcessUserSessions(ctx context.Context, arg RevokeExcessUserSessionsParams) error {
|
|
_, err := q.db.Exec(ctx, RevokeExcessUserSessions, arg.TargetUserID, arg.KeepSessionID, arg.MaxDevices)
|
|
return err
|
|
}
|
|
|
|
const RevokeRefreshTokensByDeviceKey = `-- name: RevokeRefreshTokensByDeviceKey :exec
|
|
UPDATE refresh_tokens
|
|
SET revoked = TRUE
|
|
WHERE revoked = FALSE AND session_id IN (
|
|
SELECT us.id FROM user_sessions AS us
|
|
WHERE us.user_id = $1 AND us.device_key = $2 AND us.revoked_at IS NULL
|
|
)
|
|
`
|
|
|
|
type RevokeRefreshTokensByDeviceKeyParams struct {
|
|
UserID int64 `json:"user_id"`
|
|
DeviceKey pgtype.Text `json:"device_key"`
|
|
}
|
|
|
|
func (q *Queries) RevokeRefreshTokensByDeviceKey(ctx context.Context, arg RevokeRefreshTokensByDeviceKeyParams) error {
|
|
_, err := q.db.Exec(ctx, RevokeRefreshTokensByDeviceKey, arg.UserID, arg.DeviceKey)
|
|
return err
|
|
}
|
|
|
|
const RevokeRefreshTokensBySession = `-- name: RevokeRefreshTokensBySession :exec
|
|
UPDATE refresh_tokens SET revoked = TRUE WHERE session_id = $1 AND revoked = FALSE
|
|
`
|
|
|
|
func (q *Queries) RevokeRefreshTokensBySession(ctx context.Context, sessionID uuid.NullUUID) error {
|
|
_, err := q.db.Exec(ctx, RevokeRefreshTokensBySession, sessionID)
|
|
return err
|
|
}
|
|
|
|
const RevokeRefreshTokensForRoleExcludingUser = `-- name: RevokeRefreshTokensForRoleExcludingUser :exec
|
|
UPDATE refresh_tokens
|
|
SET revoked = TRUE
|
|
WHERE revoked = FALSE AND user_id IN (
|
|
SELECT u.id FROM users AS u WHERE u.role = $1 AND u.id <> $2
|
|
)
|
|
`
|
|
|
|
type RevokeRefreshTokensForRoleExcludingUserParams struct {
|
|
Role string `json:"role"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) RevokeRefreshTokensForRoleExcludingUser(ctx context.Context, arg RevokeRefreshTokensForRoleExcludingUserParams) error {
|
|
_, err := q.db.Exec(ctx, RevokeRefreshTokensForRoleExcludingUser, arg.Role, arg.ID)
|
|
return err
|
|
}
|
|
|
|
const RevokeSessionsForRoleExcludingUser = `-- name: RevokeSessionsForRoleExcludingUser :exec
|
|
UPDATE user_sessions
|
|
SET revoked_at = NOW(), revoked_reason = 'bulk_deactivation'
|
|
WHERE revoked_at IS NULL AND user_id IN (
|
|
SELECT u.id FROM users AS u WHERE u.role = $1 AND u.id <> $2
|
|
)
|
|
`
|
|
|
|
type RevokeSessionsForRoleExcludingUserParams struct {
|
|
Role string `json:"role"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) RevokeSessionsForRoleExcludingUser(ctx context.Context, arg RevokeSessionsForRoleExcludingUserParams) error {
|
|
_, err := q.db.Exec(ctx, RevokeSessionsForRoleExcludingUser, arg.Role, arg.ID)
|
|
return err
|
|
}
|
|
|
|
const RevokeUserSession = `-- name: RevokeUserSession :one
|
|
UPDATE user_sessions
|
|
SET revoked_at = NOW(), revoked_reason = $2
|
|
WHERE id = $1 AND revoked_at IS NULL
|
|
RETURNING user_id
|
|
`
|
|
|
|
type RevokeUserSessionParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
RevokedReason pgtype.Text `json:"revoked_reason"`
|
|
}
|
|
|
|
func (q *Queries) RevokeUserSession(ctx context.Context, arg RevokeUserSessionParams) (int64, error) {
|
|
row := q.db.QueryRow(ctx, RevokeUserSession, arg.ID, arg.RevokedReason)
|
|
var user_id int64
|
|
err := row.Scan(&user_id)
|
|
return user_id, err
|
|
}
|
|
|
|
const RevokeUserSessionForUser = `-- name: RevokeUserSessionForUser :one
|
|
UPDATE user_sessions
|
|
SET revoked_at = NOW(), revoked_reason = $3
|
|
WHERE id = $1 AND user_id = $2 AND revoked_at IS NULL
|
|
RETURNING id
|
|
`
|
|
|
|
type RevokeUserSessionForUserParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
UserID int64 `json:"user_id"`
|
|
RevokedReason pgtype.Text `json:"revoked_reason"`
|
|
}
|
|
|
|
func (q *Queries) RevokeUserSessionForUser(ctx context.Context, arg RevokeUserSessionForUserParams) (uuid.UUID, error) {
|
|
row := q.db.QueryRow(ctx, RevokeUserSessionForUser, arg.ID, arg.UserID, arg.RevokedReason)
|
|
var id uuid.UUID
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const RotateRefreshToken = `-- name: RotateRefreshToken :exec
|
|
UPDATE refresh_tokens SET revoked = TRUE WHERE id = $1 AND revoked = FALSE
|
|
`
|
|
|
|
func (q *Queries) RotateRefreshToken(ctx context.Context, id int64) error {
|
|
_, err := q.db.Exec(ctx, RotateRefreshToken, id)
|
|
return err
|
|
}
|
|
|
|
const TouchUserSession = `-- name: TouchUserSession :exec
|
|
UPDATE user_sessions
|
|
SET last_seen_at = NOW(), expires_at = $2
|
|
WHERE id = $1 AND revoked_at IS NULL AND expires_at > NOW()
|
|
`
|
|
|
|
type TouchUserSessionParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
}
|
|
|
|
func (q *Queries) TouchUserSession(ctx context.Context, arg TouchUserSessionParams) error {
|
|
_, err := q.db.Exec(ctx, TouchUserSession, arg.ID, arg.ExpiresAt)
|
|
return err
|
|
}
|
|
|
|
const UseDeviceLoginChallenge = `-- name: UseDeviceLoginChallenge :exec
|
|
UPDATE device_login_challenges SET used_at = NOW() WHERE id = $1 AND used_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) UseDeviceLoginChallenge(ctx context.Context, id uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, UseDeviceLoginChallenge, id)
|
|
return err
|
|
}
|