mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-25 08:33:53 +08:00
refactor(connection): 合并旧逻辑新的业务逻辑代码
This commit is contained in:
@@ -12,8 +12,9 @@ class AuthenticationError(Exception):
|
||||
|
||||
class AuthMiddleware:
|
||||
"""
|
||||
认证中间件(兼容旧版命名)
|
||||
认证中间件
|
||||
用于 WebSocket/MQTT 连接认证
|
||||
集成 AuthManager 的 token 验证逻辑,支持多种认证方式
|
||||
"""
|
||||
|
||||
def __init__(self, config: dict):
|
||||
@@ -24,18 +25,34 @@ class AuthMiddleware:
|
||||
config: 配置字典,包含认证相关配置
|
||||
"""
|
||||
self.config = config
|
||||
auth_config = config.get("server", {}).get("auth", {})
|
||||
server_config = config.get("server", {})
|
||||
auth_config = server_config.get("auth", {})
|
||||
|
||||
self.enabled = auth_config.get("enabled", False)
|
||||
self.tokens = auth_config.get("tokens", [])
|
||||
self.allowed_devices = auth_config.get("allowed_devices", [])
|
||||
self.allowed_devices = set(auth_config.get("allowed_devices", []))
|
||||
|
||||
# 获取 auth_key 用于 HMAC token 验证
|
||||
self.auth_key = server_config.get("auth_key", "")
|
||||
expire_seconds = auth_config.get("expire_seconds", None)
|
||||
|
||||
# 创建 AuthManager 实例用于 HMAC token 验证
|
||||
if self.auth_key:
|
||||
self._auth_manager = AuthManager(
|
||||
secret_key=self.auth_key,
|
||||
expire_seconds=expire_seconds
|
||||
)
|
||||
else:
|
||||
self._auth_manager = None
|
||||
|
||||
def authenticate(self, device_id: str, token: str = None) -> bool:
|
||||
def authenticate(self, device_id: str, token: str = None, client_id: str = None) -> bool:
|
||||
"""
|
||||
验证设备认证
|
||||
验证设备认证(同步方法)
|
||||
|
||||
Args:
|
||||
device_id: 设备 ID
|
||||
token: 认证令牌
|
||||
token: 认证令牌(可以是静态 token 或 HMAC token)
|
||||
client_id: 客户端 ID(用于 HMAC token 验证)
|
||||
|
||||
Returns:
|
||||
bool: 认证是否通过
|
||||
@@ -43,18 +60,89 @@ class AuthMiddleware:
|
||||
if not self.enabled:
|
||||
return True
|
||||
|
||||
# 检查白名单
|
||||
if device_id in self.allowed_devices:
|
||||
# 1. 检查白名单
|
||||
if device_id and device_id in self.allowed_devices:
|
||||
return True
|
||||
|
||||
# 检查 token
|
||||
# 2. 检查静态 token
|
||||
if token:
|
||||
# 移除 Bearer 前缀(如果有)
|
||||
if token.startswith("Bearer "):
|
||||
token = token[7:]
|
||||
|
||||
for token_config in self.tokens:
|
||||
if token_config.get("token") == token:
|
||||
return True
|
||||
|
||||
# 3. 检查 HMAC token(需要 AuthManager)
|
||||
if token and self._auth_manager and client_id and device_id:
|
||||
if self._auth_manager.verify_token(token, client_id, device_id):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
async def authenticate_async(self, headers: dict) -> bool:
|
||||
"""
|
||||
从 headers 中提取信息并进行异步认证
|
||||
|
||||
Args:
|
||||
headers: HTTP 请求头字典
|
||||
|
||||
Returns:
|
||||
bool: 认证是否通过
|
||||
|
||||
Raises:
|
||||
AuthenticationError: 认证失败时抛出
|
||||
"""
|
||||
if not self.enabled:
|
||||
return True
|
||||
|
||||
device_id = headers.get("device-id")
|
||||
client_id = headers.get("client-id")
|
||||
authorization = headers.get("authorization", "")
|
||||
|
||||
# 提取 token
|
||||
token = None
|
||||
if authorization:
|
||||
if authorization.startswith("Bearer "):
|
||||
token = authorization[7:]
|
||||
else:
|
||||
token = authorization
|
||||
|
||||
# 执行认证
|
||||
if self.authenticate(device_id, token, client_id):
|
||||
return True
|
||||
|
||||
raise AuthenticationError(f"认证失败: device_id={device_id}")
|
||||
|
||||
def authenticate_websocket(self, websocket) -> bool:
|
||||
"""
|
||||
WebSocket 连接认证
|
||||
|
||||
Args:
|
||||
websocket: WebSocket 连接对象
|
||||
|
||||
Returns:
|
||||
bool: 认证是否通过
|
||||
"""
|
||||
if not self.enabled:
|
||||
return True
|
||||
|
||||
headers = dict(websocket.request.headers)
|
||||
device_id = headers.get("device-id")
|
||||
client_id = headers.get("client-id")
|
||||
authorization = headers.get("authorization", "")
|
||||
|
||||
# 提取 token
|
||||
token = None
|
||||
if authorization:
|
||||
if authorization.startswith("Bearer "):
|
||||
token = authorization[7:]
|
||||
else:
|
||||
token = authorization
|
||||
|
||||
return self.authenticate(device_id, token, client_id)
|
||||
|
||||
|
||||
class AuthManager:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user