2024-10-21 16:19:04 +00:00
|
|
|
package auth
|
|
|
|
|
2024-10-22 14:58:09 +00:00
|
|
|
import (
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
2024-10-21 16:19:04 +00:00
|
|
|
|
2024-10-22 15:23:58 +00:00
|
|
|
func ContextMiddleware(c *fiber.Ctx) error {
|
2024-10-21 16:19:04 +00:00
|
|
|
atk := tokenExtract(c)
|
2024-10-26 16:14:42 +00:00
|
|
|
if len(atk) == 0 {
|
|
|
|
return c.Next()
|
|
|
|
}
|
|
|
|
|
2024-10-22 14:58:09 +00:00
|
|
|
c.Locals("nex_in_token", atk)
|
2024-10-21 16:19:04 +00:00
|
|
|
|
|
|
|
if claims, err := tokenRead(atk); err == nil && claims != nil {
|
|
|
|
c.Locals("nex_principal", claims)
|
2024-10-22 14:58:09 +00:00
|
|
|
if err = userinfoFetch(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-21 16:19:04 +00:00
|
|
|
} else if err != nil {
|
|
|
|
c.Locals("nex_auth_error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Next()
|
|
|
|
}
|
|
|
|
|
2024-10-22 15:23:58 +00:00
|
|
|
func ValidatorMiddleware(c *fiber.Ctx) error {
|
2024-10-21 16:19:04 +00:00
|
|
|
if c.Locals("nex_principal") == nil {
|
|
|
|
err := c.Locals("nex_auth_error").(error)
|
|
|
|
return fiber.NewError(fiber.StatusUnauthorized, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Next()
|
|
|
|
}
|