( authCodeToken: string, )
| 40 | } |
| 41 | |
| 42 | export async function consumeCliAuthCodeToken( |
| 43 | authCodeToken: string, |
| 44 | ): Promise<CliAuthCodeTokenConsumeResult> { |
| 45 | const activeIdentifier = getCliAuthCodeTokenIdentifier(authCodeToken) |
| 46 | const consumedIdentifier = |
| 47 | getConsumedCliAuthCodeTokenIdentifier(authCodeToken) |
| 48 | const getConsumedTokenStatus = |
| 49 | async (): Promise<CliAuthCodeTokenConsumeResult> => { |
| 50 | const existingConsumed = await db |
| 51 | .select({ id: schema.verificationToken.identifier }) |
| 52 | .from(schema.verificationToken) |
| 53 | .where(eq(schema.verificationToken.identifier, consumedIdentifier)) |
| 54 | .limit(1) |
| 55 | |
| 56 | return existingConsumed[0] |
| 57 | ? { status: 'already_consumed' } |
| 58 | : { status: 'missing' } |
| 59 | } |
| 60 | |
| 61 | const active = await db |
| 62 | .select({ authCode: schema.verificationToken.token }) |
| 63 | .from(schema.verificationToken) |
| 64 | .where(eq(schema.verificationToken.identifier, activeIdentifier)) |
| 65 | .limit(1) |
| 66 | const authCode = active[0]?.authCode |
| 67 | |
| 68 | if (!authCode) { |
| 69 | return getConsumedTokenStatus() |
| 70 | } |
| 71 | |
| 72 | const consumed = await db |
| 73 | .update(schema.verificationToken) |
| 74 | .set({ |
| 75 | identifier: consumedIdentifier, |
| 76 | token: getConsumedCliAuthCodeTokenValue(), |
| 77 | }) |
| 78 | .where( |
| 79 | and( |
| 80 | eq(schema.verificationToken.identifier, activeIdentifier), |
| 81 | eq(schema.verificationToken.token, authCode), |
| 82 | ), |
| 83 | ) |
| 84 | .returning({ id: schema.verificationToken.identifier }) |
| 85 | |
| 86 | if (consumed[0]) { |
| 87 | return { status: 'resolved', authCode } |
| 88 | } |
| 89 | |
| 90 | return getConsumedTokenStatus() |
| 91 | } |
| 92 | |
| 93 | export async function checkFingerprintConflict( |
| 94 | fingerprintId: string, |
no test coverage detected