middleware

package
v0.0.0-...-7296e1c Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: Apache-2.0 Imports: 19 Imported by: 1

Documentation

Index

Constants

View Source
const RequestIDHeader = "X-Request-Id"

Variables

This section is empty.

Functions

func Apply

func Apply(h http.Handler) http.Handler

Apply applies all registered middleware functions to the given handler.

func CORSWithOptions

func CORSWithOptions(options *CORSOptions) func(http.Handler) http.Handler

CORSWithOptions creates a CORS middleware with the provided configuration. If options is nil, it will use the default CORS settings. If options is an empty struct (CORSOptions{}), it will create a middleware with no CORS headers.

func GetLogEntryMetadata

func GetLogEntryMetadata(ctx context.Context) map[string]interface{}

Retrieve log metadata from context

func LoggerWithOptions

func LoggerWithOptions(options *LoggerOptions) func(http.Handler) http.Handler

func Postgres

func Postgres(pool *pgxpool.Pool, authorizers ...AuthzFunc) func(http.Handler) http.Handler

Postgres middleware attaches a connection from pool to the request context if the http request user is authorized.

func Proxy

func Proxy(target string, opts Options) http.HandlerFunc

Serve creates a reverse proxy handler based on the given target and options

func Register

func Register(middlewares ...Middleware)

Register adds one or more middleware function to the registry.

func RequestID

func RequestID(next http.Handler) http.Handler

RequestID middleware generates a unique request ID and tracks request duration.

func VerifyBasicAuth

func VerifyBasicAuth(config *BasicAuthConfig) func(http.Handler) http.Handler

VerifyBasicAuth is a middleware function for basic authentication.

func VerifyOIDCToken

func VerifyOIDCToken(oidcCfg OIDCProviderConfig, send401Unauthorized ...bool) func(http.Handler) http.Handler

VerifyOIDCToken is middleware that verifies OIDC tokens in Authorization headers. By default, it sends a 401 Unauthorized response if the token is missing or invalid. If send401Unauthorized is false, it allows requests with other authorization schemes (e.g., Basic Auth) to continue without interference.

Types

type AuthzFunc

type AuthzFunc func(ctx context.Context) (AuthzResponse, error)

AuthzFunc defines the function signature for authorization checks

func PgAnonAuthz

func PgAnonAuthz() AuthzFunc

WithAnonAuthz returns an authorization function for anonymous users

func PgBasicAuthz

func PgBasicAuthz() AuthzFunc

WithBasicAuthz returns an authorization function for Basic Auth

func PgOIDCAuthz

func PgOIDCAuthz(oidcCfg OIDCProviderConfig, pgRoleClaimKey string) AuthzFunc

PgOIDCAuthz is the main authorization function

type AuthzResponse

type AuthzResponse struct {
	Role    string `json:"role"`
	Allowed bool   `json:"allowed"`
}

AuthzResponse represents the result of an authorization check

type BasicAuthConfig

type BasicAuthConfig struct {
	Credentials map[string]string
}

BasicAuthConfig holds the username-password pairs for basic authentication.

func BasicAuthCreds

func BasicAuthCreds(credentials map[string]string) *BasicAuthConfig

NewBasicAuthCreds creates a new instance of BasicAuthConfig with multiple username/password pairs.

type CORSOptions

type CORSOptions struct {
	AllowedOrigins   []string
	AllowedMethods   []string
	AllowedHeaders   []string
	AllowCredentials bool
}

CORSOptions defines configuration for CORS.

type Cache

type Cache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Cache is a simple in-memory cache with expiration

func NewCache

func NewCache() *Cache

NewCache creates a new Cache

func (*Cache) CleanupExpired

func (c *Cache) CleanupExpired()

CleanupExpired removes expired items from the cache

func (*Cache) Get

func (c *Cache) Get(key string) (interface{}, bool)

Get retrieves an item from the cache

func (*Cache) Set

func (c *Cache) Set(key string, value interface{}, duration time.Duration)

Set adds an item to the cache with a specified expiration duration

type LoggerOptions

type LoggerOptions struct {
	Logger *zap.Logger
	Format func(reqID string, rec *ResponseRecorder, r *http.Request, latency time.Duration) []zap.Field
}

LoggerOptions defines configuration for the logger middleware.

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware is a function that wraps an HTTP handler.

type OIDCProvider

type OIDCProvider struct {
	// contains filtered or unexported fields
}

OIDCProvider is the main OIDC provider

func InitOIDCProvider

func InitOIDCProvider(cfg OIDCProviderConfig) *OIDCProvider

type OIDCProviderConfig

type OIDCProviderConfig struct {
	ClientID     string `json:"client_id"`
	ClientSecret string `json:"client_secret"`
	Issuer       string `json:"issuer"`
}

OIDCProviderConfig holds the configuration for the OIDC provider

type Options

type Options struct {
	TrimPrefix    string
	ForwardedHost string
	TLSConfig     *tls.Config
}

Options holds the options for the proxy server

type ResponseRecorder

type ResponseRecorder struct {
	http.ResponseWriter
	StatusCode int
	// contains filtered or unexported fields
}

ResponseRecorder is a wrapper for http.ResponseWriter to capture status codes and durations.

func NewResponseRecorder

func NewResponseRecorder(w http.ResponseWriter) *ResponseRecorder

func (*ResponseRecorder) Write

func (rr *ResponseRecorder) Write(b []byte) (int, error)

func (*ResponseRecorder) WriteHeader

func (rr *ResponseRecorder) WriteHeader(statusCode int)

Jump to

Keyboard shortcuts

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