| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  | import base64 | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  | import logging | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  | import mimetypes | 
					
						
							|  |  |  | import uuid | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import aiohttp | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  | from authlib.integrations.starlette_client import OAuth | 
					
						
							|  |  |  | from authlib.oidc.core import UserInfo | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  | from fastapi import ( | 
					
						
							|  |  |  |     HTTPException, | 
					
						
							|  |  |  |     status, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  | from starlette.responses import RedirectResponse | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | from open_webui.models.auths import Auths | 
					
						
							|  |  |  | from open_webui.models.users import Users | 
					
						
							| 
									
										
										
										
											2024-12-18 03:38:07 +08:00
										 |  |  | from open_webui.models.groups import Groups, GroupModel, GroupUpdateForm | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  | from open_webui.config import ( | 
					
						
							|  |  |  |     DEFAULT_USER_ROLE, | 
					
						
							|  |  |  |     ENABLE_OAUTH_SIGNUP, | 
					
						
							|  |  |  |     OAUTH_MERGE_ACCOUNTS_BY_EMAIL, | 
					
						
							|  |  |  |     OAUTH_PROVIDERS, | 
					
						
							|  |  |  |     ENABLE_OAUTH_ROLE_MANAGEMENT, | 
					
						
							| 
									
										
										
										
											2024-12-18 03:38:07 +08:00
										 |  |  |     ENABLE_OAUTH_GROUP_MANAGEMENT, | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  |     OAUTH_ROLES_CLAIM, | 
					
						
							| 
									
										
										
										
											2024-12-18 03:38:07 +08:00
										 |  |  |     OAUTH_GROUPS_CLAIM, | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  |     OAUTH_EMAIL_CLAIM, | 
					
						
							|  |  |  |     OAUTH_PICTURE_CLAIM, | 
					
						
							|  |  |  |     OAUTH_USERNAME_CLAIM, | 
					
						
							|  |  |  |     OAUTH_ALLOWED_ROLES, | 
					
						
							| 
									
										
										
										
											2024-10-21 09:38:06 +08:00
										 |  |  |     OAUTH_ADMIN_ROLES, | 
					
						
							| 
									
										
										
										
											2024-12-02 16:36:56 +08:00
										 |  |  |     OAUTH_ALLOWED_DOMAINS, | 
					
						
							| 
									
										
										
										
											2024-10-21 09:38:06 +08:00
										 |  |  |     WEBHOOK_URL, | 
					
						
							|  |  |  |     JWT_EXPIRES_IN, | 
					
						
							|  |  |  |     AppConfig, | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2025-01-08 16:38:00 +08:00
										 |  |  | from open_webui.constants import ERROR_MESSAGES, WEBHOOK_MESSAGES | 
					
						
							| 
									
										
										
										
											2024-10-17 02:30:35 +08:00
										 |  |  | from open_webui.env import WEBUI_SESSION_COOKIE_SAME_SITE, WEBUI_SESSION_COOKIE_SECURE | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  | from open_webui.utils.misc import parse_duration | 
					
						
							| 
									
										
										
										
											2024-12-09 08:01:56 +08:00
										 |  |  | from open_webui.utils.auth import get_password_hash, create_token | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  | from open_webui.utils.webhook import post_webhook | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | log = logging.getLogger(__name__) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  | auth_manager_config = AppConfig() | 
					
						
							|  |  |  | auth_manager_config.DEFAULT_USER_ROLE = DEFAULT_USER_ROLE | 
					
						
							|  |  |  | auth_manager_config.ENABLE_OAUTH_SIGNUP = ENABLE_OAUTH_SIGNUP | 
					
						
							|  |  |  | auth_manager_config.OAUTH_MERGE_ACCOUNTS_BY_EMAIL = OAUTH_MERGE_ACCOUNTS_BY_EMAIL | 
					
						
							|  |  |  | auth_manager_config.ENABLE_OAUTH_ROLE_MANAGEMENT = ENABLE_OAUTH_ROLE_MANAGEMENT | 
					
						
							| 
									
										
										
										
											2024-12-18 03:38:07 +08:00
										 |  |  | auth_manager_config.ENABLE_OAUTH_GROUP_MANAGEMENT = ENABLE_OAUTH_GROUP_MANAGEMENT | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  | auth_manager_config.OAUTH_ROLES_CLAIM = OAUTH_ROLES_CLAIM | 
					
						
							| 
									
										
										
										
											2024-12-18 03:38:07 +08:00
										 |  |  | auth_manager_config.OAUTH_GROUPS_CLAIM = OAUTH_GROUPS_CLAIM | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  | auth_manager_config.OAUTH_EMAIL_CLAIM = OAUTH_EMAIL_CLAIM | 
					
						
							|  |  |  | auth_manager_config.OAUTH_PICTURE_CLAIM = OAUTH_PICTURE_CLAIM | 
					
						
							|  |  |  | auth_manager_config.OAUTH_USERNAME_CLAIM = OAUTH_USERNAME_CLAIM | 
					
						
							|  |  |  | auth_manager_config.OAUTH_ALLOWED_ROLES = OAUTH_ALLOWED_ROLES | 
					
						
							|  |  |  | auth_manager_config.OAUTH_ADMIN_ROLES = OAUTH_ADMIN_ROLES | 
					
						
							| 
									
										
										
										
											2024-12-02 16:36:56 +08:00
										 |  |  | auth_manager_config.OAUTH_ALLOWED_DOMAINS = OAUTH_ALLOWED_DOMAINS | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  | auth_manager_config.WEBHOOK_URL = WEBHOOK_URL | 
					
						
							|  |  |  | auth_manager_config.JWT_EXPIRES_IN = JWT_EXPIRES_IN | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class OAuthManager: | 
					
						
							|  |  |  |     def __init__(self): | 
					
						
							|  |  |  |         self.oauth = OAuth() | 
					
						
							| 
									
										
										
										
											2025-01-17 12:56:03 +08:00
										 |  |  |         for _, provider_config in OAUTH_PROVIDERS.items(): | 
					
						
							|  |  |  |             provider_config["register"](self.oauth) | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |     def get_client(self, provider_name): | 
					
						
							|  |  |  |         return self.oauth.create_client(provider_name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get_user_role(self, user, user_data): | 
					
						
							|  |  |  |         if user and Users.get_num_users() == 1: | 
					
						
							|  |  |  |             # If the user is the only user, assign the role "admin" - actually repairs role for single user on login | 
					
						
							|  |  |  |             return "admin" | 
					
						
							|  |  |  |         if not user and Users.get_num_users() == 0: | 
					
						
							|  |  |  |             # If there are no users, assign the role "admin", as the first user will be an admin | 
					
						
							|  |  |  |             return "admin" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if auth_manager_config.ENABLE_OAUTH_ROLE_MANAGEMENT: | 
					
						
							|  |  |  |             oauth_claim = auth_manager_config.OAUTH_ROLES_CLAIM | 
					
						
							|  |  |  |             oauth_allowed_roles = auth_manager_config.OAUTH_ALLOWED_ROLES | 
					
						
							|  |  |  |             oauth_admin_roles = auth_manager_config.OAUTH_ADMIN_ROLES | 
					
						
							|  |  |  |             oauth_roles = None | 
					
						
							|  |  |  |             role = "pending"  # Default/fallback role if no matching roles are found | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # Next block extracts the roles from the user data, accepting nested claims of any depth | 
					
						
							|  |  |  |             if oauth_claim and oauth_allowed_roles and oauth_admin_roles: | 
					
						
							|  |  |  |                 claim_data = user_data | 
					
						
							|  |  |  |                 nested_claims = oauth_claim.split(".") | 
					
						
							|  |  |  |                 for nested_claim in nested_claims: | 
					
						
							|  |  |  |                     claim_data = claim_data.get(nested_claim, {}) | 
					
						
							|  |  |  |                 oauth_roles = claim_data if isinstance(claim_data, list) else None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # If any roles are found, check if they match the allowed or admin roles | 
					
						
							|  |  |  |             if oauth_roles: | 
					
						
							|  |  |  |                 # If role management is enabled, and matching roles are provided, use the roles | 
					
						
							|  |  |  |                 for allowed_role in oauth_allowed_roles: | 
					
						
							|  |  |  |                     # If the user has any of the allowed roles, assign the role "user" | 
					
						
							|  |  |  |                     if allowed_role in oauth_roles: | 
					
						
							|  |  |  |                         role = "user" | 
					
						
							|  |  |  |                         break | 
					
						
							|  |  |  |                 for admin_role in oauth_admin_roles: | 
					
						
							|  |  |  |                     # If the user has any of the admin roles, assign the role "admin" | 
					
						
							|  |  |  |                     if admin_role in oauth_roles: | 
					
						
							|  |  |  |                         role = "admin" | 
					
						
							|  |  |  |                         break | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |             if not user: | 
					
						
							|  |  |  |                 # If role management is disabled, use the default role for new users | 
					
						
							|  |  |  |                 role = auth_manager_config.DEFAULT_USER_ROLE | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 # If role management is disabled, use the existing role for existing users | 
					
						
							|  |  |  |                 role = user.role | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return role | 
					
						
							| 
									
										
										
										
											2024-12-18 05:51:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-18 03:38:07 +08:00
										 |  |  |     def update_user_groups(self, user, user_data, default_permissions): | 
					
						
							|  |  |  |         oauth_claim = auth_manager_config.OAUTH_GROUPS_CLAIM | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         user_oauth_groups: list[str] = user_data.get(oauth_claim, list()) | 
					
						
							|  |  |  |         user_current_groups: list[GroupModel] = Groups.get_groups_by_member_id(user.id) | 
					
						
							|  |  |  |         all_available_groups: list[GroupModel] = Groups.get_groups() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Remove groups that user is no longer a part of | 
					
						
							|  |  |  |         for group_model in user_current_groups: | 
					
						
							|  |  |  |             if group_model.name not in user_oauth_groups: | 
					
						
							|  |  |  |                 # Remove group from user | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 user_ids = group_model.user_ids | 
					
						
							|  |  |  |                 user_ids = [i for i in user_ids if i != user.id] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 # In case a group is created, but perms are never assigned to the group by hitting "save" | 
					
						
							|  |  |  |                 group_permissions = group_model.permissions | 
					
						
							|  |  |  |                 if not group_permissions: | 
					
						
							|  |  |  |                     group_permissions = default_permissions | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-18 05:51:29 +08:00
										 |  |  |                 update_form = GroupUpdateForm( | 
					
						
							|  |  |  |                     name=group_model.name, | 
					
						
							|  |  |  |                     description=group_model.description, | 
					
						
							|  |  |  |                     permissions=group_permissions, | 
					
						
							|  |  |  |                     user_ids=user_ids, | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 Groups.update_group_by_id( | 
					
						
							|  |  |  |                     id=group_model.id, form_data=update_form, overwrite=False | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2024-12-18 03:38:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Add user to new groups | 
					
						
							|  |  |  |         for group_model in all_available_groups: | 
					
						
							| 
									
										
										
										
											2024-12-18 05:51:29 +08:00
										 |  |  |             if group_model.name in user_oauth_groups and not any( | 
					
						
							|  |  |  |                 gm.name == group_model.name for gm in user_current_groups | 
					
						
							|  |  |  |             ): | 
					
						
							| 
									
										
										
										
											2024-12-18 03:38:07 +08:00
										 |  |  |                 # Add user to group | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 user_ids = group_model.user_ids | 
					
						
							|  |  |  |                 user_ids.append(user.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 # In case a group is created, but perms are never assigned to the group by hitting "save" | 
					
						
							|  |  |  |                 group_permissions = group_model.permissions | 
					
						
							|  |  |  |                 if not group_permissions: | 
					
						
							|  |  |  |                     group_permissions = default_permissions | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-18 05:51:29 +08:00
										 |  |  |                 update_form = GroupUpdateForm( | 
					
						
							|  |  |  |                     name=group_model.name, | 
					
						
							|  |  |  |                     description=group_model.description, | 
					
						
							|  |  |  |                     permissions=group_permissions, | 
					
						
							|  |  |  |                     user_ids=user_ids, | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 Groups.update_group_by_id( | 
					
						
							|  |  |  |                     id=group_model.id, form_data=update_form, overwrite=False | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     async def handle_login(self, provider, request): | 
					
						
							|  |  |  |         if provider not in OAUTH_PROVIDERS: | 
					
						
							|  |  |  |             raise HTTPException(404) | 
					
						
							|  |  |  |         # If the provider has a custom redirect URL, use that, otherwise automatically generate one | 
					
						
							|  |  |  |         redirect_uri = OAUTH_PROVIDERS[provider].get("redirect_uri") or request.url_for( | 
					
						
							|  |  |  |             "oauth_callback", provider=provider | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         client = self.get_client(provider) | 
					
						
							|  |  |  |         if client is None: | 
					
						
							|  |  |  |             raise HTTPException(404) | 
					
						
							|  |  |  |         return await client.authorize_redirect(request, redirect_uri) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async def handle_callback(self, provider, request, response): | 
					
						
							|  |  |  |         if provider not in OAUTH_PROVIDERS: | 
					
						
							|  |  |  |             raise HTTPException(404) | 
					
						
							|  |  |  |         client = self.get_client(provider) | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             token = await client.authorize_access_token(request) | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							|  |  |  |             log.warning(f"OAuth callback error: {e}") | 
					
						
							|  |  |  |             raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED) | 
					
						
							| 
									
										
										
										
											2025-01-17 00:33:20 +08:00
										 |  |  |         user_data: UserInfo = token.get("userinfo") | 
					
						
							| 
									
										
										
										
											2024-10-23 03:55:12 +08:00
										 |  |  |         if not user_data: | 
					
						
							|  |  |  |             user_data: UserInfo = await client.userinfo(token=token) | 
					
						
							|  |  |  |         if not user_data: | 
					
						
							|  |  |  |             log.warning(f"OAuth callback failed, user data is missing: {token}") | 
					
						
							|  |  |  |             raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED) | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-17 12:56:03 +08:00
										 |  |  |         sub = user_data.get(OAUTH_PROVIDERS[provider].get("sub_claim", "sub")) | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |         if not sub: | 
					
						
							|  |  |  |             log.warning(f"OAuth callback failed, sub is missing: {user_data}") | 
					
						
							|  |  |  |             raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED) | 
					
						
							|  |  |  |         provider_sub = f"{provider}@{sub}" | 
					
						
							|  |  |  |         email_claim = auth_manager_config.OAUTH_EMAIL_CLAIM | 
					
						
							|  |  |  |         email = user_data.get(email_claim, "").lower() | 
					
						
							|  |  |  |         # We currently mandate that email addresses are provided | 
					
						
							|  |  |  |         if not email: | 
					
						
							|  |  |  |             log.warning(f"OAuth callback failed, email is missing: {user_data}") | 
					
						
							|  |  |  |             raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED) | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         if ( | 
					
						
							|  |  |  |             "*" not in auth_manager_config.OAUTH_ALLOWED_DOMAINS | 
					
						
							|  |  |  |             and email.split("@")[-1] not in auth_manager_config.OAUTH_ALLOWED_DOMAINS | 
					
						
							|  |  |  |         ): | 
					
						
							|  |  |  |             log.warning( | 
					
						
							|  |  |  |                 f"OAuth callback failed, e-mail domain is not in the list of allowed domains: {user_data}" | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-12-02 16:36:56 +08:00
										 |  |  |             raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED) | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Check if the user exists | 
					
						
							|  |  |  |         user = Users.get_user_by_oauth_sub(provider_sub) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not user: | 
					
						
							|  |  |  |             # If the user does not exist, check if merging is enabled | 
					
						
							| 
									
										
										
										
											2024-10-25 15:44:03 +08:00
										 |  |  |             if auth_manager_config.OAUTH_MERGE_ACCOUNTS_BY_EMAIL: | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |                 # Check if the user exists by email | 
					
						
							|  |  |  |                 user = Users.get_user_by_email(email) | 
					
						
							|  |  |  |                 if user: | 
					
						
							|  |  |  |                     # Update the user with the new oauth sub | 
					
						
							|  |  |  |                     Users.update_user_oauth_sub_by_id(user.id, provider_sub) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if user: | 
					
						
							|  |  |  |             determined_role = self.get_user_role(user, user_data) | 
					
						
							|  |  |  |             if user.role != determined_role: | 
					
						
							|  |  |  |                 Users.update_user_role_by_id(user.id, determined_role) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not user: | 
					
						
							|  |  |  |             # If the user does not exist, check if signups are enabled | 
					
						
							| 
									
										
										
										
											2024-10-25 15:44:03 +08:00
										 |  |  |             if auth_manager_config.ENABLE_OAUTH_SIGNUP: | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |                 # Check if an existing user with the same email already exists | 
					
						
							| 
									
										
										
										
											2024-10-21 09:38:06 +08:00
										 |  |  |                 existing_user = Users.get_user_by_email( | 
					
						
							|  |  |  |                     user_data.get("email", "").lower() | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |                 if existing_user: | 
					
						
							|  |  |  |                     raise HTTPException(400, detail=ERROR_MESSAGES.EMAIL_TAKEN) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 picture_claim = auth_manager_config.OAUTH_PICTURE_CLAIM | 
					
						
							| 
									
										
										
										
											2025-01-15 09:17:22 +08:00
										 |  |  |                 picture_url = user_data.get( | 
					
						
							|  |  |  |                     picture_claim, OAUTH_PROVIDERS[provider].get("picture_url", "") | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |                 if picture_url: | 
					
						
							|  |  |  |                     # Download the profile image into a base64 string | 
					
						
							|  |  |  |                     try: | 
					
						
							| 
									
										
										
										
											2025-01-16 19:32:35 +08:00
										 |  |  |                         access_token = token.get("access_token") | 
					
						
							|  |  |  |                         get_kwargs = {} | 
					
						
							|  |  |  |                         if access_token: | 
					
						
							|  |  |  |                             get_kwargs["headers"] = { | 
					
						
							|  |  |  |                                 "Authorization": f"Bearer {access_token}", | 
					
						
							|  |  |  |                             } | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |                         async with aiohttp.ClientSession() as session: | 
					
						
							| 
									
										
										
										
											2025-01-16 19:32:35 +08:00
										 |  |  |                             async with session.get(picture_url, **get_kwargs) as resp: | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |                                 picture = await resp.read() | 
					
						
							| 
									
										
										
										
											2024-10-21 09:38:06 +08:00
										 |  |  |                                 base64_encoded_picture = base64.b64encode( | 
					
						
							|  |  |  |                                     picture | 
					
						
							|  |  |  |                                 ).decode("utf-8") | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |                                 guessed_mime_type = mimetypes.guess_type(picture_url)[0] | 
					
						
							|  |  |  |                                 if guessed_mime_type is None: | 
					
						
							|  |  |  |                                     # assume JPG, browsers are tolerant enough of image formats | 
					
						
							|  |  |  |                                     guessed_mime_type = "image/jpeg" | 
					
						
							|  |  |  |                                 picture_url = f"data:{guessed_mime_type};base64,{base64_encoded_picture}" | 
					
						
							|  |  |  |                     except Exception as e: | 
					
						
							| 
									
										
										
										
											2024-10-21 09:38:06 +08:00
										 |  |  |                         log.error( | 
					
						
							|  |  |  |                             f"Error downloading profile image '{picture_url}': {e}" | 
					
						
							|  |  |  |                         ) | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |                         picture_url = "" | 
					
						
							|  |  |  |                 if not picture_url: | 
					
						
							|  |  |  |                     picture_url = "/user.png" | 
					
						
							|  |  |  |                 username_claim = auth_manager_config.OAUTH_USERNAME_CLAIM | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 role = self.get_user_role(None, user_data) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 user = Auths.insert_new_auth( | 
					
						
							|  |  |  |                     email=email, | 
					
						
							|  |  |  |                     password=get_password_hash( | 
					
						
							|  |  |  |                         str(uuid.uuid4()) | 
					
						
							|  |  |  |                     ),  # Random password, not used | 
					
						
							|  |  |  |                     name=user_data.get(username_claim, "User"), | 
					
						
							|  |  |  |                     profile_image_url=picture_url, | 
					
						
							|  |  |  |                     role=role, | 
					
						
							|  |  |  |                     oauth_sub=provider_sub, | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if auth_manager_config.WEBHOOK_URL: | 
					
						
							|  |  |  |                     post_webhook( | 
					
						
							|  |  |  |                         auth_manager_config.WEBHOOK_URL, | 
					
						
							| 
									
										
										
										
											2025-01-08 16:57:52 +08:00
										 |  |  |                         WEBHOOK_MESSAGES.USER_SIGNUP(user.name), | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |                         { | 
					
						
							|  |  |  |                             "action": "signup", | 
					
						
							| 
									
										
										
										
											2025-01-08 16:57:52 +08:00
										 |  |  |                             "message": WEBHOOK_MESSAGES.USER_SIGNUP(user.name), | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |                             "user": user.model_dump_json(exclude_none=True), | 
					
						
							|  |  |  |                         }, | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 raise HTTPException( | 
					
						
							|  |  |  |                     status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.ACCESS_PROHIBITED | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |         jwt_token = create_token( | 
					
						
							|  |  |  |             data={"id": user.id}, | 
					
						
							|  |  |  |             expires_delta=parse_duration(auth_manager_config.JWT_EXPIRES_IN), | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-07 02:23:42 +08:00
										 |  |  |         if auth_manager_config.ENABLE_OAUTH_GROUP_MANAGEMENT and user.role != "admin": | 
					
						
							| 
									
										
										
										
											2024-12-18 05:51:29 +08:00
										 |  |  |             self.update_user_groups( | 
					
						
							|  |  |  |                 user=user, | 
					
						
							|  |  |  |                 user_data=user_data, | 
					
						
							|  |  |  |                 default_permissions=request.app.state.config.USER_PERMISSIONS, | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-12-18 03:38:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |         # Set the cookie token | 
					
						
							|  |  |  |         response.set_cookie( | 
					
						
							|  |  |  |             key="token", | 
					
						
							|  |  |  |             value=jwt_token, | 
					
						
							|  |  |  |             httponly=True,  # Ensures the cookie is not accessible via JavaScript | 
					
						
							| 
									
										
										
										
											2024-10-17 02:30:35 +08:00
										 |  |  |             samesite=WEBUI_SESSION_COOKIE_SAME_SITE, | 
					
						
							|  |  |  |             secure=WEBUI_SESSION_COOKIE_SECURE, | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 22:21:05 +08:00
										 |  |  |         if ENABLE_OAUTH_SIGNUP.value: | 
					
						
							| 
									
										
										
										
											2024-12-10 08:25:56 +08:00
										 |  |  |             oauth_id_token = token.get("id_token") | 
					
						
							| 
									
										
										
										
											2024-12-07 20:49:12 +08:00
										 |  |  |             response.set_cookie( | 
					
						
							| 
									
										
										
										
											2024-12-10 08:25:56 +08:00
										 |  |  |                 key="oauth_id_token", | 
					
						
							|  |  |  |                 value=oauth_id_token, | 
					
						
							| 
									
										
										
										
											2024-12-07 20:49:12 +08:00
										 |  |  |                 httponly=True, | 
					
						
							|  |  |  |                 samesite=WEBUI_SESSION_COOKIE_SAME_SITE, | 
					
						
							|  |  |  |                 secure=WEBUI_SESSION_COOKIE_SECURE, | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-10-16 22:32:57 +08:00
										 |  |  |         # Redirect back to the frontend with the JWT token | 
					
						
							|  |  |  |         redirect_url = f"{request.base_url}auth#token={jwt_token}" | 
					
						
							| 
									
										
										
										
											2024-12-07 20:49:12 +08:00
										 |  |  |         return RedirectResponse(url=redirect_url, headers=response.headers) | 
					
						
							| 
									
										
										
										
											2024-10-16 15:42:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 09:38:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | oauth_manager = OAuthManager() |