From 60c8f372577c8b7b81fc4afab0b174e0745b3830 Mon Sep 17 00:00:00 2001 From: Arthur Lefebvre Date: Wed, 4 Mar 2026 14:58:35 +0100 Subject: [PATCH] Store JWT key in config file - During config file creation, the generated JWT key will be stored. - Added an option to disable this behavior. --- internal/config/config.go | 19 +++++++++++++++---- internal/jwtauth/key.go | 4 ++-- internal/setup/setup.go | 7 +------ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 187a7af..4396713 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -6,15 +6,17 @@ import ( "os" "strings" + "git.artlef.fr/PersonalLibraryManager/internal/jwtauth" "github.com/alecthomas/kong" kongtoml "github.com/alecthomas/kong-toml" "github.com/pelletier/go-toml" ) type CLI struct { - NoConfigFile bool `short:"C" default:"false" help:"Disable config file creation."` - ConfigFilePath string `short:"c" default:"plm.toml" type:"path" help:"Config file path."` - ConfigFile Config `embed:"" prefix:""` + NoConfigFile bool `short:"C" default:"false" help:"Disable config file creation."` + ConfigFilePath string `short:"c" default:"plm.toml" type:"path" help:"Config file path."` + DisableStoreJWTKeyInConfig bool `default:"false" help:"Do not store the generated key used for JWT when initializing configuration."` + ConfigFile Config `embed:"" prefix:""` } type Config struct { @@ -56,7 +58,7 @@ func defaultConfig() CLI { DemoUsername: "demo", AddUser: []string{}, } - return CLI{NoConfigFile: false, ConfigFilePath: "plm.toml", ConfigFile: c} + return CLI{NoConfigFile: false, ConfigFilePath: "plm.toml", DisableStoreJWTKeyInConfig: false, ConfigFile: c} } func LoadConfig() Config { @@ -87,6 +89,15 @@ func LoadConfig() Config { //parse in configs and cli kong.Parse(&cfg, kong.Configuration(kongtoml.Loader, configPath)) + jwtkey, err := jwtauth.InitKey(cfg.ConfigFile.JWTKey) + if err != nil { + panic(err) + } + + if !cfg.DisableStoreJWTKeyInConfig { + cfg.ConfigFile.JWTKey = jwtkey + } + if configFileNotExist && !cfg.NoConfigFile { b, err := toml.Marshal(cfg.ConfigFile) if err != nil { diff --git a/internal/jwtauth/key.go b/internal/jwtauth/key.go index ec7867d..e41f745 100644 --- a/internal/jwtauth/key.go +++ b/internal/jwtauth/key.go @@ -27,7 +27,7 @@ func getKeyVariableName() string { return "PLM_JWT_KEY" } -func InitKey(jwtkey string) error { +func InitKey(jwtkey string) (string, error) { var err error keyName := getKeyVariableName() //ignore config value, look in env first @@ -40,7 +40,7 @@ func InitKey(jwtkey string) error { } os.Setenv(keyName, key) } - return err + return key, err } func GetJwtKey() ([]byte, error) { diff --git a/internal/setup/setup.go b/internal/setup/setup.go index 9ac24d2..7df1eb1 100644 --- a/internal/setup/setup.go +++ b/internal/setup/setup.go @@ -14,21 +14,16 @@ import ( "git.artlef.fr/PersonalLibraryManager/internal/createuser" "git.artlef.fr/PersonalLibraryManager/internal/db" i18nresource "git.artlef.fr/PersonalLibraryManager/internal/i18nresource" - "git.artlef.fr/PersonalLibraryManager/internal/jwtauth" "git.artlef.fr/PersonalLibraryManager/internal/middleware" "git.artlef.fr/PersonalLibraryManager/internal/routes" ) func Setup(config *config.Config) *gin.Engine { db := db.Initdb(config.DatabaseFilePath, config.DemoDataPath) - err := jwtauth.InitKey(config.JWTKey) - if err != nil { - panic(err) - } r := gin.Default() bundle := i18nresource.InitializeI18n() - err = createuser.CreateDefaultUsers(appcontext.AppContext{C: nil, Db: db, I18n: bundle, Config: config}) + err := createuser.CreateDefaultUsers(appcontext.AppContext{C: nil, Db: db, I18n: bundle, Config: config}) if err != nil { panic(err) }