40 lines
852 B
Go
40 lines
852 B
Go
package middleware
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/BlackChaosNL/Orchestra/config"
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/kataras/golog"
|
|
|
|
jwtware "github.com/gofiber/contrib/v3/jwt"
|
|
)
|
|
|
|
func JWTProtected() fiber.Handler {
|
|
secret := config.GetStrFromEnv("ORCHESTRA_API_SECRET_KEY", "")
|
|
|
|
if secret == "" {
|
|
golog.Fatal("")
|
|
}
|
|
|
|
return jwtware.New(jwtware.Config{
|
|
SigningKey: jwtware.SigningKey{Key: []byte(secret)},
|
|
ErrorHandler: func(c fiber.Ctx, err error) error {
|
|
status := fiber.StatusUnauthorized
|
|
message := "Invalid or expired JWT"
|
|
|
|
if strings.Contains(strings.ToLower(err.Error()), "missing or malformed jwt") {
|
|
status = fiber.StatusBadRequest
|
|
message = "Missing or malformed JWT"
|
|
}
|
|
|
|
return c.Status(status).JSON(fiber.Map{
|
|
"status": "error",
|
|
"message": message,
|
|
"data": nil,
|
|
})
|
|
},
|
|
})
|
|
|
|
}
|