-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathcsp.go
More file actions
31 lines (24 loc) · 749 Bytes
/
csp.go
File metadata and controls
31 lines (24 loc) · 749 Bytes
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
package secure
import (
"crypto/rand"
"encoding/base64"
"io"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
)
func init() {
context.SetHandlerName("github.com/iris-contrib/middleware/secure.*", "iris-contrib.secure")
}
const cspNonceKey string = "iris.secure.nonce"
// CSPNonce returns the nonce value associated with the present request. If no nonce has been generated it returns an empty string.
func CSPNonce(ctx iris.Context) string {
return ctx.Values().GetString(cspNonceKey)
}
func cspRandNonce() string {
var buf [cspNonceSize]byte
_, err := io.ReadFull(rand.Reader, buf[:])
if err != nil {
panic("CSP Nonce rand.Reader failed" + err.Error())
}
return base64.RawStdEncoding.EncodeToString(buf[:])
}