increase cardinality of platform accounts

This commit is contained in:
Devin Zuczek
2026-08-03 20:32:23 -04:00
parent af2a2a0683
commit 108b061019
7 changed files with 797 additions and 229 deletions
+13 -45
View File
@@ -37,12 +37,14 @@ export interface Account {
identityFlags: number
createdAt: string
/**
* The platform-native identity linked to this account (e.g. a SteamID64 for
* platform 0). Stored as a STRING on purpose a SteamID64 exceeds 2^53 and
* would lose precision as a JS number. Set at account creation from the login's
* `platform_id`. A cached login is authorized ONLY to the account whose stored
* `platformId` matches the (platform_auth-ticket-proven) platform id presented,
* so no one but that platform user can log into the account.
* The account's PRIMARY platform identity — the first one linked (e.g. a SteamID64
* for platform 0). Stored as a STRING on purpose: a SteamID64 exceeds 2^53 and
* would lose precision as a JS number.
*
* An account can be reachable from SEVERAL platform identities (a PC and a headset),
* and those live in the `auth` worker's `platform_account` table — NOT here. Logins
* are authorized against that table alone; this pair is what the account DTO and a
* refreshed token's claims report, and it never gains a second value.
*/
platformId?: string
/** PlatformType int (0 = Steam) that `platformId` belongs to. */
@@ -216,23 +218,6 @@ export async function searchAccounts(
return parseAll(results)
}
/**
* Accounts linked to a platform-native id (e.g. a SteamID64), for the cached-login
* account picker. Backed by the indexed `platform_id` generated column. Empty id
* yields no matches (avoids matching every account whose `platformId` is null).
*/
export async function getAccountsByPlatformId(
db: D1Database,
platformId: string
): Promise<Account[]> {
if (platformId === '') return []
const { results } = await db
.prepare('SELECT data FROM account WHERE platform_id = ?1')
.bind(platformId)
.all<AccountRow>()
return parseAll(results)
}
/**
* Accounts last seen on a given device (the client-supplied `device_id` auth records
* at login). An empty id yields no matches (avoids matching every account with no
@@ -258,7 +243,9 @@ export async function getAccountsByDeviceId(db: D1Database, deviceId: string): P
/** Record the account's most recent successful login time (ISO-8601). */
export async function setLastLoginTime(db: D1Database, id: number, time: string): Promise<void> {
await db
.prepare("UPDATE account SET data = json_set(data, '$.lastLoginTime', ?2) WHERE account_id = ?1")
.prepare(
"UPDATE account SET data = json_set(data, '$.lastLoginTime', ?2) WHERE account_id = ?1"
)
.bind(id, time)
.run()
}
@@ -295,9 +282,7 @@ export async function setLoginContext(
}
if (sets.length === 0) return
await db
.prepare(
`UPDATE account SET data = json_set(data, ${sets.join(', ')}) WHERE account_id = ?1`
)
.prepare(`UPDATE account SET data = json_set(data, ${sets.join(', ')}) WHERE account_id = ?1`)
.bind(id, ...binds)
.run()
}
@@ -324,21 +309,6 @@ export async function countAccountsBySignupIp(db: D1Database, ip: string): Promi
return row?.n ?? 0
}
/**
* How many accounts are linked to a platform-native id (e.g. one SteamID64) — the
* count a per-platform signup cap is enforced against. Backed by the indexed
* `platform_id` generated column. An empty id counts 0 (accounts with no platform
* identity aren't attributable to a platform user).
*/
export async function countAccountsByPlatformId(db: D1Database, platformId: string): Promise<number> {
if (platformId === '') return 0
const row = await db
.prepare('SELECT COUNT(*) AS n FROM account WHERE platform_id = ?1')
.bind(platformId)
.first<{ n: number }>()
return row?.n ?? 0
}
/** Look up multiple accounts by AccountId (order not guaranteed). */
export async function getAccountsByIds(db: D1Database, ids: number[]): Promise<Account[]> {
if (ids.length === 0) return []
@@ -411,9 +381,7 @@ export async function getPasswordHash(db: D1Database, id: number): Promise<strin
/** Persist the account's password hash. Returns false when no such account exists. */
export async function setPasswordHash(db: D1Database, id: number, hash: string): Promise<boolean> {
const { meta } = await db
.prepare(
"UPDATE account SET data = json_set(data, '$.passwordHash', ?2) WHERE account_id = ?1"
)
.prepare("UPDATE account SET data = json_set(data, '$.passwordHash', ?2) WHERE account_id = ?1")
.bind(id, hash)
.run()
return meta.changes > 0