mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
Reapply "Merge branch 'canary' into kucherenko/canary"
This reverts commit e6cb6454db.
This commit is contained in:
39
apps/monitoring/middleware/auth.go
Normal file
39
apps/monitoring/middleware/auth.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/mauriciogm/dokploy/apps/monitoring/config"
|
||||
)
|
||||
|
||||
func AuthMiddleware() fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
expectedToken := config.GetMetricsConfig().Server.Token
|
||||
|
||||
authHeader := c.Get("Authorization")
|
||||
if authHeader == "" {
|
||||
return c.Status(401).JSON(fiber.Map{
|
||||
"error": "Authorization header is required",
|
||||
})
|
||||
}
|
||||
|
||||
// Check if the header starts with "Bearer "
|
||||
if !strings.HasPrefix(authHeader, "Bearer ") {
|
||||
return c.Status(401).JSON(fiber.Map{
|
||||
"error": "Invalid authorization format. Use 'Bearer TOKEN'",
|
||||
})
|
||||
}
|
||||
|
||||
// Extract the token
|
||||
token := strings.TrimPrefix(authHeader, "Bearer ")
|
||||
|
||||
if token != expectedToken {
|
||||
return c.Status(401).JSON(fiber.Map{
|
||||
"error": "Invalid token",
|
||||
})
|
||||
}
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user