middleware

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TokenHeaderPrefix = "X-Authorization-"
)

Variables

This section is empty.

Functions

func AccessTokenFromContext added in v0.17.0

func AccessTokenFromContext(ctx context.Context) string

AccessTokenFromContext extracts the raw bearer token from the request context.

func AuditLog

func AuditLog(lgr logr.Logger, trustProxy bool) func(http.Handler) http.Handler

AuditLog returns middleware that writes structured audit log entries. Request bodies for mutation methods are captured and redacted before logging to prevent sensitive data (passwords, tokens, secrets) from leaking into logs. Set trustProxy true only when a trusted reverse proxy sanitizes X-Forwarded-For and X-Real-IP; leave false (the safe default) to use RemoteAddr for the audit source IP and prevent clients from spoofing their identity in audit logs.

func FlightID added in v0.17.0

func FlightID(next http.Handler) http.Handler

FlightID is a middleware that assigns a unique monotonic uint64 to each request and stores it in the request context for go-flight singleflight tagging.

func FlightIDExtractor added in v0.17.0

func FlightIDExtractor() func(ctx context.Context) uint64

FlightIDExtractor returns a function compatible with go-flight's cache.RequestIDExtractor type for singleflight tagging.

func FlightIDFromContext added in v0.17.0

func FlightIDFromContext(ctx context.Context) uint64

FlightIDFromContext extracts the flight ID from context. Returns 0 if no ID was set.

func GzipEncoderFunc

func GzipEncoderFunc(w io.Writer, level int) io.Writer

GzipEncoderFunc is a chi-compatible EncoderFunc that creates a gzip writer. The compression level is validated by the chi Compressor before this function is called, so gzip.NewWriterLevel only returns an error for invalid levels.

func MaxBodySize

func MaxBodySize(maxSize int64) func(http.Handler) http.Handler

MaxBodySize returns middleware that rejects requests with bodies larger than maxSize.

func Metrics

func Metrics() func(http.Handler) http.Handler

Metrics returns middleware that records per-request metrics.

func NewAzureOIDCAuth

func NewAzureOIDCAuth(tenantID, clientID string, lgr logr.Logger) (func(http.Handler) http.Handler, error)

NewAzureOIDCAuth creates authentication middleware for Azure AD OIDC JWT validation.

func OIDCProviderFromContext added in v0.33.0

func OIDCProviderFromContext(ctx context.Context) string

OIDCProviderFromContext returns the OIDC provider name that authenticated the inbound request, or "" if no OIDC middleware authenticated it.

func RateLimit

func RateLimit(ctx context.Context, maxRequests int, window time.Duration, trustProxy bool) func(http.Handler) http.Handler

RateLimit returns middleware that limits requests per IP using a sliding window. ctx controls the lifetime of the background cleanup goroutine; cancel it (e.g. on server shutdown) to stop the goroutine and prevent leaks. Set trustProxy true only when a trusted reverse proxy sanitizes X-Forwarded-For and X-Real-IP; otherwise leave false to use RemoteAddr and prevent IP spoofing.

func RedactJSON

func RedactJSON(v any) any

RedactJSON round-trips v through JSON and redacts sensitive field values in any map keys that match the sensitiveKeys list (e.g. password, secret, token). Arrays of objects are recursively redacted. Non-JSON-serializable values are returned unchanged. Intended for use in API response sanitization.

func RequestLogging

func RequestLogging(lgr logr.Logger) func(http.Handler) http.Handler

RequestLogging returns middleware that logs every request.

func SecurityHeaders

func SecurityHeaders(tlsEnabled bool) func(http.Handler) http.Handler

SecurityHeaders returns middleware that sets standard security headers.

func TokenPassthrough added in v0.17.0

func TokenPassthrough(allowedTokenHeaderSuffixes []string) func(http.Handler) http.Handler

func TokensFromContext added in v0.17.0

func TokensFromContext(ctx context.Context) map[string]string

func Tracing

func Tracing() func(http.Handler) http.Handler

Tracing returns middleware that instruments HTTP requests with OpenTelemetry traces.

func WithAccessToken added in v0.17.0

func WithAccessToken(ctx context.Context, token string) context.Context

WithAccessToken stores the raw bearer token in the context.

func WithAuthClaims added in v0.17.0

func WithAuthClaims(ctx context.Context, claims *AuthClaims) context.Context

WithAuthClaims stores AuthClaims in the context.

func WithContextTokens added in v0.33.0

func WithContextTokens(ctx context.Context, tokens map[string]string) context.Context

WithContextTokens stores extracted passthrough tokens in the context.

func WithOIDCProvider added in v0.33.0

func WithOIDCProvider(ctx context.Context, provider string) context.Context

WithOIDCProvider stores the name of the OIDC provider that authenticated the request.

Types

type AuthClaims

type AuthClaims struct {
	Subject   string   `json:"sub"`
	Name      string   `json:"name"`
	Email     string   `json:"email"`
	TenantID  string   `json:"tid"`
	ObjectID  string   `json:"oid"`
	IDType    string   `json:"idtyp"`
	Groups    []string `json:"groups"`
	Roles     []string `json:"roles"`
	Audience  string   `json:"aud"`
	Issuer    string   `json:"iss"`
	ExpiresAt int64    `json:"exp"`
}

AuthClaims holds the validated JWT claims extracted from an Entra OIDC token.

func ClaimsFromContext

func ClaimsFromContext(ctx context.Context) *AuthClaims

ClaimsFromContext extracts auth claims from the request context.

func (*AuthClaims) CallerType added in v0.17.0

func (c *AuthClaims) CallerType() IdentityType

CallerType returns IdentityTypeApp for service principal tokens (idtyp=app) and IdentityTypeUser for all other callers. Per Entra spec, the idtyp claim is absent for user tokens and set to "app" for application/service principal tokens.

type GzipResponseWriter

type GzipResponseWriter struct {
	Writer         io.Writer
	ResponseWriter http.ResponseWriter
}

GzipResponseWriter wraps an http.ResponseWriter with a gzip writer.

func (*GzipResponseWriter) Close

func (g *GzipResponseWriter) Close() error

Close closes the underlying gzip writer.

func (*GzipResponseWriter) Flush

func (g *GzipResponseWriter) Flush()

Flush flushes the underlying gzip writer and response writer.

func (*GzipResponseWriter) Header

func (g *GzipResponseWriter) Header() http.Header

Header returns the header map.

func (*GzipResponseWriter) Write

func (g *GzipResponseWriter) Write(b []byte) (int, error)

Write writes the compressed data.

func (*GzipResponseWriter) WriteHeader

func (g *GzipResponseWriter) WriteHeader(code int)

WriteHeader sends an HTTP response header with the provided status code.

type IdentityType added in v0.33.0

type IdentityType int
const (
	IdentityTypeUnknown IdentityType = iota
	IdentityTypeUser
	IdentityTypeApp
)

Jump to

Keyboard shortcuts

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