Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CSRFManager ¶
type CSRFManager struct {
// contains filtered or unexported fields
}
CSRFManager handles CSRF token generation and validation.
A token is HMAC-SHA256(secret, identity, timestamp) + "." + timestamp, where identity is the caller the token was minted for (the signed-in user's subject, or empty for an anonymous caller) and timestamp is the Unix epoch in seconds. Binding the identity in means a token minted by one caller does not validate for another, so an attacker cannot fetch a token for themselves and replay it inside a request the victim's browser sends. Tokens older than MaxAge are rejected.
func NewCSRFManager ¶
func NewCSRFManager() *CSRFManager
NewCSRFManager creates a new CSRF manager with a cryptographically random secret.
func (*CSRFManager) GenerateToken ¶
func (m *CSRFManager) GenerateToken() string
GenerateToken mints a token for an anonymous caller. It is GenerateTokenFor(""), kept for callers that have no identity to bind.
func (*CSRFManager) GenerateTokenFor ¶ added in v1.14.0
func (m *CSRFManager) GenerateTokenFor(identity string) string
GenerateTokenFor mints a token bound to identity, normally the signed-in user's subject. The token only validates through ValidateTokenFor with the same identity.
func (*CSRFManager) MaxAge ¶ added in v1.14.0
func (m *CSRFManager) MaxAge() time.Duration
MaxAge is how long a token stays valid after it is minted. Anything that tells a client when its token expires should report this, so the client refreshes before validation starts refusing it.
func (*CSRFManager) ValidateToken ¶
func (m *CSRFManager) ValidateToken(token string) bool
ValidateToken validates a token minted for an anonymous caller. It is ValidateTokenFor(token, "").
func (*CSRFManager) ValidateTokenFor ¶ added in v1.14.0
func (m *CSRFManager) ValidateTokenFor(token, identity string) bool
ValidateTokenFor validates a token for identity. A token is valid when:
- It is well-formed (contains exactly one separator).
- The embedded timestamp is no older than MaxAge and not in the future.
- The HMAC over identity and timestamp matches, so a token minted for a different identity fails.