22 lines
587 B
Python
22 lines
587 B
Python
from datetime import datetime, timedelta
|
|
import jwt
|
|
|
|
from src.utils.config import get
|
|
|
|
SECRET_KEY = get('jwt_secret')
|
|
ALGORITHM = "HS256"
|
|
ACCESS_TOKEN_EXPIRE_MINUTES = 6000
|
|
|
|
def create_access_token(data: dict, expires_delta: timedelta | None = None) -> str:
|
|
to_encode = data.copy()
|
|
|
|
if "sub" in to_encode:
|
|
to_encode["sub"] = str(to_encode["sub"])
|
|
|
|
expire = datetime.utcnow() + (expires_delta or timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
|
|
to_encode.update({"exp": expire})
|
|
|
|
token = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
|
return token
|
|
|