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