httpcsrf

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultCSRFTokenCookieName = "XSRF-TOKEN"

DefaultCSRFTokenCookieName is the default name of CSRF cookie if not given.

View Source
const DefaultCSRFTokenHeaderName = "X-XSRF-TOKEN"

DefaultCSRFTokenHeaderName is the default name of CSRF header if not given.

View Source
const DefaultMaxAge = time.Hour

DefaultMaxAge is the default max-age for session and CSRF token cookies.

View Source
const DefaultRenewAge = time.Minute * 30

DefaultRenewAge is the default renew age for session and CSRF token cookies.

View Source
const KeySize = 32

KeySize is the required size in bytes for encryption key.

Variables

View Source
var ErrCheckSumNotMatch = errors.New("checksum does not match")

ErrCheckSumNotMatch indicate the checksum is not valid.

View Source
var ErrIncompleteEncryptedContent = errors.New("incomplete encrypted content")

ErrIncompleteEncryptedContent indicate the given encrypted content is smaller than expect.

View Source
var ErrKeySizeInsufficient = errors.New("insufficient bytes for encryption key")

ErrKeySizeInsufficient indicates the given key does not have sufficient bytes.

View Source
var ErrTokenExpired = errors.New("token expired")

ErrTokenExpired indicate given token is expired.

Functions

func GenerateKey

func GenerateKey() (keyBinary []byte, err error)

GenerateKey generate a key with required key size.

func PackKeyToString

func PackKeyToString(keyBinary []byte) (keyText string)

PackKeyToString encode given key binary into string with BASE-64 encoding.

func UnpackKeyFromString

func UnpackKeyFromString(keyText string) (keyBinary []byte, err error)

UnpackKeyFromString decode given key binary from string in BASE-64 encoding.

Types

type CSRFHelper

type CSRFHelper struct {
	CipherHelper

	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, validWithin time.Duration, shouldRenew bool, err error)

SessionIdentFromCSRFTokenCookie fetch sessionIdent from CSRF token cookie.

func (*CSRFHelper) SessionIdentFromCSRFTokenHeader

func (h *CSRFHelper) SessionIdentFromCSRFTokenHeader(r *http.Request) (sessionIdent []byte, validWithin time.Duration, 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.

type CipherHelper added in v0.1.3

type CipherHelper struct {
	KeyBinary []byte
	HashMask  uint32
}

CipherHelper provide functions to encrypt and decrypt bytes.

func (*CipherHelper) DecryptBase64RawURLEncodedString added in v0.3.0

func (h *CipherHelper) DecryptBase64RawURLEncodedString(encryptedString string) (result []byte, err error)

DecryptBase64RawURLEncodedString decode given encryptedString with base64.RawURLEncoding and decrypt result binary to bytes.

func (*CipherHelper) DecryptBytes added in v0.3.0

func (h *CipherHelper) DecryptBytes(cipherText []byte) (result []byte, err error)

DecryptBytes decode given cipherText and decrypt result binary to bytes.

func (*CipherHelper) DecryptStringToBytes added in v0.2.0

func (h *CipherHelper) DecryptStringToBytes(encryptedString string, base64Encoding *base64.Encoding) (result []byte, err error)

DecryptStringToBytes decode given encryptedString with given base64Encoding and decrypt result binary to bytes.

func (*CipherHelper) EncryptBytes added in v0.1.3

func (h *CipherHelper) EncryptBytes(data []byte) (cipherText []byte, err error)

EncryptBytes encrypt given data bytes and return encrypted binaries.

func (*CipherHelper) EncryptBytesToBase64RawURLEncodedString added in v0.3.0

func (h *CipherHelper) EncryptBytesToBase64RawURLEncodedString(data []byte) (result string, err error)

EncryptBytesToBase64RawURLEncodedString encrypt given data bytes and enode encrypted binary to string with base64.RawURLEncoding.

func (*CipherHelper) EncryptBytesToString added in v0.2.0

func (h *CipherHelper) EncryptBytesToString(data []byte, base64Encoding *base64.Encoding) (result string, err error)

EncryptBytesToString encrypt given data bytes and encode encrypted binary to string with given base64Encoding.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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