From a7106ad7e9b89b57f14d36120c7ac0907e37e4f1 Mon Sep 17 00:00:00 2001 From: James Elliott Date: Mon, 9 May 2022 08:43:12 +1000 Subject: [PATCH] fix(handler): missing notification values (#3321) This ensures all template types share the same template values and display them correctly regardless if text/html/other. Fixes #3319. --- .../handlers/handler_reset_password_step2.go | 32 ++++++---------- internal/middlewares/identity_verification.go | 37 +++++++------------ 2 files changed, 24 insertions(+), 45 deletions(-) diff --git a/internal/handlers/handler_reset_password_step2.go b/internal/handlers/handler_reset_password_step2.go index ae3f814b..4e44381b 100644 --- a/internal/handlers/handler_reset_password_step2.go +++ b/internal/handlers/handler_reset_password_step2.go @@ -36,9 +36,7 @@ func ResetPasswordPOST(ctx *middlewares.AutheliaCtx) { return } - err = ctx.Providers.UserProvider.UpdatePassword(username, requestBody.Password) - - if err != nil { + if err = ctx.Providers.UserProvider.UpdatePassword(username, requestBody.Password); err != nil { switch { case utils.IsStringInSliceContains(err.Error(), ldapPasswordComplexityCodes), utils.IsStringInSliceContains(err.Error(), ldapPasswordComplexityErrors): @@ -54,9 +52,8 @@ func ResetPasswordPOST(ctx *middlewares.AutheliaCtx) { // Reset the request. userSession.PasswordResetUsername = nil - err = ctx.SaveSession(userSession) - if err != nil { + if err = ctx.SaveSession(userSession); err != nil { ctx.Error(fmt.Errorf("unable to update password reset state: %s", err), messageOperationFailed) return } @@ -84,14 +81,14 @@ func ResetPasswordPOST(ctx *middlewares.AutheliaCtx) { disableHTML = ctx.Configuration.Notifier.SMTP.DisableHTMLEmails } - if !disableHTML { - htmlParams := map[string]interface{}{ - "Title": "Password changed successfully", - "DisplayName": userInfo.DisplayName, - "RemoteIP": ctx.RemoteIP().String(), - } + data := map[string]interface{}{ + "Title": "Password changed successfully", + "DisplayName": userInfo.DisplayName, + "RemoteIP": ctx.RemoteIP().String(), + } - err = templates.EmailPasswordResetHTML.Execute(bufHTML, htmlParams) + if !disableHTML { + err = templates.EmailPasswordResetHTML.Execute(bufHTML, data) if err != nil { ctx.Logger.Error(err) @@ -102,13 +99,8 @@ func ResetPasswordPOST(ctx *middlewares.AutheliaCtx) { } bufText := new(bytes.Buffer) - textParams := map[string]interface{}{ - "DisplayName": userInfo.DisplayName, - } - err = templates.EmailPasswordResetPlainText.Execute(bufText, textParams) - - if err != nil { + if err = templates.EmailPasswordResetPlainText.Execute(bufText, data); err != nil { ctx.Logger.Error(err) ctx.ReplyOK() @@ -118,9 +110,7 @@ func ResetPasswordPOST(ctx *middlewares.AutheliaCtx) { ctx.Logger.Debugf("Sending an email to user %s (%s) to inform that the password has changed.", username, userInfo.Emails[0]) - err = ctx.Providers.Notifier.Send(userInfo.Emails[0], "Password changed successfully", bufText.String(), bufHTML.String()) - - if err != nil { + if err = ctx.Providers.Notifier.Send(userInfo.Emails[0], "Password changed successfully", bufText.String(), bufHTML.String()); err != nil { ctx.Logger.Error(err) ctx.ReplyOK() diff --git a/internal/middlewares/identity_verification.go b/internal/middlewares/identity_verification.go index 0658978b..2c53d354 100644 --- a/internal/middlewares/identity_verification.go +++ b/internal/middlewares/identity_verification.go @@ -49,15 +49,14 @@ func IdentityVerificationStart(args IdentityVerificationStartArgs, delayFunc Tim claims := verification.ToIdentityVerificationClaim() token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) - ss, err := token.SignedString([]byte(ctx.Configuration.JWTSecret)) + ss, err := token.SignedString([]byte(ctx.Configuration.JWTSecret)) if err != nil { ctx.Error(err, messageOperationFailed) return } - err = ctx.Providers.StorageProvider.SaveIdentityVerification(ctx, verification) - if err != nil { + if err = ctx.Providers.StorageProvider.SaveIdentityVerification(ctx, verification); err != nil { ctx.Error(err, messageOperationFailed) return } @@ -77,32 +76,24 @@ func IdentityVerificationStart(args IdentityVerificationStartArgs, delayFunc Tim disableHTML = ctx.Configuration.Notifier.SMTP.DisableHTMLEmails } + data := map[string]interface{}{ + "Title": args.MailTitle, + "LinkURL": link, + "LinkText": args.MailButtonContent, + "DisplayName": identity.DisplayName, + "RemoteIP": ctx.RemoteIP().String(), + } + if !disableHTML { - htmlParams := map[string]interface{}{ - "Title": args.MailTitle, - "LinkURL": link, - "LinkText": args.MailButtonContent, - "DisplayName": identity.DisplayName, - "RemoteIP": ctx.RemoteIP().String(), - } - - err = templates.EmailIdentityVerificationHTML.Execute(bufHTML, htmlParams) - - if err != nil { + if err = templates.EmailIdentityVerificationHTML.Execute(bufHTML, data); err != nil { ctx.Error(err, messageOperationFailed) return } } bufText := new(bytes.Buffer) - textParams := map[string]interface{}{ - "LinkURL": link, - "DisplayName": identity.DisplayName, - } - err = templates.EmailIdentityVerificationPlainText.Execute(bufText, textParams) - - if err != nil { + if err = templates.EmailIdentityVerificationPlainText.Execute(bufText, data); err != nil { ctx.Error(err, messageOperationFailed) return } @@ -110,9 +101,7 @@ func IdentityVerificationStart(args IdentityVerificationStartArgs, delayFunc Tim ctx.Logger.Debugf("Sending an email to user %s (%s) to confirm identity for registering a device.", identity.Username, identity.Email) - err = ctx.Providers.Notifier.Send(identity.Email, args.MailTitle, bufText.String(), bufHTML.String()) - - if err != nil { + if err = ctx.Providers.Notifier.Send(identity.Email, args.MailTitle, bufText.String(), bufHTML.String()); err != nil { ctx.Error(err, messageOperationFailed) return }