2023-11-19 00:47:12 +00:00
|
|
|
from pydantic import BaseModel
|
2024-06-18 13:03:31 +00:00
|
|
|
from typing import Optional
|
2023-11-19 00:47:12 +00:00
|
|
|
import uuid
|
2024-03-20 23:11:36 +00:00
|
|
|
import logging
|
2024-06-24 11:21:51 +00:00
|
|
|
from sqlalchemy import String, Column, Boolean, Text
|
2023-11-19 00:47:12 +00:00
|
|
|
|
2024-05-26 08:15:48 +00:00
|
|
|
from apps.webui.models.users import UserModel, Users
|
2024-02-01 20:53:13 +00:00
|
|
|
from utils.utils import verify_password
|
2023-11-19 00:47:12 +00:00
|
|
|
|
2024-06-24 11:06:15 +00:00
|
|
|
from apps.webui.internal.db import Base, Session
|
2023-11-19 00:47:12 +00:00
|
|
|
|
2024-03-20 23:11:36 +00:00
|
|
|
from config import SRC_LOG_LEVELS
|
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-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-06-24 11:06:15 +00:00
|
|
|
log.info("insert_new_auth")
|
2023-11-19 00:47:12 +00:00
|
|
|
|
2024-06-24 11:06:15 +00:00
|
|
|
id = str(uuid.uuid4())
|
2023-11-19 00:47:12 +00:00
|
|
|
|
2024-06-24 11:06:15 +00:00
|
|
|
auth = AuthModel(
|
|
|
|
**{"id": id, "email": email, "password": password, "active": True}
|
|
|
|
)
|
|
|
|
result = Auth(**auth.model_dump())
|
|
|
|
Session.add(result)
|
2023-12-26 05:44:28 +00:00
|
|
|
|
2024-06-24 11:06:15 +00:00
|
|
|
user = Users.insert_new_user(
|
2024-06-25 06:35:55 +00:00
|
|
|
id, name, email, profile_image_url, role, oauth_sub
|
|
|
|
)
|
2023-11-19 00:47:12 +00:00
|
|
|
|
2024-06-24 11:06:15 +00:00
|
|
|
Session.commit()
|
|
|
|
Session.refresh(result)
|
2024-06-18 13:03:31 +00:00
|
|
|
|
2024-06-24 11:06:15 +00:00
|
|
|
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:
|
|
|
|
auth = Session.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
|
2023-12-26 07:43:21 +00:00
|
|
|
else:
|
|
|
|
return None
|
2024-06-24 11:06:15 +00:00
|
|
|
else:
|
2023-11-19 00:47:12 +00:00
|
|
|
return None
|
2024-06-24 11:06:15 +00:00
|
|
|
except:
|
|
|
|
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
|
|
|
|
except:
|
|
|
|
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:
|
|
|
|
auth = Session.query(Auth).filter(email=email, active=True).first()
|
|
|
|
if auth:
|
|
|
|
user = Users.get_user_by_id(auth.id)
|
|
|
|
return user
|
|
|
|
except:
|
|
|
|
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:
|
|
|
|
result = (
|
|
|
|
Session.query(Auth).filter_by(id=id).update({"password": new_password})
|
|
|
|
)
|
|
|
|
return True if result == 1 else False
|
|
|
|
except:
|
|
|
|
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:
|
|
|
|
result = Session.query(Auth).filter_by(id=id).update({"email": email})
|
|
|
|
return True if result == 1 else False
|
|
|
|
except:
|
|
|
|
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:
|
|
|
|
# Delete User
|
|
|
|
result = Users.delete_user_by_id(id)
|
2024-06-21 12:58:57 +00:00
|
|
|
|
2024-06-24 11:06:15 +00:00
|
|
|
if result:
|
|
|
|
Session.query(Auth).filter_by(id=id).delete()
|
2024-06-21 12:58:57 +00:00
|
|
|
|
2024-06-24 11:06:15 +00:00
|
|
|
return True
|
|
|
|
else:
|
2023-12-29 07:24:51 +00:00
|
|
|
return False
|
2024-06-24 11:06:15 +00:00
|
|
|
except:
|
|
|
|
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()
|