Files
goclaw/internal/http/openapi.go
T
Goon 75c570e951 feat(security): credentialed exec + HTTP RBAC + API key cache (#197)
- Secure CLI credential injection via AES-256-GCM encrypted env vars
- API key management with fine-grained RBAC scopes
- resolveAuth/requireAuth middleware across all 25+ HTTP handlers
- In-memory API key cache with TTL, negative caching, pubsub invalidation
- Sandbox-first execution (fails if unavailable, no silent fallback)
- Credential scrubbing, constant-time token comparison, Admin-only CLI creds
- SQL migration 000020: secure_cli_binaries + api_keys tables
- 14 unit tests for cache and RBAC with race detector

Closes #197
2026-03-15 20:13:18 +07:00

70 lines
2.0 KiB
Go

package http
import (
_ "embed"
"net/http"
)
//go:embed openapi_spec.json
var openapiSpec []byte
// DocsHandler serves the OpenAPI spec and Swagger UI.
type DocsHandler struct {
token string
}
// NewDocsHandler creates a handler for API documentation endpoints.
func NewDocsHandler(token string) *DocsHandler {
return &DocsHandler{token: token}
}
// RegisterRoutes registers documentation routes on the given mux.
func (h *DocsHandler) RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /v1/openapi.json", h.handleSpec)
mux.HandleFunc("GET /docs", h.handleSwaggerUI)
mux.HandleFunc("GET /docs/", h.handleSwaggerUI)
}
func (h *DocsHandler) handleSpec(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(openapiSpec)
}
func (h *DocsHandler) handleSwaggerUI(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(swaggerHTML))
}
const swaggerHTML = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GoClaw API Documentation</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.18.2/swagger-ui.css">
<style>
html { box-sizing: border-box; overflow-y: scroll; }
*, *:before, *:after { box-sizing: inherit; }
body { margin: 0; background: #fafafa; }
.topbar { display: none; }
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5.18.2/swagger-ui-bundle.js"></script>
<script>
SwaggerUIBundle({
url: "/v1/openapi.json",
dom_id: "#swagger-ui",
deepLinking: true,
presets: [SwaggerUIBundle.presets.apis, SwaggerUIBundle.SwaggerUIStandalonePreset],
layout: "BaseLayout",
defaultModelsExpandDepth: 2,
defaultModelExpandDepth: 2,
tryItOutEnabled: true,
});
</script>
</body>
</html>`