Librairie d’aide à la création d’applications web pour Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

33 lines
943 B

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, defaultOptions *sessions.Options) (sessions.Store, error) {
authenticationKey, err := generateRandomBytes(authKeySize)
if err != nil {
return nil, errors.Wrap(err, "error while generating cookie authentication key")
}
encryptionKey, err := generateRandomBytes(encryptKeySize)
if err != nil {
return nil, errors.Wrap(err, "error while generating cookie encryption key")
}
store := sessions.NewCookieStore(authenticationKey, encryptionKey)
store.Options = defaultOptions
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
}