https://github.com/joeig/gin-nonce
Nonce middleware for Gin
https://github.com/joeig/gin-nonce
csp gin go nonce
Last synced: 8 months ago
JSON representation
Nonce middleware for Gin
- Host: GitHub
- URL: https://github.com/joeig/gin-nonce
- Owner: joeig
- License: mit
- Created: 2023-10-30T17:39:23.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-29T14:23:51.000Z (over 1 year ago)
- Last Synced: 2024-05-01T12:44:31.216Z (over 1 year ago)
- Topics: csp, gin, go, nonce
- Language: Go
- Homepage: https://pkg.go.dev/go.eigsys.de/gin-nonce
- Size: 32.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Nonce middleware for Gin
This Gin middleware generates a nonce for each request.
[](https://github.com/joeig/gin-nonce/tree/main/.github/testcoverage.yml)
[](https://goreportcard.com/report/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).