117 lines
2.7 KiB
Go
117 lines
2.7 KiB
Go
package middleware
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/pahmiudahgede/senggoldong/utils"
|
|
)
|
|
|
|
func RoleRequired(roles ...string) fiber.Handler {
|
|
return func(c *fiber.Ctx) error {
|
|
tokenString := c.Get("Authorization")
|
|
if tokenString == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(utils.FormatResponse(
|
|
fiber.StatusUnauthorized,
|
|
"Token is required",
|
|
nil,
|
|
))
|
|
}
|
|
|
|
tokenString = strings.TrimPrefix(tokenString, "Bearer ")
|
|
|
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
|
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
|
return nil, errors.New("unexpected signing method")
|
|
}
|
|
return []byte(os.Getenv("API_KEY")), nil
|
|
})
|
|
if err != nil || !token.Valid {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(utils.FormatResponse(
|
|
fiber.StatusUnauthorized,
|
|
"Invalid or expired token",
|
|
nil,
|
|
))
|
|
}
|
|
|
|
claims, ok := token.Claims.(jwt.MapClaims)
|
|
if !ok {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(utils.FormatResponse(
|
|
fiber.StatusUnauthorized,
|
|
"Invalid token claims",
|
|
nil,
|
|
))
|
|
}
|
|
|
|
userID, ok := claims["sub"].(string)
|
|
if !ok || userID == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(utils.FormatResponse(
|
|
fiber.StatusUnauthorized,
|
|
"Invalid or missing user ID in token",
|
|
nil,
|
|
))
|
|
}
|
|
|
|
role, ok := claims["role"].(string)
|
|
if !ok || role == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(utils.FormatResponse(
|
|
fiber.StatusUnauthorized,
|
|
"Invalid or missing role in token",
|
|
nil,
|
|
))
|
|
}
|
|
|
|
c.Locals("userID", userID)
|
|
c.Locals("role", role)
|
|
|
|
for _, r := range roles {
|
|
if r == role {
|
|
return c.Next()
|
|
}
|
|
}
|
|
|
|
return c.Status(fiber.StatusForbidden).JSON(utils.FormatResponse(
|
|
fiber.StatusForbidden,
|
|
"You do not have permission to access this resource",
|
|
nil,
|
|
))
|
|
}
|
|
}
|
|
|
|
func AuthMiddleware(c *fiber.Ctx) error {
|
|
tokenString := c.Get("Authorization")
|
|
tokenString = strings.TrimPrefix(tokenString, "Bearer ")
|
|
|
|
if tokenString == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"message": "Missing or invalid token",
|
|
})
|
|
}
|
|
|
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(os.Getenv("API_KEY")), nil
|
|
})
|
|
|
|
if err != nil || !token.Valid {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"message": "Invalid or expired token",
|
|
})
|
|
}
|
|
|
|
claims, ok := token.Claims.(jwt.MapClaims)
|
|
if !ok {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"message": "Invalid token claims",
|
|
})
|
|
}
|
|
|
|
userID := claims["sub"].(string)
|
|
|
|
c.Locals("userID", userID)
|
|
|
|
return c.Next()
|
|
}
|