2023-03-22 22:45:17 +08:00

24 lines
563 B
Go

package router
import (
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
)
var userClaimsKey = struct{}{}
func JWTRequired(c *fiber.Ctx) error {
jwt := c.Get("Authorization")
if jwt == "" {
return c.Status(401).JSON(fiber.Map{"msg": "Unauthorized"})
}
claims, err := casdoorsdk.ParseJwtToken(jwt)
if err != nil {
log.Ctx(c.UserContext()).Error().Err(err).Msg("Unauthorized user")
return c.Status(401).JSON(fiber.Map{"msg": "Unauthorized"})
}
c.Locals(userClaimsKey, claims)
return nil
}