Skip to content

Commit

Permalink
fix: add back get_user_info to AzureADOAuthProvider (Chainlit#1075)
Browse files Browse the repository at this point in the history
* fix: add back get_user_info to AzureADOAuthProvider

* prepare patch
  • Loading branch information
willydouhard authored Jun 14, 2024
1 parent c7cc319 commit 69668f2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

Nothing unreleased.

## [1.1.301] - 2024-06-14

### Fixed

- Azure AD oauth get_user_info not implemented error

## [1.1.300] - 2024-06-13

### Added
Expand Down
36 changes: 33 additions & 3 deletions backend/chainlit/oauth_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,36 @@ async def get_token(self, code: str, url: str):
)
return token

async def get_user_info(self, token: str):
async with httpx.AsyncClient() as client:
response = await client.get(
"https://graph.microsoft.com/v1.0/me",
headers={"Authorization": f"Bearer {token}"},
)
response.raise_for_status()

azure_user = response.json()

try:
photo_response = await client.get(
"https://graph.microsoft.com/v1.0/me/photos/48x48/$value",
headers={"Authorization": f"Bearer {token}"},
)
photo_data = await photo_response.aread()
base64_image = base64.b64encode(photo_data)
azure_user["image"] = (
f"data:{photo_response.headers['Content-Type']};base64,{base64_image.decode('utf-8')}"
)
except Exception as e:
# Ignore errors getting the photo
pass

user = User(
identifier=azure_user["userPrincipalName"],
metadata={"image": azure_user.get("image"), "provider": "azure-ad"},
)
return (azure_user, user)


class AzureADHybridOAuthProvider(OAuthProvider):
id = "azure-ad-hybrid"
Expand Down Expand Up @@ -258,9 +288,9 @@ async def get_user_info(self, token: str):
)
photo_data = await photo_response.aread()
base64_image = base64.b64encode(photo_data)
azure_user[
"image"
] = f"data:{photo_response.headers['Content-Type']};base64,{base64_image.decode('utf-8')}"
azure_user["image"] = (
f"data:{photo_response.headers['Content-Type']};base64,{base64_image.decode('utf-8')}"
)
except Exception as e:
# Ignore errors getting the photo
pass
Expand Down
2 changes: 1 addition & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "chainlit"
version = "1.1.300"
version = "1.1.301"
keywords = [
'LLM',
'Agents',
Expand Down

0 comments on commit 69668f2

Please sign in to comment.