@@ -36,6 +36,11 @@ const pendingRequests = new Map<string, PendingAuthRequest>();
3636export function createAuthorizeHandler ( ) {
3737 return async ( req : Request , res : Response ) => {
3838 try {
39+ logger . debug ( "Authorization handler called" , {
40+ query : req . query ,
41+ url : req . url
42+ } ) ;
43+
3944 const config = getConfig ( ) ;
4045 const {
4146 response_type,
@@ -94,28 +99,25 @@ export function createAuthorizeHandler() {
9499 } ) ;
95100
96101 // Build authorization URL for external provider with our own PKCE
97- const authParams = new URLSearchParams ( {
98- response_type : "code" ,
99- client_id : config . OAUTH_CLIENT_ID ! ,
100- redirect_uri : config . OAUTH_REDIRECT_URI ! ,
101- scope : scope as string || "openid profile email" ,
102- state : requestId , // Use our request ID as state
103- code_challenge : externalCodeChallenge , // Use our generated challenge
104- code_challenge_method : "S256"
105- } ) ;
106-
107- const authUrl = `${ config . OAUTH_ISSUER } /oauth/authorize?${ authParams } ` ;
102+ const authUrl = new URL ( "/oauth/authorize" , config . OAUTH_ISSUER ! ) ;
103+ authUrl . searchParams . set ( "response_type" , "code" ) ;
104+ authUrl . searchParams . set ( "client_id" , config . OAUTH_CLIENT_ID ! ) ;
105+ authUrl . searchParams . set ( "redirect_uri" , config . OAUTH_REDIRECT_URI ! ) ;
106+ authUrl . searchParams . set ( "scope" , scope as string || "openid profile email" ) ;
107+ authUrl . searchParams . set ( "state" , requestId ) ;
108+ authUrl . searchParams . set ( "code_challenge" , externalCodeChallenge ) ;
109+ authUrl . searchParams . set ( "code_challenge_method" , "S256" ) ;
108110
109111 logger . info ( "Proxying OAuth authorization request" , {
110112 client_id,
111113 redirect_uri,
112114 scope,
113115 requestId,
114- external_auth_url : ` ${ config . OAUTH_ISSUER } /oauth/authorize`
116+ external_auth_url : new URL ( " /oauth/authorize" , config . OAUTH_ISSUER ! ) . toString ( )
115117 } ) ;
116118
117119 // Redirect to external OAuth provider
118- res . redirect ( authUrl ) ;
120+ res . redirect ( authUrl . toString ( ) ) ;
119121
120122 } catch ( error ) {
121123 logger . error ( "OAuth authorization proxy error" , {
@@ -279,7 +281,7 @@ export function createCallbackHandler(oauthProvider: OAuthProvider) {
279281 */
280282async function exchangeCodeForTokens ( code : string , config : any , codeVerifier : string ) : Promise < TokenExchangeResponse | null > {
281283 try {
282- const tokenEndpoint = ` ${ config . OAUTH_ISSUER } /oauth/token` ;
284+ const tokenEndpoint = new URL ( " /oauth/token" , config . OAUTH_ISSUER ! ) ;
283285
284286 const tokenParams = new URLSearchParams ( {
285287 grant_type : "authorization_code" ,
@@ -291,11 +293,11 @@ async function exchangeCodeForTokens(code: string, config: any, codeVerifier: st
291293 } ) ;
292294
293295 logger . info ( "Exchanging authorization code with external provider" , {
294- tokenEndpoint,
296+ tokenEndpoint : tokenEndpoint . toString ( ) ,
295297 clientId : config . OAUTH_CLIENT_ID
296298 } ) ;
297299
298- const response = await fetch ( tokenEndpoint , {
300+ const response = await fetch ( tokenEndpoint . toString ( ) , {
299301 method : "POST" ,
300302 headers : {
301303 "Content-Type" : "application/x-www-form-urlencoded" ,
@@ -310,7 +312,7 @@ async function exchangeCodeForTokens(code: string, config: any, codeVerifier: st
310312 status : response . status ,
311313 statusText : response . statusText ,
312314 error : errorText ,
313- tokenEndpoint,
315+ tokenEndpoint : tokenEndpoint . toString ( ) ,
314316 clientId : config . OAUTH_CLIENT_ID
315317 } ) ;
316318 return null ;
0 commit comments