Integrates the external API into the existing /api group as v2 with API key auth (ADMIN_API_KEY), replacing the separate /hermes namespace. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
33 lines
596 B
Go
33 lines
596 B
Go
package middlewares
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func AdminApiAuth() fiber.Handler {
|
|
return func(c *fiber.Ctx) error {
|
|
apiKey := os.Getenv("ADMIN_API_KEY")
|
|
if apiKey == "" {
|
|
return c.Status(503).JSON(fiber.Map{"error": "ADMIN_API_KEY not configured"})
|
|
}
|
|
|
|
token := ""
|
|
auth := c.Get("Authorization")
|
|
if strings.HasPrefix(auth, "Bearer ") {
|
|
token = auth[7:]
|
|
}
|
|
if token == "" {
|
|
token = c.Get("X-API-Key")
|
|
}
|
|
|
|
if token == "" || token != apiKey {
|
|
return c.Status(401).JSON(fiber.Map{"error": "unauthorized"})
|
|
}
|
|
|
|
return c.Next()
|
|
}
|
|
}
|