From 7c5ddaea58aee59a8763a12e836f527dbce2f182 Mon Sep 17 00:00:00 2001 From: Kat Yang <69819079+yangkb09@users.noreply.github.com> Date: Wed, 17 Aug 2022 02:34:23 -0400 Subject: [PATCH] Chore: Add loginattempt service (#53687) * Chore: Add loginattempt service * Inject loginattemptservice into httpserver --- pkg/api/http_server.go | 5 +- pkg/server/wire.go | 2 + pkg/services/login_attempt/login_attempt.go | 13 ++++++ .../loginattemptimpl/login_attempt.go | 46 +++++++++++++++++++ .../login_attempt/loginattemptimpl/store.go | 1 + .../loginattemptimpl/store_test.go | 1 + 6 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 pkg/services/login_attempt/login_attempt.go create mode 100644 pkg/services/login_attempt/loginattemptimpl/login_attempt.go create mode 100644 pkg/services/login_attempt/loginattemptimpl/store.go create mode 100644 pkg/services/login_attempt/loginattemptimpl/store_test.go diff --git a/pkg/api/http_server.go b/pkg/api/http_server.go index 5209a9ab045..bf074091a4c 100644 --- a/pkg/api/http_server.go +++ b/pkg/api/http_server.go @@ -67,6 +67,7 @@ import ( "github.com/grafana/grafana/pkg/services/quota" "github.com/grafana/grafana/pkg/services/correlations" + loginAttempt "github.com/grafana/grafana/pkg/services/login_attempt" publicdashboardsApi "github.com/grafana/grafana/pkg/services/publicdashboards/api" "github.com/grafana/grafana/pkg/services/query" "github.com/grafana/grafana/pkg/services/queryhistory" @@ -178,6 +179,7 @@ type HTTPServer struct { secretsMigrator secrets.Migrator userService user.Service tempUserService tempUser.Service + loginAttemptService loginAttempt.Service } type ServerOptions struct { @@ -213,7 +215,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi dashboardPermissionsService accesscontrol.DashboardPermissionsService, dashboardVersionService dashver.Service, starService star.Service, csrfService csrf.Service, coremodels *registry.Base, playlistService playlist.Service, apiKeyService apikey.Service, kvStore kvstore.KVStore, secretsMigrator secrets.Migrator, secretsPluginManager plugins.SecretsPluginManager, - publicDashboardsApi *publicdashboardsApi.Api, userService user.Service, tempUserService tempUser.Service) (*HTTPServer, error) { + publicDashboardsApi *publicdashboardsApi.Api, userService user.Service, tempUserService tempUser.Service, loginAttemptService loginAttempt.Service) (*HTTPServer, error) { web.Env = cfg.Env m := web.New() @@ -302,6 +304,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi secretsMigrator: secretsMigrator, userService: userService, tempUserService: tempUserService, + loginAttemptService: loginAttemptService, } if hs.Listener != nil { hs.log.Debug("Using provided listener") diff --git a/pkg/server/wire.go b/pkg/server/wire.go index 521a1bfcc7e..1d850dd1135 100644 --- a/pkg/server/wire.go +++ b/pkg/server/wire.go @@ -74,6 +74,7 @@ import ( "github.com/grafana/grafana/pkg/services/login/authinfoservice" authinfodatabase "github.com/grafana/grafana/pkg/services/login/authinfoservice/database" "github.com/grafana/grafana/pkg/services/login/loginservice" + "github.com/grafana/grafana/pkg/services/login_attempt/loginattemptimpl" "github.com/grafana/grafana/pkg/services/ngalert" ngimage "github.com/grafana/grafana/pkg/services/ngalert/image" ngmetrics "github.com/grafana/grafana/pkg/services/ngalert/metrics" @@ -313,6 +314,7 @@ var wireBasicSet = wire.NewSet( userimpl.ProvideService, orgimpl.ProvideService, tempuserimpl.ProvideService, + loginattemptimpl.ProvideService, datasourceservice.ProvideDataSourceMigrationService, secretsStore.ProvidePluginSecretMigrationService, secretsMigrations.ProvideSecretMigrationService, diff --git a/pkg/services/login_attempt/login_attempt.go b/pkg/services/login_attempt/login_attempt.go new file mode 100644 index 00000000000..cbf581a3d59 --- /dev/null +++ b/pkg/services/login_attempt/login_attempt.go @@ -0,0 +1,13 @@ +package loginattempt + +import ( + "context" + + "github.com/grafana/grafana/pkg/models" +) + +type Service interface { + CreateLoginAttempt(ctx context.Context, cmd *models.CreateLoginAttemptCommand) error + DeleteOldLoginAttempts(ctx context.Context, cmd *models.DeleteOldLoginAttemptsCommand) error + GetUserLoginAttemptCount(ctx context.Context, query *models.GetUserLoginAttemptCountQuery) error +} diff --git a/pkg/services/login_attempt/loginattemptimpl/login_attempt.go b/pkg/services/login_attempt/loginattemptimpl/login_attempt.go new file mode 100644 index 00000000000..8dbb1265f80 --- /dev/null +++ b/pkg/services/login_attempt/loginattemptimpl/login_attempt.go @@ -0,0 +1,46 @@ +package loginattemptimpl + +import ( + "context" + + "github.com/grafana/grafana/pkg/models" + loginattempt "github.com/grafana/grafana/pkg/services/login_attempt" + "github.com/grafana/grafana/pkg/services/sqlstore" +) + +type Service struct { + // TODO remove sqlstore + sqlStore *sqlstore.SQLStore +} + +func ProvideService( + ss *sqlstore.SQLStore, +) loginattempt.Service { + return &Service{ + sqlStore: ss, + } +} + +func (s *Service) CreateLoginAttempt(ctx context.Context, cmd *models.CreateLoginAttemptCommand) error { + err := s.sqlStore.CreateLoginAttempt(ctx, cmd) + if err != nil { + return err + } + return nil +} + +func (s *Service) DeleteOldLoginAttempts(ctx context.Context, cmd *models.DeleteOldLoginAttemptsCommand) error { + err := s.sqlStore.DeleteOldLoginAttempts(ctx, cmd) + if err != nil { + return err + } + return nil +} + +func (s *Service) GetUserLoginAttemptCount(ctx context.Context, cmd *models.GetUserLoginAttemptCountQuery) error { + err := s.sqlStore.GetUserLoginAttemptCount(ctx, cmd) + if err != nil { + return err + } + return nil +} diff --git a/pkg/services/login_attempt/loginattemptimpl/store.go b/pkg/services/login_attempt/loginattemptimpl/store.go new file mode 100644 index 00000000000..346fa966ebe --- /dev/null +++ b/pkg/services/login_attempt/loginattemptimpl/store.go @@ -0,0 +1 @@ +package loginattemptimpl diff --git a/pkg/services/login_attempt/loginattemptimpl/store_test.go b/pkg/services/login_attempt/loginattemptimpl/store_test.go new file mode 100644 index 00000000000..346fa966ebe --- /dev/null +++ b/pkg/services/login_attempt/loginattemptimpl/store_test.go @@ -0,0 +1 @@ +package loginattemptimpl