add jwtkey config to avoid logging in again after reboot

This commit is contained in:
2025-10-04 23:15:20 +02:00
parent c5d71bbfeb
commit f18e6a3ba8
3 changed files with 15 additions and 4 deletions

View File

@@ -12,10 +12,16 @@ type Config struct {
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."`
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 {
return Config{Port: "8080", DatabaseFilePath: "plm.db", DemoDataPath: ""}
return Config{
Port: "8080",
DatabaseFilePath: "plm.db",
DemoDataPath: "",
JWTKey: "",
}
}
func LoadConfig(configPath string) Config {

View File

@@ -27,12 +27,17 @@ func getKeyVariableName() string {
return "PLM_JWT_KEY"
}
func InitKey() error {
func InitKey(jwtkey string) error {
var err error
keyName := getKeyVariableName()
//ignore config value, look in env first
key := os.Getenv(keyName)
if key == "" {
if jwtkey != "" {
key = jwtkey
} else {
key, err = generateSecureToken(64)
}
os.Setenv(keyName, key)
}
return err

View File

@@ -19,7 +19,7 @@ func main() {
func setup(config *config.Config) *gin.Engine {
db := db.Initdb(config.DatabaseFilePath, config.DemoDataPath)
err := jwtauth.InitKey()
err := jwtauth.InitKey(config.JWTKey)
if err != nil {
panic(err)
}