You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
2.4 KiB

2 years ago
package grpc
import (
"context"
2 years ago
"errors"
2 years ago
"log"
"net"
2 years ago
"strconv"
2 years ago
"sync"
2 years ago
kafka "test3k/authDB/internal/transport/kafka"
2 years ago
api "test3k/authDB/pkg/api"
2 years ago
"time"
2 years ago
)
type user struct {
2 years ago
ID int32
Code string
Login string
Password string
Email string
Confirmed bool
2 years ago
}
2 years ago
type AuthDBServer struct {
2 years ago
mu sync.Mutex
users map[string]*user
kafkaWriter *kafka.KafkaWriter
2 years ago
api.UnimplementedAuthDBServer
2 years ago
id int32
2 years ago
}
2 years ago
func NewServer() *AuthDBServer {
return &AuthDBServer{
2 years ago
mu: sync.Mutex{},
users: make(map[string]*user),
kafkaWriter: kafka.NewWriter("registrations", net.JoinHostPort("localhost", "9094")),
id: 0,
2 years ago
}
}
2 years ago
func (s *AuthDBServer) GracefulStop() error {
return s.kafkaWriter.Close()
}
2 years ago
func (s *AuthDBServer) Login(ctx context.Context, req *api.LoginRequest) (*api.LoginResponse, error) {
2 years ago
s.mu.Lock()
defer s.mu.Unlock()
//
user, ok := s.users[req.GetLogin()]
if !ok {
2 years ago
return nil, errors.New("login unknown")
2 years ago
}
//
if !user.Confirmed {
2 years ago
return nil, errors.New("login unconfirmed")
2 years ago
}
//
if user.Password != req.Password {
2 years ago
return nil, errors.New("password incorrect")
2 years ago
}
return &api.LoginResponse{
ID: user.ID,
}, nil
2 years ago
}
2 years ago
func (s *AuthDBServer) Registration(ctx context.Context, req *api.RegistrationRequest) (*api.RegistrationResponse, error) {
2 years ago
s.mu.Lock()
defer s.mu.Unlock()
2 years ago
2 years ago
//
if _, ok := s.users[req.GetLogin()]; ok {
return nil, errors.New("login already registered")
}
2 years ago
//
2 years ago
s.id = s.id + 1
2 years ago
unique := time.Now().Nanosecond()
2 years ago
code := strconv.Itoa(unique)
2 years ago
//
2 years ago
user := &user{
2 years ago
ID: s.id,
Code: code,
2 years ago
Login: req.GetLogin(),
2 years ago
Password: code, // TODO
2 years ago
Email: req.GetEmail(),
2 years ago
Confirmed: false,
2 years ago
}
2 years ago
s.users[req.Login] = user
2 years ago
2 years ago
// TODO
err := s.kafkaWriter.WriteMessage(user.Login, user.Email)
if err != nil {
log.Println("kafka write ", err)
2 years ago
2 years ago
return nil, err
}
2 years ago
return &api.RegistrationResponse{
Code: code,
2 years ago
Email: req.GetEmail(),
2 years ago
}, nil
2 years ago
}
2 years ago
func (s *AuthDBServer) Confirmation(ctx context.Context, req *api.ConfirmationRequest) (*api.ConfirmationResponse, error) {
2 years ago
s.mu.Lock()
defer s.mu.Unlock()
//
for _, x := range s.users {
if x.Code == req.GetCode() {
if x.Confirmed {
return nil, errors.New("already confirmed")
}
//
x.Confirmed = true
return &api.ConfirmationResponse{
ID: x.ID,
}, nil
}
}
return nil, errors.New("code unknown")
2 years ago
}