fiberadapter

package
v3.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 10 Imported by: 1

Documentation

Overview

Package fiberadapter exposes middleware-kit over Fiber v3.

It lives in its own package so that importing the root package does not drag Fiber -- and with it fasthttp -- into binaries that never use it. A service on net/http, Echo, Gin or chi pays nothing for Fiber support existing; only importing this package links it in.

Every middleware here is the Fiber half of a pair whose net/http half stays in the root package as XxxStd. The rules they share -- what a valid key or signature is, which IP to trust, what counts as sensitive -- live in the root package and are read from there.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIKeyAuth

func APIKeyAuth(cfg APIKeyConfig) fiber.Handler

APIKeyAuth returns a Fiber middleware that authenticates requests by API key. It is the Fiber half of the pair whose net/http half is middleware.APIKeyAuthStd, and reads the key from the same places: the configured header, then the Authorization scheme, then the query parameter -- each only where cfg enables it.

func BodyLimit

func BodyLimit(cfg BodyLimitConfig) fiber.Handler

BodyLimit returns a Fiber middleware that refuses requests whose body exceeds cfg.MaxSize (4MB by default). It is the Fiber half of the pair whose net/http half is middleware.BodyLimitStd.

Both the advertised Content-Length and the decoded body are checked, because a chunked request advertises no length at all.

func CombinedAuth

func CombinedAuth(cfg AuthConfig) fiber.Handler

CombinedAuth returns a Fiber middleware that tries every configured authentication scheme in order of decreasing strength -- mTLS, then HMAC, then API key -- and admits the request on the first that succeeds.

Each scheme runs the same check its dedicated middleware runs, so a request this middleware admits is one MTLSAuth, HMACAuth or APIKeyAuth would admit too. With no scheme configured the request is refused unless AllowNoAuth is set.

func GetClientIPFiber

func GetClientIPFiber(c fiber.Ctx, trustedConfig *middleware.TrustedProxyConfig) string

GetClientIPFiber resolves the client IP of a Fiber request. It applies the same trusted-proxy rules and the same right-to-left chain walk as middleware.GetClientIP: forwarded headers are read only when the direct peer is a trusted proxy, and the chain is walked from the right so a client- supplied prefix cannot win.

A nil trustedConfig means middleware.DefaultTrustedProxyConfig.

func HMACAuth

func HMACAuth(cfg HMACConfig) fiber.Handler

HMACAuth returns a Fiber middleware that authenticates requests by their HMAC signature. It is the Fiber half of the pair whose net/http half is middleware.HMACAuthStd, and applies the same rules in the same order: the timestamp must be within MaxTimeDrift, the service identifier must be unambiguous for the configured signer, the signature must match, and only then is it recorded with the ReplayGuard.

func MTLSAuth

func MTLSAuth(cfg MTLSConfig) fiber.Handler

MTLSAuth returns a Fiber middleware that authenticates requests by their client certificate. It is the Fiber half of the pair whose net/http half is middleware.MTLSAuthStd, and runs the same middleware.AuthenticateMTLS check: the certificate must have been verified by the TLS layer AND satisfy every restriction in cfg.

TLS is expected to be configured with tls.RequireAndVerifyClientCert (or VerifyClientCertIfGiven together with RequireCert=false). A connection whose certificate the TLS layer did not verify is rejected, whatever Subject it carries.

func NoCacheHeaders

func NoCacheHeaders() fiber.Handler

NoCacheHeaders returns a Fiber middleware that sets the cache-control headers which keep a response out of every cache. It is the Fiber half of the pair whose net/http half is middleware.NoCacheHeadersStd.

func RateLimit

func RateLimit(cfg RateLimitConfig) fiber.Handler

RateLimit returns a Fiber middleware that rate-limits requests by key. It is the Fiber half of the pair whose net/http half is middleware.RateLimitStd and shares its limiter, so one middleware.RateLimiter can bound a service serving both frameworks.

The key comes from cfg.KeyFunc, or from GetClientIPFiber when that is nil.

func RequestLogging

func RequestLogging(cfg LoggingConfig) fiber.Handler

RequestLogging returns a Fiber middleware that logs one entry per request. It is the Fiber half of the pair whose net/http half is middleware.RequestLoggingStd, and masks the same sensitive headers.

Responses at 400 and above are logged at cfg.ErrorLogLevel, everything else at cfg.LogLevel.

func SecurityHeaders

func SecurityHeaders(cfg middleware.SecurityHeadersConfig) fiber.Handler

SecurityHeaders returns a Fiber middleware that sets the response security headers cfg enables. It is the Fiber half of the pair whose net/http half is middleware.SecurityHeadersStd, and takes the root config unchanged -- it has no Fiber-typed hooks.

Types

type APIKeyConfig

type APIKeyConfig struct {
	middleware.APIKeyConfig

	// ErrorHandler is called when authentication fails.
	// If nil, returns 401 Unauthorized with a generic message.
	ErrorHandler func(c fiber.Ctx, err error) error

	// SuccessHandler is called when authentication succeeds.
	SuccessHandler func(c fiber.Ctx)
}

APIKeyConfig is middleware.APIKeyConfig plus the Fiber hooks.

type AuthConfig

type AuthConfig struct {
	middleware.AuthConfig

	// ErrorHandler is called when every configured scheme has failed.
	ErrorHandler func(c fiber.Ctx, err error) error
}

AuthConfig is middleware.AuthConfig plus the Fiber hook.

type BodyLimitConfig

type BodyLimitConfig struct {
	middleware.BodyLimitConfig

	// ErrorHandler is called when the body exceeds the limit.
	ErrorHandler func(c fiber.Ctx) error
}

BodyLimitConfig is middleware.BodyLimitConfig plus the Fiber hook.

type HMACConfig

type HMACConfig struct {
	middleware.HMACConfig

	// ErrorHandler is called when authentication fails.
	ErrorHandler func(c fiber.Ctx, err error) error

	// SuccessHandler is called when authentication succeeds.
	SuccessHandler func(c fiber.Ctx)
}

HMACConfig is middleware.HMACConfig plus the Fiber hooks.

type LoggingConfig

type LoggingConfig struct {
	middleware.LoggingConfig

	// CustomFields adds fields to each log entry.
	CustomFields func(c fiber.Ctx) map[string]interface{}
}

LoggingConfig is middleware.LoggingConfig plus the Fiber hook.

type MTLSConfig

type MTLSConfig struct {
	middleware.MTLSConfig

	// ErrorHandler is called when authentication fails.
	ErrorHandler func(c fiber.Ctx, err error) error

	// SuccessHandler is called when authentication succeeds.
	SuccessHandler func(c fiber.Ctx, cert *x509.Certificate)
}

MTLSConfig is middleware.MTLSConfig plus the Fiber hooks.

type RateLimitConfig

type RateLimitConfig struct {
	middleware.RateLimitConfig

	// KeyFunc extracts the rate-limit key from the request.
	// Default: uses client IP.
	KeyFunc func(c fiber.Ctx) string

	// ErrorHandler is called when the rate limit is exceeded.
	ErrorHandler func(c fiber.Ctx) error
}

RateLimitConfig is middleware.RateLimitConfig plus the Fiber hooks.

Jump to

Keyboard shortcuts

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