2015-06-08 03:57:01 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-02-03 03:33:46 -06:00
|
|
|
"context"
|
2020-11-19 06:34:28 -06:00
|
|
|
"errors"
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2020-11-19 06:34:28 -06:00
|
|
|
|
2015-06-08 03:57:01 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2022-08-10 03:21:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/login"
|
2023-01-17 13:47:31 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/notifications"
|
2022-06-28 07:32:25 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2015-06-08 06:39:02 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-11-29 03:18:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-06-08 03:57:01 -05:00
|
|
|
)
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) SendResetPasswordEmail(c *contextmodel.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
form := dtos.SendResetPasswordEmailForm{}
|
|
|
|
if err := web.Bind(c.Req, &form); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2023-02-27 08:28:49 -06:00
|
|
|
if hs.Cfg.DisableLoginForm {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(401, "Not allowed to reset password when login form is disabled", nil)
|
2018-11-29 06:34:11 -06:00
|
|
|
}
|
|
|
|
|
2022-08-04 06:22:43 -05:00
|
|
|
userQuery := user.GetUserByLoginQuery{LoginOrEmail: form.UserOrEmail}
|
2015-06-08 03:57:01 -05:00
|
|
|
|
2022-08-04 06:22:43 -05:00
|
|
|
usr, err := hs.userService.GetByLogin(c.Req.Context(), &userQuery)
|
|
|
|
if err != nil {
|
2022-11-14 02:42:31 -06:00
|
|
|
c.Logger.Info("Requested password reset for user that was not found", "user", userQuery.LoginOrEmail, "error", err)
|
|
|
|
return response.Error(http.StatusOK, "Email sent", nil)
|
2015-06-08 03:57:01 -05:00
|
|
|
}
|
|
|
|
|
2022-08-08 00:12:39 -05:00
|
|
|
if usr.IsDisabled {
|
|
|
|
c.Logger.Info("Requested password reset for disabled user", "user", userQuery.LoginOrEmail)
|
|
|
|
return response.Error(http.StatusOK, "Email sent", nil)
|
|
|
|
}
|
|
|
|
|
2023-01-27 12:36:54 -06:00
|
|
|
getAuthQuery := login.GetAuthInfoQuery{UserId: usr.ID}
|
2023-03-28 13:32:21 -05:00
|
|
|
if authInfo, err := hs.authInfoService.GetAuthInfo(c.Req.Context(), &getAuthQuery); err == nil {
|
|
|
|
authModule := authInfo.AuthModule
|
2022-08-10 03:21:33 -05:00
|
|
|
if authModule == login.LDAPAuthModule || authModule == login.AuthProxyAuthModule {
|
2022-08-08 00:12:39 -05:00
|
|
|
return response.Error(401, "Not allowed to reset password for LDAP or Auth Proxy user", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 13:47:31 -06:00
|
|
|
emailCmd := notifications.SendResetPasswordEmailCommand{User: usr}
|
2022-02-03 03:33:46 -06:00
|
|
|
if err := hs.NotificationService.SendResetPasswordEmail(c.Req.Context(), &emailCmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to send email", err)
|
2015-06-08 03:57:01 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Email sent")
|
2015-06-08 03:57:01 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) ResetPassword(c *contextmodel.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
form := dtos.ResetUserPasswordForm{}
|
|
|
|
if err := web.Bind(c.Req, &form); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2023-01-17 13:47:31 -06:00
|
|
|
query := notifications.ValidateResetPasswordCodeQuery{Code: form.Code}
|
2015-06-08 06:39:02 -05:00
|
|
|
|
2022-11-23 09:57:18 -06:00
|
|
|
// For now the only way to know the username to clear login attempts for is
|
|
|
|
// to set it in the function provided to NotificationService
|
|
|
|
var username string
|
2022-06-28 07:32:25 -05:00
|
|
|
getUserByLogin := func(ctx context.Context, login string) (*user.User, error) {
|
2022-11-23 09:57:18 -06:00
|
|
|
username = login
|
2022-08-04 06:22:43 -05:00
|
|
|
userQuery := user.GetUserByLoginQuery{LoginOrEmail: login}
|
|
|
|
usr, err := hs.userService.GetByLogin(ctx, &userQuery)
|
|
|
|
return usr, err
|
2022-02-03 03:33:46 -06:00
|
|
|
}
|
|
|
|
|
2023-03-28 06:44:15 -05:00
|
|
|
userResult, err := hs.NotificationService.ValidateResetPasswordCode(c.Req.Context(), &query, getUserByLogin)
|
|
|
|
if err != nil {
|
2023-01-17 13:47:31 -06:00
|
|
|
if errors.Is(err, notifications.ErrInvalidEmailCode) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Invalid or expired reset password code", nil)
|
2015-06-08 06:39:02 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Unknown error validating email code", err)
|
2015-06-08 06:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if form.NewPassword != form.ConfirmPassword {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Passwords do not match", nil)
|
2015-06-08 06:39:02 -05:00
|
|
|
}
|
|
|
|
|
2023-01-31 04:04:55 -06:00
|
|
|
password := user.Password(form.NewPassword)
|
2022-06-20 02:16:42 -05:00
|
|
|
if password.IsWeak() {
|
|
|
|
return response.Error(400, "New password is too short", nil)
|
|
|
|
}
|
|
|
|
|
2022-08-04 08:05:05 -05:00
|
|
|
cmd := user.ChangeUserPasswordCommand{}
|
2023-03-28 06:44:15 -05:00
|
|
|
cmd.UserID = userResult.ID
|
|
|
|
cmd.NewPassword, err = util.EncodePassword(form.NewPassword, userResult.Salt)
|
2019-10-23 03:40:12 -05:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to encode password", err)
|
2019-10-23 03:40:12 -05:00
|
|
|
}
|
2015-06-08 06:39:02 -05:00
|
|
|
|
2022-08-04 08:05:05 -05:00
|
|
|
if err := hs.userService.ChangePassword(c.Req.Context(), &cmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to change user password", err)
|
2015-06-08 06:39:02 -05:00
|
|
|
}
|
|
|
|
|
2022-11-23 09:57:18 -06:00
|
|
|
if err := hs.loginAttemptService.Reset(c.Req.Context(), username); err != nil {
|
|
|
|
c.Logger.Warn("could not reset login attempts", "err", err, "username", username)
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("User password changed")
|
2015-06-08 03:57:01 -05:00
|
|
|
}
|