Skip to content

Commit 75b446b

Browse files
committed
Use secrets library to autogenerate JWT secret key for HS512 auth required length
1 parent 119ff5c commit 75b446b

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

src/config.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os, json, logging, sys
1+
import os, json, logging, sys, secrets
22
from dotenv import load_dotenv
33
from logging.handlers import TimedRotatingFileHandler
44
from urllib import parse
@@ -37,7 +37,10 @@ def __init__(self, environment="Development"):
3737
self.SECRET_KEY = config["SECRET_KEY"] or "you-will-never-guess"
3838
self.SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg://{os.environ.get('DB_USERNAME')}:{parse.quote_plus(os.environ.get('DB_PASSWORD'))}@{config['DB_HOST']}/library"
3939
self.POSTGRESQL_DATABASE_URI = f"postgresql://{os.environ.get('DB_USERNAME')}:{parse.quote_plus(os.environ.get('DB_PASSWORD'))}@{config['DB_HOST']}/library"
40-
self.JWT_SECRET_KEY = config["JWT_SECRET_KEY"]
40+
if "JWT_SECRET_KEY" in config and len(config["JWT_SECRET_KEY"]) >= 64:
41+
self.JWT_SECRET_KEY = config["JWT_SECRET_KEY"]
42+
else:
43+
self.JWT_SECRET_KEY = secrets.token_hex(64) # SHA512 requirement
4144
self.GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
4245
"""
4346
https://docs.python.org/3/library/logging.html

0 commit comments

Comments
 (0)