oauth.py 8.9 KB

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