middleware

package
v0.0.0-...-85b7a69 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2018 License: MIT Imports: 25 Imported by: 2

Documentation

Index

Constants

View Source
const (
	BestCompression = iota
	BestSpeed
	DefaultCompression
	NoCompression
	MaxCompressionLevel
)
View Source
const (
	GzipMethod = iota
	DeflateMethod
	MaxCompressionMethod
)

Variables

View Source
var BasicRealm = "Authorization Required"

BasicRealm is used when setting the WWW-Authenticate response header.

Functions

func Basic

func Basic(username string, password string) kelly.HandlerFunc

Basic returns a Handler that authenticates via Basic Auth. Writes a http.StatusUnauthorized if authentication fails.

func BasicFunc

func BasicFunc(authfn func(string, string) bool) kelly.HandlerFunc

BasicFunc returns a Handler that authenticates via Basic Auth using the provided function. The function should return true for a valid username/password combination.

func Cors

func Cors(config *CorsConfig) kelly.HandlerFunc

New returns the location middleware with user-defined custom configuration.

func Csrf

func Csrf() kelly.HandlerFunc

Middleware validates CSRF token.

func DefaultCors

func DefaultCors() kelly.HandlerFunc

Default returns the location middleware with default configuration.

func GetCsrfToken

func GetCsrfToken(c *kelly.Context) string

GetToken returns a CSRF token.

func Gzip

func Gzip(level int, method int) kelly.HandlerFunc

func InitCsrf

func InitCsrf(options CsrfConfig)

func NoCache

func NoCache() kelly.HandlerFunc

func Secure

func Secure(config *SecureConfig) kelly.HandlerFunc

func Version

func Version(ver string) kelly.HandlerFunc

Types

type CorsConfig

type CorsConfig struct {
	AllowAllOrigins bool

	// AllowedOrigins is a list of origins a cross-domain request can be executed from.
	// If the special "*" value is present in the list, all origins will be allowed.
	// Default value is ["*"]
	AllowOrigins []string

	// AllowOriginFunc is a custom function to validate the origin. It take the origin
	// as argument and returns true if allowed or false otherwise. If this option is
	// set, the content of AllowedOrigins is ignored.
	AllowOriginFunc func(origin string) bool

	// AllowedMethods is a list of methods the client is allowed to use with
	// cross-domain requests. Default value is simple methods (GET and POST)
	AllowMethods []string

	// AllowedHeaders is list of non simple headers the client is allowed to use with
	// cross-domain requests.
	// If the special "*" value is present in the list, all headers will be allowed.
	// Default value is [] but "Origin" is always appended to the list.
	AllowHeaders []string

	// AllowCredentials indicates whether the request can include user credentials like
	// cookies, HTTP authentication or client side SSL certificates.
	AllowCredentials bool

	// ExposedHeaders indicates which headers are safe to expose to the API of a CORS
	// API specification
	ExposeHeaders []string

	// MaxAge indicates how long (in seconds) the results of a preflight request
	// can be cached
	MaxAge time.Duration
}

Config represents all available options for the middleware.

func DefaultCorsConfig

func DefaultCorsConfig() *CorsConfig

DefaultConfig returns a generic default configuration mapped to localhost.

func (*CorsConfig) AddAllowHeaders

func (c *CorsConfig) AddAllowHeaders(headers ...string)

AddAllowHeaders is allowed to add custom headers

func (*CorsConfig) AddAllowMethods

func (c *CorsConfig) AddAllowMethods(methods ...string)

AddAllowMethods is allowed to add custom methods

func (*CorsConfig) AddExposeHeaders

func (c *CorsConfig) AddExposeHeaders(headers ...string)

AddExposeHeaders is allowed to add custom expose headers

func (CorsConfig) Validate

func (c CorsConfig) Validate() error

Validate is check configuration of user defined.

type CsrfConfig

type CsrfConfig struct {
	Secret        []byte
	IgnoreMethods []string
	ErrorFunc     kelly.HandlerFunc
	TokenGetter   func(c *kelly.Context) string
}

CsrfConfig stores configurations for a CSRF middleware.

type FreshnessInformer

