|
|
|
@ -25,7 +25,7 @@ import ( |
|
|
|
|
type AuthDBServer struct { |
|
|
|
|
kafkaWriter *kafka.KafkaWriter |
|
|
|
|
logger *logger.Logger |
|
|
|
|
users *repo.CustomerRepository |
|
|
|
|
repo *repo.CustomerRepository |
|
|
|
|
api.UnimplementedAuthDBServer |
|
|
|
|
api.UnimplementedHealthServer |
|
|
|
|
ctx context.Context |
|
|
|
@ -37,16 +37,16 @@ func NewServer(ctx context.Context, config *config.Config) *AuthDBServer { |
|
|
|
|
client := arango.NewClient(ctx, conn, config.Arango.User, config.Arango.Password) |
|
|
|
|
arangoDB := arango.NewDataBase(ctx, client, "test3k") |
|
|
|
|
logger := logger.NewLogger("test3k:authDBService", config.Sentry.DSN) |
|
|
|
|
users := repo.NewCustomerRepository(ctx, arangoDB) |
|
|
|
|
repo := repo.NewCustomerRepository(ctx, arangoDB) |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
err := users.Fetch() |
|
|
|
|
err := repo.Fetch() |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal(err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return &AuthDBServer{ |
|
|
|
|
users: users, |
|
|
|
|
repo: repo, |
|
|
|
|
kafkaWriter: kafka.NewWriter(ctx, logger, apiKafka.TopicRegistrations, net.JoinHostPort(config.Kafka.Host, strconv.Itoa(config.Kafka.Port))), |
|
|
|
|
logger: logger, |
|
|
|
|
ctx: ctx, |
|
|
|
@ -65,7 +65,7 @@ func (s *AuthDBServer) GracefulStop() error { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *AuthDBServer) Login(ctx context.Context, req *api.LoginRequest) (*api.LoginResponse, error) { |
|
|
|
|
user, ok := s.users.Customers[req.GetLogin()] |
|
|
|
|
user, ok := s.repo.Customers[req.GetLogin()] |
|
|
|
|
if !ok { |
|
|
|
|
return nil, errors.New("login unknown") |
|
|
|
|
} |
|
|
|
@ -86,10 +86,10 @@ func (s *AuthDBServer) Login(ctx context.Context, req *api.LoginRequest) (*api.L |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *AuthDBServer) Registration(ctx context.Context, req *api.RegistrationRequest) (*api.RegistrationResponse, error) { |
|
|
|
|
user, ok := s.users.Customers[req.GetLogin()] |
|
|
|
|
user, ok := s.repo.Customers[req.GetLogin()] |
|
|
|
|
if !ok { |
|
|
|
|
hash := s.getMD5Hash(req.GetPassword()) |
|
|
|
|
tmpUser, eru := s.users.NewCustomer(req.GetLogin(), hash, req.GetEmail(), strconv.Itoa(time.Now().Nanosecond())) |
|
|
|
|
tmpUser, eru := s.repo.NewCustomer(req.GetLogin(), hash, req.GetEmail(), strconv.Itoa(time.Now().Nanosecond())) |
|
|
|
|
if eru != nil { |
|
|
|
|
s.logger.Error(eru) |
|
|
|
|
|
|
|
|
@ -119,7 +119,7 @@ func (s *AuthDBServer) Registration(ctx context.Context, req *api.RegistrationRe |
|
|
|
|
|
|
|
|
|
// return nil, err
|
|
|
|
|
// } else {
|
|
|
|
|
s.users.Customers[req.Login] = user |
|
|
|
|
s.repo.Customers[req.Login] = user |
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
@ -132,7 +132,7 @@ func (s *AuthDBServer) Registration(ctx context.Context, req *api.RegistrationRe |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *AuthDBServer) Confirmation(ctx context.Context, req *api.ConfirmationRequest) (*api.ConfirmationResponse, error) { |
|
|
|
|
for _, x := range s.users.Customers { |
|
|
|
|
for _, x := range s.repo.Customers { |
|
|
|
|
if x.Code == req.GetCode() { |
|
|
|
|
if x.Confirmed { |
|
|
|
|
return nil, errors.New("already confirmed") |
|
|
|
@ -143,7 +143,7 @@ func (s *AuthDBServer) Confirmation(ctx context.Context, req *api.ConfirmationRe |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} else { // Что бы не перечитывать из БД, обновим локальную копию
|
|
|
|
|
s.users.Customers[x.Login] = x |
|
|
|
|
s.repo.Customers[x.Login] = x |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return &api.ConfirmationResponse{ |
|
|
|
|