Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit 9bf8de0

Browse files
committed
Make the Secret key an env variable
1 parent a0f1015 commit 9bf8de0

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

api/routes/security.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
from pydantic import BaseModel
1414
from sqlalchemy import select
1515

16+
import dotenv
17+
dotenv.load_dotenv()
18+
1619
import api.schemas as schemas
1720
import api.database as db
1821

19-
# to get a string like this run:
20-
# openssl rand -hex 32
21-
SECRET_KEY = "11937be5daeb452985fc2d4f8ab09841d2fa45f48d72960b470d52fd84f4088e"
22+
2223
ALGORITHM = "HS256"
2324
ACCESS_TOKEN_EXPIRE_MINUTES = 60
2425

@@ -111,7 +112,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
111112
headers={"WWW-Authenticate": "Bearer"},
112113
)
113114
try:
114-
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
115+
payload = jwt.decode(token, os.environ['SECRET_KEY'], algorithms=[os.environ['JWT_ENCRYPTION_ALGORITHM']])
115116
sub: str = payload.get("sub")
116117
if sub is None:
117118
raise credentials_exception
@@ -131,7 +132,7 @@ def create_access_token(data: dict, expires_delta: timedelta | None = None):
131132
else:
132133
expire = datetime.utcnow() + timedelta(minutes=15)
133134
to_encode.update({"exp": expire})
134-
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
135+
encoded_jwt = jwt.encode(to_encode, os.environ['SECRET_KEY'], algorithm=os.environ['JWT_ENCRYPTION_ALGORITHM'])
135136
return encoded_jwt
136137

137138

0 commit comments

Comments
 (0)