add signWithID in mongo for uniq sign
This commit is contained in:
parent
b767702160
commit
5728ff46e4
@ -2,7 +2,6 @@ package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/zap"
|
||||
@ -36,13 +35,12 @@ func NewRecoveryEmailSender(deps RecoveryEmailSenderDeps) *RecoveryEmailSender {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RecoveryEmailSender) SendRecoveryEmail(email string, signature []byte) error {
|
||||
signatureStr := base64.URLEncoding.EncodeToString(signature)
|
||||
func (r *RecoveryEmailSender) SendRecoveryEmail(email string, signature string) error {
|
||||
url := r.deps.SmtpApiUrl
|
||||
|
||||
fmt.Println(email, signatureStr)
|
||||
fmt.Println(email, signature)
|
||||
|
||||
message := fmt.Sprintf("http://"+r.deps.CodewordHost+":"+r.deps.CodewordPort+"/recover/%s", signatureStr)
|
||||
message := fmt.Sprintf("http://"+r.deps.CodewordHost+":"+r.deps.CodewordPort+"/recover/%s", signature)
|
||||
|
||||
form := new(bytes.Buffer)
|
||||
writer := multipart.NewWriter(form)
|
||||
|
@ -60,7 +60,9 @@ func (r *RecoveryController) HandleRecoveryRequest(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error"})
|
||||
}
|
||||
|
||||
err = r.service.RecoveryEmailTask(c.Context(), user.ID.Hex(), email, key, id)
|
||||
singWithID := sign + id // подпись с id записи
|
||||
|
||||
err = r.service.RecoveryEmailTask(c.Context(), user.ID.Hex(), email, singWithID, id)
|
||||
if err != nil {
|
||||
r.logger.Error("Failed to send recovery email", zap.Error(err))
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error"})
|
||||
|
@ -22,6 +22,7 @@ type RestoreRequest struct {
|
||||
CreatedAt time.Time `bson:"created_at,omitempty"`
|
||||
Sign string `bson:"sign,omitempty"`
|
||||
SignUrl string `bson:"sign_url,omitempty"`
|
||||
SignID string `bson:"sign_id"`
|
||||
Email string `bson:"email,omitempty"`
|
||||
UserID string `bson:"user_id,omitempty"`
|
||||
Sent bool `bson:"sent"`
|
||||
@ -32,5 +33,5 @@ type RecoveryRecord struct {
|
||||
ID string
|
||||
UserID string
|
||||
Email string
|
||||
Key []byte
|
||||
Key string
|
||||
}
|
||||
|
@ -25,12 +25,14 @@ func NewCodewordRepository(deps Deps) *codewordRepository {
|
||||
// сохраняем полученные данные о пользователе и подписи в бд
|
||||
func (r *codewordRepository) StoreRecoveryRecord(ctx context.Context, userID, email, key, url string) (string, error) {
|
||||
newID := primitive.NewObjectID()
|
||||
signID := key + newID.Hex()
|
||||
record := models.RestoreRequest{
|
||||
ID: newID,
|
||||
UserID: userID,
|
||||
Email: email,
|
||||
Sign: key,
|
||||
SignUrl: url,
|
||||
SignID: signID,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
@ -43,12 +45,12 @@ func (r *codewordRepository) StoreRecoveryRecord(ctx context.Context, userID, em
|
||||
}
|
||||
|
||||
// добавляем в очередь данные для отправки на почту в редис
|
||||
func (r *codewordRepository) InsertToQueue(ctx context.Context, userID string, email string, key []byte, id string) error {
|
||||
func (r *codewordRepository) InsertToQueue(ctx context.Context, userID string, email string, singWithID string, id string) error {
|
||||
task := models.RecoveryRecord{
|
||||
ID: id,
|
||||
UserID: userID,
|
||||
Email: email,
|
||||
Key: key,
|
||||
Key: singWithID,
|
||||
}
|
||||
|
||||
taskBytes, err := json.Marshal(task)
|
||||
@ -67,7 +69,7 @@ func (r *codewordRepository) InsertToQueue(ctx context.Context, userID string, e
|
||||
func (r *codewordRepository) GetRecoveryRecord(ctx context.Context, key string) (*models.RestoreRequest, error) {
|
||||
var restoreRequest models.RestoreRequest
|
||||
|
||||
filter := bson.M{"sign": key}
|
||||
filter := bson.M{"sign_id": key}
|
||||
|
||||
err := r.mdb.FindOne(ctx, filter).Decode(&restoreRequest)
|
||||
if err != nil {
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
|
||||
type CodewordRepository interface {
|
||||
StoreRecoveryRecord(ctx context.Context, userID, email, key, signUrl string) (string, error)
|
||||
InsertToQueue(ctx context.Context, userID string, email string, key []byte, id string) error
|
||||
InsertToQueue(ctx context.Context, userID string, email string, singWithID string, id string) error
|
||||
Ping(ctx context.Context) error
|
||||
GetRecoveryRecord(ctx context.Context, key string) (*models.RestoreRequest, error)
|
||||
}
|
||||
@ -91,8 +91,8 @@ func (s *RecoveryService) StoreRecoveryRecord(ctx context.Context, userID, email
|
||||
}
|
||||
|
||||
// RecoveryEmailTask посылает письмо для восстановления доступа пользователю
|
||||
func (s *RecoveryService) RecoveryEmailTask(ctx context.Context, userID string, email string, key []byte, id string) error {
|
||||
err := s.repositoryCodeword.InsertToQueue(ctx, userID, email, key, id)
|
||||
func (s *RecoveryService) RecoveryEmailTask(ctx context.Context, userID string, email string, singWithID string, id string) error {
|
||||
err := s.repositoryCodeword.InsertToQueue(ctx, userID, email, singWithID, id)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed creating a task to send a worker by email", zap.String("email", email), zap.Error(err))
|
||||
return err
|
||||
@ -102,7 +102,13 @@ func (s *RecoveryService) RecoveryEmailTask(ctx context.Context, userID string,
|
||||
|
||||
// GetRecoveryRecord получает запись восстановления из базы данных
|
||||
func (s *RecoveryService) GetRecoveryRecord(ctx context.Context, key string) (*models.RestoreRequest, error) {
|
||||
byteKey, err := base64.URLEncoding.DecodeString(key)
|
||||
req, err := s.repositoryCodeword.GetRecoveryRecord(ctx, key)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to obtain signature recovery data", zap.String("signature", key), zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
byteKey, err := base64.URLEncoding.DecodeString(req.Sign)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to decode string signature to []byte format", zap.String("signature", key), zap.Error(err))
|
||||
return nil, err
|
||||
@ -115,11 +121,6 @@ func (s *RecoveryService) GetRecoveryRecord(ctx context.Context, key string) (*m
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := s.repositoryCodeword.GetRecoveryRecord(ctx, key)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to obtain signature recovery data", zap.String("signature", key), zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
return req, nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user