fix: revoke API keys on password rotation and audit key owners

This commit is contained in:
TIANYAO ZHANG committed 2026-10-03 17:17:34 +08:00
1 parent 79fb8443be
commit b9c79c8138
7 files changed
+110 -20

No files matched your search

+8 -1
View File
@@ -35,6 +35,7 @@ export interface CreatedApiKey {
export interface ApiKeyStore {
/** Returns null when the per-user key cap is reached. */
create(userId: string, name: string): CreatedApiKey | null;
findById(id: string): ApiKeyWithUser | null;
listForUser(userId: string): ApiKeyRow[];
listAll(): ApiKeyWithUser[];
/** With userId, only deletes a key owned by that user. */
@@ -60,7 +61,9 @@ export function createApiKeyStore(db: Database.Database): ApiKeyStore {
ORDER BY k.createdAt DESC`
);
const selectByIdStmt = db.prepare(
"SELECT id, userId, name, keyPrefix, createdAt, lastUsedAt FROM api_keys WHERE id = ?"
`SELECT k.id, k.userId, k.name, k.keyPrefix, k.createdAt, k.lastUsedAt, u.username
FROM api_keys k INNER JOIN users u ON u.id = k.userId
WHERE k.id = ?`
);
const deleteStmt = db.prepare("DELETE FROM api_keys WHERE id = ?");
const deleteAllForUserStmt = db.prepare("DELETE FROM api_keys WHERE userId = ?");
@@ -91,6 +94,10 @@ export function createApiKeyStore(db: Database.Database): ApiKeyStore {
return { key: row, rawKey };
},
findById(id) {
return (selectByIdStmt.get(id) as ApiKeyWithUser | undefined) ?? null;
},
listForUser(userId) {
return selectForUserStmt.all(userId) as ApiKeyRow[];
},