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 {
|
2023-12-29 07:23:05 -06:00
|
|
|
if hs.Cfg.DisableLoginForm || hs.Cfg.DisableLogin {
|
|
|
|
return response.Error(http.StatusUnauthorized, "Not allowed to reset password when login form is disabled", nil)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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 {
|
2023-12-29 07:23:05 -06:00
|
|
|
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)
|
2022-08-08 00:12:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-12-29 07:23:05 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "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 {
|
2023-12-29 07:23:05 -06:00
|
|
|
if hs.Cfg.DisableLoginForm || hs.Cfg.DisableLogin {
|
|
|
|
return response.Error(http.StatusUnauthorized,
|
|
|
|
"Not allowed to reset password when grafana authentication is disabled", nil)
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-12-29 07:23:05 -06:00
|
|
|
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)
|
2015-06-08 06:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.NewPassword != form.ConfirmPassword {
|
2023-12-29 07:23:05 -06:00
|
|
|
return response.Error(http.StatusBadRequest, "Passwords do not match", nil)
|
2015-06-08 06:39:02 -05:00
|
|
|
}
|
|
|
|
|
2024-02-16 04:58:05 -06:00
|
|
|
if err := form.NewPassword.Validate(hs.Cfg); err != nil {
|
|
|
|
c.Logger.Warn("the new password doesn't meet the password policy criteria", "err", err)
|
|
|
|
return response.Err(err)
|
2022-06-20 02:16:42 -05:00
|
|
|
}
|
|
|
|
|
2022-08-04 08:05:05 -05:00
|
|
|
cmd := user.ChangeUserPasswordCommand{}
|
2023-03-28 06:44:15 -05:00
|
|
|
cmd.UserID = userResult.ID
|
2024-02-16 04:58:05 -06:00
|
|
|
encodedPassword, err := util.EncodePassword(string(form.NewPassword), userResult.Salt)
|
2019-10-23 03:40:12 -05:00
|
|
|
if err != nil {
|
2023-12-29 07:23:05 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to encode password", err)
|
2019-10-23 03:40:12 -05:00
|
|
|
}
|
2024-02-16 04:58:05 -06:00
|
|
|
cmd.NewPassword = user.Password(encodedPassword)
|
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 {
|
2023-12-29 07:23:05 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "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)
|
|
|
|
}
|
|
|
|
|
2023-12-29 07:23:05 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("User password changed")
|
2015-06-08 03:57:01 -05:00
|
|
|
}
|