Skip to content

Commit

Permalink
fix: add missing profile picture to azure oauth users (Chainlit#481)
Browse files Browse the repository at this point in the history
- Microsoft api doesn't return a url, so we need to base64-encode the returned png
- use the 48px image to minimize the base64 string
  • Loading branch information
tpatel authored Oct 16, 2023
1 parent d80e3d3 commit ba93c76
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion backend/chainlit/oauth_providers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import os
import urllib.parse
from typing import Dict, List, Optional, Tuple
Expand Down Expand Up @@ -200,8 +201,24 @@ async def get_user_info(self, token: str):
) as result:
user = await result.json()

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

app_user = AppUser(
username=user["userPrincipalName"], image="", provider="azure-ad"
username=user["userPrincipalName"],
image=user.get("image", ""),
provider="azure-ad",
)
return (user, app_user)

Expand Down

0 comments on commit ba93c76

Please sign in to comment.