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 ¶
- Constants
- 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 OauthConfigs
- type PublicKeyProvider
- type PublicKeys
- type RequestLog
- type StatusResponseWriter
Constants ¶
const APIKey authMethod = 2
const (
JWTClaim authMethod = iota // JWTClaim represents the key used to store JWT claims within the request context.
)
const Username authMethod = 1
Variables ¶
This section is empty.
Functions ¶
func APIKeyAuthMiddleware ¶ added in v1.1.1
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 ¶ added in v1.1.1
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 ¶ added in v0.3.0
Metrics is a middleware that records request response time metrics using the provided metrics interface.
func OAuth ¶ added in v1.1.1
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 ¶ added in v1.11.0
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 ¶ added in v1.1.1
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 ¶ added in v1.1.1
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 ¶ added in v1.1.1
type JWKNotFound struct { }
JWKNotFound is an error type indicating a missing JSON Web Key Set (JWKS).
func (JWKNotFound) Error ¶ added in v1.1.1
func (JWKNotFound) Error() string
type JWKS ¶ added in v1.1.1
type JWKS struct {
Keys []JSONWebKey `json:"keys"`
}
JWKS represents a JSON Web Key Set.
type JWKSProvider ¶ added in v1.1.1
type OauthConfigs ¶ added in v1.1.1
type OauthConfigs struct { Provider JWKSProvider RefreshInterval time.Duration }
OauthConfigs holds configuration for OAuth middleware.
type PublicKeyProvider ¶ added in v1.1.1
PublicKeyProvider defines an interface for retrieving a public key by its key ID.
func NewOAuth ¶ added in v1.1.1
func NewOAuth(config OauthConfigs) PublicKeyProvider
NewOAuth creates a PublicKeyProvider that periodically fetches and updates public keys from a JWKS endpoint.
type PublicKeys ¶ added in v1.1.1
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 ¶ added in v1.4.0
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)