fix(server): don't try to save JWT if it fails to encrypt

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan 2024-12-05 22:19:39 -05:00
parent 7f030b0859
commit 9cbdb20a31

View file

@ -32,6 +32,7 @@ func Init(ds model.DataStore) {
secret, err := ds.Property(ctx).Get(consts.JWTSecretKey) secret, err := ds.Property(ctx).Get(consts.JWTSecretKey)
if err != nil || secret == "" { if err != nil || secret == "" {
log.Info(ctx, "Creating new JWT secret, used for encrypting UI sessions")
secret = createNewSecret(ctx, ds) secret = createNewSecret(ctx, ds)
} else { } else {
if secret, err = utils.Decrypt(ctx, getEncKey(), secret); err != nil { if secret, err = utils.Decrypt(ctx, getEncKey(), secret); err != nil {
@ -124,17 +125,15 @@ func WithAdminUser(ctx context.Context, ds model.DataStore) context.Context {
} }
func createNewSecret(ctx context.Context, ds model.DataStore) string { func createNewSecret(ctx context.Context, ds model.DataStore) string {
log.Info(ctx, "Creating new JWT secret, used for encrypting UI sessions")
secret := uuid.NewString() secret := uuid.NewString()
encSecret, err := utils.Encrypt(ctx, getEncKey(), secret) encSecret, err := utils.Encrypt(ctx, getEncKey(), secret)
if err != nil { if err != nil {
log.Error(ctx, "Could not encrypt JWT secret", err) log.Error(ctx, "Could not encrypt JWT secret", err)
return secret
} }
if err := ds.Property(ctx).Put(consts.JWTSecretKey, encSecret); err != nil { if err := ds.Property(ctx).Put(consts.JWTSecretKey, encSecret); err != nil {
log.Error(ctx, "Could not save JWT secret in DB", err) log.Error(ctx, "Could not save JWT secret in DB", err)
} }
return secret return secret
} }