Nexus/pkg/internal/auth/http.go

35 lines
659 B
Go
Raw 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 {
c.Locals("nex_auth_error", err)
}
return c.Next()
}
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()
}