From f18e6a3ba80e3310cd1ace81a5e767a6201dab2c Mon Sep 17 00:00:00 2001 From: Arthur Lefebvre Date: Sat, 4 Oct 2025 23:15:20 +0200 Subject: [PATCH] add jwtkey config to avoid logging in again after reboot --- internal/config/config.go | 8 +++++++- internal/jwtauth/key.go | 9 +++++++-- main.go | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index c308ae2..6f8b791 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 { diff --git a/internal/jwtauth/key.go b/internal/jwtauth/key.go index 34aa2ee..ec7867d 100644 --- a/internal/jwtauth/key.go +++ b/internal/jwtauth/key.go @@ -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 == "" { - key, err = generateSecureToken(64) + if jwtkey != "" { + key = jwtkey + } else { + key, err = generateSecureToken(64) + } os.Setenv(keyName, key) } return err diff --git a/main.go b/main.go index 4d485fa..0084601 100644 --- a/main.go +++ b/main.go @@ -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) }