Documentation
¶
Index ¶
- func AdminGuard() gin.HandlerFunc
- func AuthMiddleware(authService apiinterfaces.AuthService, log pkginterfaces.LoggerInterface, ...) gin.HandlerFunc
- func AuthorizationMiddleware(authService apiinterfaces.AuthService, log pkginterfaces.LoggerInterface) gin.HandlerFunc
- func CSPReportingMiddleware(log interfaces.LoggerInterface) gin.HandlerFunc
- func ErrorHandlerMiddleware(log interfaces.LoggerInterface, config ...ErrorHandlerConfig) gin.HandlerFunc
- func FeatureGuard(reader orgPlanReader, feature string) gin.HandlerFunc
- func HandleAPIError(c *gin.Context, err error)
- func LoggingMiddleware(log interfaces.LoggerInterface, config ...LoggingConfig) gin.HandlerFunc
- func MetricsMiddleware(metricsService interfaces.MetricsService) gin.HandlerFunc
- func OrgAdminGuard(store orgMemberChecker) gin.HandlerFunc
- func OrgMemberGuard(store orgMemberChecker) gin.HandlerFunc
- func PerRouteRateLimitMiddleware(rl interfaces.RateLimiterService, log pkginterfaces.LoggerInterface, ...) gin.HandlerFunc
- func RateLimitMiddleware(rl interfaces.RateLimiterService, log pkginterfaces.LoggerInterface, ...) gin.HandlerFunc
- func RecoveryMiddleware(log interfaces.LoggerInterface, config ...RecoveryConfig) gin.HandlerFunc
- func RequirePermissions(permissions ...string) gin.HandlerFunc
- func SecurityMiddleware(log interfaces.LoggerInterface, config ...SecurityConfig) gin.HandlerFunc
- func TracingMiddleware(log interfaces.LoggerInterface, config ...TracingConfig) gin.HandlerFunc
- func Turnstile(cfg TurnstileConfig) gin.HandlerFunc
- func ValidateRequest(c *gin.Context, model interface{}) error
- func ValidationMiddleware(log interfaces.LoggerInterface, config ...ValidationConfig) gin.HandlerFunc
- func WebSocketMetricsMiddleware(metricsService interfaces.MetricsService) gin.HandlerFunc
- func WorkspaceAccessMiddleware(svc workspaceAccessService) gin.HandlerFunc
- func WorkspaceMetaFromContext(c *gin.Context) (meta *types.WorkspaceMetadata, ok bool)
- type AuthConfig
- type ErrorHandlerConfig
- type LoggingConfig
- type MeteringMiddleware
- type PerRouteRateLimitConfig
- type RateLimitConfig
- type RecoveryConfig
- type RouteRateLimit
- type SecurityConfig
- type TracingConfig
- type TurnstileConfig
- type ValidationConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AdminGuard ¶
func AdminGuard() gin.HandlerFunc
AdminGuard returns a middleware that restricts access to admin users. Non-admin requests receive 404 (not 403) to avoid revealing route existence.
func AuthMiddleware ¶
func AuthMiddleware(authService apiinterfaces.AuthService, log pkginterfaces.LoggerInterface, config ...AuthConfig) gin.HandlerFunc
AuthMiddleware returns a middleware that handles authentication
func AuthorizationMiddleware ¶
func AuthorizationMiddleware(authService apiinterfaces.AuthService, log pkginterfaces.LoggerInterface) gin.HandlerFunc
AuthorizationMiddleware returns a middleware that handles authorization
func CSPReportingMiddleware ¶
func CSPReportingMiddleware(log interfaces.LoggerInterface) gin.HandlerFunc
CSPReportingMiddleware returns a middleware that handles CSP violation reports
func ErrorHandlerMiddleware ¶
func ErrorHandlerMiddleware(log interfaces.LoggerInterface, config ...ErrorHandlerConfig) gin.HandlerFunc
ErrorHandlerMiddleware returns a middleware that handles errors
func FeatureGuard ¶
func FeatureGuard(reader orgPlanReader, feature string) gin.HandlerFunc
FeatureGuard returns Gin middleware that denies the request when the org identified by ":id" is on a plan that does not include the named feature. Feature names map 1:1 to the cases in billing.IsFeatureAllowed ("policies", "audit", "sso", "custom_credentials").
FeatureGuard MUST run after OrgAdminGuard or OrgMemberGuard so that the caller has already been authenticated; it does not perform its own membership check. It performs a GetOrg lookup to read the plan; for admin-only routes the extra query is acceptable (low request volume).
Unknown feature names are allowed (fail-open) to preserve forward compatibility with new features added in billing.IsFeatureAllowed.
func HandleAPIError ¶
HandleAPIError handles an API error in a handler
func LoggingMiddleware ¶
func LoggingMiddleware(log interfaces.LoggerInterface, config ...LoggingConfig) gin.HandlerFunc
func MetricsMiddleware ¶
func MetricsMiddleware(metricsService interfaces.MetricsService) gin.HandlerFunc
MetricsMiddleware returns a middleware that collects metrics
func OrgAdminGuard ¶
func OrgAdminGuard(store orgMemberChecker) gin.HandlerFunc
OrgAdminGuard returns Gin middleware that verifies the caller is an admin (role='admin') of the org identified by ":id". Returns 403 for non-admins and members of soft-deleted orgs.
func OrgMemberGuard ¶
func OrgMemberGuard(store orgMemberChecker) gin.HandlerFunc
OrgMemberGuard returns Gin middleware that verifies the caller is a member of the org identified by the ":id" path parameter. Returns 403 for unauthorized callers (including members of soft-deleted orgs).
func PerRouteRateLimitMiddleware ¶ added in v0.4.0
func PerRouteRateLimitMiddleware(rl interfaces.RateLimiterService, log pkginterfaces.LoggerInterface, cfg PerRouteRateLimitConfig) gin.HandlerFunc
PerRouteRateLimitMiddleware applies stricter rate limits to specific routes on top of the global RateLimitMiddleware. For paths not listed in cfg.Routes, it is a no-op (the global limiter handles them).
Identity resolution mirrors the global middleware: API-key if available (set by AuthMiddleware on authenticated routes), else client IP. Anonymous endpoints like /account/recover always fall back to IP — this matches the threat model (per-source throttling).
The underlying RateLimiterService.Allow(key, rate, burst) keys buckets on `key` alone, so this middleware MUST prefix the key with the route to get bucket isolation between routes (see ratelimit.go:Allow). The prefix is the FULL gin route pattern (e.g. "/api/v1/account/recover"), not the request URL — parameterised routes (/secrets/:id) share one bucket per route, which is the intended behavior (a user scanning IDs is rate-limited as one).
func RateLimitMiddleware ¶
func RateLimitMiddleware(rl interfaces.RateLimiterService, log pkginterfaces.LoggerInterface, config RateLimitConfig, instanceSettings *settings.InstanceService) gin.HandlerFunc
func RecoveryMiddleware ¶
func RecoveryMiddleware(log interfaces.LoggerInterface, config ...RecoveryConfig) gin.HandlerFunc
RecoveryMiddleware returns a middleware that recovers from panics
func RequirePermissions ¶
func RequirePermissions(permissions ...string) gin.HandlerFunc
RequirePermissions returns a middleware that requires specific permissions
func SecurityMiddleware ¶
func SecurityMiddleware(log interfaces.LoggerInterface, config ...SecurityConfig) gin.HandlerFunc
SecurityMiddleware returns a middleware that adds security headers
func TracingMiddleware ¶
func TracingMiddleware(log interfaces.LoggerInterface, config ...TracingConfig) gin.HandlerFunc
TracingMiddleware returns a middleware that adds request tracing
func Turnstile ¶ added in v0.2.0
func Turnstile(cfg TurnstileConfig) gin.HandlerFunc
Turnstile returns a middleware that enforces CAPTCHA validation.
func ValidateRequest ¶
ValidateRequest validates a request body against a model
func ValidationMiddleware ¶
func ValidationMiddleware(log interfaces.LoggerInterface, config ...ValidationConfig) gin.HandlerFunc
ValidationMiddleware returns a middleware that validates request bodies
func WebSocketMetricsMiddleware ¶
func WebSocketMetricsMiddleware(metricsService interfaces.MetricsService) gin.HandlerFunc
WebSocketMetricsMiddleware returns a middleware that tracks WebSocket connections
func WorkspaceAccessMiddleware ¶
func WorkspaceAccessMiddleware(svc workspaceAccessService) gin.HandlerFunc
WorkspaceAccessMiddleware is the single ownership gate for /:id workspace routes (design 0041 D1). It resolves the workspace once, runs the CheckOwnership authorisation (D5 creator-membership + D6 org-admin), and on success stores the metadata in the request context so downstream handlers and service methods can reuse it without a second DB hit.
Error mapping follows verifyOwner semantics exactly: NotFound → 404, Forbidden → 403, Internal/bare errors → 500. The middleware never rewrites an infrastructure failure as 403 — fail-closed here means "deny", not "pretend the user is unauthorized".
func WorkspaceMetaFromContext ¶
func WorkspaceMetaFromContext(c *gin.Context) (meta *types.WorkspaceMetadata, ok bool)
WorkspaceMetaFromContext returns the metadata stored by WorkspaceAccessMiddleware. The ok flag is false when the middleware did not run (e.g. the route is mounted outside an idGroup) — callers must handle that case explicitly rather than relying on a non-nil meta.
The canonical store is c.Request.Context() (set under types.ContextKeyWorkspaceMeta) so the same value is visible to service-layer code reading a plain context.Context. This accessor is kept for handler ergonomics and delegates to types.WorkspaceMetaFromCtx.
Types ¶
type AuthConfig ¶
type AuthConfig struct {
// HeaderName is the name of the header containing the authentication token
HeaderName string
// QueryParamName is the name of the query parameter containing the authentication token
QueryParamName string
// CookieName is the name of the cookie containing the authentication token
CookieName string
// TokenType is the type of token (e.g., "Bearer")
TokenType string
// SkipPaths are paths that should not be authenticated
SkipPaths []string
// SkipPathPrefixes are path prefixes that should not be authenticated
SkipPathPrefixes []string
}
AuthConfig defines configuration for the authentication middleware
func DefaultAuthConfig ¶
func DefaultAuthConfig() AuthConfig
DefaultAuthConfig returns the default authentication configuration
type ErrorHandlerConfig ¶
type ErrorHandlerConfig struct {
// IncludeStackTrace indicates whether to include stack traces in error responses
IncludeStackTrace bool
// LogStackTrace indicates whether to log stack traces
LogStackTrace bool
// MaxBodyLogSize is the maximum size of request/response bodies to log
MaxBodyLogSize int
// SensitiveFields are JSON fields that should be redacted in request/response bodies
SensitiveFields []string
}
ErrorHandlerConfig defines configuration for the error handler middleware
func DefaultErrorHandlerConfig ¶
func DefaultErrorHandlerConfig() ErrorHandlerConfig
DefaultErrorHandlerConfig returns the default error handler configuration
type LoggingConfig ¶
type LoggingConfig struct {
// LogRequestBody indicates whether to log request bodies
LogRequestBody bool
// LogResponseBody indicates whether to log response bodies
LogResponseBody bool
// MaxBodyLogSize is the maximum size of request/response bodies to log
MaxBodyLogSize int
// SensitiveFields are JSON fields that should be redacted in request/response bodies.
// Field-name matching is exact (case-sensitive). See pkg/utilities/masking.go.
//
// G25: "value" is intentionally included. The secrets endpoint carries
// plaintext credentials in the "value" field; even though /api/v1/secrets/*
// is in SkipPathPrefixes (defense in depth — bodies never logged at all
// for that path), other endpoints may also pass through sensitive values
// in a "value" field (env-var updates, settings updates with a secret
// subtype, etc.). Masking "value" globally errs on the side of caution;
// legitimate non-secret uses (e.g. settings PUT {"value":"20Gi"}) become
// "********" in logs, which is acceptable for log readability.
SensitiveFields []string
// SkipPaths are exact paths that should not be logged at all
// (typical use: liveness/readiness probes that flood logs).
SkipPaths []string
// SkipPathPrefixes are URL path prefixes that should not be logged
// (G25). Prefix matching (not exact) so a single entry like
// "/api/v1/secrets/" catches every secrets sub-path
// (/api/v1/secrets/:id, /api/v1/secrets/:id/reveal, etc.). Bodies
// on these paths can carry plaintext credentials in non-standard
// fields; the safest policy is to not log them at all.
SkipPathPrefixes []string
}
func DefaultLoggingConfig ¶
func DefaultLoggingConfig() LoggingConfig
DefaultLoggingConfig returns the default logging configuration
type MeteringMiddleware ¶
type MeteringMiddleware struct {
// contains filtered or unexported fields
}
func NewMeteringMiddleware ¶
func NewMeteringMiddleware(svc interfaces.MeteringRecorder) *MeteringMiddleware
func (*MeteringMiddleware) Handler ¶
func (m *MeteringMiddleware) Handler() gin.HandlerFunc
type PerRouteRateLimitConfig ¶ added in v0.4.0
type PerRouteRateLimitConfig struct {
Enabled bool
Routes map[string]RouteRateLimit
}
PerRouteRateLimitConfig configures the per-route rate limiter. It is INTENTIONALLY separate from the global RateLimitConfig: the global limiter applies a wide budget across every endpoint; this layer adds STRICTER limits to specific paths that warrant them (G35 — /account/recover; G41 — /secrets/:id/reveal; future endpoints).
The two layers do NOT share buckets: this middleware keys its buckets by "<path>:<hashed-identity>" while the global middleware keys by "<hashed-identity>" alone. This isolation is the whole point — without it, a user could spend 99 of their 100 global requests on /recover before any per-endpoint gate tripped.
type RateLimitConfig ¶
type RateLimitConfig struct {
Enabled bool
DefaultLimit int
DefaultWindow time.Duration
BurstSize int
Strategy string
ExemptPaths []string // path prefixes exempt from rate limiting (e.g. SSE endpoints)
CustomLimits map[string]int
CustomBursts map[string]int
}
func DefaultRateLimitConfig ¶
func DefaultRateLimitConfig() RateLimitConfig
type RecoveryConfig ¶
type RecoveryConfig struct {
// IncludeStackTrace indicates whether to include stack traces in error responses
IncludeStackTrace bool
// LogStackTrace indicates whether to log stack traces
LogStackTrace bool
// CustomRecoveryHandler is a custom function to handle recovery
CustomRecoveryHandler func(*gin.Context, interface{})
}
RecoveryConfig defines configuration for the recovery middleware
func DefaultRecoveryConfig ¶
func DefaultRecoveryConfig() RecoveryConfig
DefaultRecoveryConfig returns the default recovery configuration
type RouteRateLimit ¶ added in v0.4.0
RouteRateLimit is the per-route limit configuration applied by PerRouteRateLimitMiddleware to a specific path (matched by gin's FullPath, e.g. "/api/v1/account/recover").
Semantics (intentionally correct, unlike the global limiter's pre-existing per-second confusion): `Limit` is the maximum number of requests per `Window` per identity (API-key or IP). The middleware converts to a per-second refill rate internally (`Limit / Window.Seconds()`) so a config of {Limit: 20, Window: 1m} actually enforces 20 per minute, not 20 per second.
type SecurityConfig ¶
type SecurityConfig struct {
// AllowedOrigins is a list of allowed origins for CORS
AllowedOrigins []string
// AllowedMethods is a list of allowed HTTP methods for CORS
AllowedMethods []string
// AllowedHeaders is a list of allowed HTTP headers for CORS
AllowedHeaders []string
// ExposedHeaders is a list of headers that can be exposed to the client
ExposedHeaders []string
// AllowCredentials indicates whether the request can include user credentials
AllowCredentials bool
// MaxAge indicates how long the results of a preflight request can be cached
MaxAge int
// TrustedProxies is a list of trusted proxy IP addresses
TrustedProxies []string
// ContentSecurityPolicy is the Content-Security-Policy header value
ContentSecurityPolicy string
// ReferrerPolicy is the Referrer-Policy header value
ReferrerPolicy string
// PermissionsPolicy is the Permissions-Policy header value
PermissionsPolicy string
// RequireHTTPS indicates whether to require HTTPS
RequireHTTPS bool
// AllowHTTPSDowngrade indicates whether to allow HTTPS downgrade in development
AllowHTTPSDowngrade bool
// Development indicates whether the application is running in development mode
Development bool
}
SecurityConfig defines configuration for the security middleware
func DefaultSecurityConfig ¶
func DefaultSecurityConfig() SecurityConfig
DefaultSecurityConfig returns the default security configuration
type TracingConfig ¶
type TracingConfig struct {
// HeaderName is the name of the header to use for the request ID
HeaderName string
// PropagateHeader indicates whether to propagate the request ID in the response header
PropagateHeader bool
// GenerateIfMissing indicates whether to generate a request ID if one is not provided
GenerateIfMissing bool
// UseUUID indicates whether to use UUID for generated request IDs
UseUUID bool
// TracerName is the name of the tracer to use
TracerName string
// EnableOpenTelemetry indicates whether to use OpenTelemetry for tracing
EnableOpenTelemetry bool
}
TracingConfig defines configuration for the tracing middleware
func DefaultTracingConfig ¶
func DefaultTracingConfig() TracingConfig
DefaultTracingConfig returns the default tracing configuration
type TurnstileConfig ¶ added in v0.2.0
type TurnstileConfig struct {
SecretKey string
VerifyURL string
// Optional HTTP client override — tests substitute a stub.
HTTPClient *http.Client
// Optional logger override — tests use a nop; production wires the
// service's zap logger.
Logger *zap.Logger
}
TurnstileConfig is the minimum surface the middleware needs. Zero-value SecretKey or VerifyURL results in a permanently-failing middleware (fail-closed).
type ValidationConfig ¶
type ValidationConfig struct {
// CustomValidators is a map of custom validation functions
CustomValidators map[string]validator.Func
// CustomErrorMessages is a map of custom error messages for validation tags
CustomErrorMessages map[string]string
// ValidateQueryParams indicates whether to validate query parameters
ValidateQueryParams bool
// ValidatePathParams indicates whether to validate path parameters
ValidatePathParams bool
}
ValidationConfig defines configuration for the validation middleware
func DefaultValidationConfig ¶
func DefaultValidationConfig() ValidationConfig
DefaultValidationConfig returns the default validation configuration