2015-06-08 16:57:01 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-02-03 17:33:46 +08:00
|
|
|
"context"
|
2020-11-19 20:34:28 +08:00
|
|
|
"errors"
|
2021-11-29 17:18:01 +08:00
|
|
|
"net/http"
|
2020-11-19 20:34:28 +08:00
|
|
|
|
2015-06-08 16:57:01 +08:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 21:43:20 +08:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2023-01-27 15:50:36 +08:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2022-08-10 16:21:33 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/login"
|
2023-01-18 03:47:31 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/notifications"
|
2022-06-28 20:32:25 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2021-11-29 17:18:01 +08:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-06-08 16:57:01 +08:00
|
|
|
)
|
|
|
|
|
2023-01-27 15:50:36 +08:00
|
|
|
func (hs *HTTPServer) SendResetPasswordEmail(c *contextmodel.ReqContext) response.Response {
|
2023-12-29 21:23:05 +08: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 17:18:01 +08: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 20:34:11 +08:00
|
|
|
|
2022-08-04 19:22:43 +08:00
|
|
|
userQuery := user.GetUserByLoginQuery{LoginOrEmail: form.UserOrEmail}
|
2015-06-08 16:57:01 +08:00
|
|
|
|
2022-08-04 19:22:43 +08:00
|
|
|
usr, err := hs.userService.GetByLogin(c.Req.Context(), &userQuery)
|
|
|
|
if err != nil {
|
2022-11-14 16:42:31 +08: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 16:57:01 +08:00
|
|
|
}
|
|
|
|
|
2022-08-08 13:12:39 +08: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-28 02:36:54 +08:00
|
|
|
getAuthQuery := login.GetAuthInfoQuery{UserId: usr.ID}
|
2023-03-29 02:32:21 +08:00
|
|
|
if authInfo, err := hs.authInfoService.GetAuthInfo(c.Req.Context(), &getAuthQuery); err == nil {
|
2025-01-11 00:37:37 +08:00
|
|
|
if hs.isProviderEnabled(hs.Cfg, authInfo.AuthModule) {
|
2023-12-29 21:23:05 +08:00
|
|
|
c.Logger.Info("Requested password reset for external user", nil)
|
|
|
|
return response.Error(http.StatusOK, "Email sent", nil)
|
2022-08-08 13:12:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 03:47:31 +08:00
|
|
|
emailCmd := notifications.SendResetPasswordEmailCommand{User: usr}
|
2022-02-03 17:33:46 +08:00
|
|
|
if err := hs.NotificationService.SendResetPasswordEmail(c.Req.Context(), &emailCmd); err != nil {
|
2023-12-29 21:23:05 +08:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to send email", err)
|
2015-06-08 16:57:01 +08:00
|
|
|
}
|
|
|
|
|
2021-01-15 21:43:20 +08:00
|
|
|
return response.Success("Email sent")
|
2015-06-08 16:57:01 +08:00
|
|
|
}
|
|
|
|
|
2023-01-27 15:50:36 +08:00
|
|
|
func (hs *HTTPServer) ResetPassword(c *contextmodel.ReqContext) response.Response {
|
2023-12-29 21:23:05 +08: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 17:18:01 +08:00
|
|
|
form := dtos.ResetUserPasswordForm{}
|
|
|
|
if err := web.Bind(c.Req, &form); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2024-04-17 21:24:36 +08:00
|
|
|
|
|
|
|
if form.NewPassword != form.ConfirmPassword {
|
|
|
|
return response.Error(http.StatusBadRequest, "Passwords do not match", nil)
|
|
|
|
}
|
|
|
|
|
2023-01-18 03:47:31 +08:00
|
|
|
query := notifications.ValidateResetPasswordCodeQuery{Code: form.Code}
|
2015-06-08 19:39:02 +08:00
|
|
|
|
2022-11-23 23:57:18 +08: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 20:32:25 +08:00
|
|
|
getUserByLogin := func(ctx context.Context, login string) (*user.User, error) {
|
2022-11-23 23:57:18 +08:00
|
|
|
username = login
|
2022-08-04 19:22:43 +08:00
|
|
|
userQuery := user.GetUserByLoginQuery{LoginOrEmail: login}
|
|
|
|
usr, err := hs.userService.GetByLogin(ctx, &userQuery)
|
|
|
|
return usr, err
|
2022-02-03 17:33:46 +08:00
|
|
|
}
|
|
|
|
|
2023-03-28 19:44:15 +08:00
|
|
|
userResult, err := hs.NotificationService.ValidateResetPasswordCode(c.Req.Context(), &query, getUserByLogin)
|
|
|
|
if err != nil {
|
2023-01-18 03:47:31 +08:00
|
|
|
if errors.Is(err, notifications.ErrInvalidEmailCode) {
|
2023-12-29 21:23:05 +08:00
|
|
|
return response.Error(http.StatusBadRequest, "Invalid or expired reset password code", nil)
|
|
|
|
}
|
|
|
|
return response.Error(http.StatusInternalServerError, "Unknown error validating email code", err)
|
|
|
|
}
|
|
|
|
|
2024-04-17 21:24:36 +08:00
|
|
|
if response := hs.errOnExternalUser(c.Req.Context(), userResult.ID); response != nil {
|
|
|
|
return response
|
2019-10-23 16:40:12 +08:00
|
|
|
}
|
2015-06-08 19:39:02 +08:00
|
|
|
|
2024-04-17 21:24:36 +08:00
|
|
|
if err := hs.userService.Update(c.Req.Context(), &user.UpdateUserCommand{UserID: userResult.ID, Password: &form.ConfirmPassword}); err != nil {
|
|
|
|
return response.ErrOrFallback(http.StatusInternalServerError, "Failed to change user password", err)
|
2015-06-08 19:39:02 +08:00
|
|
|
}
|
|
|
|
|
2022-11-23 23:57:18 +08: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 21:23:05 +08: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 21:43:20 +08:00
|
|
|
return response.Success("User password changed")
|
2015-06-08 16:57:01 +08:00
|
|
|
}
|