From d62b42ab97c7d69681cb1751b7e65cfe4bed0cbb Mon Sep 17 00:00:00 2001 From: William Petit Date: Thu, 6 Dec 2018 21:42:01 +0100 Subject: [PATCH] Add cookie session store helper --- session/gorilla/cookie.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 session/gorilla/cookie.go diff --git a/session/gorilla/cookie.go b/session/gorilla/cookie.go new file mode 100644 index 0000000..01fde46 --- /dev/null +++ b/session/gorilla/cookie.go @@ -0,0 +1,32 @@ +package gorilla + +import ( + "crypto/rand" + + "github.com/gorilla/sessions" + "github.com/pkg/errors" +) + +// CreateCookieSessionStore creates and returns a new cookie session store +// with random authentication and encryption keys +func CreateCookieSessionStore(authKeySize, encryptKeySize int) (sessions.Store, error) { + authenticationKey, err := generateRandomBytes(64) + if err != nil { + return nil, errors.Wrap(err, "error while generating cookie authentication key") + } + encryptionKey, err := generateRandomBytes(32) + if err != nil { + return nil, errors.Wrap(err, "error while generating cookie encryption key") + } + store := sessions.NewCookieStore(authenticationKey, encryptionKey) + return store, nil +} + +func generateRandomBytes(n int) ([]byte, error) { + b := make([]byte, n) + _, err := rand.Read(b) + if err != nil { + return nil, err + } + return b, nil +}