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"
|
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)
|
|
|
|
}
|
2019-05-22 07:30:03 -05:00
|
|
|
if setting.LDAPEnabled || setting.AuthProxyEnabled {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(401, "Not allowed to reset password when LDAP or Auth Proxy is enabled", nil)
|
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
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
userQuery := models.GetUserByLoginQuery{LoginOrEmail: form.UserOrEmail}
|
2015-06-08 03:57:01 -05:00
|
|
|
|
2022-02-03 03:33:46 -06:00
|
|
|
if err := hs.SQLStore.GetUserByLogin(c.Req.Context(), &userQuery); 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)
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(200, "Email sent", err)
|
2015-06-08 03:57:01 -05:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
emailCmd := models.SendResetPasswordEmailCommand{User: userQuery.Result}
|
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-02-03 03:33:46 -06:00
|
|
|
getUserByLogin := func(ctx context.Context, login string) (*models.User, error) {
|
|
|
|
userQuery := models.GetUserByLoginQuery{LoginOrEmail: login}
|
|
|
|
err := hs.SQLStore.GetUserByLogin(ctx, &userQuery)
|
|
|
|
return userQuery.Result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
cmd := models.ChangeUserPasswordCommand{}
|
2015-06-08 06:39:02 -05:00
|
|
|
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-02-03 03:33:46 -06:00
|
|
|
if err := hs.SQLStore.ChangeUserPassword(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
|
|
|
}
|