|
1 | | -import jwt |
2 | 1 | from fastapi_users import BaseUserManager, IntegerIDMixin |
3 | | -from fastapi_users.jwt import decode_jwt |
4 | | -from fastapi_users import exceptions |
5 | 2 |
|
6 | | -from .exceptions import PasswordNotValidError, UserNotVerified |
| 3 | +from .mixins import AuthenticationUserManagerMixin, PasswordValidationMixin |
7 | 4 | from config.models import User |
8 | 5 | from config import settings |
9 | 6 |
|
10 | 7 |
|
11 | | -class UserManager(IntegerIDMixin, BaseUserManager[User, int]): |
| 8 | +class UserManager(AuthenticationUserManagerMixin, |
| 9 | + PasswordValidationMixin, |
| 10 | + IntegerIDMixin, |
| 11 | + BaseUserManager[User, int]): |
12 | 12 | """ |
13 | 13 | UserManager для работы с пользователем |
14 | 14 | """ |
15 | 15 |
|
16 | 16 | verification_token_secret = settings.SECRET |
17 | | - |
18 | | - async def validate_password(self, password, user): |
19 | | - if not len(password) > 7: |
20 | | - raise PasswordNotValidError('Password is not valid') |
21 | | - |
22 | | - async def authenticate_user(self, token: str | None): |
23 | | - if not token: |
24 | | - return |
25 | | - try: |
26 | | - data = decode_jwt( |
27 | | - token, |
28 | | - self.verification_token_secret, |
29 | | - [self.verification_token_audience], |
30 | | - ) |
31 | | - except jwt.PyJWTError: |
32 | | - raise exceptions.InvalidVerifyToken() |
33 | | - |
34 | | - try: |
35 | | - user_id = data["sub"] |
36 | | - email = data["email"] |
37 | | - except KeyError: |
38 | | - raise exceptions.InvalidVerifyToken() |
39 | | - |
40 | | - try: |
41 | | - user = await self.get_by_email(email) |
42 | | - except exceptions.UserNotExists: |
43 | | - return |
44 | | - |
45 | | - try: |
46 | | - parsed_id = self.parse_id(user_id) |
47 | | - except exceptions.InvalidID: |
48 | | - raise exceptions.InvalidVerifyToken() |
49 | | - |
50 | | - if parsed_id != user.id: |
51 | | - raise exceptions.InvalidVerifyToken() |
52 | | - |
53 | | - if not user.is_verified: |
54 | | - raise UserNotVerified() |
55 | | - |
56 | | - return user |
| 17 | + reset_password_token_lifetime_seconds = settings.RESET_LIFESPAN_TOKEN_SECONDS |
0 commit comments