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

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