[Bug Fix MSFT SSO] Use correct field for user email when using MSFT SSO (#9886)

* fix openid_from_response

* test_microsoft_sso_handler_openid_from_response_user_principal_name

* test upsert_sso_user
This commit is contained in:
Ishaan Jaff 2025-04-10 17:40:58 -07:00 committed by GitHub
parent 94a553dbb2
commit 72a12e91c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 83 additions and 23 deletions

View file

@ -468,9 +468,6 @@ async def auth_callback(request: Request): # noqa: PLR0915
result=result,
user_info=user_info,
user_email=user_email,
user_id_models=user_id_models,
max_internal_user_budget=max_internal_user_budget,
internal_user_budget_duration=internal_user_budget_duration,
user_defined_values=user_defined_values,
prisma_client=prisma_client,
)
@ -831,37 +828,20 @@ class SSOAuthenticationHandler:
result: Optional[Union[CustomOpenID, OpenID, dict]],
user_info: Optional[Union[NewUserResponse, LiteLLM_UserTable]],
user_email: Optional[str],
user_id_models: List[str],
max_internal_user_budget: Optional[float],
internal_user_budget_duration: Optional[str],
user_defined_values: Optional[SSOUserDefinedValues],
prisma_client: PrismaClient,
):
"""
Connects the SSO Users to the User Table in LiteLLM DB
- If user on LiteLLM DB, update the user_id with the SSO user_id
- If user on LiteLLM DB, update the user_email with the SSO user_email
- If user not on LiteLLM DB, insert the user into LiteLLM DB
"""
try:
if user_info is not None:
user_id = user_info.user_id
user_defined_values = SSOUserDefinedValues(
models=getattr(user_info, "models", user_id_models),
user_id=user_info.user_id or "",
user_email=getattr(user_info, "user_email", user_email),
user_role=getattr(user_info, "user_role", None),
max_budget=getattr(
user_info, "max_budget", max_internal_user_budget
),
budget_duration=getattr(
user_info, "budget_duration", internal_user_budget_duration
),
)
# update id
await prisma_client.db.litellm_usertable.update_many(
where={"user_email": user_email}, data={"user_id": user_id} # type: ignore
where={"user_id": user_id}, data={"user_email": user_email}
)
else:
verbose_proxy_logger.info(
@ -1045,7 +1025,7 @@ class MicrosoftSSOHandler:
response = response or {}
verbose_proxy_logger.debug(f"Microsoft SSO Callback Response: {response}")
openid_response = CustomOpenID(
email=response.get("mail"),
email=response.get("userPrincipalName") or response.get("mail"),
display_name=response.get("displayName"),
provider="microsoft",
id=response.get("id"),