| 11 | |
| 12 | |
| 13 | class AuthService: |
| 14 | def __init__( |
| 15 | self, |
| 16 | user_service: UserService, |
| 17 | hash_service: HashService, |
| 18 | app_email_service: AppMailService, |
| 19 | ) -> None: |
| 20 | self.user_service = user_service |
| 21 | self.hash_service = hash_service |
| 22 | self.app_email_service = app_email_service |
| 23 | |
| 24 | async def login(self, email: str, password: str) -> TokenBearer: |
| 25 | user = await self.user_service.one( |
| 26 | filters=[ |
| 27 | Filter("email", Oper.EQ, email), |
| 28 | ] |
| 29 | ) |
| 30 | if user is None: |
| 31 | raise UnauthorizedException(error_no=ErrorNo.USER_EMAIL_NOT_FOUND, message="Unauthorized!") |
| 32 | if user.status != UserStatus.ACTIVE: |
| 33 | raise UnauthorizedException(error_no=ErrorNo.USER_STATUS_NOT_ACTIVE, message="Unauthorized!") |
| 34 | |
| 35 | if not self.hash_service.verify_password(password=password, hashed_password=user.hash_password): |
| 36 | raise UnauthorizedException(error_no=ErrorNo.AUTHORIZATION_USER_PASSWORD_INVALID, message="Unauthorized!") |
| 37 | user = await self.user_service.update(uid=user.id, data={"session": self.hash_service.random_string()}) |
| 38 | |
| 39 | return self.hash_service.create_token_bearer(user=user) |
| 40 | |
| 41 | async def signup( |
| 42 | self, |
| 43 | first_name: str, |
| 44 | second_name: str, |
| 45 | email: str, |
| 46 | password: str, |
| 47 | ) -> User: |
| 48 | user = await self.user_service.one( |
| 49 | filters=[ |
| 50 | Filter("email", Oper.EQ, email), |
| 51 | ] |
| 52 | ) |
| 53 | if user is not None: |
| 54 | raise UnprocessableEntityException( |
| 55 | error_no=ErrorNo.USER_EMAIL_ALREADY_EXISTS, message="User already exists" |
| 56 | ) |
| 57 | |
| 58 | user = User( |
| 59 | first_name=first_name, |
| 60 | second_name=second_name, |
| 61 | email=email, |
| 62 | hash_password=self.hash_service.hash_password(password), |
| 63 | session=self.hash_service.random_string(), |
| 64 | status=UserStatus.PENDING, |
| 65 | roles=[Role.USER], |
| 66 | ) |
| 67 | user = await self.user_service.create(data=user) |
| 68 | await self.send_confirm_email(user) |
| 69 | return user |
| 70 |
nothing calls this directly
no outgoing calls
no test coverage detected