webmiddleware

package
v3.32.1 Latest Latest
Warning

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

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

Documentation

Overview

Package webmiddleware provides middleware for http Handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain added in v3.8.2

func Chain(middlewares []MiddlewareFunc, handler http.Handler) http.Handler

Chain returns a http.Handler that chains the middleware onion-style around the handler.

func GetSecureCookie added in v3.9.0

func GetSecureCookie(ctx context.Context) (*securecookie.SecureCookie, error)

GetSecureCookie retrieves the secure cookie encoder from the context.

func NoCache added in v3.16.1

func NoCache(next http.Handler) http.Handler

NoCache is a middleware function that adds headers that prevent proxies from caching responses.

Types

type Authenticator

type Authenticator interface {
	// Authenticate the user with the given password and return true if successful.
	// An error may be returned if an internal error occurred.
	Authenticate(username, password string) (ok bool, err error)
}

Authenticator is the interface the Basic Auth middleware uses to authenticate users.

func AuthUser

func AuthUser(username, password string) Authenticator

AuthUser is the same as AuthUsers, but for a single user.

func AuthUsers

func AuthUsers(usernamesPasswords map[string]string) Authenticator

AuthUsers authenticates users with the given map[username]password.

type CORSConfig

type CORSConfig struct {
	AllowedHeaders   []string
	AllowedMethods   []string
	AllowedOrigins   []string
	ExposedHeaders   []string
	MaxAge           int
	AllowCredentials bool
}

CORSConfig is the configuration for the CORS middleware.

type MiddlewareFunc

type MiddlewareFunc func(next http.Handler) http.Handler

MiddlewareFunc is a function that acts as middleware for http Handlers.

func BasicAuth

func BasicAuth(realm string, authenticator Authenticator) MiddlewareFunc

BasicAuth returns a middleware that authenticates users with Basic Auth.

func CORS

func CORS(config CORSConfig) MiddlewareFunc

CORS returns a middleware that handles Cross-Origin Resource Sharing.

func CSRF added in v3.9.0

func CSRF(authKey []byte, opts ...csrf.Option) MiddlewareFunc

CSRF returns a middleware that enables CSRF protection via a sync token. The skipCheck parameter can be used to skip CSRF protection based on the request interface.

func Conditional added in v3.9.0

func Conditional(middleware MiddlewareFunc, condition func(r *http.Request) bool) MiddlewareFunc

Conditional is a middleware that only executes middleware if the condition returns true for the request. If the condition returns false, the middleware is skipped, and request handling moves on to the next handler in the chain.

func CookieAuth added in v3.9.0

func CookieAuth(cookieName string) MiddlewareFunc

CookieAuth extracts the auth cookie and forwards it to the Authorization header.

func Cookies added in v3.9.0

func Cookies(hashKey, blockKey []byte) MiddlewareFunc

Cookies is a middleware that allows handling of secure cookies.

func FillContext

func FillContext(fillers ...fillcontext.Filler) MiddlewareFunc

FillContext returns a middleware that fills global context into a call context.

func Log

func Log(logger log.Interface, ignorePathsArray []string) MiddlewareFunc

Log returns a middleware that logs requests. If logger is nil, the logger will be extracted from the context.

func MaxBody

func MaxBody(maxBytes int64) MiddlewareFunc

MaxBody returns a middleware that limits the maximum body of requests.

func Metadata

func Metadata(keys ...string) MiddlewareFunc

Metadata returns a middleware that sets gRPC metadata from the request headers.

func Namespace added in v3.8.2

func Namespace(value string) MiddlewareFunc

Namespace is middleware that sets the namespace.

func Peer added in v3.11.2

func Peer() MiddlewareFunc

Peer sets the remote address as a peer in the request context.

func ProxyHeaders

func ProxyHeaders(config ProxyConfiguration) MiddlewareFunc

ProxyHeaders processes proxy headers for trusted proxies.

func Recover

func Recover() MiddlewareFunc

Recover returns middleware that recovers from panics.

func Redirect

func Redirect(config RedirectConfiguration) MiddlewareFunc

Redirect returns a middleware that redirects requests if they don't already match the configuration.

func RequestID

func RequestID() MiddlewareFunc

RequestID returns a middleware that inserts a request ID into each request.

func RequestURL

func RequestURL() MiddlewareFunc

RequestURL populates (*http.Request).URL with the scheme and host.

func SecurityHeaders

func SecurityHeaders() MiddlewareFunc

SecurityHeaders returns a middleware that inserts security headers into each response.

type ProxyConfiguration

type ProxyConfiguration struct {
	Trusted []*net.IPNet
}

ProxyConfiguration is the configuration for the ProxyHeaders middleware.

func (*ProxyConfiguration) ParseAndAddTrusted

func (c *ProxyConfiguration) ParseAndAddTrusted(cidrs ...string) error

ParseAndAddTrusted parses a list of CIDRs and adds them to the list of trusted ranges.

type RedirectConfiguration

type RedirectConfiguration struct {
	Scheme   func(string) string
	HostName func(string) string
	Port     func(uint) uint
	Path     func(string) string
	Code     int
}

RedirectConfiguration is the configuration for the Redirect middleware. If any of Scheme, HostName, Port, Path returns a different value than the argument passed to it, the middleware will redirect with the configured Code.

Jump to

Keyboard shortcuts

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