2024-03-20 23:11:36 +00:00
|
|
|
import logging
|
2024-08-27 22:10:27 +00:00
|
|
|
import uuid
|
|
|
|
from typing import Optional
|
2023-11-19 00:47:12 +00:00
|
|
|
|
2024-09-04 14:54:48 +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
|
2024-09-04 14:54:48 +00:00
|
|
|
from open_webui.utils.utils import verify_password
|
2024-03-31 08:13:39 +00:00
|
|
|
|
2024-03-20 23:11:36 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
log.setLevel(SRC_LOG_LEVELS["MODELS"])
|
|
|
|
|
2023-11-19 00:47:12 +00:00
|
|
|
####################
|
|
|
|
# DB MODEL
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
class Auth(Base):
|
|
|
|
__tablename__ = "auth"
|
2023-12-26 05:44:28 +00:00
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
id = Column(String, primary_key=True)
|
|
|
|
email = Column(String)
|
2024-06-24 11:21:51 +00:00
|
|
|
password = Column(Text)
|
2024-06-18 13:03:31 +00:00
|
|
|
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-11-05 22:20:54 +00:00
|
|
|
class LdapForm(BaseModel):
|
|
|
|
user: str
|
|
|
|
password: str
|
|
|
|
|
|
|
|
|
2024-01-27 04:27:45 +00:00
|
|
|
class ProfileImageUrlForm(BaseModel):
|
|
|
|
profile_image_url: str
|
|
|
|
|
|
|
|
|
2024-01-27 05:22:25 +00:00
|
|
|
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",
|
2024-05-26 07:37:09 +00:00
|
|
|
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-06-18 13:03:31 +00:00
|
|
|
|
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]:
|
2024-03-20 23:11:36 +00:00
|
|
|
log.info(f"authenticate_user: {email}")
|
2024-06-24 11:06:15 +00:00
|
|
|
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:
|
2024-06-24 11:06:15 +00:00
|
|
|
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}")
|
2024-06-24 11:06:15 +00:00
|
|
|
# if no api_key, return None
|
|
|
|
if not api_key:
|
|
|
|
return None
|
2024-04-02 16:18:15 +00:00
|
|
|
|
2024-06-24 11:06: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:
|
2024-06-24 11:06:15 +00:00
|
|
|
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]:
|
2024-03-26 21:30:53 +00:00
|
|
|
log.info(f"authenticate_user_by_trusted_header: {email}")
|
2024-06-24 11:06:15 +00:00
|
|
|
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:
|
2024-06-24 11:06:15 +00:00
|
|
|
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:
|
2024-06-24 11:06:15 +00:00
|
|
|
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})
|
|
|
|
)
|
2024-07-07 21:27:26 +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:
|
2024-06-24 11:06:15 +00:00
|
|
|
return False
|
2024-06-21 12:58:57 +00:00
|
|
|
|
|
|
|
def update_email_by_id(self, id: str, email: str) -> bool:
|
2024-06-24 11:06:15 +00:00
|
|
|
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:
|
2024-06-24 11:06:15 +00:00
|
|
|
return False
|
2024-06-21 12:58:57 +00:00
|
|
|
|
|
|
|
def delete_auth_by_id(self, id: str) -> bool:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 07:25:45 +00:00
|
|
|
with get_db() as db:
|
|
|
|
# Delete User
|
|
|
|
result = Users.delete_user_by_id(id)
|
2024-06-21 12:58:57 +00:00
|
|
|
|
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:
|
2024-06-24 11:06:15 +00:00
|
|
|
return False
|
2023-12-29 07:24:51 +00:00
|
|
|
|
2023-11-19 00:47:12 +00:00
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
Auths = AuthsTable()
|