Chore: Remove bus from password (#44482)

* Chore: Remove bus from password

* Refactor: Remove bus from password.go and adjust tests

* remove sqlstore dependency from notifications

* Chore: Remove bus from password

* Refactor: Remove bus from password.go and adjust tests

* remove sqlstore dependency (again)

* remove fmt printf

* fix dependencies in http server

* fix renamed method in tests

Co-authored-by: Serge Zaitsev <serge.zaitsev@grafana.com>
This commit is contained in:
Kat Yang
2022-02-03 04:33:46 -05:00
committed by GitHub
parent f36ed878e9
commit f582e6c86a
6 changed files with 44 additions and 29 deletions
+2 -2
View File
@@ -126,8 +126,8 @@ func (hs *HTTPServer) registerRoutes() {
r.Get("/user/password/send-reset-email", reqNotSignedIn, hs.Index)
r.Get("/user/password/reset", hs.Index)
r.Post("/api/user/password/send-reset-email", routing.Wrap(SendResetPasswordEmail))
r.Post("/api/user/password/reset", routing.Wrap(ResetPassword))
r.Post("/api/user/password/send-reset-email", routing.Wrap(hs.SendResetPasswordEmail))
r.Post("/api/user/password/reset", routing.Wrap(hs.ResetPassword))
// dashboard snapshots
r.Get("/dashboard/snapshot/*", reqNoAuth, hs.Index)
+5 -1
View File
@@ -46,6 +46,7 @@ import (
"github.com/grafana/grafana/pkg/services/login"
"github.com/grafana/grafana/pkg/services/login/authinfoservice"
"github.com/grafana/grafana/pkg/services/ngalert"
"github.com/grafana/grafana/pkg/services/notifications"
"github.com/grafana/grafana/pkg/services/provisioning"
"github.com/grafana/grafana/pkg/services/query"
"github.com/grafana/grafana/pkg/services/queryhistory"
@@ -129,6 +130,7 @@ type HTTPServer struct {
serviceAccountsService serviceaccounts.Service
authInfoService authinfoservice.Service
TeamPermissionsService *resourcepermissions.Service
NotificationService *notifications.NotificationService
}
type ServerOptions struct {
@@ -155,7 +157,8 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
pluginsUpdateChecker *updatechecker.PluginsService, searchUsersService searchusers.Service,
dataSourcesService *datasources.Service, secretsService secrets.Service, queryDataService *query.Service,
ldapGroups ldap.Groups, teamGuardian teamguardian.TeamGuardian, serviceaccountsService serviceaccounts.Service,
authInfoService authinfoservice.Service, resourcePermissionServices *resourceservices.ResourceServices) (*HTTPServer, error) {
authInfoService authinfoservice.Service, resourcePermissionServices *resourceservices.ResourceServices,
notificationService *notifications.NotificationService) (*HTTPServer, error) {
web.Env = cfg.Env
m := web.New()
@@ -215,6 +218,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
serviceAccountsService: serviceaccountsService,
authInfoService: authInfoService,
TeamPermissionsService: resourcePermissionServices.GetTeamService(),
NotificationService: notificationService,
}
if hs.Listener != nil {
hs.log.Debug("Using provided listener")
+13 -7
View File
@@ -1,19 +1,19 @@
package api
import (
"context"
"errors"
"net/http"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/web"
)
func SendResetPasswordEmail(c *models.ReqContext) response.Response {
func (hs *HTTPServer) SendResetPasswordEmail(c *models.ReqContext) response.Response {
form := dtos.SendResetPasswordEmailForm{}
if err := web.Bind(c.Req, &form); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
@@ -27,27 +27,33 @@ func SendResetPasswordEmail(c *models.ReqContext) response.Response {
userQuery := models.GetUserByLoginQuery{LoginOrEmail: form.UserOrEmail}
if err := bus.Dispatch(c.Req.Context(), &userQuery); err != nil {
if err := hs.SQLStore.GetUserByLogin(c.Req.Context(), &userQuery); err != nil {
c.Logger.Info("Requested password reset for user that was not found", "user", userQuery.LoginOrEmail)
return response.Error(200, "Email sent", err)
}
emailCmd := models.SendResetPasswordEmailCommand{User: userQuery.Result}
if err := bus.Dispatch(c.Req.Context(), &emailCmd); err != nil {
if err := hs.NotificationService.SendResetPasswordEmail(c.Req.Context(), &emailCmd); err != nil {
return response.Error(500, "Failed to send email", err)
}
return response.Success("Email sent")
}
func ResetPassword(c *models.ReqContext) response.Response {
func (hs *HTTPServer) ResetPassword(c *models.ReqContext) response.Response {
form := dtos.ResetUserPasswordForm{}
if err := web.Bind(c.Req, &form); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
query := models.ValidateResetPasswordCodeQuery{Code: form.Code}
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
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 {
if errors.Is(err, models.ErrInvalidEmailCode) {
return response.Error(400, "Invalid or expired reset password code", nil)
}
@@ -66,7 +72,7 @@ func ResetPassword(c *models.ReqContext) response.Response {
return response.Error(500, "Failed to encode password", err)
}
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
if err := hs.SQLStore.ChangeUserPassword(c.Req.Context(), &cmd); err != nil {
return response.Error(500, "Failed to change user password", err)
}
-1
View File
@@ -70,7 +70,6 @@ func validateUserEmailCode(cfg *setting.Cfg, user *models.User, code string) (bo
if err != nil {
return false, err
}
fmt.Printf("code : %s\ncode2: %s", retCode, code)
if retCode == code && minutes > 0 {
// check time is expired or not
before, _ := time.ParseInLocation("200601021504", start, time.Local)
+14 -8
View File
@@ -29,6 +29,10 @@ type Service interface {
EmailSender
}
type Store interface {
GetUserByLogin(context.Context, *models.GetUserByLoginQuery) error
}
var mailTemplates *template.Template
var tmplResetPassword = "reset_password"
var tmplSignUpStarted = "signup_started"
@@ -44,8 +48,8 @@ func ProvideService(bus bus.Bus, cfg *setting.Cfg, mailer Mailer) (*Notification
mailer: mailer,
}
ns.Bus.AddHandler(ns.sendResetPasswordEmail)
ns.Bus.AddHandler(ns.validateResetPasswordCode)
ns.Bus.AddHandler(ns.SendResetPasswordEmail)
ns.Bus.AddHandler(ns.ValidateResetPasswordCode)
ns.Bus.AddHandler(ns.SendEmailCommandHandler)
ns.Bus.AddHandler(ns.SendEmailCommandHandlerSync)
@@ -163,7 +167,7 @@ func (ns *NotificationService) SendEmailCommandHandler(ctx context.Context, cmd
return nil
}
func (ns *NotificationService) sendResetPasswordEmail(ctx context.Context, cmd *models.SendResetPasswordEmailCommand) error {
func (ns *NotificationService) SendResetPasswordEmail(ctx context.Context, cmd *models.SendResetPasswordEmailCommand) error {
code, err := createUserEmailCode(ns.Cfg, cmd.User, nil)
if err != nil {
return err
@@ -178,18 +182,20 @@ func (ns *NotificationService) sendResetPasswordEmail(ctx context.Context, cmd *
})
}
func (ns *NotificationService) validateResetPasswordCode(ctx context.Context, query *models.ValidateResetPasswordCodeQuery) error {
type GetUserByLoginFunc = func(c context.Context, login string) (*models.User, error)
func (ns *NotificationService) ValidateResetPasswordCode(ctx context.Context, query *models.ValidateResetPasswordCodeQuery, userByLogin GetUserByLoginFunc) error {
login := getLoginForEmailCode(query.Code)
if login == "" {
return models.ErrInvalidEmailCode
}
userQuery := models.GetUserByLoginQuery{LoginOrEmail: login}
if err := bus.Dispatch(ctx, &userQuery); err != nil {
user, err := userByLogin(ctx, login)
if err != nil {
return err
}
validEmailCode, err := validateUserEmailCode(ns.Cfg, userQuery.Result, query.Code)
validEmailCode, err := validateUserEmailCode(ns.Cfg, user, query.Code)
if err != nil {
return err
}
@@ -197,7 +203,7 @@ func (ns *NotificationService) validateResetPasswordCode(ctx context.Context, qu
return models.ErrInvalidEmailCode
}
query.Result = userQuery.Result
query.Result = user
return nil
}
@@ -17,7 +17,7 @@ func TestProvideService(t *testing.T) {
t.Run("When invalid from_address in configuration", func(t *testing.T) {
cfg := createSmtpConfig()
cfg.Smtp.FromAddress = "@notanemail@"
_, _, err := createSutWithConfig(bus, cfg)
_, _, err := createSutWithConfig(t, bus, cfg)
require.Error(t, err)
})
@@ -25,7 +25,7 @@ func TestProvideService(t *testing.T) {
t.Run("When template_patterns fails to parse", func(t *testing.T) {
cfg := createSmtpConfig()
cfg.Smtp.TemplatesPatterns = append(cfg.Smtp.TemplatesPatterns, "/usr/not-a-dir/**")
_, _, err := createSutWithConfig(bus, cfg)
_, _, err := createSutWithConfig(t, bus, cfg)
require.Error(t, err)
})
@@ -119,7 +119,7 @@ func TestSendEmailSync(t *testing.T) {
t.Run("When SMTP disabled in configuration", func(t *testing.T) {
cfg := createSmtpConfig()
cfg.Smtp.Enabled = false
_, mailer, err := createSutWithConfig(bus, cfg)
_, mailer, err := createSutWithConfig(t, bus, cfg)
require.NoError(t, err)
cmd := &models.SendEmailCommandSync{
SendEmailCommand: models.SendEmailCommand{
@@ -139,7 +139,7 @@ func TestSendEmailSync(t *testing.T) {
t.Run("When invalid content type in configuration", func(t *testing.T) {
cfg := createSmtpConfig()
cfg.Smtp.ContentTypes = append(cfg.Smtp.ContentTypes, "multipart/form-data")
_, mailer, err := createSutWithConfig(bus, cfg)
_, mailer, err := createSutWithConfig(t, bus, cfg)
require.NoError(t, err)
cmd := &models.SendEmailCommandSync{
SendEmailCommand: models.SendEmailCommand{
@@ -178,7 +178,7 @@ func TestSendEmailAsync(t *testing.T) {
t.Run("When sending reset email password", func(t *testing.T) {
sut, _ := createSut(t, bus)
err := sut.sendResetPasswordEmail(context.Background(), &models.SendResetPasswordEmailCommand{User: &models.User{Email: "asd@asd.com"}})
err := sut.SendResetPasswordEmail(context.Background(), &models.SendResetPasswordEmailCommand{User: &models.User{Email: "asd@asd.com"}})
require.NoError(t, err)
sentMsg := <-sut.mailQueue
@@ -192,7 +192,7 @@ func TestSendEmailAsync(t *testing.T) {
t.Run("When SMTP disabled in configuration", func(t *testing.T) {
cfg := createSmtpConfig()
cfg.Smtp.Enabled = false
_, mailer, err := createSutWithConfig(bus, cfg)
ns, mailer, err := createSutWithConfig(t, bus, cfg)
require.NoError(t, err)
cmd := &models.SendEmailCommand{
Subject: "subject",
@@ -201,7 +201,7 @@ func TestSendEmailAsync(t *testing.T) {
Template: "welcome_on_signup",
}
err = bus.Dispatch(context.Background(), cmd)
err = ns.SendEmailCommandHandler(context.Background(), cmd)
require.ErrorIs(t, err, models.ErrSmtpNotEnabled)
require.Empty(t, mailer.Sent)
@@ -210,7 +210,7 @@ func TestSendEmailAsync(t *testing.T) {
t.Run("When invalid content type in configuration", func(t *testing.T) {
cfg := createSmtpConfig()
cfg.Smtp.ContentTypes = append(cfg.Smtp.ContentTypes, "multipart/form-data")
_, mailer, err := createSutWithConfig(bus, cfg)
_, mailer, err := createSutWithConfig(t, bus, cfg)
require.NoError(t, err)
cmd := &models.SendEmailCommand{
Subject: "subject",
@@ -245,12 +245,12 @@ func createSut(t *testing.T, bus bus.Bus) (*NotificationService, *FakeMailer) {
t.Helper()
cfg := createSmtpConfig()
ns, fm, err := createSutWithConfig(bus, cfg)
ns, fm, err := createSutWithConfig(t, bus, cfg)
require.NoError(t, err)
return ns, fm
}
func createSutWithConfig(bus bus.Bus, cfg *setting.Cfg) (*NotificationService, *FakeMailer, error) {
func createSutWithConfig(t *testing.T, bus bus.Bus, cfg *setting.Cfg) (*NotificationService, *FakeMailer, error) {
smtp := NewFakeMailer()
ns, err := ProvideService(bus, cfg, smtp)
return ns, smtp, err