File tree Expand file tree Collapse file tree
freebuff/web/src/app/api/auth/cli/code
web/src/app/api/auth/cli/code Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ import { z } from 'zod/v4'
88
99import { logger } from '@/util/logger'
1010
11+ import { getLoginUrlOrigin } from './_origin'
12+
1113export 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 } ` ,
Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ import { z } from 'zod/v4'
88
99import { logger } from '@/util/logger'
1010
11+ import { getLoginUrlOrigin } from './_origin'
12+
1113export 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 } ` ,
You can’t perform that action at this time.
0 commit comments