Skip to content

Commit 324a7d9

Browse files
committed
🔧 Refactored code to use encryption algorithm name from settings for consistency
1 parent 98bd7e6 commit 324a7d9

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

Diff for: ‎backend/app/api/deps.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from pydantic import ValidationError
88
from sqlmodel import Session
99

10-
from app.core import security
1110
from app.core.config import settings
1211
from app.core.db import engine
1312
from app.models import TokenPayload, User
@@ -29,7 +28,7 @@ def get_db() -> Generator[Session, None, None]:
2928
def get_current_user(session: SessionDep, token: TokenDep) -> User:
3029
try:
3130
payload = jwt.decode(
32-
token, settings.SECRET_KEY, algorithms=[security.ALGORITHM]
31+
token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]
3332
)
3433
token_data = TokenPayload(**payload)
3534
except (JWTError, ValidationError):

Diff for: ‎backend/app/core/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Settings(BaseSettings):
3333
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
3434
DOMAIN: str = "localhost"
3535
ENVIRONMENT: Literal["local", "staging", "production"] = "local"
36+
ALGORITHM: str = "HS256"
3637

3738
@computed_field # type: ignore[misc]
3839
@property

Diff for: ‎backend/app/core/security.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
1010

1111

12-
ALGORITHM = "HS256"
13-
14-
1512
def create_access_token(subject: str | Any, expires_delta: timedelta) -> str:
1613
expire = datetime.utcnow() + expires_delta
1714
to_encode = {"exp": expire, "sub": str(subject)}
18-
encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=ALGORITHM)
15+
encoded_jwt = jwt.encode(
16+
to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM
17+
)
1918
return encoded_jwt
2019

2120

Diff for: ‎backend/app/utils.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,16 @@ def generate_password_reset_token(email: str) -> str:
103103
encoded_jwt = jwt.encode(
104104
{"exp": exp, "nbf": now, "sub": email},
105105
settings.SECRET_KEY,
106-
algorithm="HS256",
106+
algorithm=settings.ALGORITHM,
107107
)
108108
return encoded_jwt
109109

110110

111111
def verify_password_reset_token(token: str) -> str | None:
112112
try:
113-
decoded_token = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"])
113+
decoded_token = jwt.decode(
114+
token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]
115+
)
114116
return str(decoded_token["sub"])
115117
except JWTError:
116118
return None

0 commit comments

Comments
 (0)