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"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/jwtauth"
|
||||||
"github.com/alecthomas/kong"
|
"github.com/alecthomas/kong"
|
||||||
kongtoml "github.com/alecthomas/kong-toml"
|
kongtoml "github.com/alecthomas/kong-toml"
|
||||||
"github.com/pelletier/go-toml"
|
"github.com/pelletier/go-toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CLI struct {
|
type CLI struct {
|
||||||
NoConfigFile bool `short:"C" default:"false" help:"Disable config file creation."`
|
NoConfigFile bool `short:"C" default:"false" help:"Disable config file creation."`
|
||||||
ConfigFilePath string `short:"c" default:"plm.toml" type:"path" help:"Config file path."`
|
ConfigFilePath string `short:"c" default:"plm.toml" type:"path" help:"Config file path."`
|
||||||
ConfigFile Config `embed:"" prefix:""`
|
DisableStoreJWTKeyInConfig bool `default:"false" help:"Do not store the generated key used for JWT when initializing configuration."`
|
||||||
|
ConfigFile Config `embed:"" prefix:""`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@@ -56,7 +58,7 @@ func defaultConfig() CLI {
|
|||||||
DemoUsername: "demo",
|
DemoUsername: "demo",
|
||||||
AddUser: []string{},
|
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 {
|
func LoadConfig() Config {
|
||||||
@@ -87,6 +89,15 @@ func LoadConfig() Config {
|
|||||||
//parse in configs and cli
|
//parse in configs and cli
|
||||||
kong.Parse(&cfg, kong.Configuration(kongtoml.Loader, configPath))
|
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 {
|
if configFileNotExist && !cfg.NoConfigFile {
|
||||||
b, err := toml.Marshal(cfg.ConfigFile)
|
b, err := toml.Marshal(cfg.ConfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ func getKeyVariableName() string {
|
|||||||
return "PLM_JWT_KEY"
|
return "PLM_JWT_KEY"
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitKey(jwtkey string) error {
|
func InitKey(jwtkey string) (string, error) {
|
||||||
var err error
|
var err error
|
||||||
keyName := getKeyVariableName()
|
keyName := getKeyVariableName()
|
||||||
//ignore config value, look in env first
|
//ignore config value, look in env first
|
||||||
@@ -40,7 +40,7 @@ func InitKey(jwtkey string) error {
|
|||||||
}
|
}
|
||||||
os.Setenv(keyName, key)
|
os.Setenv(keyName, key)
|
||||||
}
|
}
|
||||||
return err
|
return key, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetJwtKey() ([]byte, error) {
|
func GetJwtKey() ([]byte, error) {
|
||||||
|
|||||||
@@ -14,21 +14,16 @@ import (
|
|||||||
"git.artlef.fr/PersonalLibraryManager/internal/createuser"
|
"git.artlef.fr/PersonalLibraryManager/internal/createuser"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/db"
|
"git.artlef.fr/PersonalLibraryManager/internal/db"
|
||||||
i18nresource "git.artlef.fr/PersonalLibraryManager/internal/i18nresource"
|
i18nresource "git.artlef.fr/PersonalLibraryManager/internal/i18nresource"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/jwtauth"
|
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/middleware"
|
"git.artlef.fr/PersonalLibraryManager/internal/middleware"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/routes"
|
"git.artlef.fr/PersonalLibraryManager/internal/routes"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Setup(config *config.Config) *gin.Engine {
|
func Setup(config *config.Config) *gin.Engine {
|
||||||
db := db.Initdb(config.DatabaseFilePath, config.DemoDataPath)
|
db := db.Initdb(config.DatabaseFilePath, config.DemoDataPath)
|
||||||
err := jwtauth.InitKey(config.JWTKey)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
bundle := i18nresource.InitializeI18n()
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user