csrfx

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Code generated by apic; DO NOT EDIT.

Package csrfx implements stateless, HMAC-signed, session-bound CSRF tokens using the signed double-submit pattern (OWASP). A token embeds a random nonce, an expiry, and an HMAC binding it to a session identifier; the generated middleware additionally requires the cookie value to equal the header value (double-submit) before verifying the signature. Code generated by apic; DO NOT EDIT.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCSRFMalformed indicates the token is not valid base64url or has the
	// wrong length/version.
	ErrCSRFMalformed = errors.New("csrfx: malformed token")
	// ErrCSRFExpired indicates the token's embedded expiry is in the past.
	ErrCSRFExpired = errors.New("csrfx: token expired")
	// ErrCSRFMismatch indicates the HMAC did not verify (tampered token or
	// wrong key) or the double-submit cookie/header pair did not match.
	ErrCSRFMismatch = errors.New("csrfx: token signature mismatch")
	// ErrCSRFSessionMismatch indicates the token was issued for a different
	// session than the one presenting it.
	ErrCSRFSessionMismatch = errors.New("csrfx: token session mismatch")
	// ErrKeyTooShort indicates the signing key is shorter than the 32-byte
	// minimum.
	ErrKeyTooShort = errors.New("csrfx: signing key must be at least 32 bytes")
)

Functions

This section is empty.

Types

type Signer

type Signer struct {
	// contains filtered or unexported fields
}

Signer issues and verifies signed, session-bound CSRF tokens.

func New

func New(key []byte, ttl time.Duration) (*Signer, error)

New returns a Signer. key must be at least 32 bytes; it is copied.

func (*Signer) Issue

func (s *Signer) Issue(sessionID string) (string, error)

Issue mints a token bound to sessionID, expiring after the Signer's ttl.

func (*Signer) Verify

func (s *Signer) Verify(token, sessionID string) error

Verify checks the token's signature, session binding, and expiry. A token minted for a different session fails the HMAC check and returns ErrCSRFMismatch (the binding is cryptographic, so wrong-session and tamper are indistinguishable at this layer).

Jump to

Keyboard shortcuts

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