Documentation
¶
Index ¶
- func APIKeyMiddleware(db *gorm.DB) func(http.Handler) http.Handler
- func AdminAuthMiddleware(secretKey string) func(http.Handler) http.Handler
- func CORSMiddleware(allowAll bool) func(http.Handler) http.Handler
- func FailedAuthRateLimitMiddleware(requestsPerMinute int) func(http.Handler) http.Handler
- func GetAPIKeyFromContext(r *http.Request) *models.APIKey
- func GetAdminUsernameFromContext(r *http.Request) string
- func MCPAuthMiddleware(db *gorm.DB) func(http.Handler) http.Handler
- func MCPEnabledMiddleware(db *gorm.DB) func(http.Handler) http.Handler
- func MCPHandler(db *gorm.DB, version string) http.Handler
- func MCPTokenFromContext(ctx context.Context) *models.MCPToken
- func MaxBodyBytesMiddleware(limit int64) func(http.Handler) http.Handler
- func NewRouter(db *gorm.DB, secretKey string, cfg RouterConfig) http.Handler
- func PerKeyRateLimitMiddleware() func(http.Handler) http.Handler
- func RateLimitMiddleware(requestsPerMinute int) func(http.Handler) http.Handler
- func RequestLogMiddleware(logger *slog.Logger, logClientIP bool) func(http.Handler) http.Handler
- func SecurityHeadersMiddleware(enableHSTS bool) func(http.Handler) http.Handler
- func SourceAndKeyRateLimitMiddleware(requestsPerMinute int) func(http.Handler) http.Handler
- type AdminHandler
- func (h *AdminHandler) ChangePassword(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) CreateKey(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) CreateMCPToken(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) DeleteKey(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) DeleteMCPToken(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) GetKey(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) GetSettings(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) HISCalibration(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) IssueStatsToken(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) KeyStats(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) KeysDailyStats(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) KeysStatsSummary(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) ListKeys(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) ListMCPTokens(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) Login(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) Me(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) RevokeStatsToken(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) RotateSecret(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) StatsOverview(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) UpdateKey(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) UpdateSettings(w http.ResponseWriter, r *http.Request)
- func (h *AdminHandler) Version(w http.ResponseWriter, r *http.Request)
- type ChallengeHandler
- type KeyStatsHandler
- type PublicHandler
- type RouterConfig
- type VerifyHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AdminAuthMiddleware ¶
func FailedAuthRateLimitMiddleware ¶
FailedAuthRateLimitMiddleware caps, per IP, the requests that fail API key authentication (401). It runs before APIKeyMiddleware, so an IP cycling through made-up keys is stopped before each one costs a database lookup, while requests carrying a valid key never draw on this budget.
func MCPAuthMiddleware ¶
MCPAuthMiddleware authenticates a bearer MCP token.
The token is deliberately not accepted from the query string: URLs end up in proxy logs and browser history, and this credential carries admin capability.
func MCPEnabledMiddleware ¶
MCPEnabledMiddleware refuses every request while the endpoint is switched off.
It answers 404 because that is what the path is: nothing is served here. This does not hide the endpoint's existence, and is not meant to. The SPA fallback answers 200 for unknown paths, so a 404 is distinguishable either way, and the dashboard already announces which product is running at this domain.
func MCPHandler ¶
MCPHandler returns the streamable HTTP handler for the MCP endpoint.
Stateless mode is required to serve protocol version 2026-07-28, and suits this endpoint anyway: there is no server-to-client request to make, so there is nothing for a session to carry between requests.
func MCPTokenFromContext ¶
MCPTokenFromContext returns the token that authenticated the current request.
func MaxBodyBytesMiddleware ¶
MaxBodyBytesMiddleware caps the size of request bodies to guard against memory-exhaustion. An oversized body surfaces as a read error, which the JSON-decoding handlers already translate into a 4xx response.
func PerKeyRateLimitMiddleware ¶
PerKeyRateLimitMiddleware enforces each API key's own RateLimitPerMin budget, independent of the global per-IP limiter. It must run after APIKeyMiddleware so the authenticated key is available on the request context; keys with a limit of 0 are never throttled here.
func RateLimitMiddleware ¶
RateLimitMiddleware limits requests per client IP over a one-minute window and answers with a JSON 429 when the limit is exceeded.
func RequestLogMiddleware ¶
RequestLogMiddleware writes one line per request through slog, so GATECHA_LOG_LEVEL applies to it. It replaces chi's Logger, which printed every visitor's IP to stdout although nothing reads it there: rate limiting, adaptive difficulty and country stats take the IP from the request itself. The path is logged without its query string, which carries ?apiKey=. logClientIP (GATECHA_LOG_CLIENT_IP) adds the IP back, for debugging only.
func SecurityHeadersMiddleware ¶
SecurityHeadersMiddleware sets a baseline of security-related response headers. HSTS is opt-in because GateCHA is commonly run over plain HTTP locally or behind a TLS-terminating reverse proxy.
func SourceAndKeyRateLimitMiddleware ¶
SourceAndKeyRateLimitMiddleware applies the per-IP budget to each (client IP, API key) pair rather than to the IP alone. It guards /verify, which is called by site backends, not browsers: on shared hosting many sites post from one outbound IP, and a plain per-IP bucket made them throttle each other. Each site now gets the full budget from that IP, and one site still cannot exceed it. It must run after APIKeyMiddleware, so only real keys open a bucket.
Types ¶
type AdminHandler ¶
func (*AdminHandler) ChangePassword ¶
func (h *AdminHandler) ChangePassword(w http.ResponseWriter, r *http.Request)
POST /api/admin/change-password
func (*AdminHandler) CreateKey ¶
func (h *AdminHandler) CreateKey(w http.ResponseWriter, r *http.Request)
POST /api/admin/keys
func (*AdminHandler) CreateMCPToken ¶
func (h *AdminHandler) CreateMCPToken(w http.ResponseWriter, r *http.Request)
POST /api/admin/mcp-tokens
func (*AdminHandler) DeleteKey ¶
func (h *AdminHandler) DeleteKey(w http.ResponseWriter, r *http.Request)
DELETE /api/admin/keys/{id}
func (*AdminHandler) DeleteMCPToken ¶
func (h *AdminHandler) DeleteMCPToken(w http.ResponseWriter, r *http.Request)
DELETE /api/admin/mcp-tokens/{id}
func (*AdminHandler) GetKey ¶
func (h *AdminHandler) GetKey(w http.ResponseWriter, r *http.Request)
GET /api/admin/keys/{id}
func (*AdminHandler) GetSettings ¶
func (h *AdminHandler) GetSettings(w http.ResponseWriter, r *http.Request)
GET /api/admin/settings
func (*AdminHandler) HISCalibration ¶
func (h *AdminHandler) HISCalibration(w http.ResponseWriter, r *http.Request)
GET /api/admin/his/calibration?key_id=&days= Returns the score distribution and signal averages over stored HIS samples, to help calibrate an enforcement threshold before any blocking is enabled.
func (*AdminHandler) IssueStatsToken ¶
func (h *AdminHandler) IssueStatsToken(w http.ResponseWriter, r *http.Request)
POST /api/admin/keys/{id}/stats-token
Issues the key's read-only stats token, replacing any previous one. The secret is in this response and nowhere else afterwards.
func (*AdminHandler) KeyStats ¶
func (h *AdminHandler) KeyStats(w http.ResponseWriter, r *http.Request)
GET /api/admin/stats/keys/{id}
func (*AdminHandler) KeysDailyStats ¶
func (h *AdminHandler) KeysDailyStats(w http.ResponseWriter, r *http.Request)
GET /api/admin/stats/keys-daily?days=7
func (*AdminHandler) KeysStatsSummary ¶
func (h *AdminHandler) KeysStatsSummary(w http.ResponseWriter, r *http.Request)
GET /api/admin/stats/keys-summary
func (*AdminHandler) ListKeys ¶
func (h *AdminHandler) ListKeys(w http.ResponseWriter, r *http.Request)
GET /api/admin/keys
func (*AdminHandler) ListMCPTokens ¶
func (h *AdminHandler) ListMCPTokens(w http.ResponseWriter, r *http.Request)
GET /api/admin/mcp-tokens
func (*AdminHandler) Login ¶
func (h *AdminHandler) Login(w http.ResponseWriter, r *http.Request)
POST /api/admin/login
func (*AdminHandler) Me ¶
func (h *AdminHandler) Me(w http.ResponseWriter, r *http.Request)
GET /api/admin/me
func (*AdminHandler) RevokeStatsToken ¶
func (h *AdminHandler) RevokeStatsToken(w http.ResponseWriter, r *http.Request)
DELETE /api/admin/keys/{id}/stats-token
func (*AdminHandler) RotateSecret ¶
func (h *AdminHandler) RotateSecret(w http.ResponseWriter, r *http.Request)
POST /api/admin/keys/{id}/rotate-secret
func (*AdminHandler) StatsOverview ¶
func (h *AdminHandler) StatsOverview(w http.ResponseWriter, r *http.Request)
GET /api/admin/stats/overview
func (*AdminHandler) UpdateKey ¶
func (h *AdminHandler) UpdateKey(w http.ResponseWriter, r *http.Request)
PUT /api/admin/keys/{id}
func (*AdminHandler) UpdateSettings ¶
func (h *AdminHandler) UpdateSettings(w http.ResponseWriter, r *http.Request)
PUT /api/admin/settings
func (*AdminHandler) Version ¶
func (h *AdminHandler) Version(w http.ResponseWriter, r *http.Request)
GET /api/admin/version Returns the build version. Kept behind admin auth so the running version is not disclosed to unauthenticated visitors.
type ChallengeHandler ¶
type ChallengeHandler struct {
DB *gorm.DB
// Adaptive scales proof-of-work difficulty by source request rate for keys
// that opt in via AdaptiveDifficulty.
Adaptive *adaptiveLimiter
}
func (*ChallengeHandler) ServeHTTP ¶
func (h *ChallengeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type KeyStatsHandler ¶
KeyStatsHandler serves GET /api/v1/stats: one key's statistics, read with that key's stats token (gs_) rather than its key ID. The key ID is public, it sits in the HTML of every protected page; the token is a server-side secret a site's backend (the WordPress plugin, for one) keeps to show the owner their numbers without admin access to the instance.
func (*KeyStatsHandler) ServeHTTP ¶
func (h *KeyStatsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type PublicHandler ¶
func (*PublicHandler) HISCollector ¶
func (h *PublicHandler) HISCollector(w http.ResponseWriter, r *http.Request)
GET /api/public/his.js
Serves the standalone interaction-signal collector so a site that does not build this dashboard can still emit `his_signals` on POST /api/v1/verify. Without it the only client producing signals is GateCHA's own login page, which is why calibration data has been unrepresentative.
Unauthenticated on purpose: it is public client code with no secrets in it, and a <script src> cannot carry an API key. It is scoped per request by the key the site already uses on /verify, not by who fetched the script.
func (*PublicHandler) LoginConfig ¶
func (h *PublicHandler) LoginConfig(w http.ResponseWriter, r *http.Request)
GET /api/public/login-config
type RouterConfig ¶
type RouterConfig struct {
CORSAllowAll bool
TrustProxy bool // honor X-Forwarded-For/X-Real-IP (only safe behind a trusted proxy)
EnableHSTS bool
MaxBodyBytes int64
RateLimitEnabled bool
RateLimitLogin int // requests per minute on /api/admin/login
RateLimitAPI int // requests per minute on /api/v1/*
LogClientIP bool // include the client IP in request logs (debugging only)
Version string // build version surfaced to authenticated admins
}
RouterConfig holds the request-handling and hardening options for NewRouter.
type VerifyHandler ¶
func (*VerifyHandler) ServeHTTP ¶
func (h *VerifyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)