Nexus/pkg/internal/auth/http.go

38 lines
755 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 {
2024-10-26 16:20:44 +00:00
if err, ok := c.Locals("nex_auth_error").(error); ok {
return fiber.NewError(fiber.StatusUnauthorized, err.Error())
} else {
return fiber.NewError(fiber.StatusUnauthorized, "unauthorized")
}
2024-10-21 16:19:04 +00:00
}
return c.Next()
}