feature: added X-API-KEY
parent
62d876a17f
commit
d4d05a498a
|
|
@ -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 == "" {
|
||||
|
|
|
|||
Loading…
Reference in New Issue