add jwtkey config to avoid logging in again after reboot
This commit is contained in:
@@ -12,10 +12,16 @@ type Config struct {
|
|||||||
Port string `toml:"port" comment:"The port to listen on for the server."`
|
Port string `toml:"port" comment:"The port to listen on for the server."`
|
||||||
DatabaseFilePath string `toml:"database_file_path" comment:"Path to sqlite database file."`
|
DatabaseFilePath string `toml:"database_file_path" comment:"Path to sqlite database file."`
|
||||||
DemoDataPath string `toml:"demo_data_path" comment:"The path to the sql file to load for demo data."`
|
DemoDataPath string `toml:"demo_data_path" comment:"The path to the sql file to load for demo data."`
|
||||||
|
JWTKey string `toml:"jwt_key" comment:"The key used to encrypt the generated JWT. Encoded in base64. If empty a random one will be generated on every restart."`
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultConfig() Config {
|
func defaultConfig() Config {
|
||||||
return Config{Port: "8080", DatabaseFilePath: "plm.db", DemoDataPath: ""}
|
return Config{
|
||||||
|
Port: "8080",
|
||||||
|
DatabaseFilePath: "plm.db",
|
||||||
|
DemoDataPath: "",
|
||||||
|
JWTKey: "",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig(configPath string) Config {
|
func LoadConfig(configPath string) Config {
|
||||||
|
|||||||
@@ -27,12 +27,17 @@ func getKeyVariableName() string {
|
|||||||
return "PLM_JWT_KEY"
|
return "PLM_JWT_KEY"
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitKey() error {
|
func InitKey(jwtkey string) error {
|
||||||
var err error
|
var err error
|
||||||
keyName := getKeyVariableName()
|
keyName := getKeyVariableName()
|
||||||
|
//ignore config value, look in env first
|
||||||
key := os.Getenv(keyName)
|
key := os.Getenv(keyName)
|
||||||
if key == "" {
|
if key == "" {
|
||||||
|
if jwtkey != "" {
|
||||||
|
key = jwtkey
|
||||||
|
} else {
|
||||||
key, err = generateSecureToken(64)
|
key, err = generateSecureToken(64)
|
||||||
|
}
|
||||||
os.Setenv(keyName, key)
|
os.Setenv(keyName, key)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -19,7 +19,7 @@ func main() {
|
|||||||
|
|
||||||
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()
|
err := jwtauth.InitKey(config.JWTKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user