From 0e40d003382dec762ff6c4ae862d16d28bfa292e Mon Sep 17 00:00:00 2001 From: odumag99 Date: Thu, 9 Jan 2025 00:52:27 +0900 Subject: [PATCH] =?UTF-8?q?=ED=86=A0=ED=81=B0=EC=9D=B4=20=EC=A0=9C?= =?UTF-8?q?=EB=8C=80=EB=A1=9C=20=EB=A7=8C=EB=A3=8C=EB=90=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EB=8A=94=20=EB=AC=B8=EC=A0=9C=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- snuvote/app/user/service.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/snuvote/app/user/service.py b/snuvote/app/user/service.py index 37dd2f0..c9fb6ce 100644 --- a/snuvote/app/user/service.py +++ b/snuvote/app/user/service.py @@ -6,7 +6,7 @@ from snuvote.app.user.errors import InvalidUsernameOrPasswordError, NotAccessTokenError, NotRefreshTokenError, InvalidTokenError, ExpiredTokenError, BlockedRefreshTokenError import jwt -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from enum import Enum from uuid import uuid4 from dotenv import load_dotenv @@ -36,7 +36,7 @@ def get_user_by_userid(self, userid: str) -> User | None: def issue_tokens(self, userid: str) -> tuple[str, str]: access_payload = { "sub": userid, # 추후 성능 개선을 위해 payload에 단과대 등 추가 - "exp": datetime.now() + timedelta(hours=1), + "exp": datetime.now(timezone.utc) + timedelta(hours=1), "typ": TokenType.ACCESS.value, # "typ": "access" } access_token = jwt.encode(access_payload, SECRET, algorithm="HS256") @@ -44,7 +44,7 @@ def issue_tokens(self, userid: str) -> tuple[str, str]: refresh_payload = { "sub": userid, "jti": uuid4().hex, # 토큰의 고유 ID 생성 -> BlockedRefreshToken.token_id로 사용 - "exp": datetime.now() + timedelta(days=7), + "exp": datetime.now(timezone.utc) + timedelta(days=7), "typ": TokenType.REFRESH.value, # "typ": "refresh" } refresh_token = jwt.encode(refresh_payload, SECRET, algorithm="HS256") @@ -69,10 +69,10 @@ def validate_access_token(self, token: str) -> str: if payload["typ"] != TokenType.ACCESS.value: # payload["typ"] != "access" raise NotAccessTokenError() return payload["sub"] - except jwt.InvalidTokenError: - raise InvalidTokenError() except jwt.ExpiredSignatureError: raise ExpiredTokenError() + except jwt.InvalidTokenError: + raise InvalidTokenError() #리프레쉬토큰 검증 @@ -87,10 +87,10 @@ def validate_refresh_token(self, token: str) -> str: algorithms=["HS256"], options={"require": ["sub"]}, ) - except jwt.InvalidTokenError: - raise InvalidTokenError() except jwt.ExpiredSignatureError: raise ExpiredTokenError() + except jwt.InvalidTokenError: + raise InvalidTokenError() if payload["typ"] != TokenType.REFRESH.value: raise NotRefreshTokenError() if self.user_store.is_refresh_token_blocked(payload["jti"]):