feature: added X-API-KEY

main
Nilo Roberto C Paim 2023-11-06 08:14:50 -03:00
parent 62d876a17f
commit d4d05a498a
1 changed files with 14 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"api/config"
"api/models"
"api/services"
"encoding/base64"
"strings"
"github.com/gofiber/fiber/v2"
@ -14,6 +15,19 @@ var usr models.User
func Authenticate(c *fiber.Ctx) error {
apik, _ := base64.StdEncoding.DecodeString(c.Get("X-API-KEY"))
apikdata := strings.Split(string(apik), ":")
if len(apikdata) == 2 {
usr = services.GetUser(apikdata[0])
if usr.ID == 0 {
return fiber.NewError(fiber.StatusUnauthorized, "Unauthorized: Inexistent user")
}
c.Locals("user", usr)
return c.Next()
}
tk := c.Get("Authorization")
if tk == "" {