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.

153 lines
3.1 KiB

2 years ago
package authDB
2 years ago
import (
"context"
2 years ago
"encoding/json"
2 years ago
"errors"
2 years ago
"log"
"net"
2 years ago
"strconv"
2 years ago
"sync"
2 years ago
"time"
2 years ago
2 years ago
kafka "git.slaventius.ru/test3k/authDB/internal/transport/kafka"
2 years ago
2 years ago
"git.slaventius.ru/test3k/authDB/internal/config"
api "git.slaventius.ru/test3k/umate/pkg/api"
2 years ago
apiKafka "git.slaventius.ru/test3k/umate/pkg/kafka"
2 years ago
)
type user struct {
2 years ago
ID int32
Login string
Password string
Confirmed bool
2 years ago
Time time.Time
2 years ago
apiKafka.MessageRegistration
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
ctx context.Context
id int32
2 years ago
}
2 years ago
func NewServer(ctx context.Context, config *config.Config) *AuthDBServer {
2 years ago
return &AuthDBServer{
2 years ago
mu: sync.Mutex{},
users: make(map[string]*user),
2 years ago
kafkaWriter: kafka.NewWriter(ctx, apiKafka.TopicRegistrations, net.JoinHostPort(config.Kafka.Host, strconv.Itoa(config.Kafka.Port))),
2 years ago
ctx: ctx,
2 years ago
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 val, ok := s.users[req.GetLogin()]; ok {
if time.Now().Before(val.Time) {
return nil, errors.New("login already registered")
}
} else {
s.id = s.id + 1
}
2 years ago
//
2 years ago
user := &user{
2 years ago
ID: s.id,
2 years ago
Login: req.GetLogin(),
2 years ago
Password: req.GetPassword(),
2 years ago
Confirmed: false,
2 years ago
Time: time.Now().Add(time.Minute * 15),
2 years ago
MessageRegistration: apiKafka.MessageRegistration{
2 years ago
Code: strconv.Itoa(time.Now().Nanosecond()),
2 years ago
Email: req.GetEmail(),
},
2 years ago
}
2 years ago
// TODO
2 years ago
value, eru := json.Marshal(user.MessageRegistration)
2 years ago
if eru != nil {
return nil, eru
}
2 years ago
//
2 years ago
log.Printf("publication code %s to %s ...", user.MessageRegistration.Code, user.MessageRegistration.Email)
2 years ago
2 years ago
//
err := s.kafkaWriter.WriteMessage([]byte(user.Login), value)
2 years ago
if err != nil {
2 years ago
log.Print(err)
2 years ago
2 years ago
return nil, err
2 years ago
} else {
s.users[req.Login] = user
2 years ago
}
2 years ago
2 years ago
//
2 years ago
log.Printf("publication code %s to %s completed", user.MessageRegistration.Code, user.MessageRegistration.Email)
2 years ago
2 years ago
return &api.RegistrationResponse{
2 years ago
Code: user.MessageRegistration.Code,
Email: user.MessageRegistration.Email,
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
}