Refactoring cont.

This commit is contained in:
2025-04-23 09:44:19 +01:00
parent 5c3a847900
commit 2ce810a4dd
19 changed files with 117 additions and 150 deletions

View File

@@ -4,11 +4,15 @@ import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/gob"
"fmt"
"net/http"
"os"
"time"
sessionHandlers "synlotto-website/handlers/session"
sessionHelpers "synlotto-website/helpers/session"
helpers "synlotto-website/helpers/session"
"synlotto-website/logging"
"synlotto-website/models"
@@ -17,12 +21,13 @@ import (
var (
sessionStore *sessions.CookieStore
sessionName string
Name string
authKey []byte
encryptKey []byte
)
func InitSession(cfg *models.Config) error {
gob.Register(time.Time{})
authPath := cfg.Session.AuthKeyPath
encPath := cfg.Session.EncryptionKeyPath
@@ -32,7 +37,7 @@ func InitSession(cfg *models.Config) error {
if err != nil {
return err
}
encoded := helpers.EncodeKey(key)
encoded := sessionHelpers.EncodeKey(key)
err = os.WriteFile(authPath, []byte(encoded), 0600)
if err != nil {
return err
@@ -45,7 +50,7 @@ func InitSession(cfg *models.Config) error {
if err != nil {
return err
}
encoded := helpers.EncodeKey(key)
encoded := sessionHelpers.EncodeKey(key)
err = os.WriteFile(encPath, []byte(encoded), 0600)
if err != nil {
return err
@@ -96,15 +101,15 @@ func loadSessionKeys(authPath, encryptionPath, name string, isProduction bool) e
return fmt.Errorf("auth and encryption keys must be 32 bytes each (got auth=%d, enc=%d)", len(authKey), len(encryptKey))
}
sessionStore = sessions.NewCookieStore(authKey, encryptKey)
sessionStore.Options = &sessions.Options{
sessionHandlers.SessionStore = sessions.NewCookieStore(authKey, encryptKey)
sessionHandlers.SessionStore.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 1,
MaxAge: 86400,
HttpOnly: true,
Secure: isProduction,
SameSite: http.SameSiteLaxMode,
}
sessionName = name
sessionHandlers.Name = name
return nil
}