""" Enhanced Dashboard with User Management UI =========================================== Provides a comprehensive web interface for managing users and API keys. Vue 3 compatible. """ import os import time import hashlib from starlette.requests import Request from starlette.responses import HTMLResponse, JSONResponse _STATIC_API_KEY = os.environ.get("GATEWAY_STATIC_API_KEY", "") def _admin_require_auth(request: Request): """Returns None if authorized, or a 401 JSONResponse if not.""" auth_header = request.headers.get("Authorization", "") if not auth_header.startswith("Bearer "): return JSONResponse({"error": "unauthorized"}, status_code=401) token = auth_header[7:] if _STATIC_API_KEY and token == _STATIC_API_KEY: return None try: from gateway_proxy import ACCESS_TOKENS token_hash = hashlib.sha256(token.encode()).hexdigest() info = ACCESS_TOKENS.get(token_hash) if info and info["expires_at"] >= time.time(): return None except ImportError: pass return JSONResponse({"error": "unauthorized"}, status_code=401) USER_DASHBOARD_HTML = """
Leave both empty = access to all MCPs. Toggle to explicitly allow or block.
POST /users
{ "username": "alice", "email": "alice@example.com", "description": "Engineering" }
POST /users/{username}/keys
{ "key_name": "my-key", "ttl_days": 90 }
=> { "api_key": "mcpgw_..." } ← save immediately, shown once
PUT /users/{username}/mcp-access
{ "allowed_mcps": ["erpnext", "wave"], "blocked_mcps": [] }
MCP Server URL: https://mcp.wilddragon.net/mcp Authorization: Bearer mcpgw_...