Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions freebuff/web/src/app/api/auth/cli/code/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { randomBytes } from 'node:crypto'

import { genAuthCode } from '@codebuff/common/util/credentials'
import db from '@codebuff/internal/db'
import * as schema from '@codebuff/internal/db/schema'
Expand Down Expand Up @@ -55,6 +57,15 @@ export async function POST(req: Request) {
)
}

const authCode = `${fingerprintId}.${expiresAt}.${fingerprintHash}`
const loginToken = randomBytes(32).toString('base64url')

await db.insert(schema.verificationToken).values({
identifier: `cli-login:${loginToken}`,
token: authCode,
expires: new Date(expiresAt),
})

const loginUrl = new URL(
'/login',
getLoginUrlOrigin(
Expand All @@ -64,10 +75,7 @@ export async function POST(req: Request) {
env.NEXT_PUBLIC_CB_ENVIRONMENT !== 'prod',
),
)
loginUrl.searchParams.set(
'auth_code',
`${fingerprintId}.${expiresAt}.${fingerprintHash}`,
)
loginUrl.searchParams.set('auth_code', loginToken)

return NextResponse.json({
fingerprintId,
Expand Down
17 changes: 17 additions & 0 deletions freebuff/web/src/app/onboard/_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ export async function hasCliSessionForAuthHash(
return existing.length > 0
}

export async function getCliAuthCodeForToken(
authCodeToken: string,
): Promise<string | null> {
const existing = await db
.select({ authCode: schema.verificationToken.token })
.from(schema.verificationToken)
.where(
and(
eq(schema.verificationToken.identifier, `cli-login:${authCodeToken}`),
gt(schema.verificationToken.expires, new Date()),
),
)
.limit(1)

return existing[0]?.authCode ?? null
}

export async function checkFingerprintConflict(
fingerprintId: string,
userId: string,
Expand Down
7 changes: 6 additions & 1 deletion freebuff/web/src/app/onboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getServerSession } from 'next-auth'
import {
checkFingerprintConflict,
createCliSession,
getCliAuthCodeForToken,
getSessionTokenFromCookies,
hasCliSessionForAuthHash,
} from './_db'
Expand Down Expand Up @@ -91,7 +92,9 @@ const Onboard = async ({ searchParams }: PageProps) => {
)
}

const { fingerprintId, expiresAt, receivedHash } = parseAuthCode(authCode)
const resolvedAuthCode = (await getCliAuthCodeForToken(authCode)) ?? authCode
const { fingerprintId, expiresAt, receivedHash } =
parseAuthCode(resolvedAuthCode)
const { valid, expectedHash: fingerprintHash } = validateAuthCode(
receivedHash,
fingerprintId,
Expand All @@ -103,6 +106,8 @@ const Onboard = async ({ searchParams }: PageProps) => {
logger.warn(
{
authCodeLength: authCode.length,
resolvedAuthCode: resolvedAuthCode !== authCode,
resolvedAuthCodeLength: resolvedAuthCode.length,
dotCount: authCode.match(/\./g)?.length ?? 0,
hyphenCount: authCode.match(/-/g)?.length ?? 0,
fingerprintIdPrefix: fingerprintId.slice(0, 24),
Expand Down
16 changes: 12 additions & 4 deletions web/src/app/api/auth/cli/code/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { randomBytes } from 'node:crypto'

import { genAuthCode } from '@codebuff/common/util/credentials'
import db from '@codebuff/internal/db'
import * as schema from '@codebuff/internal/db/schema'
Expand Down Expand Up @@ -57,6 +59,15 @@ export async function POST(req: Request) {
)
}

const authCode = `${fingerprintId}.${expiresAt}.${fingerprintHash}`
const loginToken = randomBytes(32).toString('base64url')

await db.insert(schema.verificationToken).values({
identifier: `cli-login:${loginToken}`,
token: authCode,
expires: new Date(expiresAt),
})

const loginUrl = new URL(
'/login',
getLoginUrlOrigin(
Expand All @@ -66,10 +77,7 @@ export async function POST(req: Request) {
env.NEXT_PUBLIC_CB_ENVIRONMENT !== 'prod',
),
)
loginUrl.searchParams.set(
'auth_code',
`${fingerprintId}.${expiresAt}.${fingerprintHash}`,
)
loginUrl.searchParams.set('auth_code', loginToken)

return NextResponse.json({
fingerprintId,
Expand Down
17 changes: 17 additions & 0 deletions web/src/app/onboard/_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ export async function hasCliSessionForAuthHash(
return existing.length > 0
}

export async function getCliAuthCodeForToken(
authCodeToken: string,
): Promise<string | null> {
const existing = await db
.select({ authCode: schema.verificationToken.token })
.from(schema.verificationToken)
.where(
and(
eq(schema.verificationToken.identifier, `cli-login:${authCodeToken}`),
gt(schema.verificationToken.expires, new Date()),
),
)
.limit(1)

return existing[0]?.authCode ?? null
}

export async function checkFingerprintConflict(
fingerprintId: string,
userId: string,
Expand Down
5 changes: 4 additions & 1 deletion web/src/app/onboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getServerSession } from 'next-auth'
import {
checkFingerprintConflict,
createCliSession,
getCliAuthCodeForToken,
getSessionTokenFromCookies,
hasCliSessionForAuthHash,
} from './_db'
Expand Down Expand Up @@ -48,7 +49,9 @@ const Onboard = async ({ searchParams }: PageProps) => {
)
}

const { fingerprintId, expiresAt, receivedHash } = parseAuthCode(authCode)
const resolvedAuthCode = (await getCliAuthCodeForToken(authCode)) ?? authCode
const { fingerprintId, expiresAt, receivedHash } =
parseAuthCode(resolvedAuthCode)
const { valid, expectedHash: fingerprintHash } = validateAuthCode(
receivedHash,
fingerprintId,
Expand Down
Loading