auth.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import logging
  2. import uuid
  3. import jwt
  4. from datetime import UTC, datetime, timedelta
  5. from typing import Optional, Union, List, Dict
  6. from open_webui.models.users import Users
  7. from open_webui.constants import ERROR_MESSAGES
  8. from open_webui.env import WEBUI_SECRET_KEY
  9. from fastapi import Depends, HTTPException, Request, Response, status
  10. from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
  11. from passlib.context import CryptContext
  12. logging.getLogger("passlib").setLevel(logging.ERROR)
  13. SESSION_SECRET = WEBUI_SECRET_KEY
  14. ALGORITHM = "HS256"
  15. ##############
  16. # Auth Utils
  17. ##############
  18. bearer_security = HTTPBearer(auto_error=False)
  19. pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
  20. def verify_password(plain_password, hashed_password):
  21. return (
  22. pwd_context.verify(plain_password, hashed_password) if hashed_password else None
  23. )
  24. def get_password_hash(password):
  25. return pwd_context.hash(password)
  26. def create_token(data: dict, expires_delta: Union[timedelta, None] = None) -> str:
  27. payload = data.copy()
  28. if expires_delta:
  29. expire = datetime.now(UTC) + expires_delta
  30. payload.update({"exp": expire})
  31. encoded_jwt = jwt.encode(payload, SESSION_SECRET, algorithm=ALGORITHM)
  32. return encoded_jwt
  33. def decode_token(token: str) -> Optional[dict]:
  34. try:
  35. decoded = jwt.decode(token, SESSION_SECRET, algorithms=[ALGORITHM])
  36. return decoded
  37. except Exception:
  38. return None
  39. def extract_token_from_auth_header(auth_header: str):
  40. return auth_header[len("Bearer ") :]
  41. def create_api_key():
  42. key = str(uuid.uuid4()).replace("-", "")
  43. return f"sk-{key}"
  44. def get_http_authorization_cred(auth_header: str):
  45. try:
  46. scheme, credentials = auth_header.split(" ")
  47. return HTTPAuthorizationCredentials(scheme=scheme, credentials=credentials)
  48. except Exception:
  49. raise ValueError(ERROR_MESSAGES.INVALID_TOKEN)
  50. def get_current_user(
  51. request: Request,
  52. auth_token: HTTPAuthorizationCredentials = Depends(bearer_security),
  53. ):
  54. token = None
  55. if auth_token is not None:
  56. token = auth_token.credentials
  57. if token is None and "token" in request.cookies:
  58. token = request.cookies.get("token")
  59. if token is None:
  60. raise HTTPException(status_code=403, detail="Not authenticated")
  61. # auth by api key
  62. if token.startswith("sk-"):
  63. if not request.state.enable_api_key:
  64. raise HTTPException(
  65. status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.API_KEY_NOT_ALLOWED
  66. )
  67. if request.app.state.ENABLE_API_KEY_ENDPOINT_RESTRICTIONS:
  68. allowed_paths = [
  69. path.strip()
  70. for path in str(request.app.state.API_KEY_ALLOWED_PATHS).split(",")
  71. ]
  72. if request.url.path not in allowed_paths:
  73. raise HTTPException(
  74. status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.API_KEY_NOT_ALLOWED
  75. )
  76. return get_current_user_by_api_key(token)
  77. # auth by jwt token
  78. try:
  79. data = decode_token(token)
  80. except Exception as e:
  81. raise HTTPException(
  82. status_code=status.HTTP_401_UNAUTHORIZED,
  83. detail="Invalid token",
  84. )
  85. if data is not None and "id" in data:
  86. user = Users.get_user_by_id(data["id"])
  87. if user is None:
  88. raise HTTPException(
  89. status_code=status.HTTP_401_UNAUTHORIZED,
  90. detail=ERROR_MESSAGES.INVALID_TOKEN,
  91. )
  92. else:
  93. Users.update_user_last_active_by_id(user.id)
  94. return user
  95. else:
  96. raise HTTPException(
  97. status_code=status.HTTP_401_UNAUTHORIZED,
  98. detail=ERROR_MESSAGES.UNAUTHORIZED,
  99. )
  100. def get_current_user_by_api_key(api_key: str):
  101. user = Users.get_user_by_api_key(api_key)
  102. if user is None:
  103. raise HTTPException(
  104. status_code=status.HTTP_401_UNAUTHORIZED,
  105. detail=ERROR_MESSAGES.INVALID_TOKEN,
  106. )
  107. else:
  108. Users.update_user_last_active_by_id(user.id)
  109. return user
  110. def get_verified_user(user=Depends(get_current_user)):
  111. if user.role not in {"user", "admin"}:
  112. raise HTTPException(
  113. status_code=status.HTTP_401_UNAUTHORIZED,
  114. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  115. )
  116. return user
  117. def get_admin_user(user=Depends(get_current_user)):
  118. if user.role != "admin":
  119. raise HTTPException(
  120. status_code=status.HTTP_401_UNAUTHORIZED,
  121. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  122. )
  123. return user