Documentation
¶
Overview ¶
Package csrf is a middleware that generates and validates CSRF tokens for Flamego.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Csrfer ¶
Csrfer returns a middleware handler that injects csrf.CSRF into the request context, and only generates a new CSRF token on every GET request.
func GenerateToken ¶
GenerateToken returns a URL-safe secure XSRF token that expires in 24 hours.
The key is a secret key for your application, userID is a unique identifier for the user, actionID is the action the user is taking (e.g. POSTing to a particular path).
func ValidToken ¶
ValidToken returns true if token is a valid and unexpired.
Types ¶
type CSRF ¶
type CSRF interface {
// Token returns the current token. This is typically used to populate a hidden
// form in an HTML template.
Token() string
// ValidToken validates the passed token against the existing Secret and ID.
ValidToken(t string) bool
// Error executes the error function with given http.ResponseWriter.
Error(w http.ResponseWriter)
// Validate validates CSRF using given context. It attempts to get the token
// from the HTTP header and then the form value. If any of these is found, the
// token will be validated using ValidToken. If the validation fails, custom
// Error is sent as the response. If neither the header nor form value is found,
// http.StatusBadRequest is sent.
Validate(ctx flamego.Context)
}
CSRF represents a CSRF service and is used to get the current token and validate a suspect token.
type Options ¶
type Options struct {
// Secret is the secret value used to generate tokens. Default is an
// auto-generated 10-char random string.
Secret string
// Header specifies which HTTP header to be used to set and get token. Default
// is "X-CSRF-Token".
Header string
// Form specifies which form value to be used to set and get token. Default is
// "_csrf".
Form string
// SessionKey is the session key used to get the unique ID of users. Default is
// "userID".
SessionKey string
// SetHeader indicates whether to send token via Header. Default is false.
SetHeader bool
// NoOrigin indicates whether to disallow Origin appear in the request header.
// Default is false.
NoOrigin bool
// ErrorFunc defines the function to be executed when ValidToken fails.
ErrorFunc func(w http.ResponseWriter)
}
Options contains options for the csrf.Csrfer middleware.