Documentation
¶
Overview ¶
Package csrf offers stateless protection against CSRF attacks using the HTTP Origin header and falling back to HMAC tokens stored on secured and HTTP-only cookies.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Handler ¶
Handler checks Origin header first, if not set or has value "null" it validates using a HMAC CSRF token. For enabling Single Page Applications to send the XSRF cookie using async HTTP requests, use CORS and make sure Access-Control-Allow-Credential is enabled.
Example ¶
package main
import (
"fmt"
"net/http"
"github.com/c4milo/handlers/csrf"
)
func main() {
mux := http.DefaultServeMux
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
// The "/" pattern matches everything, so we need to check
// that we're at the root here.
if req.URL.Path != "/" {
http.NotFound(w, req)
return
}
fmt.Fprintf(w, "Welcome to the home page!")
})
opts := []csrf.Option{
csrf.WithUserID("user ID"),
csrf.WithSecret("my secret!"),
}
rack := csrf.Handler(mux, opts...)
http.ListenAndServe(":8080", rack)
}
Types ¶
type Option ¶
type Option func(*handler)
Option implements http://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html
func WithDomain ¶
WithDomain configures the domain under which the CSRF cookie is going to be set.
func WithSecret ¶
WithSecret configures the secret cryptographic key for signing the token.
func WithUserID ¶
WithUserID allows to configure a random and unique user ID identifier used to generate the CSRF token.