mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
# web/auth/security.py
|
|
import bcrypt
|
|
from datetime import datetime, timedelta
|
|
from jose import JWTError, jwt
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login")
|
|
|
|
# JWT 配置
|
|
SECRET_KEY = "9f24447481c6012c3ee0217fa252dd1692af0f67c5eab78b5c19eab87f196215" # 建议放到环境变量中
|
|
ALGORITHM = "HS256"
|
|
ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
|
|
|
def get_password_hash(password: str) -> str:
|
|
"""
|
|
生成密码哈希
|
|
获取密码hash
|
|
"""
|
|
# 生成盐并计算密码哈希
|
|
hashed = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
|
return hashed.decode('utf-8')
|
|
|
|
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
|
"""
|
|
校验密码
|
|
校验密码是否匹配
|
|
"""
|
|
return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))
|
|
|
|
def create_access_token(data: dict, expires_delta: timedelta = None) -> str:
|
|
"""
|
|
生成 JWT 令牌
|
|
"""
|
|
to_encode = data.copy()
|
|
if expires_delta:
|
|
expire = datetime.utcnow() + expires_delta
|
|
else:
|
|
expire = datetime.utcnow() + timedelta(minutes=15)
|
|
to_encode.update({"exp": expire})
|
|
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
|
return encoded_jwt
|
|
|
|
def decode_access_token(token: str) -> dict:
|
|
"""
|
|
解码 JWT 令牌并返回 payload 数据
|
|
"""
|
|
try:
|
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
|
return payload # payload 是一个字典,包含 "sub" 等数据
|
|
except JWTError:
|
|
return None # 令牌无效时返回 None
|