open-webui/backend/open_webui/apps/webui/models/auths.py

202 lines
5.0 KiB
Python
Raw Permalink Normal View History

import logging
2024-08-27 22:10:27 +00:00
import uuid
from typing import Optional
2023-11-19 00:47:12 +00:00
from open_webui.apps.webui.internal.db import Base, get_db
from open_webui.apps.webui.models.users import UserModel, Users
from open_webui.env import SRC_LOG_LEVELS
2024-08-27 22:10:27 +00:00
from pydantic import BaseModel
from sqlalchemy import Boolean, Column, String, Text
from open_webui.utils.utils import verify_password
2024-03-31 08:13:39 +00:00
log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["MODELS"])
2023-11-19 00:47:12 +00:00
####################
# DB MODEL
####################
class Auth(Base):
__tablename__ = "auth"
2023-12-26 05:44:28 +00:00
id = Column(String, primary_key=True)
email = Column(String)
password = Column(Text)
active = Column(Boolean)
2023-12-26 05:44:28 +00:00
2023-11-19 00:47:12 +00:00
class AuthModel(BaseModel):
id: str
email: str
password: str
active: bool = True
####################
# Forms
####################
class Token(BaseModel):
token: str
token_type: str
2024-04-02 16:18:15 +00:00
2024-03-26 10:22:17 +00:00
class ApiKey(BaseModel):
api_key: Optional[str] = None
2023-11-19 00:47:12 +00:00
2024-04-02 16:18:15 +00:00
2023-11-19 00:47:12 +00:00
class UserResponse(BaseModel):
id: str
email: str
name: str
role: str
2023-11-19 05:41:43 +00:00
profile_image_url: str
2023-11-19 00:47:12 +00:00
class SigninResponse(Token, UserResponse):
pass
class SigninForm(BaseModel):
email: str
password: str
2024-01-27 04:27:45 +00:00
class ProfileImageUrlForm(BaseModel):
profile_image_url: str
class UpdateProfileForm(BaseModel):
profile_image_url: str
name: str
2023-12-29 08:12:30 +00:00
class UpdatePasswordForm(BaseModel):
password: str
new_password: str
2023-11-19 00:47:12 +00:00
class SignupForm(BaseModel):
name: str
email: str
password: str
2024-04-07 06:16:29 +00:00
profile_image_url: Optional[str] = "/user.png"
2023-11-19 00:47:12 +00:00
2024-05-02 00:55:18 +00:00
class AddUserForm(SignupForm):
2024-05-02 01:06:02 +00:00
role: Optional[str] = "pending"
2024-05-02 00:55:18 +00:00
2023-11-19 00:47:12 +00:00
class AuthsTable:
2024-01-06 04:59:56 +00:00
def insert_new_auth(
2024-04-04 08:10:51 +00:00
self,
email: str,
password: str,
name: str,
2024-04-07 06:16:29 +00:00
profile_image_url: str = "/user.png",
2024-04-04 08:10:51 +00:00
role: str = "pending",
oauth_sub: Optional[str] = None,
2024-01-06 04:59:56 +00:00
) -> Optional[UserModel]:
2024-07-04 07:25:45 +00:00
with get_db() as db:
log.info("insert_new_auth")
2023-11-19 00:47:12 +00:00
2024-07-04 07:25:45 +00:00
id = str(uuid.uuid4())
2023-12-26 05:44:28 +00:00
2024-07-04 07:25:45 +00:00
auth = AuthModel(
**{"id": id, "email": email, "password": password, "active": True}
)
result = Auth(**auth.model_dump())
db.add(result)
2023-11-19 00:47:12 +00:00
2024-07-04 07:25:45 +00:00
user = Users.insert_new_user(
id, name, email, profile_image_url, role, oauth_sub
)
2024-07-04 07:25:45 +00:00
db.commit()
db.refresh(result)
if result and user:
return user
else:
return None
2023-11-19 00:47:12 +00:00
2024-06-24 07:57:08 +00:00
def authenticate_user(self, email: str, password: str) -> Optional[UserModel]:
log.info(f"authenticate_user: {email}")
try:
2024-07-04 07:25:45 +00:00
with get_db() as db:
auth = db.query(Auth).filter_by(email=email, active=True).first()
if auth:
if verify_password(password, auth.password):
user = Users.get_user_by_id(auth.id)
return user
else:
return None
2023-12-26 07:43:21 +00:00
else:
return None
2024-08-14 12:38:19 +00:00
except Exception:
return None
2023-11-19 00:47:12 +00:00
2024-06-24 07:57:08 +00:00
def authenticate_user_by_api_key(self, api_key: str) -> Optional[UserModel]:
2024-03-26 10:22:17 +00:00
log.info(f"authenticate_user_by_api_key: {api_key}")
# if no api_key, return None
if not api_key:
return None
2024-04-02 16:18:15 +00:00
try:
user = Users.get_user_by_api_key(api_key)
return user if user else None
2024-08-14 12:38:19 +00:00
except Exception:
return False
2024-04-02 16:18:15 +00:00
2024-06-24 07:57:08 +00:00
def authenticate_user_by_trusted_header(self, email: str) -> Optional[UserModel]:
log.info(f"authenticate_user_by_trusted_header: {email}")
try:
2024-07-04 07:25:45 +00:00
with get_db() as db:
2024-07-10 20:35:52 +00:00
auth = db.query(Auth).filter_by(email=email, active=True).first()
2024-07-04 07:25:45 +00:00
if auth:
user = Users.get_user_by_id(auth.id)
return user
2024-08-14 12:38:19 +00:00
except Exception:
return None
2024-03-26 10:22:17 +00:00
2024-06-24 07:57:08 +00:00
def update_user_password_by_id(self, id: str, new_password: str) -> bool:
try:
2024-07-04 07:25:45 +00:00
with get_db() as db:
result = (
db.query(Auth).filter_by(id=id).update({"password": new_password})
)
db.commit()
2024-07-04 07:25:45 +00:00
return True if result == 1 else False
2024-08-14 12:38:19 +00:00
except Exception:
return False
def update_email_by_id(self, id: str, email: str) -> bool:
try:
2024-07-04 07:25:45 +00:00
with get_db() as db:
result = db.query(Auth).filter_by(id=id).update({"email": email})
2024-07-07 21:07:12 +00:00
db.commit()
2024-07-04 07:25:45 +00:00
return True if result == 1 else False
2024-08-14 12:38:19 +00:00
except Exception:
return False
def delete_auth_by_id(self, id: str) -> bool:
try:
2024-07-04 07:25:45 +00:00
with get_db() as db:
# Delete User
result = Users.delete_user_by_id(id)
2024-07-04 07:25:45 +00:00
if result:
db.query(Auth).filter_by(id=id).delete()
2024-07-06 15:10:58 +00:00
db.commit()
2024-07-04 07:25:45 +00:00
return True
else:
return False
2024-08-14 12:38:19 +00:00
except Exception:
return False
2023-12-29 07:24:51 +00:00
2023-11-19 00:47:12 +00:00
Auths = AuthsTable()