88 lines
2.7 KiB
Go
88 lines
2.7 KiB
Go
package controllers
|
||
|
||
import (
|
||
"amocrm/internal/service"
|
||
"amocrm/internal/tools"
|
||
"fmt"
|
||
"github.com/gofiber/fiber/v2"
|
||
"go.uber.org/zap"
|
||
"net/http"
|
||
"strconv"
|
||
)
|
||
|
||
// контроллер на который редиректятся ответы по авторизации в амо
|
||
func (c *WebhookController) WebhookCreate(ctx *fiber.Ctx) error {
|
||
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")
|
||
}
|
||
|
||
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 == "" || code == "" || referer == "" {
|
||
c.logger.Error("error required fields do not be nil", zap.Error(err))
|
||
return ctx.Status(fiber.StatusBadRequest).SendString("nil required fields")
|
||
}
|
||
|
||
req := service.ParamsWebhookCreate{
|
||
Code: code,
|
||
Referer: referer,
|
||
AccountID: accountID,
|
||
FromWidget: fromWidget,
|
||
Platform: platform,
|
||
}
|
||
|
||
err = c.service.WebhookCreate(ctx.Context(), req)
|
||
if err != nil {
|
||
c.logger.Error("error create webhook", zap.Error(err))
|
||
return ctx.Status(fiber.StatusInternalServerError).SendString(fmt.Sprintf("Internal Server Error: %v", err.Error()))
|
||
}
|
||
|
||
return ctx.Redirect(c.redirectURL)
|
||
}
|
||
|
||
// todo проверить надо
|
||
func (c *WebhookController) WebhookDelete(ctx *fiber.Ctx) error {
|
||
clientUUID := ctx.Query("client_uuid")
|
||
signature := ctx.Query("signature")
|
||
amoIDStr := ctx.Query("account_id")
|
||
|
||
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)
|
||
if err != nil {
|
||
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
||
}
|
||
|
||
return ctx.SendStatus(fiber.StatusOK)
|
||
}
|