@@ -55,7 +55,9 @@ def prepare_github_login_redirect(self, request: Request) -> RedirectResponse:
5555
5656 # For production, use explicit environment variable
5757 # For development, construct from request
58- if config_settings .ENV_TYPE == "production" and config_settings .API_URL and "localhost" not in config_settings .API_URL :
58+ is_production = config_settings .ENV_TYPE == "production"
59+
60+ if is_production and config_settings .API_URL and "localhost" not in config_settings .API_URL :
5961 redirect_uri = f"{ config_settings .API_URL .rstrip ('/' )} /api/auth/github/callback"
6062 logger .info (f"GitHub OAuth (production): Using explicit API_URL: { redirect_uri } " )
6163 else :
@@ -64,11 +66,16 @@ def prepare_github_login_redirect(self, request: Request) -> RedirectResponse:
6466 if not redirect_uri .startswith ("http" ):
6567 base_url = str (request .base_url ).rstrip ('/' )
6668 redirect_uri = f"{ base_url } /api/auth/github/callback"
67- logger .info (f"GitHub OAuth (dev) : Using request.url_for: { redirect_uri } " )
69+ logger .info (f"GitHub OAuth: Using request.url_for: { redirect_uri } " )
6870 except Exception as e :
6971 logger .error (f"Error constructing redirect_uri: { e } " )
7072 raise OAuthException (detail = "Error preparing authentication request to GitHub." )
7173
74+ # CRITICAL: Ensure HTTPS in production (belt-and-suspenders with proxy_headers)
75+ if is_production and redirect_uri .startswith ("http://" ):
76+ redirect_uri = redirect_uri .replace ("http://" , "https://" , 1 )
77+ logger .info (f"GitHub OAuth: Forced HTTPS in production: { redirect_uri } " )
78+
7279 # Diagnostic logging - VERY detailed for debugging
7380 logger .info (f"[GITHUB_OAUTH_DEBUG] redirect_uri = '{ redirect_uri } '" )
7481 logger .info (f"[GITHUB_OAUTH_DEBUG] redirect_uri length = { len (redirect_uri )} " )
@@ -146,7 +153,9 @@ async def handle_github_callback(
146153 return RedirectResponse (url = f"{ frontend_url } /?login_error=github_no_code" , status_code = 307 )
147154
148155 # Match the same construction as prepare_github_login_redirect
149- if config_settings .ENV_TYPE == "production" and config_settings .API_URL and "localhost" not in config_settings .API_URL :
156+ is_production = config_settings .ENV_TYPE == "production"
157+
158+ if is_production and config_settings .API_URL and "localhost" not in config_settings .API_URL :
150159 actual_redirect_uri = f"{ config_settings .API_URL .rstrip ('/' )} /api/auth/github/callback"
151160 else :
152161 try :
@@ -158,6 +167,11 @@ async def handle_github_callback(
158167 base_url = str (request .base_url ).rstrip ('/' )
159168 actual_redirect_uri = f"{ base_url } /api/auth/github/callback"
160169
170+ # CRITICAL: Ensure HTTPS in production (belt-and-suspenders with proxy_headers)
171+ if is_production and actual_redirect_uri .startswith ("http://" ):
172+ actual_redirect_uri = actual_redirect_uri .replace ("http://" , "https://" , 1 )
173+ logger .info (f"GitHub OAuth callback: Forced HTTPS in production: { actual_redirect_uri } " )
174+
161175 # Diagnostic logging - VERY detailed for debugging callback
162176 logger .info (f"[GITHUB_CALLBACK_DEBUG] actual_redirect_uri = '{ actual_redirect_uri } '" )
163177 logger .info (f"[GITHUB_CALLBACK_DEBUG] actual_redirect_uri length = { len (actual_redirect_uri )} " )
0 commit comments