type FreshnessInformer interface {
	IsFresh() bool
}

type KeyValueStorer

type KeyValueStorer interface {
	// Simple Get Function
	Get(key string) ([]byte, error)
	// Simple Set Function
	Set(key string, value []byte) error
}

KeyValueStorer is the required interface for the Store Option This should allow for either drop-in replacement with compatible libraries, or easy write-up of adapters

type MapStoreError

type MapStoreError string

Error Type for the key value store

func (MapStoreError) Error

func (err MapStoreError) Error() string

The Error for Key Value Store

type Quota

type Quota struct {
	// The Request Limit
	Limit uint64
	// The time window for the request Limit
	Within time.Duration
}

The Quota is Request Rates per Time for a given policy

func (*Quota) KeyId

func (q *Quota) KeyId() string

type SecureConfig

type SecureConfig struct {
	// AllowedHosts is a list of fully qualified domain names that are allowed.
	//Default is empty list, which allows any and all host names.
	AllowedHosts []string
	// If SSLRedirect is set to true, then only allow https requests.
	// Default is false.
	SSLRedirect bool
	// If SSLTemporaryRedirect is true, the a 302 will be used while redirecting.
	// Default is false (301).
	SSLTemporaryRedirect bool
	// SSLHost is the host name that is used to redirect http requests to https.
	// Default is "", which indicates to use the same host.
	SSLHost string
	// STSSeconds is the max-age of the Strict-Transport-Security header.
	// Default is 0, which would NOT include the header.
	STSSeconds int64
	// If STSIncludeSubdomains is set to true, the `includeSubdomains` will
	// be appended to the Strict-Transport-Security header. Default is false.
	STSIncludeSubdomains bool
	// If FrameDeny is set to true, adds the X-Frame-Options header with
	// the value of `DENY`. Default is false.
	FrameDeny bool
	// CustomFrameOptionsValue allows the X-Frame-Options header value
	// to be set with a custom value. This overrides the FrameDeny option.
	CustomFrameOptionsValue string
	// If ContentTypeNosniff is true, adds the X-Content-Type-Options header
	// with the value `nosniff`. Default is false.
	ContentTypeNosniff bool
	// If BrowserXssFilter is true, adds the X-XSS-Protection header with
	// the value `1; mode=block`. Default is false.
	BrowserXssFilter bool
	// ContentSecurityPolicy allows the Content-Security-Policy header value
	// to be set with a custom value. Default is "".
	// http://www.ruanyifeng.com/blog/2016/09/csp.html  XSS攻击
	ContentSecurityPolicy string
	// When true, the whole secury policy applied by the middleware is disable
	// completely.
	IsDevelopment bool
	//// Handlers for when an error occurs (ie bad host).
	BadHostHandler kelly.HandlerFunc
}

Options is a struct for specifying configuration options for the secure.

func DefaultSecureConfig

func DefaultSecureConfig() *SecureConfig

type ServeHTTP

type ServeHTTP func(http.ResponseWriter, *http.Request) (*http.Request, bool)

func Throttle

func Throttle(quota *Quota, options ...*ThrottleConfig) ServeHTTP

A throttling Policy Takes two arguments, one required: First is a Quota (A Limit with an associated time). When the given Limit of requests is reached by a user within the given time window, access to access to resources will be denied to this user Second is ThrottleConfig to use with this policy. For further information on options, see ThrottleConfig further above.

type ThrottleConfig

type ThrottleConfig struct {
	// The status code to be returned for throttled requests
	// Defaults to 429 Too Many Requests
	StatusCode int

	// The message to be returned as the body of throttled requests
	Message string

	// The function used to identify the requester
	// Defaults to IP identification
	IdentificationFunction func(*http.Request) string

	// The key prefix to use in any key value store
	// defaults to "throttle"
	KeyPrefix string

	// The store to use
	// defaults to a simple concurrent-safe map[string]string
	Store KeyValueStorer

	// If the throttle is disabled or not
	// defaults to false
	Disabled bool
}

func (*ThrottleConfig) Identify

func (o *ThrottleConfig) Identify(req *http.Request) string

Identify via the given Identification Function

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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