|
| 1 | +from datetime import datetime |
| 2 | +from datetime import timedelta |
| 3 | +from typing import Annotated |
| 4 | + |
| 5 | +from authlib.jose import jwt |
| 6 | +from authlib.jose import JWTClaims |
| 7 | +from authlib.jose.errors import JoseError |
| 8 | +from fastapi import Depends |
| 9 | + |
| 10 | +from wacruit.src.apps.auth.exceptions import InvalidTokenException |
| 11 | +from wacruit.src.apps.auth.exceptions import UserNotFoundException |
| 12 | +from wacruit.src.apps.auth.repositories import AuthRepository |
| 13 | +from wacruit.src.apps.common.security import get_token_secret |
| 14 | +from wacruit.src.apps.common.security import PasswordService |
| 15 | +from wacruit.src.apps.user.models import User |
| 16 | + |
| 17 | + |
| 18 | +class AuthService: |
| 19 | + def __init__( |
| 20 | + self, |
| 21 | + auth_repository: Annotated[AuthRepository, Depends()], |
| 22 | + token_secret: Annotated[str, Depends(get_token_secret)], |
| 23 | + ) -> None: |
| 24 | + self.auth_repository = auth_repository |
| 25 | + self.token_secret = token_secret |
| 26 | + |
| 27 | + def get_user_by_id(self, user_id: int) -> User | None: |
| 28 | + return self.auth_repository.get_user_by_id(user_id) |
| 29 | + |
| 30 | + def login(self, username: str, password: str) -> tuple[str, str]: |
| 31 | + user = self.auth_repository.get_user_by_username(username) |
| 32 | + |
| 33 | + if user is None: |
| 34 | + raise UserNotFoundException() |
| 35 | + if user.password is None: |
| 36 | + raise UserNotFoundException() |
| 37 | + |
| 38 | + if PasswordService.verify_password(password, user.password): |
| 39 | + access_token = self.issue_token(user.id, 24, "access") |
| 40 | + refresh_token = self.issue_token(user.id, 24 * 7, "refresh") |
| 41 | + return (access_token, refresh_token) |
| 42 | + raise UserNotFoundException() |
| 43 | + |
| 44 | + def refresh_token(self, refresh_token: str) -> tuple[str, str]: |
| 45 | + decoded_token = self.decode_token(refresh_token) |
| 46 | + if decoded_token["token_type"] != "refresh": |
| 47 | + raise UserNotFoundException() |
| 48 | + |
| 49 | + user_id = decoded_token["sub"] |
| 50 | + user = self.auth_repository.get_user_by_id(user_id) |
| 51 | + |
| 52 | + if self.auth_repository.is_blocked_token(refresh_token): |
| 53 | + raise UserNotFoundException() |
| 54 | + if user is None: |
| 55 | + raise UserNotFoundException() |
| 56 | + |
| 57 | + self.block_token(refresh_token) |
| 58 | + access_token = self.issue_token(user.id, 24, "access") |
| 59 | + new_refresh_token = self.issue_token(user.id, 24 * 7, "refresh") |
| 60 | + return (access_token, new_refresh_token) |
| 61 | + |
| 62 | + def block_token(self, token: str) -> None: |
| 63 | + if self.auth_repository.is_blocked_token(token): |
| 64 | + raise InvalidTokenException() |
| 65 | + |
| 66 | + self.auth_repository.block_token(token) |
| 67 | + |
| 68 | + def decode_token(self, token: str) -> JWTClaims: |
| 69 | + try: |
| 70 | + claims = jwt.decode(token, key=self.token_secret) |
| 71 | + claims.validate() |
| 72 | + return claims |
| 73 | + except JoseError as e: |
| 74 | + raise InvalidTokenException() from e |
| 75 | + |
| 76 | + def issue_token(self, user_id: int, expiration_hour: int, token_type: str) -> str: |
| 77 | + header = {"alg": "HS256"} |
| 78 | + payload = { |
| 79 | + "sub": user_id, |
| 80 | + "exp": int((datetime.now() + timedelta(hours=expiration_hour)).timestamp()), |
| 81 | + "token_type": token_type, |
| 82 | + } |
| 83 | + |
| 84 | + return jwt.encode(header, payload, key=self.token_secret).decode("utf-8") |
0 commit comments