Skip to content

Commit 21580f1

Browse files
committed
Fix CLI login URL origin
1 parent b2575cc commit 21580f1

6 files changed

Lines changed: 70 additions & 6 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, test } from 'bun:test'
2+
3+
import { getLoginUrlOrigin } from '../_origin'
4+
5+
describe('api/auth/cli/code/_origin', () => {
6+
test('uses the configured public app URL over the request origin', () => {
7+
const req = new Request('https://localhost:10000/api/auth/cli/code')
8+
9+
expect(getLoginUrlOrigin(req, 'https://freebuff.com')).toBe(
10+
'https://freebuff.com',
11+
)
12+
})
13+
14+
test('falls back to the request origin when configured URL is invalid', () => {
15+
const req = new Request('http://localhost:3002/api/auth/cli/code')
16+
17+
expect(getLoginUrlOrigin(req, 'not a url')).toBe('http://localhost:3002')
18+
})
19+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function getLoginUrlOrigin(
2+
req: Request,
3+
configuredAppUrl: string,
4+
): string {
5+
try {
6+
return new URL(configuredAppUrl).origin
7+
} catch {
8+
return new URL(req.url).origin
9+
}
10+
}

freebuff/web/src/app/api/auth/cli/code/route.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { z } from 'zod/v4'
88

99
import { logger } from '@/util/logger'
1010

11+
import { getLoginUrlOrigin } from './_origin'
12+
1113
export async function POST(req: Request) {
1214
const reqSchema = z.object({
1315
fingerprintId: z.string(),
@@ -53,9 +55,10 @@ export async function POST(req: Request) {
5355
)
5456
}
5557

56-
// Generate login URL on the same origin that issued the auth code. This
57-
// avoids bouncing between apex/www hosts during the browser OAuth flow.
58-
const loginUrl = new URL('/login', new URL(req.url).origin)
58+
const loginUrl = new URL(
59+
'/login',
60+
getLoginUrlOrigin(req, env.NEXT_PUBLIC_CODEBUFF_APP_URL),
61+
)
5962
loginUrl.searchParams.set(
6063
'auth_code',
6164
`${fingerprintId}.${expiresAt}.${fingerprintHash}`,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, test } from 'bun:test'
2+
3+
import { getLoginUrlOrigin } from '../_origin'
4+
5+
describe('api/auth/cli/code/_origin', () => {
6+
test('uses the configured public app URL over the request origin', () => {
7+
const req = new Request('https://localhost:10000/api/auth/cli/code')
8+
9+
expect(getLoginUrlOrigin(req, 'https://www.codebuff.com')).toBe(
10+
'https://www.codebuff.com',
11+
)
12+
})
13+
14+
test('falls back to the request origin when configured URL is invalid', () => {
15+
const req = new Request('http://localhost:3000/api/auth/cli/code')
16+
17+
expect(getLoginUrlOrigin(req, 'not a url')).toBe('http://localhost:3000')
18+
})
19+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function getLoginUrlOrigin(
2+
req: Request,
3+
configuredAppUrl: string,
4+
): string {
5+
try {
6+
return new URL(configuredAppUrl).origin
7+
} catch {
8+
return new URL(req.url).origin
9+
}
10+
}

web/src/app/api/auth/cli/code/route.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { z } from 'zod/v4'
88

99
import { logger } from '@/util/logger'
1010

11+
import { getLoginUrlOrigin } from './_origin'
12+
1113
export async function POST(req: Request) {
1214
const reqSchema = z.object({
1315
fingerprintId: z.string(),
@@ -55,9 +57,10 @@ export async function POST(req: Request) {
5557
)
5658
}
5759

58-
// Generate login URL on the same origin that issued the auth code. This
59-
// avoids bouncing between apex/www hosts during the browser OAuth flow.
60-
const loginUrl = new URL('/login', new URL(req.url).origin)
60+
const loginUrl = new URL(
61+
'/login',
62+
getLoginUrlOrigin(req, env.NEXT_PUBLIC_CODEBUFF_APP_URL),
63+
)
6164
loginUrl.searchParams.set(
6265
'auth_code',
6366
`${fingerprintId}.${expiresAt}.${fingerprintHash}`,

0 commit comments

Comments
 (0)