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