auths.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. from pydantic import BaseModel
  2. from typing import List, Union, Optional
  3. import time
  4. import uuid
  5. import logging
  6. from peewee import *
  7. from apps.web.models.users import UserModel, Users
  8. from utils.utils import verify_password
  9. from apps.web.internal.db import DB
  10. from config import SRC_LOG_LEVELS
  11. log = logging.getLogger(__name__)
  12. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  13. ####################
  14. # DB MODEL
  15. ####################
  16. class Auth(Model):
  17. id = CharField(unique=True)
  18. email = CharField()
  19. password = CharField()
  20. active = BooleanField()
  21. class Meta:
  22. database = DB
  23. class AuthModel(BaseModel):
  24. id: str
  25. email: str
  26. password: str
  27. active: bool = True
  28. ####################
  29. # Forms
  30. ####################
  31. class Token(BaseModel):
  32. token: str
  33. token_type: str
  34. class UserResponse(BaseModel):
  35. id: str
  36. email: str
  37. name: str
  38. role: str
  39. profile_image_url: str
  40. class SigninResponse(Token, UserResponse):
  41. pass
  42. class SigninForm(BaseModel):
  43. email: str
  44. password: str
  45. class ProfileImageUrlForm(BaseModel):
  46. profile_image_url: str
  47. class UpdateProfileForm(BaseModel):
  48. profile_image_url: str
  49. name: str
  50. class UpdatePasswordForm(BaseModel):
  51. password: str
  52. new_password: str
  53. class SignupForm(BaseModel):
  54. name: str
  55. email: str
  56. password: str
  57. class AuthsTable:
  58. def __init__(self, db):
  59. self.db = db
  60. self.db.create_tables([Auth])
  61. def insert_new_auth(
  62. self, email: str, password: str, name: str, role: str = "pending"
  63. ) -> Optional[UserModel]:
  64. log.info("insert_new_auth")
  65. id = str(uuid.uuid4())
  66. auth = AuthModel(
  67. **{"id": id, "email": email, "password": password, "active": True}
  68. )
  69. result = Auth.create(**auth.model_dump())
  70. user = Users.insert_new_user(id, name, email, role)
  71. if result and user:
  72. return user
  73. else:
  74. return None
  75. def authenticate_user(self, email: str, password: str) -> Optional[UserModel]:
  76. log.info(f"authenticate_user: {email}")
  77. try:
  78. auth = Auth.get(Auth.email == email, Auth.active == True)
  79. if auth:
  80. if verify_password(password, auth.password):
  81. user = Users.get_user_by_id(auth.id)
  82. return user
  83. else:
  84. return None
  85. else:
  86. return None
  87. except:
  88. return None
  89. def authenticate_user_by_trusted_header(self,
  90. email: str) -> Optional[UserModel]:
  91. log.info(f"authenticate_user_by_trusted_header: {email}")
  92. try:
  93. auth = Auth.get(Auth.email == email, Auth.active == True)
  94. if auth:
  95. user = Users.get_user_by_id(auth.id)
  96. return user
  97. except:
  98. return None
  99. def update_user_password_by_id(self, id: str, new_password: str) -> bool:
  100. try:
  101. query = Auth.update(password=new_password).where(Auth.id == id)
  102. result = query.execute()
  103. return True if result == 1 else False
  104. except:
  105. return False
  106. def update_email_by_id(self, id: str, email: str) -> bool:
  107. try:
  108. query = Auth.update(email=email).where(Auth.id == id)
  109. result = query.execute()
  110. return True if result == 1 else False
  111. except:
  112. return False
  113. def delete_auth_by_id(self, id: str) -> bool:
  114. try:
  115. # Delete User
  116. result = Users.delete_user_by_id(id)
  117. if result:
  118. # Delete Auth
  119. query = Auth.delete().where(Auth.id == id)
  120. query.execute() # Remove the rows, return number of rows removed.
  121. return True
  122. else:
  123. return False
  124. except:
  125. return False
  126. Auths = AuthsTable(DB)