2026-01-19 00:09:36 +08:00
|
|
|
|
"""
|
2026-07-01 22:20:47 +08:00
|
|
|
|
安全相关功能:密码加密、JWT、Refresh Token
|
2026-01-19 00:09:36 +08:00
|
|
|
|
"""
|
2026-07-01 22:20:47 +08:00
|
|
|
|
import json
|
|
|
|
|
|
import uuid
|
2026-01-19 00:09:36 +08:00
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
from jose import JWTError, jwt
|
|
|
|
|
|
import bcrypt
|
|
|
|
|
|
from app.core.config import settings
|
|
|
|
|
|
|
2026-07-01 22:20:47 +08:00
|
|
|
|
# Redis client (lazy init)
|
|
|
|
|
|
_redis = None
|
|
|
|
|
|
|
|
|
|
|
|
def _get_redis():
|
|
|
|
|
|
global _redis
|
|
|
|
|
|
if _redis is None:
|
|
|
|
|
|
try:
|
|
|
|
|
|
from app.core.redis_client import get_redis_client
|
|
|
|
|
|
_redis = get_redis_client()
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
_redis = False
|
|
|
|
|
|
return _redis
|
|
|
|
|
|
|
|
|
|
|
|
# Refresh token TTL: 30 days
|
|
|
|
|
|
REFRESH_TOKEN_TTL_SEC = 30 * 24 * 3600 # 30 days
|
|
|
|
|
|
|
2026-01-19 00:09:36 +08:00
|
|
|
|
|
|
|
|
|
|
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
|
|
|
|
|
"""验证密码"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
# bcrypt限制密码长度最多72字节
|
|
|
|
|
|
password_bytes = plain_password.encode('utf-8')
|
|
|
|
|
|
if len(password_bytes) > 72:
|
|
|
|
|
|
password_bytes = password_bytes[:72]
|
|
|
|
|
|
return bcrypt.checkpw(password_bytes, hashed_password.encode('utf-8'))
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_password_hash(password: str) -> str:
|
|
|
|
|
|
"""获取密码哈希"""
|
|
|
|
|
|
# bcrypt限制密码长度最多72字节
|
|
|
|
|
|
password_bytes = password.encode('utf-8')
|
|
|
|
|
|
if len(password_bytes) > 72:
|
|
|
|
|
|
password_bytes = password_bytes[:72]
|
2026-07-01 22:20:47 +08:00
|
|
|
|
|
2026-01-19 00:09:36 +08:00
|
|
|
|
# 生成盐并哈希密码
|
|
|
|
|
|
salt = bcrypt.gensalt()
|
|
|
|
|
|
hashed = bcrypt.hashpw(password_bytes, salt)
|
|
|
|
|
|
return hashed.decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
|
|
|
|
|
|
"""创建访问令牌"""
|
|
|
|
|
|
to_encode = data.copy()
|
|
|
|
|
|
if expires_delta:
|
|
|
|
|
|
expire = datetime.utcnow() + expires_delta
|
|
|
|
|
|
else:
|
|
|
|
|
|
expire = datetime.utcnow() + timedelta(minutes=settings.JWT_ACCESS_TOKEN_EXPIRE_MINUTES)
|
2026-07-01 22:20:47 +08:00
|
|
|
|
|
2026-01-19 00:09:36 +08:00
|
|
|
|
to_encode.update({"exp": expire})
|
|
|
|
|
|
encoded_jwt = jwt.encode(to_encode, settings.JWT_SECRET_KEY or settings.SECRET_KEY, algorithm=settings.JWT_ALGORITHM)
|
|
|
|
|
|
return encoded_jwt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def decode_access_token(token: str) -> Optional[dict]:
|
|
|
|
|
|
"""解码访问令牌"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
payload = jwt.decode(token, settings.JWT_SECRET_KEY or settings.SECRET_KEY, algorithms=[settings.JWT_ALGORITHM])
|
|
|
|
|
|
return payload
|
|
|
|
|
|
except JWTError:
|
|
|
|
|
|
return None
|
2026-07-01 22:20:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ─── Refresh Token ──────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
def create_refresh_token(user_id: str, username: str, workspace_id: str = "") -> str:
|
|
|
|
|
|
"""创建 refresh token,存储到 Redis,返回 token 字符串。"""
|
|
|
|
|
|
token = str(uuid.uuid4())
|
|
|
|
|
|
data = {
|
|
|
|
|
|
"user_id": user_id,
|
|
|
|
|
|
"username": username,
|
|
|
|
|
|
"ws": workspace_id,
|
|
|
|
|
|
"created_at": datetime.utcnow().isoformat(),
|
|
|
|
|
|
}
|
|
|
|
|
|
r = _get_redis()
|
|
|
|
|
|
if r:
|
|
|
|
|
|
r.setex(f"refresh_token:{token}", REFRESH_TOKEN_TTL_SEC, json.dumps(data))
|
|
|
|
|
|
return token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def verify_refresh_token(token: str) -> Optional[dict]:
|
|
|
|
|
|
"""验证 refresh token,返回存储的数据或 None。"""
|
|
|
|
|
|
r = _get_redis()
|
|
|
|
|
|
if not r:
|
|
|
|
|
|
return None
|
|
|
|
|
|
raw = r.get(f"refresh_token:{token}")
|
|
|
|
|
|
if not raw:
|
|
|
|
|
|
return None
|
|
|
|
|
|
try:
|
|
|
|
|
|
return json.loads(raw)
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def revoke_refresh_token(token: str):
|
|
|
|
|
|
"""撤销 refresh token。"""
|
|
|
|
|
|
r = _get_redis()
|
|
|
|
|
|
if r:
|
|
|
|
|
|
r.delete(f"refresh_token:{token}")
|