auth.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. from __future__ import annotations # for Python 3.10+
  2. from typing import Optional, Union
  3. from uuid import UUID
  4. from typing_extensions import deprecated
  5. from ..models import Token, User
  6. class AuthMixins:
  7. @deprecated("Use client.users.create() instead")
  8. async def register(self, email: str, password: str) -> User:
  9. """
  10. Registers a new user with the given email and password.
  11. Args:
  12. email (str): The email of the user to register.
  13. password (str): The password of the user to register.
  14. Returns:
  15. User: The response from the server.
  16. """
  17. data = {"email": email, "password": password}
  18. return await self._make_request("POST", "register", json=data) # type: ignore
  19. @deprecated("Use client.users.verify_email() instead")
  20. async def verify_email(self, email: str, verification_code: str) -> dict:
  21. """
  22. Verifies the email of a user with the given verification code.
  23. Args:
  24. verification_code (str): The verification code to verify the email with.
  25. """
  26. data = {"email": email, "verification_code": verification_code}
  27. return await self._make_request( # type: ignore
  28. "POST",
  29. "verify_email",
  30. json=data,
  31. )
  32. @deprecated("Use client.users.login() instead")
  33. async def login(self, email: str, password: str) -> dict[str, Token]:
  34. """
  35. Attempts to log in a user with the given email and password.
  36. Args:
  37. email (str): The email of the user to log in.
  38. password (str): The password of the user to log in.
  39. Returns:
  40. dict[str, Token]: The access and refresh tokens from the server.
  41. """
  42. data = {"username": email, "password": password}
  43. response = await self._make_request("POST", "login", data=data) # type: ignore
  44. self.access_token = response["results"]["access_token"]["token"]
  45. self._refresh_token = response["results"]["refresh_token"]["token"]
  46. return response
  47. @deprecated("Use client.users.logout() instead")
  48. async def logout(self) -> dict:
  49. """
  50. Logs out the currently authenticated user.
  51. Returns:
  52. dict: The response from the server.
  53. """
  54. response = await self._make_request("POST", "logout") # type: ignore
  55. self.access_token = None
  56. self._refresh_token = None
  57. return response
  58. @deprecated("Use client.users.retrieve() instead")
  59. async def user(self) -> User:
  60. """
  61. Retrieves the user information for the currently authenticated user.
  62. Returns:
  63. User: The response from the server.
  64. """
  65. return await self._make_request("GET", "user") # type: ignore
  66. @deprecated("Use client.users.update() instead")
  67. async def update_user(
  68. self,
  69. user_id: Union[str, UUID],
  70. email: Optional[str] = None,
  71. is_superuser: Optional[bool] = None,
  72. name: Optional[str] = None,
  73. bio: Optional[str] = None,
  74. profile_picture: Optional[str] = None,
  75. ) -> User:
  76. """
  77. Updates the profile information for the currently authenticated user.
  78. Args:
  79. user_id (Union[str, UUID]): The ID of the user to update.
  80. email (str, optional): The updated email for the user.
  81. is_superuser (bool, optional): The updated superuser status for the user.
  82. name (str, optional): The updated name for the user.
  83. bio (str, optional): The updated bio for the user.
  84. profile_picture (str, optional): The updated profile picture URL for the user.
  85. Returns:
  86. User: The response from the server.
  87. """
  88. data = {
  89. "user_id": user_id,
  90. "email": email,
  91. "is_superuser": is_superuser,
  92. "name": name,
  93. "bio": bio,
  94. "profile_picture": profile_picture,
  95. }
  96. data = {k: v for k, v in data.items() if v is not None}
  97. return await self._make_request("PUT", "user", json=data) # type: ignore
  98. @deprecated("Use client.users.refresh_token() instead")
  99. async def refresh_access_token(self) -> dict[str, Token]:
  100. """
  101. Refreshes the access token for the currently authenticated user.
  102. Returns:
  103. dict[str, Token]: The access and refresh tokens from the server.
  104. """
  105. response = await self._make_request( # type: ignore
  106. "POST", "refresh_access_token", json=self._refresh_token
  107. )
  108. self.access_token = response["results"]["access_token"]["token"]
  109. self._refresh_token = response["results"]["refresh_token"]["token"]
  110. return response
  111. @deprecated("Use client.users.change_password() instead")
  112. async def change_password(
  113. self, current_password: str, new_password: str
  114. ) -> dict:
  115. """
  116. Changes the password of the currently authenticated user.
  117. Args:
  118. current_password (str): The current password of the user.
  119. new_password (str): The new password to set for the user.
  120. Returns:
  121. dict: The response from the server.
  122. """
  123. data = {
  124. "current_password": current_password,
  125. "new_password": new_password,
  126. }
  127. return await self._make_request("POST", "change_password", json=data) # type: ignore
  128. @deprecated("Use client.users.request_password_reset() instead")
  129. async def request_password_reset(self, email: str) -> dict:
  130. """
  131. Requests a password reset for the user with the given email.
  132. Args:
  133. email (str): The email of the user to request a password reset for.
  134. Returns:
  135. dict: The response from the server.
  136. """
  137. return await self._make_request( # type: ignore
  138. "POST", "request_password_reset", json=email
  139. )
  140. @deprecated("Use client.users.reset_password() instead")
  141. async def confirm_password_reset(
  142. self, reset_token: str, new_password: str
  143. ) -> dict:
  144. """
  145. Confirms a password reset for the user with the given reset token.
  146. Args:
  147. reset_token (str): The reset token to confirm the password reset with.
  148. new_password (str): The new password to set for the user.
  149. Returns:
  150. dict: The response from the server.
  151. """
  152. data = {"reset_token": reset_token, "new_password": new_password}
  153. return await self._make_request("POST", "reset_password", json=data) # type: ignore
  154. @deprecated("Use client.users.login_with_token() instead")
  155. async def login_with_token(
  156. self,
  157. access_token: str,
  158. ) -> dict[str, Token]:
  159. """
  160. Logs in a user using existing access and refresh tokens.
  161. Args:
  162. access_token (str): The existing access token.
  163. refresh_token (str): The existing refresh token.
  164. Returns:
  165. dict[str, Token]: The access and refresh tokens from the server.
  166. """
  167. self.access_token = access_token
  168. # Verify the tokens by making a request to the user endpoint
  169. try:
  170. await self._make_request("GET", "user") # type: ignore
  171. return {
  172. "access_token": Token(
  173. token=access_token, token_type="access_token"
  174. ),
  175. }
  176. except Exception:
  177. # If the request fails, clear the tokens and raise an exception
  178. self.access_token = None
  179. self._refresh_token = None
  180. raise ValueError("Invalid tokens provided")
  181. @deprecated("")
  182. async def get_user_verification_code(
  183. self, user_id: Union[str, UUID]
  184. ) -> dict:
  185. """
  186. Retrieves only the verification code for a specific user. Requires superuser access.
  187. Args:
  188. user_id (Union[str, UUID]): The ID of the user to get verification code for.
  189. Returns:
  190. dict: Contains verification code and its expiry date
  191. """
  192. return await self._make_request( # type: ignore
  193. "GET", f"user/{user_id}/verification_data"
  194. )
  195. async def get_user_reset_token(self, user_id: Union[str, UUID]) -> dict:
  196. """
  197. Retrieves only the verification code for a specific user. Requires superuser access.
  198. Args:
  199. user_id (Union[str, UUID]): The ID of the user to get verification code for.
  200. Returns:
  201. dict: Contains verification code and its expiry date
  202. """
  203. return await self._make_request( # type: ignore
  204. "GET", f"user/{user_id}/reset_token"
  205. )
  206. async def send_reset_email(self, email: str) -> dict:
  207. """
  208. Generates a new verification code and sends a reset email to the user.
  209. Args:
  210. email (str): The email address of the user to send the reset email to.
  211. Returns:
  212. dict: Contains verification code and message from the server.
  213. """
  214. return await self._make_request( # type: ignore
  215. "POST", "send_reset_email", json=email
  216. )