#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 管理员API处理器 提供认证相关API(无 keys 管理) """ import json import time from core.api_auth import APIAuthManager, require_auth class AdminAPI: """管理员API处理器""" def __init__(self, config: dict): self.config = config # 复用共享认证管理器(router 注入的 config['_auth_manager']), # 避免多实例各自持有会话表导致状态分裂 self.auth_manager = config.get('_auth_manager') or APIAuthManager(config) if config.get('_auth_manager') is None: config['_auth_manager'] = self.auth_manager def handle_request(self, handler, method, path, query_params): """处理管理员API请求""" # 解析路径 parts = path.strip('/').split('/') # 根路径 - 列出API if len(parts) == 0 or parts[0] == '': self._api_overview(handler) return action = parts[0] if action == 'auth': self._handle_auth(handler, method, query_params) elif action == 'sessions': self._handle_sessions(handler, method, parts[1:] if len(parts) > 1 else []) elif action == 'stats': self._handle_stats(handler) else: handler.send_json_response({ "error": f"Unknown admin action: {action}", "available_actions": ["auth", "sessions", "stats"] }, 404) def _api_overview(self, handler): """API概览""" handler.send_json_response({ "name": "HYC Admin API", "version": "1.0", "description": "管理员认证API", "endpoints": { "GET /api/v2/admin/sessions": "列出活跃会话", "DELETE /api/v2/admin/sessions/{session_id}": "销毁会话", "POST /api/v2/admin/auth/verify": "验证认证状态", "GET /api/v2/admin/stats": "获取认证统计" }, "authentication": { "methods": [ "Authorization: Bearer ", "X-API-Key: ", "Cookie: hyc_auth=", "?key=" ] } }) def _handle_auth(self, handler, method, query_params): """处理认证相关""" # POST /api/v2/admin/auth/verify - 验证当前认证状态 if method == 'POST': auth_result = handler.auth_result if hasattr(handler, 'auth_result') else {} if auth_result.get('authenticated'): handler.send_json_response({ "authenticated": True, "level": auth_result.get('level'), "key_id": auth_result.get('key_id'), "name": auth_result.get('name'), "permissions": auth_result.get('permissions', []) }) else: handler.send_json_response({ "authenticated": False }, 401) else: handler.send_json_response({"error": "Invalid method"}, 405) def _handle_sessions(self, handler, method, parts): """处理会话管理""" if method == 'GET': # 列出活跃会话(auth_manager 内部加锁遍历) sessions = self.auth_manager.list_sessions() handler.send_json_response({ "sessions": sessions, "count": len(sessions) }) elif method == 'DELETE' and len(parts) >= 1 and parts[0]: session_id = parts[0] success = self.auth_manager.destroy_session(session_id) if success: handler.send_json_response({ "success": True, "message": f"Session {session_id} destroyed" }) else: handler.send_json_response({"error": "Session not found"}, 404) else: handler.send_json_response({"error": "Invalid request"}, 400) def _handle_stats(self, handler): """获取认证统计""" stats = self.auth_manager.get_stats() handler.send_json_response(stats)