feat(python): add jwt token lifetime from config

This commit is contained in:
sshakndr
2025-01-31 12:47:44 +07:00
committed by Sergey Linnik
parent 3102f4835c
commit 3c4b2e2877
3 changed files with 8 additions and 0 deletions

View File

@ -92,6 +92,9 @@ class ConfigurationManager:
def jwt_header(self) -> str:
return environ.get('JWT_HEADER') or 'Authorization'
def jwt_expires_in(self) -> int:
return int(environ.get('JWT_EXPIRES_IN', 5))
def jwt_use_for_request(self) -> bool:
use = environ.get('JWT_USE_FOR_REQUEST')
return string.boolean(use, True)

View File

@ -16,6 +16,7 @@
"""
import time
import jwt
from src.configuration import ConfigurationManager
@ -34,6 +35,9 @@ def useForRequest():
# encode a payload object into a token using a secret key and decodes it into the utf-8 format
def encode(payload):
now = time.time()
payload['iat'] = now
payload['exp'] = now + config_manager.jwt_expires_in() * 60
return jwt.encode(payload, config_manager.jwt_secret(), algorithm='HS256')