oauth.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import base64
  2. import logging
  3. import mimetypes
  4. import uuid
  5. import aiohttp
  6. from authlib.integrations.starlette_client import OAuth
  7. from authlib.oidc.core import UserInfo
  8. from fastapi import (
  9. HTTPException,
  10. status,
  11. )
  12. from starlette.responses import RedirectResponse
  13. from open_webui.apps.webui.models.auths import Auths
  14. from open_webui.apps.webui.models.users import Users
  15. from open_webui.config import (
  16. DEFAULT_USER_ROLE,
  17. ENABLE_OAUTH_SIGNUP,
  18. OAUTH_MERGE_ACCOUNTS_BY_EMAIL,
  19. OAUTH_PROVIDERS,
  20. ENABLE_OAUTH_ROLE_MANAGEMENT,
  21. OAUTH_ROLES_CLAIM,
  22. OAUTH_EMAIL_CLAIM,
  23. OAUTH_PICTURE_CLAIM,
  24. OAUTH_USERNAME_CLAIM,
  25. OAUTH_ALLOWED_ROLES,
  26. OAUTH_ADMIN_ROLES,
  27. WEBHOOK_URL,
  28. JWT_EXPIRES_IN,
  29. AppConfig,
  30. )
  31. from open_webui.constants import ERROR_MESSAGES
  32. from open_webui.env import WEBUI_SESSION_COOKIE_SAME_SITE, WEBUI_SESSION_COOKIE_SECURE
  33. from open_webui.utils.misc import parse_duration
  34. from open_webui.utils.utils import get_password_hash, create_token
  35. from open_webui.utils.webhook import post_webhook
  36. log = logging.getLogger(__name__)
  37. auth_manager_config = AppConfig()
  38. auth_manager_config.DEFAULT_USER_ROLE = DEFAULT_USER_ROLE
  39. auth_manager_config.ENABLE_OAUTH_SIGNUP = ENABLE_OAUTH_SIGNUP
  40. auth_manager_config.OAUTH_MERGE_ACCOUNTS_BY_EMAIL = OAUTH_MERGE_ACCOUNTS_BY_EMAIL
  41. auth_manager_config.ENABLE_OAUTH_ROLE_MANAGEMENT = ENABLE_OAUTH_ROLE_MANAGEMENT
  42. auth_manager_config.OAUTH_ROLES_CLAIM = OAUTH_ROLES_CLAIM
  43. auth_manager_config.OAUTH_EMAIL_CLAIM = OAUTH_EMAIL_CLAIM
  44. auth_manager_config.OAUTH_PICTURE_CLAIM = OAUTH_PICTURE_CLAIM
  45. auth_manager_config.OAUTH_USERNAME_CLAIM = OAUTH_USERNAME_CLAIM
  46. auth_manager_config.OAUTH_ALLOWED_ROLES = OAUTH_ALLOWED_ROLES
  47. auth_manager_config.OAUTH_ADMIN_ROLES = OAUTH_ADMIN_ROLES
  48. auth_manager_config.WEBHOOK_URL = WEBHOOK_URL
  49. auth_manager_config.JWT_EXPIRES_IN = JWT_EXPIRES_IN
  50. class OAuthManager:
  51. def __init__(self):
  52. self.oauth = OAuth()
  53. for provider_name, provider_config in OAUTH_PROVIDERS.items():
  54. self.oauth.register(
  55. name=provider_name,
  56. client_id=provider_config["client_id"],
  57. client_secret=provider_config["client_secret"],
  58. server_metadata_url=provider_config["server_metadata_url"],
  59. client_kwargs={
  60. "scope": provider_config["scope"],
  61. },
  62. redirect_uri=provider_config["redirect_uri"],
  63. )
  64. def get_client(self, provider_name):
  65. return self.oauth.create_client(provider_name)
  66. def get_user_role(self, user, user_data):
  67. if user and Users.get_num_users() == 1:
  68. # If the user is the only user, assign the role "admin" - actually repairs role for single user on login
  69. return "admin"
  70. if not user and Users.get_num_users() == 0:
  71. # If there are no users, assign the role "admin", as the first user will be an admin
  72. return "admin"
  73. if auth_manager_config.ENABLE_OAUTH_ROLE_MANAGEMENT:
  74. oauth_claim = auth_manager_config.OAUTH_ROLES_CLAIM
  75. oauth_allowed_roles = auth_manager_config.OAUTH_ALLOWED_ROLES
  76. oauth_admin_roles = auth_manager_config.OAUTH_ADMIN_ROLES
  77. oauth_roles = None
  78. role = "pending" # Default/fallback role if no matching roles are found
  79. # Next block extracts the roles from the user data, accepting nested claims of any depth
  80. if oauth_claim and oauth_allowed_roles and oauth_admin_roles:
  81. claim_data = user_data
  82. nested_claims = oauth_claim.split(".")
  83. for nested_claim in nested_claims:
  84. claim_data = claim_data.get(nested_claim, {})
  85. oauth_roles = claim_data if isinstance(claim_data, list) else None
  86. # If any roles are found, check if they match the allowed or admin roles
  87. if oauth_roles:
  88. # If role management is enabled, and matching roles are provided, use the roles
  89. for allowed_role in oauth_allowed_roles:
  90. # If the user has any of the allowed roles, assign the role "user"
  91. if allowed_role in oauth_roles:
  92. role = "user"
  93. break
  94. for admin_role in oauth_admin_roles:
  95. # If the user has any of the admin roles, assign the role "admin"
  96. if admin_role in oauth_roles:
  97. role = "admin"
  98. break
  99. else:
  100. if not user:
  101. # If role management is disabled, use the default role for new users
  102. role = auth_manager_config.DEFAULT_USER_ROLE
  103. else:
  104. # If role management is disabled, use the existing role for existing users
  105. role = user.role
  106. return role
  107. async def handle_login(self, provider, request):
  108. if provider not in OAUTH_PROVIDERS:
  109. raise HTTPException(404)
  110. # If the provider has a custom redirect URL, use that, otherwise automatically generate one
  111. redirect_uri = OAUTH_PROVIDERS[provider].get("redirect_uri") or request.url_for(
  112. "oauth_callback", provider=provider
  113. )
  114. client = self.get_client(provider)
  115. if client is None:
  116. raise HTTPException(404)
  117. return await client.authorize_redirect(request, redirect_uri)
  118. async def handle_callback(self, provider, request, response):
  119. if provider not in OAUTH_PROVIDERS:
  120. raise HTTPException(404)
  121. client = self.get_client(provider)
  122. try:
  123. token = await client.authorize_access_token(request)
  124. except Exception as e:
  125. log.warning(f"OAuth callback error: {e}")
  126. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  127. user_data: UserInfo = token["userinfo"]
  128. if not user_data:
  129. user_data: UserInfo = await client.userinfo(token=token)
  130. if not user_data:
  131. log.warning(f"OAuth callback failed, user data is missing: {token}")
  132. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  133. sub = user_data.get("sub")
  134. if not sub:
  135. log.warning(f"OAuth callback failed, sub is missing: {user_data}")
  136. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  137. provider_sub = f"{provider}@{sub}"
  138. email_claim = auth_manager_config.OAUTH_EMAIL_CLAIM
  139. email = user_data.get(email_claim, "").lower()
  140. # We currently mandate that email addresses are provided
  141. if not email:
  142. log.warning(f"OAuth callback failed, email is missing: {user_data}")
  143. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  144. # Check if the user exists
  145. user = Users.get_user_by_oauth_sub(provider_sub)
  146. if not user:
  147. # If the user does not exist, check if merging is enabled
  148. if auth_manager_config.OAUTH_MERGE_ACCOUNTS_BY_EMAIL:
  149. # Check if the user exists by email
  150. user = Users.get_user_by_email(email)
  151. if user:
  152. # Update the user with the new oauth sub
  153. Users.update_user_oauth_sub_by_id(user.id, provider_sub)
  154. if user:
  155. determined_role = self.get_user_role(user, user_data)
  156. if user.role != determined_role:
  157. Users.update_user_role_by_id(user.id, determined_role)
  158. if not user:
  159. # If the user does not exist, check if signups are enabled
  160. if auth_manager_config.ENABLE_OAUTH_SIGNUP:
  161. # Check if an existing user with the same email already exists
  162. existing_user = Users.get_user_by_email(
  163. user_data.get("email", "").lower()
  164. )
  165. if existing_user:
  166. raise HTTPException(400, detail=ERROR_MESSAGES.EMAIL_TAKEN)
  167. picture_claim = auth_manager_config.OAUTH_PICTURE_CLAIM
  168. picture_url = user_data.get(picture_claim, "")
  169. if picture_url:
  170. # Download the profile image into a base64 string
  171. try:
  172. async with aiohttp.ClientSession() as session:
  173. async with session.get(picture_url) as resp:
  174. picture = await resp.read()
  175. base64_encoded_picture = base64.b64encode(
  176. picture
  177. ).decode("utf-8")
  178. guessed_mime_type = mimetypes.guess_type(picture_url)[0]
  179. if guessed_mime_type is None:
  180. # assume JPG, browsers are tolerant enough of image formats
  181. guessed_mime_type = "image/jpeg"
  182. picture_url = f"data:{guessed_mime_type};base64,{base64_encoded_picture}"
  183. except Exception as e:
  184. log.error(
  185. f"Error downloading profile image '{picture_url}': {e}"
  186. )
  187. picture_url = ""
  188. if not picture_url:
  189. picture_url = "/user.png"
  190. username_claim = auth_manager_config.OAUTH_USERNAME_CLAIM
  191. role = self.get_user_role(None, user_data)
  192. user = Auths.insert_new_auth(
  193. email=email,
  194. password=get_password_hash(
  195. str(uuid.uuid4())
  196. ), # Random password, not used
  197. name=user_data.get(username_claim, "User"),
  198. profile_image_url=picture_url,
  199. role=role,
  200. oauth_sub=provider_sub,
  201. )
  202. if auth_manager_config.WEBHOOK_URL:
  203. post_webhook(
  204. auth_manager_config.WEBHOOK_URL,
  205. auth_manager_config.WEBHOOK_MESSAGES.USER_SIGNUP(user.name),
  206. {
  207. "action": "signup",
  208. "message": auth_manager_config.WEBHOOK_MESSAGES.USER_SIGNUP(
  209. user.name
  210. ),
  211. "user": user.model_dump_json(exclude_none=True),
  212. },
  213. )
  214. else:
  215. raise HTTPException(
  216. status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.ACCESS_PROHIBITED
  217. )
  218. jwt_token = create_token(
  219. data={"id": user.id},
  220. expires_delta=parse_duration(auth_manager_config.JWT_EXPIRES_IN),
  221. )
  222. # Set the cookie token
  223. response.set_cookie(
  224. key="token",
  225. value=jwt_token,
  226. httponly=True, # Ensures the cookie is not accessible via JavaScript
  227. samesite=WEBUI_SESSION_COOKIE_SAME_SITE,
  228. secure=WEBUI_SESSION_COOKIE_SECURE,
  229. )
  230. # Redirect back to the frontend with the JWT token
  231. redirect_url = f"{request.base_url}auth#token={jwt_token}"
  232. return RedirectResponse(url=redirect_url)
  233. oauth_manager = OAuthManager()