-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonce.go
66 lines (54 loc) · 1.45 KB
/
nonce.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Package nonce is a Gin middleware which generates a nonce for each request.
package nonce
import (
"crypto/rand"
"encoding/base64"
"github.com/gin-gonic/gin"
"net/http"
)
const defaultKey = "go.eigsys.de/gin-nonce:nonce"
type randReadFunc func(b []byte) (n int, err error)
// Handler defines a nonce handler.
type Handler struct {
key string
randRead randReadFunc
}
// New creates a new Handler instance.
func New() *Handler {
return &Handler{
key: defaultKey,
randRead: rand.Read,
}
}
// WithKey overrides the default Gin context key.
func (h *Handler) WithKey(key string) *Handler {
h.key = key
return h
}
// WithRandRead overrides the default random reader ([rand.Read]).
func (h *Handler) WithRandRead(randRead randReadFunc) *Handler {
h.randRead = randRead
return h
}
// Middleware creates a new Gin middleware which generates a nonce for each request.
func (h *Handler) Middleware() gin.HandlerFunc {
return func(c *gin.Context) {
rawID := make([]byte, 16)
if _, err := h.randRead(rawID); err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
}
c.Set(h.key, base64.RawStdEncoding.EncodeToString(rawID))
}
}
// GetKey returns the Gin context key.
func (h *Handler) GetKey() string {
return h.key
}
// GetNonce returns the nonce for the current Gin context.
func (h *Handler) GetNonce(c *gin.Context) (string, bool) {
nonce, ok := c.Get(h.key)
if nonce == nil {
return "", false
}
return nonce.(string), ok
}