2024-04-09 15:52:37 +00:00
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"amocrm/internal/service"
|
2024-04-19 16:05:42 +00:00
|
|
|
|
"amocrm/internal/tools"
|
2024-04-23 17:53:33 +00:00
|
|
|
|
"fmt"
|
2024-04-09 15:52:37 +00:00
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
"net/http"
|
2024-04-23 17:53:33 +00:00
|
|
|
|
"strconv"
|
2024-04-09 15:52:37 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// контроллер на который редиректятся ответы по авторизации в амо
|
2024-05-14 21:07:39 +00:00
|
|
|
|
func (c *WebhookController) WebhookCreate(ctx *fiber.Ctx) error {
|
2024-04-09 15:52:37 +00:00
|
|
|
|
code := ctx.Query("code") // Authorization 20 минут
|
|
|
|
|
referer := ctx.Query("referer") // адрес аккаунта пользователя
|
|
|
|
|
state := ctx.Query("state") // строка которая передавалась в соц аус сервисе
|
|
|
|
|
fromWidget := ctx.Query("from_widget")
|
|
|
|
|
platform := ctx.Query("platform") // ru/global 1/2
|
|
|
|
|
noAccess := ctx.Query("error")
|
|
|
|
|
|
|
|
|
|
if noAccess != "" {
|
|
|
|
|
return ctx.Status(http.StatusForbidden).SendString("Access denied")
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 16:05:42 +00:00
|
|
|
|
accountID, err := tools.DeserializeProtobufMessage(state)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.logger.Error("error Deserialize Protobuf Message", zap.Error(err))
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if accountID == nil {
|
|
|
|
|
c.logger.Error("error account id do not be nil", zap.Error(err))
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("nil account id")
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 15:52:37 +00:00
|
|
|
|
req := service.ParamsWebhookCreate{
|
|
|
|
|
Code: code,
|
|
|
|
|
Referer: referer,
|
2024-04-19 16:05:42 +00:00
|
|
|
|
AccountID: *accountID,
|
2024-04-09 15:52:37 +00:00
|
|
|
|
FromWidget: fromWidget,
|
|
|
|
|
Platform: platform,
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 16:05:42 +00:00
|
|
|
|
err = c.service.WebhookCreate(ctx.Context(), req)
|
2024-04-09 15:52:37 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.logger.Error("error create webhook", zap.Error(err))
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-23 17:53:33 +00:00
|
|
|
|
// todo проверить надо
|
2024-05-14 21:07:39 +00:00
|
|
|
|
func (c *WebhookController) WebhookDelete(ctx *fiber.Ctx) error {
|
2024-04-23 17:53:33 +00:00
|
|
|
|
clientUUID := ctx.Query("client_uuid")
|
|
|
|
|
signature := ctx.Query("signature")
|
|
|
|
|
amoIDStr := ctx.Query("account_id")
|
2024-04-09 15:52:37 +00:00
|
|
|
|
|
2024-04-23 17:53:33 +00:00
|
|
|
|
fmt.Println(clientUUID)
|
|
|
|
|
fmt.Println(signature)
|
|
|
|
|
fmt.Println(amoIDStr)
|
|
|
|
|
|
|
|
|
|
if clientUUID == "" || signature == "" || amoIDStr == "" {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("some nil values")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
amoID, err := strconv.Atoi(amoIDStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("invalid account_id type")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !c.verify.CheckIntegrationID(clientUUID) {
|
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("invalid hook signature")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !c.verify.VerifySignature(clientUUID, signature, amoID) {
|
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("invalid hook signature")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = c.service.WebhookDelete(ctx.Context(), amoID)
|
2024-04-09 15:52:37 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
|
}
|
2024-04-23 17:53:33 +00:00
|
|
|
|
|
2024-04-09 15:52:37 +00:00
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
|
}
|