Bootstrapped the creation and loading of session keys to lighten main.

This commit is contained in:
2025-04-16 08:21:02 +01:00
parent 0a21973237
commit 4bb3b58ddb
8 changed files with 95 additions and 13 deletions

View File

@@ -2,6 +2,7 @@ package security
import (
"bytes"
"encoding/base64"
"encoding/gob"
"fmt"
"net/http"
@@ -25,21 +26,27 @@ func init() {
func LoadSessionKeys(authPath, encryptionPath, name string, isProduction bool) error {
var err error
authKey, err = os.ReadFile(authPath)
rawAuth, err := os.ReadFile(authPath)
if err != nil {
return fmt.Errorf("error loading auth key: %w", err)
return fmt.Errorf("error reading auth key: %w", err)
}
authKey, err = base64.StdEncoding.DecodeString(string(bytes.TrimSpace(rawAuth)))
if err != nil {
return fmt.Errorf("error decoding auth key: %w", err)
}
encryptKey, err = os.ReadFile(encryptionPath)
rawEnc, err := os.ReadFile(encryptionPath)
if err != nil {
return fmt.Errorf("error loading encryption key: %w", err)
return fmt.Errorf("error reading encryption key: %w", err)
}
encryptKey, err = base64.StdEncoding.DecodeString(string(bytes.TrimSpace(rawEnc)))
if err != nil {
return fmt.Errorf("error decoding encryption key: %w", err)
}
authKey = bytes.TrimSpace(authKey)
encryptKey = bytes.TrimSpace(encryptKey)
if len(authKey) != 32 || len(encryptKey) != 32 {
return fmt.Errorf("auth and encryption keys must be 32 bytes each")
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)