2024-05-03 17:11:56 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2024-05-04 12:52:22 +00:00
|
|
|
"github.com/ditatompel/xmr-nodes/internal/database"
|
|
|
|
"github.com/ditatompel/xmr-nodes/internal/repo"
|
|
|
|
|
2024-05-03 17:11:56 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CookieProtected(c *fiber.Ctx) error {
|
|
|
|
cookie := c.Cookies("xmr-nodes-ui")
|
|
|
|
if cookie == "" {
|
|
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
|
|
"status": "error",
|
|
|
|
"message": "Unauthorized",
|
|
|
|
"data": nil,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Next()
|
|
|
|
}
|
2024-05-04 12:52:22 +00:00
|
|
|
|
|
|
|
func CheckProber(c *fiber.Ctx) error {
|
|
|
|
key := c.Get("X-Prober-Api-Key")
|
|
|
|
if key == "" {
|
|
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
|
|
"status": "error",
|
|
|
|
"message": "Unauthorized",
|
|
|
|
"data": nil,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
proberRepo := repo.NewProberRepo(database.GetDB())
|
|
|
|
|
|
|
|
prober, err := proberRepo.CheckApi(key)
|
|
|
|
if err != nil {
|
|
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
|
|
"status": "error",
|
|
|
|
"message": "No API key match",
|
|
|
|
"data": nil,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-04 18:42:47 +00:00
|
|
|
c.Locals("prober_id", prober.Id)
|
2024-05-04 12:52:22 +00:00
|
|
|
return c.Next()
|
|
|
|
}
|