- 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.
169 lines
5.1 KiB
SQL
169 lines
5.1 KiB
SQL
-- name: LockUserSessions :exec
|
|
SELECT pg_advisory_xact_lock($1);
|
|
|
|
-- name: ResolveRefreshTokenUserID :one
|
|
SELECT user_id
|
|
FROM refresh_tokens
|
|
WHERE token = $1;
|
|
|
|
-- name: ResolveDeviceLoginChallengeUserID :one
|
|
SELECT user_id
|
|
FROM device_login_challenges
|
|
WHERE token_hash = $1;
|
|
|
|
-- 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;
|
|
|
|
-- 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
|
|
);
|
|
|
|
-- name: CountActiveUserSessions :one
|
|
SELECT COUNT(*)
|
|
FROM user_sessions
|
|
WHERE user_id = $1 AND revoked_at IS NULL AND expires_at > NOW();
|
|
|
|
-- name: CreateUserSession :one
|
|
INSERT INTO user_sessions (user_id, device_key, device_label, platform, expires_at)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING *;
|
|
|
|
-- name: CreateRefreshTokenForSession :exec
|
|
INSERT INTO refresh_tokens (user_id, token, expires_at, created_at, revoked, session_id)
|
|
VALUES ($1, $2, $3, $4, FALSE, $5);
|
|
|
|
-- 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);
|
|
|
|
-- name: ListActiveUserSessions :many
|
|
SELECT *
|
|
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;
|
|
|
|
-- name: RevokeExcessUserSessionRefreshTokens :exec
|
|
WITH excess AS (
|
|
SELECT us.id
|
|
FROM user_sessions AS us
|
|
WHERE us.user_id = sqlc.arg(target_user_id)
|
|
AND us.id <> sqlc.arg(keep_session_id)
|
|
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(sqlc.arg(max_devices)::bigint - 1, 0)
|
|
)
|
|
UPDATE refresh_tokens
|
|
SET revoked = TRUE
|
|
WHERE revoked = FALSE AND session_id IN (SELECT id FROM excess);
|
|
|
|
-- name: RevokeExcessUserSessions :exec
|
|
WITH excess AS (
|
|
SELECT us.id
|
|
FROM user_sessions AS us
|
|
WHERE us.user_id = sqlc.arg(target_user_id)
|
|
AND us.id <> sqlc.arg(keep_session_id)
|
|
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(sqlc.arg(max_devices)::bigint - 1, 0)
|
|
)
|
|
UPDATE user_sessions
|
|
SET revoked_at = NOW(), revoked_reason = 'over_device_limit'
|
|
WHERE id IN (SELECT id FROM excess);
|
|
|
|
-- name: GetActiveUserSession :one
|
|
SELECT *
|
|
FROM user_sessions
|
|
WHERE id = $1 AND revoked_at IS NULL AND expires_at > NOW();
|
|
|
|
-- name: GetActiveUserSessionForUser :one
|
|
SELECT *
|
|
FROM user_sessions
|
|
WHERE id = $1 AND user_id = $2 AND revoked_at IS NULL AND expires_at > NOW();
|
|
|
|
-- 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;
|
|
|
|
-- name: RevokeUserSession :one
|
|
UPDATE user_sessions
|
|
SET revoked_at = NOW(), revoked_reason = $2
|
|
WHERE id = $1 AND revoked_at IS NULL
|
|
RETURNING user_id;
|
|
|
|
-- name: RevokeRefreshTokensBySession :exec
|
|
UPDATE refresh_tokens SET revoked = TRUE WHERE session_id = $1 AND revoked = FALSE;
|
|
|
|
-- name: RevokeAllUserSessions :exec
|
|
UPDATE user_sessions
|
|
SET revoked_at = NOW(), revoked_reason = $2
|
|
WHERE user_id = $1 AND revoked_at IS NULL;
|
|
|
|
-- name: RevokeAllUserRefreshTokens :exec
|
|
UPDATE refresh_tokens SET revoked = TRUE WHERE user_id = $1 AND revoked = FALSE;
|
|
|
|
-- 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
|
|
);
|
|
|
|
-- 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
|
|
);
|
|
|
|
-- name: ListUserIDsForRoleExcludingUser :many
|
|
SELECT id
|
|
FROM users
|
|
WHERE role = $1 AND id <> $2
|
|
ORDER BY id;
|
|
|
|
-- name: InvalidateUserDeviceChallenges :exec
|
|
UPDATE device_login_challenges
|
|
SET used_at = NOW()
|
|
WHERE user_id = $1 AND used_at IS NULL;
|
|
|
|
-- 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
|
|
);
|
|
|
|
-- name: GetDeviceLoginChallengeForUpdate :one
|
|
SELECT *
|
|
FROM device_login_challenges
|
|
WHERE token_hash = $1
|
|
FOR UPDATE;
|
|
|
|
-- name: UseDeviceLoginChallenge :exec
|
|
UPDATE device_login_challenges SET used_at = NOW() WHERE id = $1 AND used_at IS NULL;
|
|
|
|
-- name: GetRefreshTokenForUpdate :one
|
|
SELECT * FROM refresh_tokens WHERE token = $1 FOR UPDATE;
|
|
|
|
-- name: RotateRefreshToken :exec
|
|
UPDATE refresh_tokens SET revoked = TRUE WHERE id = $1 AND revoked = FALSE;
|
|
|
|
-- 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();
|
|
|
|
-- name: AttachRefreshTokenToSession :exec
|
|
UPDATE refresh_tokens SET session_id = $2 WHERE id = $1 AND session_id IS NULL;
|