2024-06-22 05:04:21 +00:00
|
|
|
package exts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
|
2024-10-30 15:26:13 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
2024-06-22 05:04:21 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func EnsureAuthenticated(c *fiber.Ctx) error {
|
2024-10-30 15:26:13 +00:00
|
|
|
if _, ok := c.Locals("user").(*sec.UserInfo); !ok {
|
2024-06-22 05:04:21 +00:00
|
|
|
return fiber.NewError(fiber.StatusUnauthorized)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func EnsureGrantedPerm(c *fiber.Ctx, key string, val any) error {
|
|
|
|
if err := EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
perms := c.Locals("permissions").(map[string]any)
|
|
|
|
if !services.HasPermNode(perms, key, val) {
|
|
|
|
return fiber.NewError(fiber.StatusForbidden, fmt.Sprintf("missing permission: %s", key))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|