|
|
|
@ -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) |
|
|
|
|