middleware

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: Apache-2.0 Imports: 24 Imported by: 0

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

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 GetConfigs

func GetConfigs(c config.Config) map[string]string

func Logging

func Logging(logger logger) func(inner http.Handler) http.Handler

Logging is a middleware which logs response status and time in milliseconds along with other data.

func Metrics

func Metrics(metrics metrics) func(inner http.Handler) http.Handler

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.

func Tracer

func Tracer(inner http.Handler) http.Handler

Tracer is a middleware that starts a new OpenTelemetry trace span for each request.

func WSHandlerUpgrade

func WSHandlerUpgrade(c *container.Container, wsManager *websocket.Manager) func(inner http.Handler) http.Handler

WSHandlerUpgrade middleware upgrades the incoming http request to a websocket connection using websocket upgrader.

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 JWKSProvider interface {
	GetWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
		headers map[string]string) (*http.Response, error)
}

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

type PublicKeyProvider interface {
	Get(kid string) *rsa.PublicKey
}

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).

func (*PublicKeys) Get

func (p *PublicKeys) Get(kid string) *rsa.PublicKey

Get retrieves a public key from the PublicKeys map by its key ID.

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)

Jump to

Keyboard shortcuts

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