Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit 43a1eed

Browse files
authored
Make secure cookies working (#901)
We must provide keys for session storage (gothic and guerillla) to work with SSL. Both frameworks switch to secure cookies on SSL. The solution is to generate keys on on start. This is for now. This is not a final solution.
1 parent 911f24a commit 43a1eed

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

quesma/quesma/ui/console_routes.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"errors"
1010
"github.com/gorilla/mux"
11+
"github.com/gorilla/securecookie"
1112
"github.com/gorilla/sessions"
1213
"github.com/markbates/goth"
1314
"github.com/markbates/goth/gothic"
@@ -29,6 +30,14 @@ var uiFs embed.FS
2930

3031
const quesmaSessionName = "quesma-session"
3132

33+
func init() {
34+
35+
// Here we generate a random key for the session store
36+
// TODO We should use a secure key from the environment on production.
37+
// 32 - is a default key length, taken for example
38+
gothic.Store = sessions.NewCookieStore(securecookie.GenerateRandomKey(32))
39+
}
40+
3241
func authCallbackHandler(w http.ResponseWriter, r *http.Request) {
3342
user, err := gothic.CompleteUserAuth(w, r)
3443
if err != nil {
@@ -247,7 +256,12 @@ func (qmc *QuesmaManagementConsole) initPprof(router *mux.Router) {
247256
router.HandleFunc("/debug/pprof/trace", pprof.Trace)
248257
}
249258

250-
var store = sessions.NewCookieStore([]byte("test"))
259+
// Here we generate keys for the session store.
260+
// TODO We should use a secure key from the environment on production.
261+
// 32,64 are default key lengths.
262+
var authKey = securecookie.GenerateRandomKey(64)
263+
var encryptionKey = securecookie.GenerateRandomKey(32)
264+
var store = sessions.NewCookieStore(authKey, encryptionKey)
251265

252266
func authMiddleware(next http.Handler) http.Handler {
253267
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)