middleware

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: Apache-2.0 Imports: 17 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 added in v1.1.1

func APIKeyAuthMiddleware(validator func(apiKey string) bool, 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() func(inner http.Handler) http.Handler

CORS ia a middleware that adds CORS (Cross-Origin Resource Sharing) headers to the response.

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 added in v0.3.0

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

func Tracer

func Tracer(inner http.Handler) http.Handler

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

Types

type BasicAuthProvider added in v1.1.1

type BasicAuthProvider struct {
	Users        map[string]string
	ValidateFunc func(username, password string) bool
}

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

type JWTClaim added in v1.1.1

type JWTClaim string

JWTClaim represents a custom key used to store JWT claims within the request context.

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

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

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

func (*PublicKeys) Get added in v1.1.1

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

Jump to

Keyboard shortcuts

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