@@ -52,18 +52,21 @@ async function handleToken(request: Request, env: Env): Promise<Response> {
5252 return withCors ( request , env , new Response ( 'Invalid JSON' , { status : 400 } ) ) ;
5353 }
5454
55- const client_id = typeof payload ?. client_id === 'string' ? payload . client_id . trim ( ) : '' ;
55+ const payloadClientId = typeof payload ?. client_id === 'string' ? payload . client_id . trim ( ) : '' ;
5656 const code = typeof payload ?. code === 'string' ? payload . code . trim ( ) : '' ;
5757 const redirect_uri = typeof payload ?. redirect_uri === 'string' ? payload . redirect_uri . trim ( ) : '' ;
5858 const code_verifier = typeof payload ?. code_verifier === 'string' ? payload . code_verifier . trim ( ) : '' ;
5959
60+ // If the worker is configured with a fixed client_id, prefer it and allow callers to omit client_id entirely.
61+ const configuredClientId = String ( env . GITHUB_CLIENT_ID || '' ) . trim ( ) ;
62+ const client_id = payloadClientId || configuredClientId ;
63+
6064 if ( ! client_id || ! code || ! redirect_uri || ! code_verifier ) {
6165 return withCors ( request , env , new Response ( 'Missing required fields' , { status : 400 } ) ) ;
6266 }
6367
6468 // Optional hardening: if the worker is configured with a fixed client_id, reject mismatches.
65- const configuredClientId = String ( env . GITHUB_CLIENT_ID || '' ) . trim ( ) ;
66- if ( configuredClientId && configuredClientId !== client_id ) {
69+ if ( configuredClientId && payloadClientId && configuredClientId !== payloadClientId ) {
6770 return withCors ( request , env , new Response ( 'Invalid client_id' , { status : 400 } ) ) ;
6871 }
6972
0 commit comments