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 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 ValidateRequest(c *gin.Context, model interface{}) error
- func ValidationMiddleware(log interfaces.LoggerInterface, config ...ValidationConfig) gin.HandlerFunc
- func WebSocketMetricsMiddleware(metricsService interfaces.MetricsService) gin.HandlerFunc
- func WebSocketSecurityMiddleware(log interfaces.LoggerInterface, allowedOrigins ...string) 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 RateLimitConfig
- type RecoveryConfig
- type SecurityConfig
- type TracingConfig
- 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 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 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 WebSocketSecurityMiddleware ¶
func WebSocketSecurityMiddleware(log interfaces.LoggerInterface, allowedOrigins ...string) gin.HandlerFunc
WebSocketSecurityMiddleware returns a middleware that adds WebSocket security
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
SensitiveFields []string
// SkipPaths are paths that should not be logged
SkipPaths []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 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 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 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