Skip to content
This repository has been archived by the owner on May 10, 2020. It is now read-only.
/ gin-csrf Public archive
forked from utrack/gin-csrf

CSRF protection middleware for Gin.

License

Notifications You must be signed in to change notification settings

mikeee-archive/gin-csrf

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gin-csrf Build Status

CSRF protection middleware for Gin. This middleware has to be used with gin-contrib/sessions.

Original credit to tommy351, this fork makes it work with gin-gonic contrib sessions.

Installation

$ go get github.com/utrack/gin-csrf

Usage

import (
    "errors"

    "github.com/gin-gonic/gin"
    "github.com/gin-contrib/sessions"
    "github.com/utrack/gin-csrf"
)

func main(){
    r := gin.Default()
    store := sessions.NewCookieStore([]byte("secret"))
    r.Use(sessions.Sessions("mysession", store))
    r.Use(csrf.Middleware(csrf.Options{
        Secret: "secret123",
        ErrorFunc: func(c *gin.Context){
            c.String(400, "CSRF token mismatch")
			c.Abort()
        },
    }))

    r.GET("/protected", func(c *gin.Context){
        c.String(200, csrf.GetToken(c))
    })

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

About

CSRF protection middleware for Gin.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%