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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user