init: Initial commit

This commit is contained in:
Björn Benouarets
2026-01-21 06:36:38 +01:00
commit 346100feb6
27 changed files with 1126 additions and 0 deletions

38
app/middlewares/auth.go Normal file
View File

@@ -0,0 +1,38 @@
package middlewares
import (
"slices"
"strings"
"git.secnex.io/secnex/masterlog"
"git.secnex.io/secnex/oauth2-api/config"
"github.com/gofiber/fiber/v2"
)
func AuthMiddleware() fiber.Handler {
return func(c *fiber.Ctx) error {
if slices.Contains(config.CONFIG.UnprotectedEndpoints, c.Path()) {
masterlog.Debug("Unprotected endpoint", map[string]interface{}{"path": c.Path()})
return c.Next()
}
token := c.Get("Authorization")
if token == "" {
masterlog.Debug("No token provided", map[string]interface{}{"path": c.Path(), "authorization": c.Get("Authorization")})
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Unauthorized"})
}
tokenParts := strings.Split(token, " ")
if len(tokenParts) != 2 {
masterlog.Debug("Invalid token parts", map[string]interface{}{"token_parts": tokenParts})
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Unauthorized"})
}
tokenPartType, _ := tokenParts[0], tokenParts[1]
if tokenPartType != "Bearer" {
masterlog.Debug("Invalid token type", map[string]interface{}{"token_type": tokenPartType})
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Unauthorized"})
}
return c.Next()
}
}

49
app/middlewares/log.go Normal file
View File

@@ -0,0 +1,49 @@
package middlewares
import (
"time"
"git.secnex.io/secnex/masterlog"
"github.com/gofiber/fiber/v2"
)
// RequestLogger logs incoming HTTP requests via masterlog.
func RequestLogger() fiber.Handler {
return func(c *fiber.Ctx) error {
start := time.Now()
err := c.Next()
duration := time.Since(start)
entry := map[string]interface{}{
"method": c.Method(),
"path": c.OriginalURL(),
"ip": c.IP(),
"duration": duration.String(),
"status": c.Response().StatusCode(),
"user_agent": c.Get("User-Agent"),
}
if err != nil {
entry["error"] = err.Error()
masterlog.Error(
"HTTP request failed",
entry,
)
return err
}
if status := c.Response().StatusCode(); status >= fiber.StatusInternalServerError {
masterlog.Error(
"HTTP request failed",
entry,
)
} else {
masterlog.Info(
"HTTP request successful",
entry,
)
}
return nil
}
}