Skip to content

Commit aaffb8a

Browse files
committed
feat(#10): move constants to environment variables
1 parent 1352378 commit aaffb8a

4 files changed

Lines changed: 20 additions & 9 deletions

File tree

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ POSTGRES_PORT=5432
1111

1212
POSTGRES_ECHO=true
1313
DATABASE_ENABLE_CONNECTION_POOLING=true
14+
15+
SECRET_KEY=
16+
ALGORITHM=
17+
ACCESS_TOKEN_EXPIRE_MINUTES=

api/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ class DatabaseConfig:
5858
}
5959

6060

61+
class AuthConfig:
62+
SECRET_KEY: str = os.getenv("SECRET_KEY")
63+
ALGORITHM: str = os.getenv("ALGORITHM")
64+
ACCESS_TOKEN_EXPIRE_MINUTES: int = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES"))
65+
66+
6167
class Config:
6268
"""Base configuration."""
6369

@@ -74,6 +80,7 @@ class Config:
7480
APPLICATION_ROOT = os.getenv("APPLICATION_ROOT", "")
7581

7682
DATABASE: DatabaseConfig = DatabaseConfig()
83+
AUTH: AuthConfig = AuthConfig()
7784

7885

7986
class TestConfig(Config):

api/entrypoints/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async def get_users() -> List[UserOut]:
2323
return await UserService().get_all()
2424

2525

26-
@authenticated_router.get("/me", response_model=UserOut)
26+
@router.get("/me", response_model=UserOut)
2727
async def read_user_me(
2828
current_user: Annotated[User, Depends(AuthService.get_current_active_user)]
2929
):

api/services/auth.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Annotated, Union
33

44
import jwt
5+
from api.config import Config
56
from api.database.models.users import User
67
from api.exceptions.http_exceptions import CredentialsException
78
from api.schemas.auth import TokenData
@@ -13,11 +14,6 @@
1314
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/token")
1415

1516

16-
SECRET_KEY = "ef555b4c8637c33623fe8e91ba7256725e7e2a1bcc75fe84acb189bcaa6c8693"
17-
ALGORITHM = "HS256"
18-
ACCESS_TOKEN_EXPIRE_MINUTES = 30
19-
20-
2117
class AuthService:
2218
@staticmethod
2319
def verify_password(plain_password, hashed_password):
@@ -42,16 +38,20 @@ def create_access_token(
4238
):
4339
to_encode = data.copy()
4440
expires_at = datetime.now(timezone.utc) + timedelta(
45-
minutes=expires_delta_in_minutes or ACCESS_TOKEN_EXPIRE_MINUTES
41+
minutes=expires_delta_in_minutes or Config.AUTH.ACCESS_TOKEN_EXPIRE_MINUTES
4642
)
4743
to_encode.update({"exp": expires_at})
48-
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
44+
encoded_jwt = jwt.encode(
45+
to_encode, Config.AUTH.SECRET_KEY, algorithm=Config.AUTH.ALGORITHM
46+
)
4947
return encoded_jwt, expires_at
5048

5149
# @staticmethod
5250
async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
5351
try:
54-
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
52+
payload = jwt.decode(
53+
token, Config.AUTH.SECRET_KEY, algorithms=[Config.AUTH.ALGORITHM]
54+
)
5555
username: Union[str, None] = payload.get("sub")
5656
if username is None:
5757
raise CredentialsException()

0 commit comments

Comments
 (0)