middleware

package
v0.0.0-...-201d7c9 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultCORSOptions = CORSOptions{
	AllowOrigin:      "*",
	AllowHeaders:     []string{"Content-Type", "Authorization"},
	ExposeHeaders:    []string{},
	AllowMethods:     []string{http.MethodGet, http.MethodHead, http.MethodDelete, http.MethodOptions, http.MethodPatch, http.MethodPost},
	AllowCredentials: false,
	MaxAge:           600,
}

Functions

func CORSMiddleware

func CORSMiddleware(opts CORSOptions) func(http.HandlerFunc) http.HandlerFunc

CORSMiddleware creates a middleware function that applies the given CORS options.

func CSRFMiddleware

func CSRFMiddleware(next http.HandlerFunc, isValidToken func(string) bool) http.HandlerFunc

func DefaultErrorHandler

func DefaultErrorHandler(w http.ResponseWriter, message string, code int)

func DefaultTokenExtractor

func DefaultTokenExtractor(r *http.Request) (string, error)

func GenerateCSRFToken

func GenerateCSRFToken() string

func LoggingMiddleware

func LoggingMiddleware(next http.HandlerFunc) http.HandlerFunc

func NewAuthMiddleware

func NewAuthMiddleware(config JWTConfig) func(http.HandlerFunc) http.HandlerFunc

func TimeoutMiddleware

func TimeoutMiddleware(timeout time.Duration) router.Middleware

Types

type CORSOptions

type CORSOptions struct {
	// AllowOrigin specifies the allowed origin for cross-origin requests.
	// Use "*" to allow any origin or specify a specific origin like "http://example.com".
	AllowOrigin string

	// AllowMethods lists the HTTP methods that are allowed for cross-origin requests.
	// Common methods include "GET", "POST", "PUT", etc.
	AllowMethods []string

	// AllowHeaders lists the headers that can be used during a request.
	// This includes headers such as "Content-Type", "Authorization", etc.
	AllowHeaders []string

	// AllowCredentials indicates whether credentials (cookies, HTTP authentication, etc.)
	// are allowed in cross-origin requests. Set to true to allow credentials.
	AllowCredentials bool

	// ExposeHeaders specifies the headers that can be exposed to the browser.
	// This allows the client to read these headers from the response.
	ExposeHeaders []string

	// MaxAge defines how long (in seconds) the results of a preflight request can be cached.
	// A longer MaxAge reduces the number of preflight requests. Set to 0 to disable caching.
	MaxAge int
}

type JWTConfig

type JWTConfig struct {
	SecretKey      []byte
	SigningMethod  jwt.SigningMethod
	ErrorHandler   func(w http.ResponseWriter, message string, code int)
	TokenExtractor func(r *http.Request) (string, error)
}

Jump to

Keyboard shortcuts

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