Nexus/pkg/internal/auth/http.go

34 lines
647 B
Go
Raw Permalink Normal View History

2024-10-21 16:19:04 +00:00
package auth
import (
"github.com/gofiber/fiber/v2"
)
2024-10-21 16:19:04 +00:00
func ContextMiddleware(c *fiber.Ctx) error {
2024-10-21 16:19:04 +00:00
atk := tokenExtract(c)
if len(atk) == 0 {
return c.Next()
}
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)
if err = userinfoFetch(c); err != nil {
return err
}
2024-10-21 16:19:04 +00:00
} else if err != nil {
2024-10-30 15:26:39 +00:00
return fiber.NewError(fiber.StatusUnauthorized, err.Error())
2024-10-21 16:19:04 +00:00
}
return c.Next()
}
func ValidatorMiddleware(c *fiber.Ctx) error {
2024-10-21 16:19:04 +00:00
if c.Locals("nex_principal") == nil {
2024-10-30 15:26:39 +00:00
return fiber.NewError(fiber.StatusUnauthorized, "unauthorized")
2024-10-21 16:19:04 +00:00
}
return c.Next()
}