|
1 | | -from typing import ClassVar, Self |
| 1 | +import uuid |
| 2 | +from datetime import UTC, datetime, timedelta |
| 3 | +from typing import Any, ClassVar |
2 | 4 |
|
3 | | -from pydantic import EmailStr, HttpUrl, model_validator |
| 5 | +import jwt |
| 6 | +from pydantic import EmailStr, HttpUrl |
4 | 7 | from sqlalchemy import Result, Select, select |
5 | 8 | from sqlalchemy.exc import NoResultFound |
6 | 9 |
|
7 | 10 | from futuramaapi.core import feature_flags, settings |
8 | 11 | from futuramaapi.helpers.pydantic import BaseModel |
9 | | -from futuramaapi.mixins.pydantic import TemplateBodyMixin |
10 | 12 | from futuramaapi.repositories.models import UserModel |
11 | | -from futuramaapi.routers.rest.tokens.schemas import DecodedUserToken |
12 | 13 | from futuramaapi.routers.services import BaseSessionService |
13 | 14 |
|
14 | 15 |
|
15 | 16 | class RequestChangeUserPasswordRequest(BaseModel): |
16 | 17 | email: EmailStr |
17 | 18 |
|
18 | 19 |
|
19 | | -class PasswordResetBody(BaseModel, TemplateBodyMixin): |
| 20 | +class RequestChangeUserPasswordService(BaseSessionService[None]): |
| 21 | + request_data: RequestChangeUserPasswordRequest |
| 22 | + |
20 | 23 | expiration_time: ClassVar[int] = 15 * 60 |
21 | 24 |
|
22 | 25 | @property |
23 | | - def signature(self) -> str: |
24 | | - return DecodedUserToken( |
25 | | - type="access", |
26 | | - user={ |
27 | | - "id": self.user.id, |
| 26 | + def _user_statement(self) -> Select[tuple[UserModel]]: |
| 27 | + return select(UserModel).where(UserModel.email == self.request_data.email) |
| 28 | + |
| 29 | + def _get_signature( |
| 30 | + self, |
| 31 | + user: UserModel, |
| 32 | + /, |
| 33 | + *, |
| 34 | + algorithm: str = "HS256", |
| 35 | + ) -> str: |
| 36 | + return jwt.encode( |
| 37 | + { |
| 38 | + "exp": datetime.now(UTC) + timedelta(seconds=self.expiration_time), |
| 39 | + "nonce": uuid.uuid4().hex, |
| 40 | + "type": "access", |
| 41 | + "user": { |
| 42 | + "id": user.id, |
| 43 | + }, |
28 | 44 | }, |
29 | | - ).tokenize(self.expiration_time) |
| 45 | + settings.secret_key.get_secret_value(), |
| 46 | + algorithm=algorithm, |
| 47 | + ) |
30 | 48 |
|
31 | | - @model_validator(mode="after") |
32 | | - def build_confirmation_url(self) -> Self: |
33 | | - self.url = HttpUrl.build( |
34 | | - scheme=self.url.scheme, |
35 | | - host=self.url.host, |
| 49 | + def _get_confirmation_url(self, user: UserModel, /) -> str: |
| 50 | + url: HttpUrl = HttpUrl.build( |
| 51 | + scheme="https", |
| 52 | + host=settings.trusted_host, |
36 | 53 | path="passwords/change", |
37 | | - query=f"sig={self.signature}", |
| 54 | + query=f"sig={self._get_signature(user)}", |
38 | 55 | ) |
39 | | - return self |
40 | | - |
| 56 | + return str(url) |
41 | 57 |
|
42 | | -class RequestChangeUserPasswordService(BaseSessionService[None]): |
43 | | - request_data: RequestChangeUserPasswordRequest |
44 | | - |
45 | | - @property |
46 | | - def _user_statement(self) -> Select[tuple[UserModel]]: |
47 | | - return select(UserModel).where(UserModel.email == self.request_data.email) |
| 58 | + def _get_template_body(self, user: UserModel, /) -> dict[str, Any]: |
| 59 | + return { |
| 60 | + "user": { |
| 61 | + "id": user.id, |
| 62 | + "name": user.name, |
| 63 | + "surname": user.surname, |
| 64 | + }, |
| 65 | + "url": self._get_confirmation_url(user), |
| 66 | + } |
48 | 67 |
|
49 | 68 | async def process(self, *args, **kwargs) -> None: |
50 | 69 | if not feature_flags.activate_users: |
51 | 70 | return |
52 | 71 |
|
53 | 72 | result: Result[tuple[UserModel]] = await self.session.execute(self._user_statement) |
54 | 73 | try: |
55 | | - user_model: UserModel = result.scalars().one() |
| 74 | + user: UserModel = result.scalars().one() |
56 | 75 | except NoResultFound: |
57 | 76 | return |
58 | 77 |
|
59 | 78 | await settings.email.send( |
60 | | - [user_model.email], |
| 79 | + [user.email], |
61 | 80 | "FuturamaAPI - Password Reset", |
62 | | - PasswordResetBody.model_validate( |
63 | | - { |
64 | | - "user": { |
65 | | - "id": user_model.id, |
66 | | - "name": user_model.name, |
67 | | - "surname": user_model.surname, |
68 | | - }, |
69 | | - } |
70 | | - ), |
| 81 | + self._get_template_body(user), |
71 | 82 | "emails/password_reset.html", |
72 | 83 | ) |
0 commit comments