csrf

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: MIT Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-csrf

Golang helper library for generating and verifying anti-CSRF tokens

Example init:

import (
  csrf "github.com/andys/go-csrf"
)

   // Initialize CSRF protection
   csrfSecret := make([]byte, 32)
   if _, err := rand.Read(csrfSecret); err != nil {
           log.Fatal().Err(err).Msg("Failed to generate CSRF secret")
   }
   csrf.Setup(csrfSecret)

Example Fiber middleware:

   // CSRF protection middleware - verify token on POST requests
   app.Use(func(c *fiber.Ctx) error {
           if c.Method() == "POST" {
                   token := c.FormValue("csrf_token")
                   if !csrf.IsTokenValid(token) {
                           return c.Status(fiber.StatusForbidden).SendString("Invalid CSRF token")
                   }
           }
           return c.Next()
   })

Example use:

<input type="hidden" name="csrf_token" value="{%s csrf.GenerateToken() %}">

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateToken

func GenerateToken() string

func IsTokenValid

func IsTokenValid(token string) bool

func Setup

func Setup(randomSecret []byte)

Types

This section is empty.

Jump to

Keyboard shortcuts

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