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
24 changes: 23 additions & 1 deletion freebuff/web/src/app/api/auth/cli/code/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { and, eq, gt } from 'drizzle-orm'
import { NextResponse } from 'next/server'
import { z } from 'zod/v4'

import { buildCliAuthCode } from '@/app/onboard/_helpers'
import {
buildCliAuthCode,
getCliAuthCodeHashPrefix,
} from '@/app/onboard/_helpers'
import { logger } from '@/util/logger'

import { getLoginUrlOrigin } from './_origin'
Expand Down Expand Up @@ -82,6 +85,25 @@ export async function POST(req: Request) {
)
loginUrl.searchParams.set('auth_code', loginToken)

logger.info(
{
authCodeTokenHashPrefix: getCliAuthCodeHashPrefix(loginToken),
authCodeTokenLength: loginToken.length,
fingerprintIdPrefix: fingerprintId.slice(0, 24),
fingerprintIdLength: fingerprintId.length,
expiresAt,
loginUrlOrigin: loginUrl.origin,
requestOrigin: new URL(req.url).origin,
requestHost: req.headers.get('host'),
forwardedHost: req.headers.get('x-forwarded-host'),
forwardedProto: req.headers.get('x-forwarded-proto'),
originHeader: req.headers.get('origin'),
configuredAppUrl: env.NEXT_PUBLIC_CODEBUFF_APP_URL,
environment: env.NEXT_PUBLIC_CB_ENVIRONMENT,
},
'Issued Freebuff CLI auth code token',
)

