MCPcopy Index your code
hub / github.com/vastsa/FileCodeBox / create_token

Function create_token

apps/admin/dependencies.py:22–38  ·  view source on GitHub ↗

创建JWT token :param data: 数据负载 :param expires_in: 过期时间(秒)

(data: dict, expires_in: int = 3600 * 24 * 30)

Source from the content-addressed store, hash-verified

20
21
22def create_token(data: dict, expires_in: int = 3600 * 24 * 30) -> str:
23 """
24 创建JWT token
25 :param data: 数据负载
26 :param expires_in: 过期时间(秒)
27 """
28 header = base64.b64encode(
29 json.dumps({"alg": "HS256", "typ": "JWT"}).encode()
30 ).decode()
31 payload = base64.b64encode(
32 json.dumps({**data, "exp": int(time.time()) + expires_in}).encode()
33 ).decode()
34
35 signature = hmac.new(_get_jwt_secret(), f"{header}.{payload}".encode(), "sha256").digest()
36 signature = base64.b64encode(signature).decode()
37
38 return f"{header}.{payload}.{signature}"
39
40
41def verify_token(token: str) -> dict:

Callers 2

loginFunction · 0.90

Calls 1

_get_jwt_secretFunction · 0.85