secret generation on startup

This commit is contained in:
Jörn-Michael Miehe 2022-03-19 16:57:25 +00:00
parent f56c2fb19b
commit b38216a223

View file

@ -4,11 +4,12 @@ import functools
import json import json
from datetime import datetime, timedelta from datetime import datetime, timedelta
from enum import Enum from enum import Enum
from secrets import token_hex
from jose import JWTError, jwt from jose import JWTError, jwt
from jose.constants import ALGORITHMS from jose.constants import ALGORITHMS
from passlib.context import CryptContext from passlib.context import CryptContext
from pydantic import BaseModel, BaseSettings, Field from pydantic import BaseModel, BaseSettings, Field, validator
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.engine import Engine from sqlalchemy.engine import Engine
@ -70,6 +71,14 @@ class JWTConfig(BaseModel):
hash_algorithm: str = ALGORITHMS.HS256 hash_algorithm: str = ALGORITHMS.HS256
expiry_minutes: int = 30 expiry_minutes: int = 30
@validator("secret")
@classmethod
def ensure_secret(cls, value: str | None) -> str:
if value is None:
return token_hex(32)
return value
async def create_token( async def create_token(
self, self,
username: str, username: str,