return NextResponse.json({
fingerprintId,
fingerprintHash,
Expand Down
8 changes: 8 additions & 0 deletions freebuff/web/src/app/onboard/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, test } from 'bun:test'

import {
buildCliAuthCode,
getCliAuthCodeHashPrefix,
isAuthCodeExpired,
isOpaqueCliAuthCodeToken,
parseAuthCode,
Expand Down Expand Up @@ -110,6 +111,13 @@ describe('freebuff onboard/_helpers', () => {
expect(isOpaqueCliAuthCodeToken(`${'A'.repeat(42)}.`)).toBe(false)
})

test('hashes auth codes for log correlation without logging the token', () => {
expect(getCliAuthCodeHashPrefix('a'.repeat(43))).toBe('66d34fba71f8')
expect(getCliAuthCodeHashPrefix(` ${'a'.repeat(43)}\n`)).toBe(
'66d34fba71f8',
)
})

test('resolves an opaque browser token before validation', async () => {
const expiresAt = '4102444800000'
const fingerprintHash = genAuthCode(
Expand Down
6 changes: 6 additions & 0 deletions freebuff/web/src/app/onboard/_helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createHash } from 'node:crypto'

import { genAuthCode } from '@codebuff/common/util/credentials'

const OPAQUE_CLI_AUTH_CODE_TOKEN_RE = /^[A-Za-z0-9_-]{43}$/
Expand All @@ -14,6 +16,10 @@ export function isOpaqueCliAuthCodeToken(authCode: string): boolean {
return OPAQUE_CLI_AUTH_CODE_TOKEN_RE.test(authCode.trim())
}

export function getCliAuthCodeHashPrefix(authCode: string): string {
return createHash('sha256').update(authCode.trim()).digest('hex').slice(0, 12)
}

export async function resolveCliAuthCode(
authCode: string,
consumeCliAuthCodeToken: (authCodeToken: string) => Promise<string | null>,
Expand Down
6 changes: 6 additions & 0 deletions freebuff/web/src/app/onboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {
hasCliSessionForAuthHash,
} from './_db'
import {
getCliAuthCodeHashPrefix,
isAuthCodeExpired,
isOpaqueCliAuthCodeToken,
parseAuthCode,
resolveCliAuthCode,
validateAuthCode,
Expand Down Expand Up @@ -112,8 +114,12 @@ const Onboard = async ({ searchParams }: PageProps) => {
logger.warn(
{
authCodeLength: authCode.length,
authCodeTrimmedLength: authCode.trim().length,
authCodeHashPrefix: getCliAuthCodeHashPrefix(authCode),
isOpaqueAuthCodeToken: isOpaqueCliAuthCodeToken(authCode),
resolvedAuthCode: resolvedOpaqueToken,
resolvedAuthCodeLength: resolvedAuthCode.length,
userId: user.id,
dotCount: authCode.match(/\./g)?.length ?? 0,
hyphenCount: authCode.match(/-/g)?.length ?? 0,
fingerprintIdPrefix: fingerprintId.slice(0, 24),
Expand Down
24 changes: 23 additions & 1 deletion web/src/app/api/auth/cli/code/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { and, eq, gt } from 'drizzle-orm'
import { NextResponse } from 'next/server'
import { z } from 'zod/v4'

import { buildCliAuthCode } from '@/app/onboard/_helpers'
import {
buildCliAuthCode,
getCliAuthCodeHashPrefix,
} from '@/app/onboard/_helpers'
import { logger } from '@/util/logger'

import { getLoginUrlOrigin } from './_origin'
Expand Down Expand Up @@ -84,6 +87,25 @@ export async function POST(req: Request) {
)
loginUrl.searchParams.set('auth_code', loginToken)

logger.info(
{
authCodeTokenHashPrefix: getCliAuthCodeHashPrefix(loginToken),
authCodeTokenLength: loginToken.length,
fingerprintIdPrefix: fingerprintId.slice(0, 24),
fingerprintIdLength: fingerprintId.length,
expiresAt,
loginUrlOrigin: loginUrl.origin,
requestOrigin: new URL(req.url).origin,
requestHost: req.headers.get('host'),
forwardedHost: req.headers.get('x-forwarded-host'),
forwardedProto: req.headers.get('x-forwarded-proto'),
originHeader: req.headers.get('origin'),
configuredAppUrl: env.NEXT_PUBLIC_CODEBUFF_APP_URL,
environment: env.NEXT_PUBLIC_CB_ENVIRONMENT,
},
'Issued Codebuff CLI auth code token',
)

return NextResponse.json({
fingerprintId,
fingerprintHash,
Expand Down
8 changes: 8 additions & 0 deletions web/src/app/onboard/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, test } from 'bun:test'

import {
buildCliAuthCode,
getCliAuthCodeHashPrefix,
isAuthCodeExpired,
isOpaqueCliAuthCodeToken,
parseAuthCode,
Expand Down Expand Up @@ -238,6 +239,13 @@ describe('onboard/_helpers', () => {
expect(isOpaqueCliAuthCodeToken(`${'A'.repeat(42)}.`)).toBe(false)
})

test('hashes auth codes for log correlation without logging the token', () => {
expect(getCliAuthCodeHashPrefix('a'.repeat(43))).toBe('66d34fba71f8')
expect(getCliAuthCodeHashPrefix(` ${'a'.repeat(43)}\n`)).toBe(
'66d34fba71f8',
)
})

test('resolves an opaque browser token before validation', async () => {
const expiresAt = '4102444800000'
const fingerprintHash = genAuthCode(
Expand Down
6 changes: 6 additions & 0 deletions web/src/app/onboard/_helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createHash } from 'node:crypto'

import { genAuthCode } from '@codebuff/common/util/credentials'

const OPAQUE_CLI_AUTH_CODE_TOKEN_RE = /^[A-Za-z0-9_-]{43}$/
Expand All @@ -14,6 +16,10 @@ export function isOpaqueCliAuthCodeToken(authCode: string): boolean {
return OPAQUE_CLI_AUTH_CODE_TOKEN_RE.test(authCode.trim())
}

export function getCliAuthCodeHashPrefix(authCode: string): string {
return createHash('sha256').update(authCode.trim()).digest('hex').slice(0, 12)
}

export async function resolveCliAuthCode(
authCode: string,
consumeCliAuthCodeToken: (authCodeToken: string) => Promise<string | null>,
Expand Down
Loading