@@ -82,17 +82,20 @@ async def get_auth_status_for_connector(
82
82
raise HTTPException (status_code = 500 , detail = "An internal server error occurred." )
83
83
84
84
85
- @router .get ("/{connector_type}/auth/initiate" ) # No specific response model, as it redirects
86
- async def initiate_connector_authentication (
87
- request : Request , # FastAPI Request object to access session
85
+ @router .get ("/{connector_type}/auth/initiate_url" , response_model = Dict [ str , str ])
86
+ async def get_initiate_auth_url (
87
+ request : Request ,
88
88
connector_type : str ,
89
- app_redirect_uri : Optional [str ] = None , # New parameter for frontend redirect
89
+ app_redirect_uri : Optional [str ] = None ,
90
90
service : ConnectorService = Depends (get_connector_service ),
91
91
):
92
+ """Return the provider's *authorization_url* for the given connector.
93
+
94
+ The method mirrors the logic of the `/auth/initiate` endpoint but sends a
95
+ JSON payload instead of a redirect so that browsers can stay on the same
96
+ origin until they intentionally navigate away.
92
97
"""
93
- Initiates the OAuth 2.0 authentication flow for the specified connector.
94
- Stores state in session and redirects user to the provider's authorization URL.
95
- """
98
+
96
99
try :
97
100
connector = await service .get_connector (connector_type )
98
101
auth_details = await connector .initiate_auth ()
@@ -102,42 +105,32 @@ async def initiate_connector_authentication(
102
105
103
106
if not authorization_url or not state :
104
107
logger .error (
105
- f"Connector '{ connector_type } ' did not return authorization URL or state "
106
- f"for user '{ service .user_identifier } '."
108
+ "Connector '%s' did not return authorization URL or state for user '%s'." ,
109
+ connector_type ,
110
+ service .user_identifier ,
107
111
)
108
112
raise HTTPException (status_code = 500 , detail = "Failed to initiate authentication with the provider." )
109
113
110
- # Store state and connector type in session for later validation in the callback
114
+ # Store state and connector type in session for later validation.
111
115
request .session ["oauth_state" ] = state
112
116
request .session ["connector_type_for_callback" ] = connector_type
113
117
if app_redirect_uri :
114
118
request .session ["app_redirect_uri" ] = app_redirect_uri
115
- logger .info (f"Stored app_redirect_uri in session: { app_redirect_uri } " )
116
119
117
- logger .info (
118
- f"Initiating auth for '{ connector_type } ' for user '{ service .user_identifier } '. "
119
- f"Redirecting to: { authorization_url [:70 ]} ..."
120
- )
121
- return RedirectResponse (url = authorization_url )
120
+ logger .info ("Prepared auth URL for '%s' for user '%s'." , connector_type , service .user_identifier )
121
+
122
+ return {"authorization_url" : authorization_url }
122
123
123
- except ValueError as ve : # Raised by get_connector for unsupported type or by connector for config issues
124
- logger .warning (f"Auth initiation for '{ connector_type } ' failed: { ve } for user '{ service .user_identifier } '" )
125
- # Determine if it's a 404 (unsupported) or 500 (config error within connector)
124
+ except ValueError as ve :
125
+ logger .warning ("Auth URL preparation for '%s' failed: %s" , connector_type , ve )
126
126
if "Unsupported connector type" in str (ve ):
127
127
raise HTTPException (status_code = 404 , detail = str (ve ))
128
- else :
129
- # E.g. Google Client ID/Secret not configured in GoogleDriveConnector
130
- raise HTTPException (
131
- status_code = 500 , detail = f"Configuration error for connector '{ connector_type } ': { str (ve )} "
132
- )
128
+ raise HTTPException (status_code = 500 , detail = str (ve ))
133
129
except NotImplementedError :
134
- logger .error (f"Connector '{ connector_type } ' is not fully implemented for auth initiation." )
135
130
raise HTTPException (status_code = 501 , detail = f"Connector '{ connector_type } ' not fully implemented." )
136
- except Exception as e :
137
- logger .exception (
138
- f"Error initiating auth for connector '{ connector_type } ' for user '{ service .user_identifier } ': { e } "
139
- )
140
- raise HTTPException (status_code = 500 , detail = "Internal server error initiating authentication." )
131
+ except Exception as exc :
132
+ logger .exception ("Error preparing auth URL for '%s': %s" , connector_type , exc )
133
+ raise HTTPException (status_code = 500 , detail = "Internal server error preparing authentication URL." )
141
134
142
135
143
136
@router .get ("/{connector_type}/oauth2callback" )
0 commit comments