middleware

package
v1.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2026 License: Apache-2.0 Imports: 38 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthMiddleware

func AuthMiddleware(config *AuthMiddlewareConfig) func(http.HandlerFunc) http.HandlerFunc

func AuthSecurityMiddleware

func AuthSecurityMiddleware() func(http.HandlerFunc) http.HandlerFunc

func CORSMiddleware

func CORSMiddleware() func(http.HandlerFunc) http.HandlerFunc

func ExternalHostMiddleware

func ExternalHostMiddleware() func(http.HandlerFunc) http.HandlerFunc

ExternalHostMiddleware stores the host the browser addressed in the request context. Requests proxied through the frontend on a customer's custom portal domain arrive with the original host in X-Forwarded-Host; direct requests use the Host header. Cookie scoping uses this to decide between the shared .openmrp.ai domain and a host-only cookie — a spoofed X-Forwarded-Host can only downgrade the caller to a host-only cookie on the spoofed host, so the header does not need to be validated against a domain allowlist.

func IPBlockMiddleware

func IPBlockMiddleware(trustedProxyHops int) func(http.HandlerFunc) http.HandlerFunc

IPBlockMiddleware rejects requests whose client IP is on the block list. It runs early in the chain so blocked traffic is not rate-limited, authenticated, or otherwise processed.

func IdempotencyMiddleware

func IdempotencyMiddleware(config *IdempotencyMiddlewareConfig) func(http.HandlerFunc) http.HandlerFunc

func InternalAuthMiddleware

func InternalAuthMiddleware(config *InternalAuthMiddlewareConfig) func(http.HandlerFunc) http.HandlerFunc

InternalAuthMiddleware authenticates requests on the gateway's internal listener. Instead of validating a user credential against auth-service, it trusts an agent identity supplied directly in InternalIdentityHeader — but ONLY when InternalServiceTokenHeader matches the configured secret (constant-time) and the identity is a well-formed agent identity. This listener must never be exposed behind the public ALB, and the edge must strip X-OpenMRP-Internal-* from external traffic.

func IsIPBlocked

func IsIPBlocked(ip net.IP) bool

IsIPBlocked reports whether the given IP is on the block list.

func LoggingMiddleware

func LoggingMiddleware(logger *log.Logger, next http.HandlerFunc, saver saver, router any, trustedProxyHops int) http.HandlerFunc

func NewRequestLogSaver

func NewRequestLogSaver(publisher domain.RequestLogPublisher) *requestLogSaver

func PlatformMiddleware

func PlatformMiddleware(platform constants.PlatformMode) func(http.HandlerFunc) http.HandlerFunc

func RateLimitMiddleware

func RateLimitMiddleware(trustedProxyHops int) func(http.HandlerFunc) http.HandlerFunc

func RateLimitMiddlewareWithConfig

func RateLimitMiddlewareWithConfig(limit int, window time.Duration, trustedProxyHops int) func(http.HandlerFunc) http.HandlerFunc

func RecoverMiddleware

func RecoverMiddleware() func(http.HandlerFunc) http.HandlerFunc

RecoverMiddleware recovers from a handler panic, records the panic as the request log's error message (when one is not already set), and responds to the client with a generic internal server error instead of letting the connection drop.

func SandboxBillingMiddleware

func SandboxBillingMiddleware() func(http.HandlerFunc) http.HandlerFunc

SandboxBillingMiddleware blocks sandbox accounts from accessing billing endpoints. Sandbox accounts inherit their owner's plan and should never interact with Stripe directly.

func SubscriptionMiddleware

func SubscriptionMiddleware() func(http.HandlerFunc) http.HandlerFunc

func TracingMiddleware

func TracingMiddleware() func(http.HandlerFunc) http.HandlerFunc

TracingMiddleware adds OpenTelemetry tracing to the request. It skips tracing for health check endpoints to save resources.

func VersionMiddleware

func VersionMiddleware() func(http.HandlerFunc) http.HandlerFunc

Types

type AuthMiddlewareConfig

type AuthMiddlewareConfig struct {
	// AuthClient (required) is the auth-service gRPC client used to validate access tokens and API keys.
	AuthClient *grpcclient.AuthServiceClient
}

type IdempotencyMiddlewareConfig

type IdempotencyMiddlewareConfig struct {
	// PlatformClient (optional; default: nil) is the platform-service gRPC client used to persist and replay idempotency keys. When nil, idempotency keys are still propagated on the request context but requests are not deduplicated.
	PlatformClient *grpcclient.PlatformServiceClient
}

type InternalAuthMiddlewareConfig

type InternalAuthMiddlewareConfig struct {
	// ServiceToken (required) is the shared secret that gates identity trust on the internal listener. Requests must present a matching InternalServiceTokenHeader.
	ServiceToken string
}

InternalAuthMiddlewareConfig configures the trusted internal listener's auth.

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

A rate limiter with exponential backoff and jitter

func NewRateLimiter

func NewRateLimiter(limit int, window time.Duration) *RateLimiter

func (*RateLimiter) Check

func (rl *RateLimiter) Check(key string) (bool, int)

Check reports whether the given key is currently within the rate limit without recording a new attempt. Pair with RecordFailure to throttle only specific outcomes (e.g. failed login attempts) instead of every request.

func (*RateLimiter) GetResetAfterSeconds

func (rl *RateLimiter) GetResetAfterSeconds(key string) int

GetResetAfterSeconds returns the number of seconds until the rate limit resets for a given key.

func (*RateLimiter) IsAllowed

func (rl *RateLimiter) IsAllowed(key string) (bool, int, int)

IsAllowed records the attempt and reports whether the request is within the rate limit, the number of seconds until the limit resets, and the number of remaining requests in the current window.

func (*RateLimiter) RecordFailure

func (rl *RateLimiter) RecordFailure(key string)

RecordFailure records a single failed attempt for the given key, advancing the rate limit window and (when the limit is exceeded) the backoff state.

type RouteMatcher

type RouteMatcher interface {
	GetRoutes() []any
}

type SerializedCookie

type SerializedCookie struct {
	Name     string `json:"name"`
	Value    string `json:"value"`
	Path     string `json:"path,omitempty"`
	Domain   string `json:"domain,omitempty"`
	MaxAge   int    `json:"max_age,omitempty"`
	Secure   bool   `json:"secure,omitempty"`
	HttpOnly bool   `json:"http_only,omitempty"`
	SameSite string `json:"same_site,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL