L-Nafaryus 997f37d5ee
new: reconstruct project
new: pdm package manager (python)
new: workspace for three subprojects
new: dream2nix module for packaging
new: postgresql and redis images
more: and more
2024-06-17 19:52:24 +05:00

28 lines
698 B
Python

from typing import Self
import jwt
from pydantic import BaseModel
import time
import datetime
class TokenClaims(BaseModel):
sub: str
exp: int
iat: int
@staticmethod
def create(sub: str, secret: str, duration: int) -> str:
now = datetime.datetime.now()
iat = now.timestamp()
exp = (now + datetime.timedelta(seconds = duration)).timestamp()
claims = TokenClaims(sub = sub, exp = int(exp), iat = int(iat))
return jwt.encode(claims.model_dump(), secret)
@staticmethod
def verify(token: str, secret: str) -> Self:
data = jwt.decode(token, secret, algorithms = ["HS256"])
return TokenClaims(**data)