Skip to content

Commit 9e63b92

Browse files
committed
Add method for building organization authorization url with organization discovery
1 parent 358f188 commit 9e63b92

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

packages/asgardeo-ai/src/asgardeo_ai/agent_auth_manager.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,126 @@ def get_authorization_url_with_pkce(
228228
)
229229
return auth_url, state, code_verifier
230230

231+
def get_org_authorization_url(
232+
self,
233+
scopes: List[str],
234+
org_discovery_type: str,
235+
value: str,
236+
state: Optional[str] = None,
237+
resource: Optional[str] = None,
238+
**kwargs: Any,
239+
) -> Tuple[str, str]:
240+
"""Generate authorization URL for organization-specific user authentication.
241+
242+
:param scopes: List of OAuth scopes to request
243+
:param org_discovery_type: The type of organization discovery ('orgID', 'orgHandle', 'org', 'emailDomain')
244+
:param value: The value for the discovery type
245+
:param state: Optional state parameter (generated if not provided)
246+
:param resource: Optional resource parameter
247+
:param kwargs: Additional parameters for the authorization URL
248+
:return: Tuple of (authorization_url, state)
249+
"""
250+
if not state:
251+
state = generate_state()
252+
253+
auth_params = {
254+
"client_id": self.config.client_id,
255+
"redirect_uri": self.config.redirect_uri,
256+
"scope": " ".join(scopes),
257+
"state": state,
258+
"response_type": "code",
259+
"fidp": "OrganizationSSO",
260+
}
261+
262+
# Switch case to handle each discovery type
263+
if org_discovery_type == "orgID":
264+
auth_params["orgId"] = value
265+
elif org_discovery_type == "orgHandle":
266+
auth_params["orgHandle"] = value
267+
elif org_discovery_type == "org":
268+
auth_params["org"] = value
269+
elif org_discovery_type == "emailDomain":
270+
auth_params["login_hint"] = value
271+
auth_params["orgDiscoveryType"] = "emailDomain"
272+
else:
273+
raise ValueError(f"Unsupported org_discovery_type: {org_discovery_type}")
274+
275+
if resource:
276+
auth_params["resource"] = resource
277+
278+
if self.agent_config:
279+
auth_params["requested_actor"] = self.agent_config.agent_id
280+
281+
auth_params.update(kwargs)
282+
283+
auth_url = build_authorization_url(
284+
f"{self.config.base_url}/oauth2/authorize",
285+
auth_params
286+
)
287+
return auth_url, state
288+
289+
def get_org_authorization_url_with_pkce(
290+
self,
291+
scopes: List[str],
292+
org_discovery_type: str,
293+
value: str,
294+
state: Optional[str] = None,
295+
resource: Optional[str] = None,
296+
**kwargs: Any,
297+
) -> Tuple[str, str, str]:
298+
"""Generate authorization URL for organization-specific user authentication with PKCE.
299+
300+
:param scopes: List of OAuth scopes to request
301+
:param org_discovery_type: The type of organization discovery ('orgID', 'orgHandle', 'org', 'emailDomain')
302+
:param value: The value for the discovery type
303+
:param state: Optional state parameter (generated if not provided)
304+
:param resource: Optional resource parameter
305+
:param kwargs: Additional parameters for the authorization URL
306+
:return: Tuple of (authorization_url, state, code_verifier)
307+
"""
308+
if not state:
309+
state = generate_state()
310+
311+
code_verifier, code_challenge = generate_pkce_pair()
312+
313+
auth_params = {
314+
"client_id": self.config.client_id,
315+
"redirect_uri": self.config.redirect_uri,
316+
"scope": " ".join(scopes),
317+
"state": state,
318+
"response_type": "code",
319+
"code_challenge": code_challenge,
320+
"code_challenge_method": "S256",
321+
"fidp": "OrganizationSSO",
322+
}
323+
324+
# Switch case to handle each discovery type
325+
if org_discovery_type == "orgID":
326+
auth_params["orgId"] = value
327+
elif org_discovery_type == "orgHandle":
328+
auth_params["orgHandle"] = value
329+
elif org_discovery_type == "org":
330+
auth_params["org"] = value
331+
elif org_discovery_type == "emailDomain":
332+
auth_params["login_hint"] = value
333+
auth_params["orgDiscoveryType"] = "emailDomain"
334+
else:
335+
raise ValueError(f"Unsupported org_discovery_type: {org_discovery_type}")
336+
337+
if resource:
338+
auth_params["resource"] = resource
339+
340+
if self.agent_config:
341+
auth_params["requested_actor"] = self.agent_config.agent_id
342+
343+
auth_params.update(kwargs)
344+
345+
auth_url = build_authorization_url(
346+
f"{self.config.base_url}/oauth2/authorize",
347+
auth_params
348+
)
349+
return auth_url, state, code_verifier
350+
231351
async def get_obo_token(
232352
self,
233353
auth_code: str,

0 commit comments

Comments
 (0)