@@ -1037,8 +1037,45 @@ def oidc_callback():
10371037 client_kwargs = client_kwargs
10381038 )
10391039
1040- # Get the token
1041- token = oidc_client .authorize_access_token ()
1040+ # Exchange authorization code for tokens manually to avoid JWT validation issues
1041+ try :
1042+ import requests
1043+
1044+ # Get authorization code from callback
1045+ code = request .args .get ('code' )
1046+ if not code :
1047+ flash ('No authorization code received.' , 'error' )
1048+ return redirect (url_for ('main.login' ))
1049+
1050+ # Exchange code for token
1051+ token_response = requests .post (
1052+ oidc_config .token_endpoint ,
1053+ data = {
1054+ 'grant_type' : 'authorization_code' ,
1055+ 'code' : code ,
1056+ 'redirect_uri' : url_for ('main.oidc_callback' , _external = True ),
1057+ 'client_id' : oidc_config .client_id ,
1058+ 'client_secret' : oidc_config .client_secret
1059+ },
1060+ timeout = 10
1061+ )
1062+
1063+ if token_response .status_code != 200 :
1064+ current_app .logger .error (f"Token endpoint returned { token_response .status_code } : { token_response .text } " )
1065+ flash ('Failed to obtain access token from identity provider.' , 'error' )
1066+ return redirect (url_for ('main.login' ))
1067+
1068+ token = token_response .json ()
1069+ access_token = token .get ('access_token' )
1070+
1071+ if not access_token :
1072+ flash ('No access token received from identity provider.' , 'error' )
1073+ return redirect (url_for ('main.login' ))
1074+
1075+ except Exception as token_error :
1076+ current_app .logger .error (f"Token exchange failed: { token_error } " )
1077+ flash (f'An error occurred during login: { str (token_error )} ' , 'error' )
1078+ return redirect (url_for ('main.login' ))
10421079
10431080 # Parse the ID token to get user info - skip JWT validation for manual endpoints
10441081 user_info = token .get ('userinfo' )
@@ -1066,8 +1103,6 @@ def oidc_callback():
10661103 # If still no user_info, try fetching from userinfo endpoint
10671104 if not user_info :
10681105 try :
1069- import requests
1070- access_token = token .get ('access_token' )
10711106 if access_token and oidc_config .userinfo_endpoint :
10721107 response = requests .get (
10731108 oidc_config .userinfo_endpoint ,
0 commit comments