From 77cd8613c15da73eb49e20fb2f812c2a2981e9bd Mon Sep 17 00:00:00 2001 From: slaventius Date: Wed, 1 Mar 2023 14:24:49 +0300 Subject: [PATCH] * --- .vscode/launch.json | 1 + internal/authDB.go | 29 +++++++++------- internal/config/config.go | 9 ++--- internal/customer/amodel.go | 14 ++++---- internal/customer/customer.go | 33 +++++++++++++++---- internal/customer/repository.go | 12 +++---- .../test3k/umate/pkg/kafka/auth.kafka.go | 4 +-- 7 files changed, 64 insertions(+), 38 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index c665ddc..83043f1 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -19,6 +19,7 @@ "ARANGO_HOST": "127.0.0.1", "ARANGO_USER": "root", "ARANGO_PASSWORD": "dbpassword", + "MINUTES_REREGISTRATION": "2" }, "args": [] } diff --git a/internal/authDB.go b/internal/authDB.go index 04545e8..b4c602c 100644 --- a/internal/authDB.go +++ b/internal/authDB.go @@ -27,6 +27,7 @@ type AuthDBServer struct { mu sync.Mutex ctx context.Context kafkaWriter *kafka.KafkaWriter + config *config.Config logger *logger.Logger repo *repo.CustomerRepository api.UnimplementedAuthDBServer @@ -51,6 +52,7 @@ func NewServer(ctx context.Context, config *config.Config) *AuthDBServer { ctx: ctx, repo: repo, kafkaWriter: kafka.NewWriter(ctx, logger, apiKafka.TopicRegistrations, net.JoinHostPort(config.Kafka.Host, strconv.Itoa(config.Kafka.Port))), + config: config, logger: logger, } } @@ -81,7 +83,7 @@ func (s *AuthDBServer) Login(ctx context.Context, req *api.LoginRequest) (*api.L } // - if customer.Password != s.getMD5Hash(req.Password) { + if customer.Password != s.getMD5Hash(req.GetPassword()) { return nil, errors.New("password incorrect") } @@ -98,24 +100,29 @@ func (s *AuthDBServer) Registration(ctx context.Context, req *api.RegistrationRe customer, ok := s.repo.Customers[req.GetLogin()] if !ok { hash := s.getMD5Hash(req.GetPassword()) - customer = s.repo.NewCustomer(req.GetLogin(), hash, req.GetEmail(), strconv.Itoa(time.Now().Nanosecond())) + customer = s.repo.NewCustomer(req.GetLogin(), hash, req.GetEmail()) // - erk := customer.Add() + erk := customer.Add(s.config.MinutesReregistration) if erk != nil { return nil, erk } // Добавим в локальную копию s.repo.Customers[req.GetLogin()] = customer - } else if customer.Confirmed || time.Now().Before(customer.Time) { + } else if customer.Confirmed || time.Now().Before(customer.TimeReregistration) { return nil, errors.New("login already registered") } else { // Обновим время регистрации - customer.Refresh() + ers := customer.Refresh(s.config.MinutesReregistration) + if ers != nil { + return nil, ers + } + + s.repo.Customers[customer.Login] = customer } // TODO - value, era := json.Marshal(customer.MessageRegistration) + _, era := json.Marshal(customer.MessageRegistration) if era != nil { return nil, era } @@ -124,12 +131,12 @@ func (s *AuthDBServer) Registration(ctx context.Context, req *api.RegistrationRe s.logger.Printf("publication code %s to %s ...", customer.MessageRegistration.Code, customer.MessageRegistration.Email) // Отправим уведомление о необходимости подтверждения - err := s.kafkaWriter.WriteMessage([]byte(customer.Login), value) - if err != nil { - s.logger.Error(err) + // err := s.kafkaWriter.WriteMessage([]byte(customer.Login), value) + // if err != nil { + // s.logger.Error(err) - return nil, err - } + // return nil, err + // } // s.logger.Printf("publication code %s to %s completed", customer.MessageRegistration.Code, customer.MessageRegistration.Email) diff --git a/internal/config/config.go b/internal/config/config.go index b372ae4..39d1752 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -28,10 +28,11 @@ type sentryConfig struct { // ... type Config struct { - App appConfig - Kafka kafkaConfig - Sentry sentryConfig - Arango arangoConfig + App appConfig + Kafka kafkaConfig + Sentry sentryConfig + Arango arangoConfig + MinutesReregistration int `envconfig:"MINUTES_REREGISTRATION"` // number of minutes until the next registration } func NewConfig() *Config { diff --git a/internal/customer/amodel.go b/internal/customer/amodel.go index e803283..5f496e7 100644 --- a/internal/customer/amodel.go +++ b/internal/customer/amodel.go @@ -25,15 +25,14 @@ type Row struct { // Базовые поля type SimpleRow struct { Row - ID string `json:"id"` - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` - DeletedAt string `json:"deleted_at"` + ID string `json:"id"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } func NewSimpleRow(ctx context.Context, database driver.Database, collection driver.Collection) *SimpleRow { now := time.Now() - nowStr := now.Format(StampTemplate) + // nowStr := now.Format(StampTemplate) key := strconv.Itoa(now.Nanosecond() * -1) return &SimpleRow{ @@ -44,9 +43,8 @@ func NewSimpleRow(ctx context.Context, database driver.Database, collection driv key: key, }, ID: key, - CreatedAt: nowStr, - UpdatedAt: nowStr, - DeletedAt: DateTemplatePast, + CreatedAt: now, + UpdatedAt: now, } } diff --git a/internal/customer/customer.go b/internal/customer/customer.go index 506a645..6c1e787 100644 --- a/internal/customer/customer.go +++ b/internal/customer/customer.go @@ -1,6 +1,7 @@ package customer import ( + "strconv" "time" apiKafka "git.slaventius.ru/test3k/umate/pkg/kafka" @@ -9,14 +10,28 @@ import ( // Покупатель type Customer struct { SimpleRow - Login string - Password string - Confirmed bool + Login string `json:"login"` + Password string `json:"password"` + Confirmed bool `json:"confirmed"` apiKafka.MessageRegistration - Time time.Time + TimeReregistration time.Time `json:"reregistration_at"` // Время начиная с которого допустима повторная регистрация } -func (c *Customer) Add() error { +func (c *Customer) refreshCode(minutes int) string { + return strconv.Itoa(time.Now().Nanosecond()) + +} + +func (c *Customer) refreshTime(minutes int) time.Time { + return time.Now().Add(time.Minute * time.Duration(minutes)) + +} + +func (c *Customer) Add(minutes int) error { + c.Code = c.refreshCode(minutes) + c.TimeReregistration = c.refreshTime(minutes) + + // meta, err := c.collection.CreateDocument(c.ctx, c) if err != nil { return err @@ -28,6 +43,9 @@ func (c *Customer) Add() error { } func (c *Customer) Update() error { + c.UpdatedAt = time.Now() + + // _, err := c.collection.UpdateDocument(c.ctx, c.key, c) return err @@ -37,8 +55,9 @@ func (c *Customer) Delete() error { return c.SimpleRow.Delete() } -func (c *Customer) Refresh() error { - c.Time = time.Now().Add(time.Minute * 15) +func (c *Customer) Refresh(minutes int) error { + c.Code = c.refreshCode(minutes) + c.TimeReregistration = c.refreshTime(minutes) return c.Update() } diff --git a/internal/customer/repository.go b/internal/customer/repository.go index e182de9..444a977 100644 --- a/internal/customer/repository.go +++ b/internal/customer/repository.go @@ -35,15 +35,15 @@ func NewCustomerRepository(ctx context.Context, database *arango_db.DataBase) *C } // Новый покупатель -func (r *CustomerRepository) NewCustomer(login string, password string, email string, code string) Customer { +func (r *CustomerRepository) NewCustomer(login string, password string, email string) Customer { return Customer{ - SimpleRow: *NewSimpleRow(r.ctx, r.database, r.collection), - Login: login, - Password: password, - Time: time.Now().Add(time.Minute * 15), + SimpleRow: *NewSimpleRow(r.ctx, r.database, r.collection), + Login: login, + Password: password, + TimeReregistration: time.Now(), MessageRegistration: kafka.MessageRegistration{ Email: email, - Code: code, + Code: time.Now().Format(StampTemplate), }, } } diff --git a/vendor/git.slaventius.ru/test3k/umate/pkg/kafka/auth.kafka.go b/vendor/git.slaventius.ru/test3k/umate/pkg/kafka/auth.kafka.go index 0874640..88caf59 100644 --- a/vendor/git.slaventius.ru/test3k/umate/pkg/kafka/auth.kafka.go +++ b/vendor/git.slaventius.ru/test3k/umate/pkg/kafka/auth.kafka.go @@ -6,6 +6,6 @@ const ( // Структура сообщения передаваемого при регистрации type MessageRegistration struct { - Code string - Email string + Code string `json:"code"` + Email string `json:"email"` }