|
1 | 1 | from datetime import datetime, timedelta |
2 | 2 | from typing import Any |
3 | 3 |
|
4 | | -from fastapi import HTTPException, Request, Response, status |
| 4 | +from fastapi import Request, Response |
5 | 5 | from jwt import decode, encode |
6 | 6 | from jwt.exceptions import ExpiredSignatureError, InvalidTokenError |
7 | 7 |
|
8 | 8 | from app.config import settings |
| 9 | +from app.exceptions import ( |
| 10 | + ExpiredTokenException, |
| 11 | + InvalidTokenException, |
| 12 | + NotAuthenticatedException, |
| 13 | +) |
9 | 14 |
|
10 | 15 |
|
11 | 16 | async def set_auth_cookies(resp: Response, access_token: str, refresh_token: str) -> None: |
@@ -57,11 +62,11 @@ def __init__(self, cookie_name: str = "access_token"): |
57 | 62 | async def __call__(self, request: Request) -> dict[str, Any]: |
58 | 63 | token = request.cookies.get(self.cookie_name) |
59 | 64 | if not token: |
60 | | - raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Not authenticated") |
| 65 | + raise NotAuthenticatedException() |
61 | 66 | try: |
62 | 67 | payload = decode(token, settings.JWT_ACCESS_SECRET_KEY, algorithms=[settings.ALGORITHM]) |
63 | 68 | except ExpiredSignatureError as err: |
64 | | - raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Token expired") from err |
| 69 | + raise ExpiredTokenException() from err |
65 | 70 | except InvalidTokenError as err: |
66 | | - raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token") from err |
| 71 | + raise InvalidTokenException() from err |
67 | 72 | return payload |
0 commit comments