Documentation
¶
Index ¶
- Constants
- Variables
- func GenerateKey() (keyBinary []byte, err error)
- func PackKeyToString(keyBinary []byte) (keyText string)
- func UnpackKeyFromString(keyText string) (keyBinary []byte, err error)
- type CSRFHelper
- func (h *CSRFHelper) ClearCSRFTokenCookie(w http.ResponseWriter) (err error)
- func (h *CSRFHelper) Initialize() (err error)
- func (h *CSRFHelper) SessionIdentFromCSRFTokenCookie(r *http.Request) (sessionIdent []byte, shouldRenew bool, err error)
- func (h *CSRFHelper) SessionIdentFromCSRFTokenHeader(r *http.Request) (sessionIdent []byte, shouldRenew bool, err error)
- func (h *CSRFHelper) SetCSRFTokenCookie(w http.ResponseWriter, sessionIdent []byte) (err error)
Constants ¶
const DefaultCSRFTokenCookieName = "XSRF-TOKEN"
DefaultCSRFTokenCookieName is the default name of CSRF cookie if not given.
const DefaultCSRFTokenHeaderName = "X-XSRF-TOKEN"
DefaultCSRFTokenHeaderName is the default name of CSRF header if not given.
const DefaultMaxAge = time.Hour
DefaultMaxAge is the default max-age for session and CSRF token cookies.
const DefaultRenewAge = time.Minute * 30
DefaultRenewAge is the default renew age for session and CSRF token cookies.
const KeySize = 32
KeySize is the required size in bytes for encryption key.
Variables ¶
var ErrCheckSumNotMatch = errors.New("checksum does not match")
ErrCheckSumNotMatch indicate the checksum is not valid.
var ErrIncompleteEncryptedContent = errors.New("incomplete encrypted content")
ErrIncompleteEncryptedContent indicate the given encrypted content is smaller than expect.
var ErrKeySizeInsufficient = errors.New("insufficient bytes for encryption key")
ErrKeySizeInsufficient indicates the given key does not have sufficient bytes.
var ErrTokenExpired = errors.New("token expired")
ErrTokenExpired indicate given token is expired.
Functions ¶
func GenerateKey ¶
GenerateKey generate a key with required key size.
func PackKeyToString ¶
PackKeyToString encode given key binary into string with BASE-64 encoding.
func UnpackKeyFromString ¶
UnpackKeyFromString decode given key binary from string in BASE-64 encoding.
Types ¶
type CSRFHelper ¶
type CSRFHelper struct { KeyBinary []byte HashMask uint32 CookiePath string CookieDomain string CSRFTokenCookieName string CSRFTokenHeaderName string MaxCSRFTokenAge time.Duration RenewCSRFTokenAge time.Duration // contains filtered or unexported fields }
CSRFHelper provide functions to verify CSRF token.
func (*CSRFHelper) ClearCSRFTokenCookie ¶
func (h *CSRFHelper) ClearCSRFTokenCookie(w http.ResponseWriter) (err error)
ClearCSRFTokenCookie set the CSRF token to empty value.
func (*CSRFHelper) Initialize ¶
func (h *CSRFHelper) Initialize() (err error)
Initialize fill empty fields with default values and prepare internal fields.
func (*CSRFHelper) SessionIdentFromCSRFTokenCookie ¶
func (h *CSRFHelper) SessionIdentFromCSRFTokenCookie(r *http.Request) (sessionIdent []byte, shouldRenew bool, err error)
SessionIdentFromCSRFTokenCookie fetch sessionIdent from CSRF token cookie.
func (*CSRFHelper) SessionIdentFromCSRFTokenHeader ¶
func (h *CSRFHelper) SessionIdentFromCSRFTokenHeader(r *http.Request) (sessionIdent []byte, shouldRenew bool, err error)
SessionIdentFromCSRFTokenHeader fetch sessionIdent from CSRF token header.
func (*CSRFHelper) SetCSRFTokenCookie ¶
func (h *CSRFHelper) SetCSRFTokenCookie(w http.ResponseWriter, sessionIdent []byte) (err error)
SetCSRFTokenCookie encrypt sessionIdent and set cookie header with given session cookie name.