Store JWT key in config file

- During config file creation, the generated JWT key will be stored.
- Added an option to disable this behavior.
This commit is contained in:
2026-03-04 14:58:35 +01:00
parent e066321468
commit 60c8f37257
3 changed files with 18 additions and 12 deletions

View File

@@ -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 {