Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/joeig/gin-nonce

Nonce middleware for Gin
https://github.com/joeig/gin-nonce

csp gin go nonce

Last synced: about 1 month ago
JSON representation

Nonce middleware for Gin

Awesome Lists containing this project

README

        

# Nonce middleware for Gin

This Gin middleware generates a nonce for each request.

[![Test coverage](https://img.shields.io/badge/coverage-100%25-success)](https://github.com/joeig/gin-nonce/tree/main/.github/testcoverage.yml)
[![Go Report Card](https://goreportcard.com/badge/go.eigsys.de/gin-nonce)](https://goreportcard.com/report/go.eigsys.de/gin-nonce)
[![PkgGoDev](https://pkg.go.dev/badge/go.eigsys.de/gin-nonce)](https://pkg.go.dev/go.eigsys.de/gin-nonce)

## Usage

```go
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](https://pkg.go.dev/go.eigsys.de/gin-nonce).