|
5 | 5 | /** |
6 | 6 | * Pure data access functions for passkey storage using Kysely query builder. |
7 | 7 | * |
8 | | - * TODO: Implement repository functions once database migration is applied in FXA-12901. |
9 | | - * Expected functions: |
10 | | - * - findPasskeysByUid(db, uid) |
11 | | - * - findPasskeyByCredentialId(db, credentialId) |
12 | | - * - insertPasskey(db, passkey) |
13 | | - * - updatePasskeyCounterAndLastUsed(db, credentialId, signCount) |
14 | | - * - deletePasskey(db, uid, credentialId) |
15 | | - * - deleteAllPasskeysForUser(db, uid) |
16 | | - * - countPasskeysByUid(db, uid) |
| 8 | + * These are pure functions that take the database instance as the first parameter, |
| 9 | + * following the functional repository pattern used throughout FxA. |
| 10 | + * |
| 11 | + * Note: On successful authentication, update: |
| 12 | + * - lastUsedAt: Current timestamp |
| 13 | + * - signCount: Value from authenticator data (should increment) |
| 14 | + * - backupState: Current backup state flag (BS) from authenticator data (0 or 1) |
| 15 | + * |
| 16 | + * On registration: |
| 17 | + * - Set backupEligible from authenticator data (BE flag) - 0 or 1 |
| 18 | + * - Set backupState from authenticator data (BS flag) - 0 or 1 |
| 19 | + * - Leave lastUsedAt as NULL (never used for authentication) |
| 20 | + * |
| 21 | + * On failed authentication, do not update anything. |
| 22 | + * |
| 23 | + * Type conversion: Backup flags are stored as TINYINT(1) in MySQL (0 or 1). |
| 24 | + * - For INSERT/UPDATE: Pass numbers (0 or 1) |
| 25 | + * - For SELECT: Kysely returns booleans (false or true) |
| 26 | + */ |
| 27 | + |
| 28 | +import type { |
| 29 | + AccountDatabase, |
| 30 | + Passkey, |
| 31 | + NewPasskey, |
| 32 | +} from '@fxa/shared/db/mysql/account'; |
| 33 | + |
| 34 | +/** |
| 35 | + * Find all passkeys for a given user. |
| 36 | + * |
| 37 | + * Note: Results are not ordered. The number of passkeys per user will be |
| 38 | + * constrained (typically < 10), so ordering is not necessary and clients |
| 39 | + * can sort as needed. |
| 40 | + * |
| 41 | + * @param db - Database instance |
| 42 | + * @param uid - User ID (16-byte Buffer) |
| 43 | + * @returns Array of passkeys for the user (unordered) |
| 44 | + */ |
| 45 | +export async function findPasskeysByUid( |
| 46 | + db: AccountDatabase, |
| 47 | + uid: Buffer |
| 48 | +): Promise<Passkey[]> { |
| 49 | + return await db |
| 50 | + .selectFrom('passkeys') |
| 51 | + .selectAll() |
| 52 | + .where('uid', '=', uid) |
| 53 | + .execute(); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Find a passkey by its credential ID. |
| 58 | + * |
| 59 | + * @param db - Database instance |
| 60 | + * @param credentialId - WebAuthn credential ID (Buffer) |
| 61 | + * @returns Passkey if found, undefined otherwise |
17 | 62 | */ |
| 63 | +export async function findPasskeyByCredentialId( |
| 64 | + db: AccountDatabase, |
| 65 | + credentialId: Buffer |
| 66 | +): Promise<Passkey | undefined> { |
| 67 | + return await db |
| 68 | + .selectFrom('passkeys') |
| 69 | + .selectAll() |
| 70 | + .where('credentialId', '=', credentialId) |
| 71 | + .executeTakeFirst(); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Insert a new passkey record. |
| 76 | + * |
| 77 | + * @param db - Database instance |
| 78 | + * @param passkey - New passkey data to insert |
| 79 | + */ |
| 80 | +export async function insertPasskey( |
| 81 | + db: AccountDatabase, |
| 82 | + passkey: NewPasskey |
| 83 | +): Promise<void> { |
| 84 | + await db.insertInto('passkeys').values(passkey).execute(); |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Update passkey metadata after successful authentication. |
| 89 | + * |
| 90 | + * Updates lastUsedAt, signCount, and backupState for a passkey. |
| 91 | + * Should only be called after successful authentication. |
| 92 | + * |
| 93 | + * @param db - Database instance |
| 94 | + * @param credentialId - WebAuthn credential ID (Buffer) |
| 95 | + * @param signCount - New signature count from authenticator data |
| 96 | + * @param backupState - Current backup state flag (0 or 1) from authenticator data |
| 97 | + */ |
| 98 | +export async function updatePasskeyCounterAndLastUsed( |
| 99 | + db: AccountDatabase, |
| 100 | + credentialId: Buffer, |
| 101 | + signCount: number, |
| 102 | + backupState: number |
| 103 | +): Promise<void> { |
| 104 | + await db |
| 105 | + .updateTable('passkeys') |
| 106 | + .set({ |
| 107 | + lastUsedAt: Date.now(), |
| 108 | + signCount: signCount, |
| 109 | + backupState: backupState, |
| 110 | + }) |
| 111 | + .where('credentialId', '=', credentialId) |
| 112 | + .execute(); |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Update the friendly name for a passkey. |
| 117 | + * |
| 118 | + * @param db - Database instance |
| 119 | + * @param credentialId - WebAuthn credential ID (Buffer) |
| 120 | + * @param name - New friendly name for the passkey |
| 121 | + * @returns Number of rows updated (should be 1 if successful) |
| 122 | + */ |
| 123 | +export async function updatePasskeyName( |
| 124 | + db: AccountDatabase, |
| 125 | + credentialId: Buffer, |
| 126 | + name: string |
| 127 | +): Promise<number> { |
| 128 | + const result = await db |
| 129 | + .updateTable('passkeys') |
| 130 | + .set({ name }) |
| 131 | + .where('credentialId', '=', credentialId) |
| 132 | + .execute(); |
| 133 | + |
| 134 | + return result.length; |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * Delete a specific passkey for a user. |
| 139 | + * |
| 140 | + * @param db - Database instance |
| 141 | + * @param uid - User ID (16-byte Buffer) |
| 142 | + * @param credentialId - WebAuthn credential ID (Buffer) |
| 143 | + * @returns true if a passkey was deleted, false otherwise |
| 144 | + */ |
| 145 | +export async function deletePasskey( |
| 146 | + db: AccountDatabase, |
| 147 | + uid: Buffer, |
| 148 | + credentialId: Buffer |
| 149 | +): Promise<boolean> { |
| 150 | + const result = await db |
| 151 | + .deleteFrom('passkeys') |
| 152 | + .where('uid', '=', uid) |
| 153 | + .where('credentialId', '=', credentialId) |
| 154 | + .executeTakeFirst(); |
| 155 | + |
| 156 | + return result.numDeletedRows === BigInt(1); |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Delete all passkeys for a user. |
| 161 | + * |
| 162 | + * @param db - Database instance |
| 163 | + * @param uid - User ID (16-byte Buffer) |
| 164 | + * @returns Number of passkeys deleted |
| 165 | + */ |
| 166 | +export async function deleteAllPasskeysForUser( |
| 167 | + db: AccountDatabase, |
| 168 | + uid: Buffer |
| 169 | +): Promise<number> { |
| 170 | + const result = await db |
| 171 | + .deleteFrom('passkeys') |
| 172 | + .where('uid', '=', uid) |
| 173 | + .executeTakeFirst(); |
| 174 | + |
| 175 | + return Number(result.numDeletedRows); |
| 176 | +} |
| 177 | + |
| 178 | +/** |
| 179 | + * Count the number of passkeys for a user. |
| 180 | + * |
| 181 | + * @param db - Database instance |
| 182 | + * @param uid - User ID (16-byte Buffer) |
| 183 | + * @returns Number of passkeys for the user |
| 184 | + */ |
| 185 | +export async function countPasskeysByUid( |
| 186 | + db: AccountDatabase, |
| 187 | + uid: Buffer |
| 188 | +): Promise<number> { |
| 189 | + const result = await db |
| 190 | + .selectFrom('passkeys') |
| 191 | + .select(db.fn.count('credentialId').as('count')) |
| 192 | + .where('uid', '=', uid) |
| 193 | + .executeTakeFirst(); |
| 194 | + |
| 195 | + return Number(result?.count ?? 0); |
| 196 | +} |
0 commit comments