oauth.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. import base64
  2. import logging
  3. import mimetypes
  4. import sys
  5. import uuid
  6. import aiohttp
  7. from authlib.integrations.starlette_client import OAuth
  8. from authlib.oidc.core import UserInfo
  9. from fastapi import (
  10. HTTPException,
  11. status,
  12. )
  13. from starlette.responses import RedirectResponse
  14. from open_webui.models.auths import Auths
  15. from open_webui.models.users import Users
  16. from open_webui.models.groups import Groups, GroupModel, GroupUpdateForm, GroupForm
  17. from open_webui.config import (
  18. DEFAULT_USER_ROLE,
  19. ENABLE_OAUTH_SIGNUP,
  20. OAUTH_MERGE_ACCOUNTS_BY_EMAIL,
  21. OAUTH_PROVIDERS,
  22. ENABLE_OAUTH_ROLE_MANAGEMENT,
  23. ENABLE_OAUTH_GROUP_MANAGEMENT,
  24. ENABLE_OAUTH_GROUP_CREATION,
  25. OAUTH_ROLES_CLAIM,
  26. OAUTH_GROUPS_CLAIM,
  27. OAUTH_EMAIL_CLAIM,
  28. OAUTH_PICTURE_CLAIM,
  29. OAUTH_USERNAME_CLAIM,
  30. OAUTH_ALLOWED_ROLES,
  31. OAUTH_ADMIN_ROLES,
  32. OAUTH_ALLOWED_DOMAINS,
  33. WEBHOOK_URL,
  34. JWT_EXPIRES_IN,
  35. AppConfig,
  36. )
  37. from open_webui.constants import ERROR_MESSAGES, WEBHOOK_MESSAGES
  38. from open_webui.env import (
  39. WEBUI_NAME,
  40. WEBUI_AUTH_COOKIE_SAME_SITE,
  41. WEBUI_AUTH_COOKIE_SECURE,
  42. )
  43. from open_webui.utils.misc import parse_duration
  44. from open_webui.utils.auth import get_password_hash, create_token
  45. from open_webui.utils.webhook import post_webhook
  46. from open_webui.env import SRC_LOG_LEVELS, GLOBAL_LOG_LEVEL
  47. logging.basicConfig(stream=sys.stdout, level=GLOBAL_LOG_LEVEL)
  48. log = logging.getLogger(__name__)
  49. log.setLevel(SRC_LOG_LEVELS["OAUTH"])
  50. auth_manager_config = AppConfig()
  51. auth_manager_config.DEFAULT_USER_ROLE = DEFAULT_USER_ROLE
  52. auth_manager_config.ENABLE_OAUTH_SIGNUP = ENABLE_OAUTH_SIGNUP
  53. auth_manager_config.OAUTH_MERGE_ACCOUNTS_BY_EMAIL = OAUTH_MERGE_ACCOUNTS_BY_EMAIL
  54. auth_manager_config.ENABLE_OAUTH_ROLE_MANAGEMENT = ENABLE_OAUTH_ROLE_MANAGEMENT
  55. auth_manager_config.ENABLE_OAUTH_GROUP_MANAGEMENT = ENABLE_OAUTH_GROUP_MANAGEMENT
  56. auth_manager_config.ENABLE_OAUTH_GROUP_CREATION = ENABLE_OAUTH_GROUP_CREATION
  57. auth_manager_config.OAUTH_ROLES_CLAIM = OAUTH_ROLES_CLAIM
  58. auth_manager_config.OAUTH_GROUPS_CLAIM = OAUTH_GROUPS_CLAIM
  59. auth_manager_config.OAUTH_EMAIL_CLAIM = OAUTH_EMAIL_CLAIM
  60. auth_manager_config.OAUTH_PICTURE_CLAIM = OAUTH_PICTURE_CLAIM
  61. auth_manager_config.OAUTH_USERNAME_CLAIM = OAUTH_USERNAME_CLAIM
  62. auth_manager_config.OAUTH_ALLOWED_ROLES = OAUTH_ALLOWED_ROLES
  63. auth_manager_config.OAUTH_ADMIN_ROLES = OAUTH_ADMIN_ROLES
  64. auth_manager_config.OAUTH_ALLOWED_DOMAINS = OAUTH_ALLOWED_DOMAINS
  65. auth_manager_config.WEBHOOK_URL = WEBHOOK_URL
  66. auth_manager_config.JWT_EXPIRES_IN = JWT_EXPIRES_IN
  67. class OAuthManager:
  68. def __init__(self, app):
  69. self.oauth = OAuth()
  70. self.app = app
  71. for _, provider_config in OAUTH_PROVIDERS.items():
  72. provider_config["register"](self.oauth)
  73. def get_client(self, provider_name):
  74. return self.oauth.create_client(provider_name)
  75. def get_user_role(self, user, user_data):
  76. if user and Users.get_num_users() == 1:
  77. # If the user is the only user, assign the role "admin" - actually repairs role for single user on login
  78. log.debug("Assigning the only user the admin role")
  79. return "admin"
  80. if not user and Users.get_num_users() == 0:
  81. # If there are no users, assign the role "admin", as the first user will be an admin
  82. log.debug("Assigning the first user the admin role")
  83. return "admin"
  84. if auth_manager_config.ENABLE_OAUTH_ROLE_MANAGEMENT:
  85. log.debug("Running OAUTH Role management")
  86. oauth_claim = auth_manager_config.OAUTH_ROLES_CLAIM
  87. oauth_allowed_roles = auth_manager_config.OAUTH_ALLOWED_ROLES
  88. oauth_admin_roles = auth_manager_config.OAUTH_ADMIN_ROLES
  89. oauth_roles = []
  90. # Default/fallback role if no matching roles are found
  91. role = auth_manager_config.DEFAULT_USER_ROLE
  92. # Next block extracts the roles from the user data, accepting nested claims of any depth
  93. if oauth_claim and oauth_allowed_roles and oauth_admin_roles:
  94. claim_data = user_data
  95. nested_claims = oauth_claim.split(".")
  96. for nested_claim in nested_claims:
  97. claim_data = claim_data.get(nested_claim, {})
  98. oauth_roles = claim_data if isinstance(claim_data, list) else []
  99. log.debug(f"Oauth Roles claim: {oauth_claim}")
  100. log.debug(f"User roles from oauth: {oauth_roles}")
  101. log.debug(f"Accepted user roles: {oauth_allowed_roles}")
  102. log.debug(f"Accepted admin roles: {oauth_admin_roles}")
  103. # If any roles are found, check if they match the allowed or admin roles
  104. if oauth_roles:
  105. # If role management is enabled, and matching roles are provided, use the roles
  106. for allowed_role in oauth_allowed_roles:
  107. # If the user has any of the allowed roles, assign the role "user"
  108. if allowed_role in oauth_roles:
  109. log.debug("Assigned user the user role")
  110. role = "user"
  111. break
  112. for admin_role in oauth_admin_roles:
  113. # If the user has any of the admin roles, assign the role "admin"
  114. if admin_role in oauth_roles:
  115. log.debug("Assigned user the admin role")
  116. role = "admin"
  117. break
  118. else:
  119. if not user:
  120. # If role management is disabled, use the default role for new users
  121. role = auth_manager_config.DEFAULT_USER_ROLE
  122. else:
  123. # If role management is disabled, use the existing role for existing users
  124. role = user.role
  125. return role
  126. def update_user_groups(self, user, user_data, default_permissions):
  127. log.debug("Running OAUTH Group management")
  128. oauth_claim = auth_manager_config.OAUTH_GROUPS_CLAIM
  129. user_oauth_groups = []
  130. # Nested claim search for groups claim
  131. if oauth_claim:
  132. claim_data = user_data
  133. nested_claims = oauth_claim.split(".")
  134. for nested_claim in nested_claims:
  135. claim_data = claim_data.get(nested_claim, {})
  136. user_oauth_groups = claim_data if isinstance(claim_data, list) else []
  137. user_current_groups: list[GroupModel] = Groups.get_groups_by_member_id(user.id)
  138. all_available_groups: list[GroupModel] = Groups.get_groups()
  139. # Create groups if they don't exist and creation is enabled
  140. if auth_manager_config.ENABLE_OAUTH_GROUP_CREATION:
  141. log.debug("Checking for missing groups to create...")
  142. all_group_names = {g.name for g in all_available_groups}
  143. groups_created = False
  144. # Determine creator ID: Prefer admin, fallback to current user if no admin exists
  145. admin_user = Users.get_admin_user()
  146. creator_id = admin_user.id if admin_user else user.id
  147. log.debug(f"Using creator ID {creator_id} for potential group creation.")
  148. for group_name in user_oauth_groups:
  149. if group_name not in all_group_names:
  150. log.info(f"Group '{group_name}' not found via OAuth claim. Creating group...")
  151. try:
  152. new_group_form = GroupForm(
  153. name=group_name,
  154. description=f"Group '{group_name}' created automatically via OAuth.",
  155. permissions=default_permissions, # Use default permissions from function args
  156. user_ids=[], # Start with no users, user will be added later by subsequent logic
  157. )
  158. # Use determined creator ID (admin or fallback to current user)
  159. created_group = Groups.insert_new_group(creator_id, new_group_form)
  160. if created_group:
  161. log.info(f"Successfully created group '{group_name}' with ID {created_group.id} using creator ID {creator_id}")
  162. groups_created = True
  163. # Add to local set to prevent duplicate creation attempts in this run
  164. all_group_names.add(group_name)
  165. else:
  166. log.error(f"Failed to create group '{group_name}' via OAuth.")
  167. except Exception as e:
  168. log.error(f"Error creating group '{group_name}' via OAuth: {e}")
  169. # Refresh the list of all available groups if any were created
  170. if groups_created:
  171. all_available_groups = Groups.get_groups()
  172. log.debug("Refreshed list of all available groups after creation.")
  173. log.debug(f"Oauth Groups claim: {oauth_claim}")
  174. log.debug(f"User oauth groups: {user_oauth_groups}")
  175. log.debug(f"User's current groups: {[g.name for g in user_current_groups]}")
  176. log.debug(
  177. f"All groups available in OpenWebUI: {[g.name for g in all_available_groups]}"
  178. )
  179. # Remove groups that user is no longer a part of
  180. for group_model in user_current_groups:
  181. if user_oauth_groups and group_model.name not in user_oauth_groups:
  182. # Remove group from user
  183. log.debug(
  184. f"Removing user from group {group_model.name} as it is no longer in their oauth groups"
  185. )
  186. user_ids = group_model.user_ids
  187. user_ids = [i for i in user_ids if i != user.id]
  188. # In case a group is created, but perms are never assigned to the group by hitting "save"
  189. group_permissions = group_model.permissions
  190. if not group_permissions:
  191. group_permissions = default_permissions
  192. update_form = GroupUpdateForm(
  193. name=group_model.name,
  194. description=group_model.description,
  195. permissions=group_permissions,
  196. user_ids=user_ids,
  197. )
  198. Groups.update_group_by_id(
  199. id=group_model.id, form_data=update_form, overwrite=False
  200. )
  201. # Add user to new groups
  202. for group_model in all_available_groups:
  203. if (
  204. user_oauth_groups
  205. and group_model.name in user_oauth_groups
  206. and not any(gm.name == group_model.name for gm in user_current_groups)
  207. ):
  208. # Add user to group
  209. log.debug(
  210. f"Adding user to group {group_model.name} as it was found in their oauth groups"
  211. )
  212. user_ids = group_model.user_ids
  213. user_ids.append(user.id)
  214. # In case a group is created, but perms are never assigned to the group by hitting "save"
  215. group_permissions = group_model.permissions
  216. if not group_permissions:
  217. group_permissions = default_permissions
  218. update_form = GroupUpdateForm(
  219. name=group_model.name,
  220. description=group_model.description,
  221. permissions=group_permissions,
  222. user_ids=user_ids,
  223. )
  224. Groups.update_group_by_id(
  225. id=group_model.id, form_data=update_form, overwrite=False
  226. )
  227. async def handle_login(self, request, provider):
  228. if provider not in OAUTH_PROVIDERS:
  229. raise HTTPException(404)
  230. # If the provider has a custom redirect URL, use that, otherwise automatically generate one
  231. redirect_uri = OAUTH_PROVIDERS[provider].get("redirect_uri") or request.url_for(
  232. "oauth_callback", provider=provider
  233. )
  234. client = self.get_client(provider)
  235. if client is None:
  236. raise HTTPException(404)
  237. return await client.authorize_redirect(request, redirect_uri)
  238. async def handle_callback(self, request, provider, response):
  239. if provider not in OAUTH_PROVIDERS:
  240. raise HTTPException(404)
  241. client = self.get_client(provider)
  242. try:
  243. token = await client.authorize_access_token(request)
  244. except Exception as e:
  245. log.warning(f"OAuth callback error: {e}")
  246. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  247. user_data: UserInfo = token.get("userinfo")
  248. if not user_data or auth_manager_config.OAUTH_EMAIL_CLAIM not in user_data:
  249. user_data: UserInfo = await client.userinfo(token=token)
  250. if not user_data:
  251. log.warning(f"OAuth callback failed, user data is missing: {token}")
  252. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  253. sub = user_data.get(OAUTH_PROVIDERS[provider].get("sub_claim", "sub"))
  254. if not sub:
  255. log.warning(f"OAuth callback failed, sub is missing: {user_data}")
  256. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  257. provider_sub = f"{provider}@{sub}"
  258. email_claim = auth_manager_config.OAUTH_EMAIL_CLAIM
  259. email = user_data.get(email_claim, "")
  260. # We currently mandate that email addresses are provided
  261. if not email:
  262. # If the provider is GitHub,and public email is not provided, we can use the access token to fetch the user's email
  263. if provider == "github":
  264. try:
  265. access_token = token.get("access_token")
  266. headers = {"Authorization": f"Bearer {access_token}"}
  267. async with aiohttp.ClientSession() as session:
  268. async with session.get(
  269. "https://api.github.com/user/emails", headers=headers
  270. ) as resp:
  271. if resp.ok:
  272. emails = await resp.json()
  273. # use the primary email as the user's email
  274. primary_email = next(
  275. (e["email"] for e in emails if e.get("primary")),
  276. None,
  277. )
  278. if primary_email:
  279. email = primary_email
  280. else:
  281. log.warning(
  282. "No primary email found in GitHub response"
  283. )
  284. raise HTTPException(
  285. 400, detail=ERROR_MESSAGES.INVALID_CRED
  286. )
  287. else:
  288. log.warning("Failed to fetch GitHub email")
  289. raise HTTPException(
  290. 400, detail=ERROR_MESSAGES.INVALID_CRED
  291. )
  292. except Exception as e:
  293. log.warning(f"Error fetching GitHub email: {e}")
  294. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  295. else:
  296. log.warning(f"OAuth callback failed, email is missing: {user_data}")
  297. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  298. email = email.lower()
  299. if (
  300. "*" not in auth_manager_config.OAUTH_ALLOWED_DOMAINS
  301. and email.split("@")[-1] not in auth_manager_config.OAUTH_ALLOWED_DOMAINS
  302. ):
  303. log.warning(
  304. f"OAuth callback failed, e-mail domain is not in the list of allowed domains: {user_data}"
  305. )
  306. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  307. # Check if the user exists
  308. user = Users.get_user_by_oauth_sub(provider_sub)
  309. if not user:
  310. # If the user does not exist, check if merging is enabled
  311. if auth_manager_config.OAUTH_MERGE_ACCOUNTS_BY_EMAIL:
  312. # Check if the user exists by email
  313. user = Users.get_user_by_email(email)
  314. if user:
  315. # Update the user with the new oauth sub
  316. Users.update_user_oauth_sub_by_id(user.id, provider_sub)
  317. if user:
  318. determined_role = self.get_user_role(user, user_data)
  319. if user.role != determined_role:
  320. Users.update_user_role_by_id(user.id, determined_role)
  321. if not user:
  322. user_count = Users.get_num_users()
  323. # If the user does not exist, check if signups are enabled
  324. if auth_manager_config.ENABLE_OAUTH_SIGNUP:
  325. # Check if an existing user with the same email already exists
  326. existing_user = Users.get_user_by_email(email)
  327. if existing_user:
  328. raise HTTPException(400, detail=ERROR_MESSAGES.EMAIL_TAKEN)
  329. picture_claim = auth_manager_config.OAUTH_PICTURE_CLAIM
  330. if picture_claim:
  331. picture_url = user_data.get(
  332. picture_claim, OAUTH_PROVIDERS[provider].get("picture_url", "")
  333. )
  334. if picture_url:
  335. # Download the profile image into a base64 string
  336. try:
  337. access_token = token.get("access_token")
  338. get_kwargs = {}
  339. if access_token:
  340. get_kwargs["headers"] = {
  341. "Authorization": f"Bearer {access_token}",
  342. }
  343. async with aiohttp.ClientSession() as session:
  344. async with session.get(
  345. picture_url, **get_kwargs
  346. ) as resp:
  347. if resp.ok:
  348. picture = await resp.read()
  349. base64_encoded_picture = base64.b64encode(
  350. picture
  351. ).decode("utf-8")
  352. guessed_mime_type = mimetypes.guess_type(
  353. picture_url
  354. )[0]
  355. if guessed_mime_type is None:
  356. # assume JPG, browsers are tolerant enough of image formats
  357. guessed_mime_type = "image/jpeg"
  358. picture_url = f"data:{guessed_mime_type};base64,{base64_encoded_picture}"
  359. else:
  360. picture_url = "/user.png"
  361. except Exception as e:
  362. log.error(
  363. f"Error downloading profile image '{picture_url}': {e}"
  364. )
  365. picture_url = "/user.png"
  366. if not picture_url:
  367. picture_url = "/user.png"
  368. else:
  369. picture_url = "/user.png"
  370. username_claim = auth_manager_config.OAUTH_USERNAME_CLAIM
  371. name = user_data.get(username_claim)
  372. if not name:
  373. log.warning("Username claim is missing, using email as name")
  374. name = email
  375. role = self.get_user_role(None, user_data)
  376. user = Auths.insert_new_auth(
  377. email=email,
  378. password=get_password_hash(
  379. str(uuid.uuid4())
  380. ), # Random password, not used
  381. name=name,
  382. profile_image_url=picture_url,
  383. role=role,
  384. oauth_sub=provider_sub,
  385. )
  386. if auth_manager_config.WEBHOOK_URL:
  387. post_webhook(
  388. WEBUI_NAME,
  389. auth_manager_config.WEBHOOK_URL,
  390. WEBHOOK_MESSAGES.USER_SIGNUP(user.name),
  391. {
  392. "action": "signup",
  393. "message": WEBHOOK_MESSAGES.USER_SIGNUP(user.name),
  394. "user": user.model_dump_json(exclude_none=True),
  395. },
  396. )
  397. else:
  398. raise HTTPException(
  399. status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.ACCESS_PROHIBITED
  400. )
  401. jwt_token = create_token(
  402. data={"id": user.id},
  403. expires_delta=parse_duration(auth_manager_config.JWT_EXPIRES_IN),
  404. )
  405. if auth_manager_config.ENABLE_OAUTH_GROUP_MANAGEMENT and user.role != "admin":
  406. self.update_user_groups(
  407. user=user,
  408. user_data=user_data,
  409. default_permissions=request.app.state.config.USER_PERMISSIONS,
  410. )
  411. # Set the cookie token
  412. response.set_cookie(
  413. key="token",
  414. value=jwt_token,
  415. httponly=True, # Ensures the cookie is not accessible via JavaScript
  416. samesite=WEBUI_AUTH_COOKIE_SAME_SITE,
  417. secure=WEBUI_AUTH_COOKIE_SECURE,
  418. )
  419. if ENABLE_OAUTH_SIGNUP.value:
  420. oauth_id_token = token.get("id_token")
  421. response.set_cookie(
  422. key="oauth_id_token",
  423. value=oauth_id_token,
  424. httponly=True,
  425. samesite=WEBUI_AUTH_COOKIE_SAME_SITE,
  426. secure=WEBUI_AUTH_COOKIE_SECURE,
  427. )
  428. # Redirect back to the frontend with the JWT token
  429. redirect_url = f"{request.base_url}auth#token={jwt_token}"
  430. return RedirectResponse(url=redirect_url, headers=response.headers)