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"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-08-10 03:21:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/login"
|
2022-06-28 07:32:25 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2018-11-29 06:34:11 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
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
|
|
|
)
|
|
|
|
|
2022-02-03 03:33:46 -06:00
|
|
|
func (hs *HTTPServer) SendResetPasswordEmail(c *models.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)
|
|
|
|
}
|
2018-11-29 06:34:11 -06:00
|
|
|
if setting.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 {
|
2017-04-11 09:50:16 -05:00
|
|
|
c.Logger.Info("Requested password reset for user that was not found", "user", userQuery.LoginOrEmail)
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.Error(http.StatusOK, "Email sent", err)
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
getAuthQuery := models.GetAuthInfoQuery{UserId: usr.ID}
|
|
|
|
if err := hs.authInfoService.GetAuthInfo(c.Req.Context(), &getAuthQuery); err == nil {
|
|
|
|
authModule := getAuthQuery.Result.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-04 06:22:43 -05:00
|
|
|
emailCmd := models.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
|
|
|
}
|
|
|
|
|
2022-02-03 03:33:46 -06:00
|
|
|
func (hs *HTTPServer) ResetPassword(c *models.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)
|
|
|
|
}
|
2020-03-04 05:57:20 -06:00
|
|
|
query := models.ValidateResetPasswordCodeQuery{Code: form.Code}
|
2015-06-08 06:39:02 -05:00
|
|
|
|
2022-06-28 07:32:25 -05:00
|
|
|
getUserByLogin := func(ctx context.Context, login string) (*user.User, error) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if err := hs.NotificationService.ValidateResetPasswordCode(c.Req.Context(), &query, getUserByLogin); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.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
|
|
|
}
|
|
|
|
|
2022-06-20 02:16:42 -05:00
|
|
|
password := models.Password(form.NewPassword)
|
|
|
|
if password.IsWeak() {
|
|
|
|
return response.Error(400, "New password is too short", nil)
|
|
|
|
}
|
|
|
|
|
2022-08-04 08:05:05 -05:00
|
|
|
cmd := user.ChangeUserPasswordCommand{}
|
|
|
|
cmd.UserID = query.Result.ID
|
2019-10-23 03:40:12 -05:00
|
|
|
var err error
|
|
|
|
cmd.NewPassword, err = util.EncodePassword(form.NewPassword, query.Result.Salt)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("User password changed")
|
2015-06-08 03:57:01 -05:00
|
|
|
}
|