38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
from sqlalchemy.orm import Session
|
|
from app.models import SystemParam
|
|
from app.models.enums import ParamTypeEnum
|
|
import json
|
|
|
|
|
|
ACCESS_TOKEN_KEY = "security.access_token_minutes"
|
|
REFRESH_TOKEN_KEY = "security.refresh_token_minutes"
|
|
|
|
|
|
def parse_param_value(param: SystemParam):
|
|
if param.param_type == ParamTypeEnum.INT:
|
|
return int(param.param_value)
|
|
if param.param_type == ParamTypeEnum.BOOL:
|
|
return param.param_value.lower() in {"1", "true", "yes"}
|
|
if param.param_type == ParamTypeEnum.JSON:
|
|
return json.loads(param.param_value)
|
|
return param.param_value
|
|
|
|
|
|
def get_param_value(db: Session, key: str):
|
|
return db.query(SystemParam).filter(SystemParam.param_key == key).first()
|
|
|
|
|
|
def get_token_minutes(db: Session, default_access: int, default_refresh: int) -> tuple[int, int]:
|
|
access = get_param_value(db, ACCESS_TOKEN_KEY)
|
|
refresh = get_param_value(db, REFRESH_TOKEN_KEY)
|
|
|
|
access_minutes = default_access
|
|
refresh_minutes = default_refresh
|
|
|
|
if access:
|
|
access_minutes = int(parse_param_value(access))
|
|
if refresh:
|
|
refresh_minutes = int(parse_param_value(refresh))
|
|
|
|
return access_minutes, refresh_minutes
|