@@ -258,13 +258,18 @@ async def init_oauth_flow(
258258 """Initialize OAuth flow and return authorization URL."""
259259
260260 try :
261+ logger .debug ('OAuth init requested for auth_id=%s' , oauth_request .auth_id )
261262 # Get authenticator instance by ID
262263 auth_id = UUID (oauth_request .auth_id )
263264 authenticator = await get_authenticator_instance (
264265 auth_id , authenticator_repository
265266 )
266267
267268 if not authenticator :
269+ logger .debug (
270+ 'OAuth init: no enabled authenticator for auth_id=%s' ,
271+ oauth_request .auth_id ,
272+ )
268273 return JSONResponse (
269274 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
270275 content = response_formatter .buildErrorResponse (
@@ -275,6 +280,13 @@ async def init_oauth_flow(
275280 # Mint opaque CSRF state + OIDC nonce, persist server-side, and pass
276281 # both into the provider so they end up in the authorize URL.
277282 state , nonce = _store_oauth_flow (cache_manager , oauth_request .auth_id )
283+ logger .debug (
284+ 'OAuth flow stored: auth_id=%s state=%s nonce=%s ttl=%ss' ,
285+ oauth_request .auth_id ,
286+ state ,
287+ nonce ,
288+ OAUTH_FLOW_TTL_SECONDS ,
289+ )
278290 auth_url = authenticator .get_authorization_url (state , nonce = nonce )
279291
280292 if not auth_url :
@@ -325,6 +337,13 @@ async def google_oauth_callback(
325337 token_service : TokenService = Depends (Provide [AuthContainer .token_service ]),
326338):
327339 """Handle Google OAuth callback."""
340+ logger .debug (
341+ 'Google OAuth callback: has_code=%s has_state=%s has_error=%s state=%s' ,
342+ bool (code ),
343+ bool (state ),
344+ bool (error ),
345+ state ,
346+ )
328347 flow = _consume_oauth_flow (cache_manager , state )
329348 if flow is None :
330349 logger .warning ('Google OAuth callback received unknown/expired state' )
@@ -371,6 +390,13 @@ async def microsoft_oauth_callback(
371390 token_service : TokenService = Depends (Provide [AuthContainer .token_service ]),
372391):
373392 """Handle Microsoft OAuth callback."""
393+ logger .debug (
394+ 'Microsoft OAuth callback: has_code=%s has_state=%s has_error=%s state=%s' ,
395+ bool (code ),
396+ bool (state ),
397+ bool (error ),
398+ state ,
399+ )
374400 flow = _consume_oauth_flow (cache_manager , state )
375401 if flow is None :
376402 logger .warning ('Microsoft OAuth callback received unknown/expired state' )
@@ -415,6 +441,13 @@ async def microsoft_adfs_oauth_callback(
415441 token_service : TokenService = Depends (Provide [AuthContainer .token_service ]),
416442):
417443 """Handle Microsoft ADFS OAuth callback."""
444+ logger .debug (
445+ 'Microsoft ADFS callback: has_code=%s has_state=%s has_error=%s state=%s' ,
446+ bool (code ),
447+ bool (state ),
448+ bool (error ),
449+ state ,
450+ )
418451 flow = _consume_oauth_flow (cache_manager , state )
419452 if flow is None :
420453 logger .warning ('Microsoft ADFS callback received unknown/expired state' )
@@ -451,6 +484,14 @@ async def _handle_oauth_callback(
451484 """Common OAuth callback handler."""
452485
453486 try :
487+ logger .debug (
488+ '_handle_oauth_callback: auth_id=%s has_code=%s has_error=%s '
489+ 'expected_nonce_set=%s' ,
490+ auth_id ,
491+ bool (callback_data .get ('authorization_code' )),
492+ bool (callback_data .get ('error' )),
493+ expected_nonce is not None ,
494+ )
454495 # Get authenticator instance and config
455496 auth_uuid = UUID (auth_id )
456497 authenticator , config_data = await get_authenticator_with_config (
@@ -481,6 +522,12 @@ def get_failure_redirect(error_msg: str) -> RedirectResponse:
481522 provider = config_data .get ('auth_type' )
482523 success_url = config_data .get ('config' , {}).get ('client_redirect_success_url' )
483524 failure_url = config_data .get ('config' , {}).get ('client_redirect_failure_url' )
525+ logger .debug (
526+ '_handle_oauth_callback: provider=%s success_url=%s failure_url=%s' ,
527+ provider ,
528+ success_url ,
529+ failure_url ,
530+ )
484531
485532 # Handle OAuth error from provider
486533 if callback_data .get ('error' ):
@@ -498,6 +545,13 @@ def get_failure_redirect(error_msg: str) -> RedirectResponse:
498545 auth_result = authenticator .handle_callback (
499546 callback_data , expected_nonce = expected_nonce
500547 )
548+ logger .debug (
549+ '_handle_oauth_callback: provider auth_result success=%s error_code=%s '
550+ 'email=%s' ,
551+ auth_result .success ,
552+ auth_result .error_code ,
553+ auth_result .user_info .email if auth_result .user_info else None ,
554+ )
501555
502556 if not auth_result .success :
503557 if failure_url :
@@ -512,6 +566,12 @@ def get_failure_redirect(error_msg: str) -> RedirectResponse:
512566
513567 # Create session from auth result
514568 user = await user_repository .find_one (email = auth_result .user_info .email )
569+ logger .debug (
570+ '_handle_oauth_callback: user lookup by email=%s found=%s deleted=%s' ,
571+ auth_result .user_info .email ,
572+ user is not None ,
573+ getattr (user , 'deleted' , None ),
574+ )
515575 if user is None :
516576 if failure_url :
517577 params = urlencode (
@@ -552,6 +612,11 @@ def get_failure_redirect(error_msg: str) -> RedirectResponse:
552612 role_id = await user_service .get_user_role_for_scope (
553613 user_id = str (user .id ), scope = ResourceScope .CONSOLE
554614 )
615+ logger .debug (
616+ '_handle_oauth_callback: console role lookup user_id=%s role_id=%s' ,
617+ str (user .id ),
618+ role_id ,
619+ )
555620
556621 if not role_id :
557622 if failure_url :
@@ -572,12 +637,24 @@ def get_failure_redirect(error_msg: str) -> RedirectResponse:
572637
573638 # Success: redirect to success URL with access token
574639 if success_url :
640+ logger .debug (
641+ '_handle_oauth_callback: success redirect provider=%s user_id=%s '
642+ 'session_id=%s -> %s' ,
643+ provider ,
644+ str (user .id ),
645+ str (session .id ),
646+ success_url ,
647+ )
575648 params = urlencode ({'provider' : provider , 'access_token' : token })
576649 return RedirectResponse (url = f'{ success_url } ?{ params } ' )
577650
651+ logger .debug (
652+ '_handle_oauth_callback: no success_url configured, redirecting to about:blank'
653+ )
578654 return RedirectResponse (url = 'about:blank' )
579655
580656 except Exception as e :
657+ logger .debug ('_handle_oauth_callback raised: %s' , e )
581658 # Try to get config for failure URL
582659 try :
583660 auth_uuid = UUID (auth_id )
0 commit comments