24 lines
494 B
Go
24 lines
494 B
Go
package router
|
|
|
|
import (
|
|
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
var userKey = struct{}{}
|
|
|
|
func JWTRequired(c *fiber.Ctx) error {
|
|
jwt := c.Get("Authorization")
|
|
if jwt == "" {
|
|
return fiber.ErrUnauthorized
|
|
}
|
|
claims, err := casdoorsdk.ParseJwtToken(jwt)
|
|
if err != nil {
|
|
log.Ctx(c.UserContext()).Error().Err(err).Msg("Unauthorized user")
|
|
return fiber.ErrUnauthorized
|
|
}
|
|
c.Locals(userKey, claims.User)
|
|
return nil
|
|
}
|