package backend import ( "crypto/rand" "encoding/gob" "fmt" "io" "os" "github.com/gorilla/sessions" ) type ( CookieStore struct{ sessions.CookieStore } Session struct{ sessions.Session } ) func NewKey() ([]byte, error) { key := make([]byte, 32) if _, err := io.ReadFull(rand.Reader, key); err != nil { return nil, fmt.Errorf("error generating key: %v", err) } return key, nil } func SaveKey(key []byte, filename string) error { file, err := os.Create(filename) if err != nil { return fmt.Errorf("error creating key file: %v", err) } defer file.Close() file.Chmod(0600) if err = gob.NewEncoder(file).Encode(key); err != nil { return fmt.Errorf("error ecoding key: %v", err) } return nil } func LoadKey(filename string) ([]byte, error) { file, err := os.Open(filename) if err != nil { return nil, fmt.Errorf("error opening key file: %v", err) } defer file.Close() key := make([]byte, 32) if err = gob.NewDecoder(file).Decode(&key); err != nil { return nil, fmt.Errorf("error decoding key: %v", err) } return key, nil } func NewCookieStore(key []byte) *CookieStore { store := sessions.NewCookieStore(key) store.Options.Secure = true store.Options.HttpOnly = true return &CookieStore{*store} }