Configure authentication and security settings for MCPHub
# Basic auth credentials AUTH_USERNAME=admin AUTH_PASSWORD=your-secure-password # JWT settings JWT_SECRET=your-jwt-secret-key JWT_EXPIRES_IN=24h
{ "auth": { "provider": "database", "database": { "url": "postgresql://user:pass@localhost:5432/mcphub", "userTable": "users" } } }
# Via API curl -X POST http://localhost:3000/api/auth/users \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $ADMIN_TOKEN" \ -d '{ "username": "newuser", "email": "user@example.com", "password": "securepassword", "role": "user" }'
# Add user to group curl -X POST http://localhost:3000/api/groups/{groupId}/users \ -H "Authorization: Bearer $TOKEN" \ -d '{"userId": "user123"}'
{ "groupId": "dev-team", "permissions": { "servers": ["read", "write", "execute"], "tools": ["read", "execute"], "logs": ["read"], "config": ["read"] } }
// Login to get token const response = await fetch('/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: 'your-username', password: 'your-password', }), }); const { token } = await response.json(); // Use token for authenticated requests const serversResponse = await fetch('/api/servers', { headers: { Authorization: `Bearer ${token}` }, });
# Generate API key curl -X POST http://localhost:3000/api/auth/api-keys \ -H "Authorization: Bearer $ADMIN_TOKEN" \ -d '{ "name": "my-service", "permissions": ["servers:read", "tools:execute"] }' # Use API key curl -H "X-API-Key: your-api-key" \ http://localhost:3000/api/servers
# docker-compose.yml services: mcphub: environment: - HTTPS_ENABLED=true - SSL_CERT_PATH=/certs/cert.pem - SSL_KEY_PATH=/certs/key.pem volumes: - ./certs:/certs:ro
{ "cors": { "origin": ["https://your-frontend.com"], "credentials": true, "methods": ["GET", "POST", "PUT", "DELETE"] } }
{ "rateLimit": { "windowMs": 900000, "max": 100, "message": "Too many requests from this IP" } }
{ "session": { "secret": "your-session-secret", "cookie": { "secure": true, "httpOnly": true, "maxAge": 86400000 }, "store": "redis", "redis": { "host": "localhost", "port": 6379 } } }
// Logout endpoint app.post('/api/auth/logout', (req, res) => { req.session.destroy(); res.json({ message: 'Logged out successfully' }); });
// Log security events const auditLog = { event: 'login_attempt', user: username, ip: req.ip, userAgent: req.headers['user-agent'], success: true, timestamp: new Date(), };
# Check user exists and password is correct curl -X POST http://localhost:3000/api/auth/verify \ -d '{"username": "user", "password": "pass"}'
// Handle token refresh if (response.status === 401) { const newToken = await refreshToken(); // Retry request with new token }
# Check user permissions curl -H "Authorization: Bearer $TOKEN" \ http://localhost:3000/api/auth/permissions
DEBUG=mcphub:auth npm start
// React authentication hook const useAuth = () => { const [user, setUser] = useState(null); const login = async (credentials) => { const response = await fetch('/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(credentials), }); if (response.ok) { const userData = await response.json(); setUser(userData.user); return true; } return false; }; return { user, login }; };
// Express middleware const authMiddleware = (req, res, next) => { const token = req.headers.authorization?.split(' ')[1]; if (!token) { return res.status(401).json({ error: 'No token provided' }); } try { const decoded = jwt.verify(token, process.env.JWT_SECRET); req.user = decoded; next(); } catch (error) { res.status(401).json({ error: 'Invalid token' }); } };