Skip to content

joeig/gin-nonce

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nonce middleware for Gin

This Gin middleware generates a nonce for each request.

Test coverage Go Report Card PkgGoDev

Usage

package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
	"go.eigsys.de/gin-nonce"
)

func main() {
	router := gin.Default()

	handler := nonce.New()
	router.Use(handler.Middleware())

	router.GET("/", func(ginCtx *gin.Context) {
		currentNonce, _ := handler.GetNonce(ginCtx)
		ginCtx.Header("Content-Security-Policy", fmt.Sprintf("style-src 'nonce-%s';", currentNonce))

		ginCtx.String(http.StatusOK, "Hello, Gopher!")
	})

	_ = router.Run()
}

Documentation

See Go reference.