internal/config: Cache csrf secret
This commit is contained in:
parent
71ef3a290e
commit
dfb97405ed
1 changed files with 15 additions and 9 deletions
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/tkw1536/pkglib/lazy"
|
||||||
"github.com/tkw1536/pkglib/reflectx"
|
"github.com/tkw1536/pkglib/reflectx"
|
||||||
"github.com/tkw1536/pkglib/yamlx"
|
"github.com/tkw1536/pkglib/yamlx"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
|
@ -46,6 +47,9 @@ type Config struct {
|
||||||
|
|
||||||
// ConfigPath is the path this configuration was loaded from (if any)
|
// ConfigPath is the path this configuration was loaded from (if any)
|
||||||
ConfigPath string `yaml:"-"`
|
ConfigPath string `yaml:"-"`
|
||||||
|
|
||||||
|
// csrfSecret holds the cached csrf secret
|
||||||
|
csrfSecret lazy.Lazy[[]byte]
|
||||||
}
|
}
|
||||||
|
|
||||||
func zeroSensitive(v reflect.Value) {
|
func zeroSensitive(v reflect.Value) {
|
||||||
|
|
@ -113,6 +117,7 @@ func Marshal(config *Config, previous []byte) ([]byte, error) {
|
||||||
|
|
||||||
// CSRFSecret return the csrfSecret derived from the session secret
|
// CSRFSecret return the csrfSecret derived from the session secret
|
||||||
func (config *Config) CSRFSecret() []byte {
|
func (config *Config) CSRFSecret() []byte {
|
||||||
|
return config.csrfSecret.Get(func() []byte {
|
||||||
// take the hash of the secret
|
// take the hash of the secret
|
||||||
h := fnv.New32a()
|
h := fnv.New32a()
|
||||||
h.Write([]byte(config.SessionSecret))
|
h.Write([]byte(config.SessionSecret))
|
||||||
|
|
@ -124,4 +129,5 @@ func (config *Config) CSRFSecret() []byte {
|
||||||
secret := make([]byte, 32)
|
secret := make([]byte, 32)
|
||||||
rand.Read(secret)
|
rand.Read(secret)
|
||||||
return secret
|
return secret
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue