Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Prompt 2FA on password reset for accounts that have it enabled.

Kailash Nadh14b25edc3161
diff --git a/cmd/auth.go b/cmd/auth.go
index 2a3397c7..8cb99449 100644
--- a/cmd/auth.go
+++ b/cmd/auth.go
@@ -98,7 +98,7 @@ func (a *App) LoginPage(c *echo.Context) error {
if c.Request().Method == http.MethodPost {
loginErr = a.doLogin(c)
if loginErr == nil {
- return c.Redirect(http.StatusFound, utils.SanitizeURI(c.FormValue("next")))
+ return nil
}
}
@@ -538,6 +538,15 @@ func (a *App) doLogin(c *echo.Context) error {
return err
}
+ return a.finishPasswordLogin(c, user, c.FormValue("next"))
+}
+
+// finishPasswordLogin handles the last step after a password has been auth'd.
+// If 2FA is enabled, that's prompted, otherwise, the session is set and the user
+// is redirected to the next page.
+func (a *App) finishPasswordLogin(c *echo.Context, user auth.User, next string) error {
+ next = utils.SanitizeURI(next)
+
// If TOTP is enabled for the user, create a temp token and redirect to the 2FA page.
if user.TwofaType == models.TwofaTypeTOTP {
// Generate a random token.
@@ -551,7 +560,6 @@ func (a *App) doLogin(c *echo.Context) error {
tmptokens.Set(token, twofaTokenTTL, user.ID)
// Redirect to 2FA page.
- next := utils.SanitizeURI(c.FormValue("next"))
return c.Redirect(http.StatusFound, fmt.Sprintf("%s/login/twofa?token=%s&next=%s", uriAdmin, token, url.QueryEscape(next)))
}
@@ -560,7 +568,7 @@ func (a *App) doLogin(c *echo.Context) error {
return err
}
- return nil
+ return c.Redirect(http.StatusFound, next)
}
// doFirstTimeSetup sets a user up for the first time.
@@ -756,13 +764,7 @@ func (a *App) doResetPassword(c *echo.Context, token, email string) error {
a.log.Printf("error destroying sessions after password reset for user_id=%d: %v", user.ID, err)
}
- // Log the user in directly without forcing a manual login right after password change.
- if err := a.auth.SaveSession(user, "", c); err != nil {
- return err
- }
-
- // Redirect to the admin page.
- return c.Redirect(http.StatusFound, uriAdmin)
+ return a.finishPasswordLogin(c, user, uriAdmin)
}
// renderTwofaPage renders the 2FA verification page.
@@ -803,8 +805,10 @@ func (a *App) doTwofaVerify(c *echo.Context, token string, userID int, next stri
return a.renderTwofaPage(c, token, next, a.i18n.T("globals.messages.invalidValue"))
}
- // Invalidate the token.
- tmptokens.Delete(token)
+ // Invalidate the token tomically to prevent concurrency/replay.
+ if _, err := tmptokens.Get(token); err != nil {
+ return c.Redirect(http.StatusFound, uriAdmin)
+ }
// Set the session.
if err := a.auth.SaveSession(user, "", c); err != nil {