joeig / gin-nonce

Nonce middleware for Gin

Home Page:https://pkg.go.dev/go.eigsys.de/gin-nonce

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

Nonce middleware for Gin

https://pkg.go.dev/go.eigsys.de/gin-nonce

License:MIT License


Languages

Language:Go 91.5%Language:Makefile 8.5%