Documentation ¶
Overview ¶
Package middleware provides a collection of middleware functions that handles various aspects of request handling, such as authentication, logging, tracing, and metrics collection.
Index ¶
- func APIKeyAuthMiddleware(a APIKeyAuthProvider, apiKeys ...string) func(handler http.Handler) http.Handler
- func BasicAuthMiddleware(basicAuthProvider BasicAuthProvider) func(handler http.Handler) http.Handler
- func CORS(middlewareConfigs map[string]string, routes *[]string) func(inner http.Handler) http.Handler
- func GetConfigs(c config.Config) map[string]string
- func Logging(logger logger) func(inner http.Handler) http.Handler
- func Metrics(metrics metrics) func(inner http.Handler) http.Handler
- func OAuth(key PublicKeyProvider) func(inner http.Handler) http.Handler
- func Tracer(inner http.Handler) http.Handler
- func WSHandlerUpgrade(c *container.Container, wsManager *websocket.Manager) func(inner http.Handler) http.Handler
- type APIKeyAuthProvider
- type BasicAuthProvider
- type JSONWebKey
- type JWKNotFound
- type JWKS
- type JWKSProvider
- type JWTClaim
- type OauthConfigs
- type PublicKeyProvider
- type PublicKeys
- type RequestLog
- type StatusResponseWriter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func APIKeyAuthMiddleware ¶
func APIKeyAuthMiddleware(a APIKeyAuthProvider, apiKeys ...string) func(handler http.Handler) http.Handler
APIKeyAuthMiddleware creates a middleware function that enforces API key authentication based on the provided API keys or a validation function.
func BasicAuthMiddleware ¶
func BasicAuthMiddleware(basicAuthProvider BasicAuthProvider) func(handler http.Handler) http.Handler
BasicAuthMiddleware creates a middleware function that enforces basic authentication using the provided BasicAuthProvider.
func CORS ¶
func CORS(middlewareConfigs map[string]string, routes *[]string) func(inner http.Handler) http.Handler
CORS is a middleware that adds CORS (Cross-Origin Resource Sharing) headers to the response.
func Logging ¶
Logging is a middleware which logs response status and time in milliseconds along with other data.
func Metrics ¶
Metrics is a middleware that records request response time metrics using the provided metrics interface.
func OAuth ¶
func OAuth(key PublicKeyProvider) func(inner http.Handler) http.Handler
OAuth is a middleware function that validates JWT access tokens using a provided PublicKeyProvider.
Types ¶
type APIKeyAuthProvider ¶
type APIKeyAuthProvider struct { ValidateFunc func(apiKey string) bool ValidateFuncWithDatasources func(c *container.Container, apiKey string) bool Container *container.Container }
APIKeyAuthProvider represents a basic authentication provider.
type BasicAuthProvider ¶
type BasicAuthProvider struct { Users map[string]string ValidateFunc func(username, password string) bool ValidateFuncWithDatasources func(c *container.Container, username, password string) bool Container *container.Container }
BasicAuthProvider represents a basic authentication provider.
type JSONWebKey ¶
type JSONWebKey struct { ID string `json:"kid"` Type string `json:"kty"` Modulus string `json:"n"` PublicExponent string `json:"e"` PrivateExponent string `json:"d"` }
JSONWebKey represents a JSON Web Key.
type JWKNotFound ¶
type JWKNotFound struct { }
JWKNotFound is an error type indicating a missing JSON Web Key Set (JWKS).
func (JWKNotFound) Error ¶
func (JWKNotFound) Error() string
type JWKS ¶
type JWKS struct {
Keys []JSONWebKey `json:"keys"`
}
JWKS represents a JSON Web Key Set.
type JWKSProvider ¶
type JWTClaim ¶
type JWTClaim string
JWTClaim represents a custom key used to store JWT claims within the request context.
type OauthConfigs ¶
type OauthConfigs struct { Provider JWKSProvider RefreshInterval time.Duration }
OauthConfigs holds configuration for OAuth middleware.
type PublicKeyProvider ¶
PublicKeyProvider defines an interface for retrieving a public key by its key ID.
func NewOAuth ¶
func NewOAuth(config OauthConfigs) PublicKeyProvider
NewOAuth creates a PublicKeyProvider that periodically fetches and updates public keys from a JWKS endpoint.
type PublicKeys ¶
type PublicKeys struct {
// contains filtered or unexported fields
}
PublicKeys stores a map of public keys identified by their key ID (kid).
type RequestLog ¶
type RequestLog struct { TraceID string `json:"trace_id,omitempty"` SpanID string `json:"span_id,omitempty"` StartTime string `json:"start_time,omitempty"` ResponseTime int64 `json:"response_time,omitempty"` Method string `json:"method,omitempty"` UserAgent string `json:"user_agent,omitempty"` IP string `json:"ip,omitempty"` URI string `json:"uri,omitempty"` Response int `json:"response,omitempty"` }
RequestLog represents a log entry for HTTP requests.
func (*RequestLog) PrettyPrint ¶
func (rl *RequestLog) PrettyPrint(writer io.Writer)
type StatusResponseWriter ¶
type StatusResponseWriter struct { http.ResponseWriter // contains filtered or unexported fields }
StatusResponseWriter Defines own Response Writer to be used for logging of status - as http.ResponseWriter does not let us read status.
func (*StatusResponseWriter) WriteHeader ¶
func (w *StatusResponseWriter) WriteHeader(status int)