csrf_cookie

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 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

This is an implementation of CSRF protection using cookies with gin-csrf.

This library was created using and with reference to https://github.com/utrack/gin-csrf.

Installation

go get github.com/okoshi-f/gin-csrf-cookie

Usage

package main

import (
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
	csrf "github.com/okoshi-f/gin-csrf-cookie"
)

func main() {
	r := gin.Default()
	store := cookie.NewStore([]byte("secret"))
	r.Use(sessions.Sessions("mysession", store))
	r.Use(csrf.Middleware(csrf.Options{Secret: "secret123"}))

	r.GET("/protected", func(c *gin.Context) {
		csrf.LoadToken(c, "/", "localhost", false)
		c.String(200, "OK")
	})

	r.POST("/protected", func(c *gin.Context) {
		c.String(200, "CSRF token is valid")
	})

	r.Run(":8080")
}

Disclaimer

We make no warranty for any damages caused by the use of this library.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadToken

func LoadToken(c *gin.Context, path string, domain string, secure bool)

Set a verification token for CSRF in the cookie.

func Middleware

func Middleware(options Options) gin.HandlerFunc

Middleware validates CSRF tokens in cookies.

Types

type Options

type Options struct {
	Secret        string
	IgnoreMethods []string
	ErrorFunc     gin.HandlerFunc
}

The option saves the CSRF middleware settings using cookies.

Jump to

Keyboard shortcuts

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