From ba93c76ce4eaeedb64773f0caf0087e9c0564707 Mon Sep 17 00:00:00 2001 From: Thibaut Patel Date: Mon, 16 Oct 2023 18:47:09 +0200 Subject: [PATCH] fix: add missing profile picture to azure oauth users (#481) - 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 --- backend/chainlit/oauth_providers.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/backend/chainlit/oauth_providers.py b/backend/chainlit/oauth_providers.py index e41890d6f3..2fce8083c6 100644 --- a/backend/chainlit/oauth_providers.py +++ b/backend/chainlit/oauth_providers.py @@ -1,3 +1,4 @@ +import base64 import os import urllib.parse from typing import Dict, List, Optional, Tuple @@ -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)