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