import logging import uuid import hmac import hashlib import time import jwt as pyjwt from . import config def encrypt_phone(phone): return hmac.new( config.PHONE_HMAC_KEY.encode("utf-8"), phone.encode("utf-8"), hashlib.sha256, ).hexdigest() def create_access_token(user_id): now = int(time.time()) payload = {"sub": str(user_id), "iat": now, "exp": now + config.JWT_TTL_SECONDS} return pyjwt.encode(payload, config.JWT_SECRET, algorithm=config.JWT_ALGORITHM) def decode_access_token(token): return pyjwt.decode(token, config.JWT_SECRET, algorithms=[config.JWT_ALGORITHM]) def create_session_token(): """生成写入 auth_sessions 的会话 token(长度适合 token VARCHAR(64))。""" return uuid.uuid4().hex + uuid.uuid4().hex[:16]