|
|
|
|
@ -16,13 +16,14 @@ import ( |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func (hs *HTTPServer) SendResetPasswordEmail(c *contextmodel.ReqContext) response.Response { |
|
|
|
|
if hs.Cfg.DisableLoginForm || hs.Cfg.DisableLogin { |
|
|
|
|
return response.Error(http.StatusUnauthorized, "Not allowed to reset password when login form is disabled", nil) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
form := dtos.SendResetPasswordEmailForm{} |
|
|
|
|
if err := web.Bind(c.Req, &form); err != nil { |
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err) |
|
|
|
|
} |
|
|
|
|
if hs.Cfg.DisableLoginForm { |
|
|
|
|
return response.Error(401, "Not allowed to reset password when login form is disabled", nil) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
userQuery := user.GetUserByLoginQuery{LoginOrEmail: form.UserOrEmail} |
|
|
|
|
|
|
|
|
|
@ -39,21 +40,27 @@ func (hs *HTTPServer) SendResetPasswordEmail(c *contextmodel.ReqContext) respons |
|
|
|
|
|
|
|
|
|
getAuthQuery := login.GetAuthInfoQuery{UserId: usr.ID} |
|
|
|
|
if authInfo, err := hs.authInfoService.GetAuthInfo(c.Req.Context(), &getAuthQuery); err == nil { |
|
|
|
|
authModule := authInfo.AuthModule |
|
|
|
|
if authModule == login.LDAPAuthModule || authModule == login.AuthProxyAuthModule { |
|
|
|
|
return response.Error(401, "Not allowed to reset password for LDAP or Auth Proxy user", nil) |
|
|
|
|
oauthInfo := hs.SocialService.GetOAuthInfoProvider(authInfo.AuthModule) |
|
|
|
|
if login.IsProviderEnabled(hs.Cfg, authInfo.AuthModule, oauthInfo) { |
|
|
|
|
c.Logger.Info("Requested password reset for external user", nil) |
|
|
|
|
return response.Error(http.StatusOK, "Email sent", nil) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
emailCmd := notifications.SendResetPasswordEmailCommand{User: usr} |
|
|
|
|
if err := hs.NotificationService.SendResetPasswordEmail(c.Req.Context(), &emailCmd); err != nil { |
|
|
|
|
return response.Error(500, "Failed to send email", err) |
|
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to send email", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return response.Success("Email sent") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (hs *HTTPServer) ResetPassword(c *contextmodel.ReqContext) response.Response { |
|
|
|
|
if hs.Cfg.DisableLoginForm || hs.Cfg.DisableLogin { |
|
|
|
|
return response.Error(http.StatusUnauthorized, |
|
|
|
|
"Not allowed to reset password when grafana authentication is disabled", nil) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
form := dtos.ResetUserPasswordForm{} |
|
|
|
|
if err := web.Bind(c.Req, &form); err != nil { |
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err) |
|
|
|
|
@ -73,34 +80,48 @@ func (hs *HTTPServer) ResetPassword(c *contextmodel.ReqContext) response.Respons |
|
|
|
|
userResult, err := hs.NotificationService.ValidateResetPasswordCode(c.Req.Context(), &query, getUserByLogin) |
|
|
|
|
if err != nil { |
|
|
|
|
if errors.Is(err, notifications.ErrInvalidEmailCode) { |
|
|
|
|
return response.Error(400, "Invalid or expired reset password code", nil) |
|
|
|
|
return response.Error(http.StatusBadRequest, "Invalid or expired reset password code", nil) |
|
|
|
|
} |
|
|
|
|
return response.Error(http.StatusInternalServerError, "Unknown error validating email code", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getAuthQuery := login.GetAuthInfoQuery{UserId: userResult.ID} |
|
|
|
|
if authInfo, err := hs.authInfoService.GetAuthInfo(c.Req.Context(), &getAuthQuery); err == nil { |
|
|
|
|
oauthInfo := hs.SocialService.GetOAuthInfoProvider(authInfo.AuthModule) |
|
|
|
|
if login.IsProviderEnabled(hs.Cfg, authInfo.AuthModule, oauthInfo) { |
|
|
|
|
return response.Error(http.StatusBadRequest, "Cannot update external user password", err) |
|
|
|
|
} |
|
|
|
|
return response.Error(500, "Unknown error validating email code", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if form.NewPassword != form.ConfirmPassword { |
|
|
|
|
return response.Error(400, "Passwords do not match", nil) |
|
|
|
|
return response.Error(http.StatusBadRequest, "Passwords do not match", nil) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
password := user.Password(form.NewPassword) |
|
|
|
|
if password.IsWeak() { |
|
|
|
|
return response.Error(400, "New password is too short", nil) |
|
|
|
|
return response.Error(http.StatusBadRequest, "New password is too short", nil) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cmd := user.ChangeUserPasswordCommand{} |
|
|
|
|
cmd.UserID = userResult.ID |
|
|
|
|
cmd.NewPassword, err = util.EncodePassword(form.NewPassword, userResult.Salt) |
|
|
|
|
if err != nil { |
|
|
|
|
return response.Error(500, "Failed to encode password", err) |
|
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to encode password", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := hs.userService.ChangePassword(c.Req.Context(), &cmd); err != nil { |
|
|
|
|
return response.Error(500, "Failed to change user password", err) |
|
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to change user password", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := hs.loginAttemptService.Reset(c.Req.Context(), username); err != nil { |
|
|
|
|
c.Logger.Warn("could not reset login attempts", "err", err, "username", username) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := hs.AuthTokenService.RevokeAllUserTokens(c.Req.Context(), |
|
|
|
|
userResult.ID); err != nil { |
|
|
|
|
return response.Error(http.StatusExpectationFailed, |
|
|
|
|
"User password updated but unable to revoke user sessions", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return response.Success("User password changed") |
|
|
|
|
} |
|
|
|
|
|