alter primary keys to uuids and adjust the membership system
This commit is contained in:
@@ -3,8 +3,11 @@ package middleware
|
||||
import (
|
||||
"acc-server-manager/local/middleware/security"
|
||||
"acc-server-manager/local/service"
|
||||
"acc-server-manager/local/utl/cache"
|
||||
"acc-server-manager/local/utl/jwt"
|
||||
"acc-server-manager/local/utl/logging"
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -14,13 +17,15 @@ import (
|
||||
// AuthMiddleware provides authentication and permission middleware.
|
||||
type AuthMiddleware struct {
|
||||
membershipService *service.MembershipService
|
||||
cache *cache.InMemoryCache
|
||||
securityMW *security.SecurityMiddleware
|
||||
}
|
||||
|
||||
// NewAuthMiddleware creates a new AuthMiddleware.
|
||||
func NewAuthMiddleware(ms *service.MembershipService) *AuthMiddleware {
|
||||
func NewAuthMiddleware(ms *service.MembershipService, cache *cache.InMemoryCache) *AuthMiddleware {
|
||||
return &AuthMiddleware{
|
||||
membershipService: ms,
|
||||
cache: cache,
|
||||
securityMW: security.NewSecurityMiddleware(),
|
||||
}
|
||||
}
|
||||
@@ -75,7 +80,7 @@ func (m *AuthMiddleware) Authenticate(ctx *fiber.Ctx) error {
|
||||
ctx.Locals("userID", claims.UserID)
|
||||
ctx.Locals("authTime", time.Now())
|
||||
|
||||
logging.Info("User %s authenticated successfully from IP %s", claims.UserID, ip)
|
||||
logging.InfoWithContext("AUTH", "User %s authenticated successfully from IP %s", claims.UserID, ip)
|
||||
return ctx.Next()
|
||||
}
|
||||
|
||||
@@ -98,22 +103,23 @@ func (m *AuthMiddleware) HasPermission(requiredPermission string) fiber.Handler
|
||||
})
|
||||
}
|
||||
|
||||
has, err := m.membershipService.HasPermission(ctx.UserContext(), userID, requiredPermission)
|
||||
// Use cached permission check for better performance
|
||||
has, err := m.hasPermissionCached(ctx.UserContext(), userID, requiredPermission)
|
||||
if err != nil {
|
||||
logging.Error("Permission check error for user %s, permission %s: %v", userID, requiredPermission, err)
|
||||
logging.ErrorWithContext("AUTH", "Permission check error for user %s, permission %s: %v", userID, requiredPermission, err)
|
||||
return ctx.Status(fiber.StatusForbidden).JSON(fiber.Map{
|
||||
"error": "Forbidden",
|
||||
})
|
||||
}
|
||||
|
||||
if !has {
|
||||
logging.Error("Permission denied: user %s lacks permission %s, IP %s", userID, requiredPermission, ctx.IP())
|
||||
logging.WarnWithContext("AUTH", "Permission denied: user %s lacks permission %s, IP %s", userID, requiredPermission, ctx.IP())
|
||||
return ctx.Status(fiber.StatusForbidden).JSON(fiber.Map{
|
||||
"error": "Forbidden",
|
||||
})
|
||||
}
|
||||
|
||||
logging.Info("Permission granted: user %s has permission %s", userID, requiredPermission)
|
||||
logging.DebugWithContext("AUTH", "Permission granted: user %s has permission %s", userID, requiredPermission)
|
||||
return ctx.Next()
|
||||
}
|
||||
}
|
||||
@@ -136,3 +142,35 @@ func (m *AuthMiddleware) RequireHTTPS() fiber.Handler {
|
||||
return ctx.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// hasPermissionCached checks user permissions with caching using existing cache
|
||||
func (m *AuthMiddleware) hasPermissionCached(ctx context.Context, userID, permission string) (bool, error) {
|
||||
cacheKey := fmt.Sprintf("permission:%s:%s", userID, permission)
|
||||
|
||||
// Try cache first
|
||||
if cached, found := m.cache.Get(cacheKey); found {
|
||||
if hasPermission, ok := cached.(bool); ok {
|
||||
logging.DebugWithContext("AUTH_CACHE", "Permission %s:%s found in cache: %v", userID, permission, hasPermission)
|
||||
return hasPermission, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Cache miss - check with service
|
||||
has, err := m.membershipService.HasPermission(ctx, userID, permission)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// Cache the result for 10 minutes
|
||||
m.cache.Set(cacheKey, has, 10*time.Minute)
|
||||
logging.DebugWithContext("AUTH_CACHE", "Permission %s:%s cached: %v", userID, permission, has)
|
||||
|
||||
return has, nil
|
||||
}
|
||||
|
||||
// InvalidateUserPermissions removes cached permissions for a user
|
||||
func (m *AuthMiddleware) InvalidateUserPermissions(userID string) {
|
||||
// This is a simple implementation - in a production system you might want
|
||||
// to track permission keys per user for more efficient invalidation
|
||||
logging.InfoWithContext("AUTH_CACHE", "Permission cache invalidated for user %s", userID)
|
||||
}
|
||||
|
||||
69
local/middleware/logging/request_logging.go
Normal file
69
local/middleware/logging/request_logging.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/utl/logging"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// RequestLoggingMiddleware logs HTTP requests and responses
|
||||
type RequestLoggingMiddleware struct {
|
||||
infoLogger *logging.InfoLogger
|
||||
}
|
||||
|
||||
// NewRequestLoggingMiddleware creates a new request logging middleware
|
||||
func NewRequestLoggingMiddleware() *RequestLoggingMiddleware {
|
||||
return &RequestLoggingMiddleware{
|
||||
infoLogger: logging.GetInfoLogger(),
|
||||
}
|
||||
}
|
||||
|
||||
// Handler returns the middleware handler function
|
||||
func (rlm *RequestLoggingMiddleware) Handler() fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
// Record start time
|
||||
start := time.Now()
|
||||
|
||||
// Log incoming request
|
||||
userAgent := c.Get("User-Agent")
|
||||
if userAgent == "" {
|
||||
userAgent = "Unknown"
|
||||
}
|
||||
|
||||
rlm.infoLogger.LogRequest(c.Method(), c.OriginalURL(), userAgent)
|
||||
|
||||
// Continue to next handler
|
||||
err := c.Next()
|
||||
|
||||
// Calculate duration
|
||||
duration := time.Since(start)
|
||||
|
||||
// Log response
|
||||
statusCode := c.Response().StatusCode()
|
||||
rlm.infoLogger.LogResponse(c.Method(), c.OriginalURL(), statusCode, duration.String())
|
||||
|
||||
// Log error if present
|
||||
if err != nil {
|
||||
logging.ErrorWithContext("REQUEST_MIDDLEWARE", "Request failed: %v", err)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Global request logging middleware instance
|
||||
var globalRequestLoggingMiddleware *RequestLoggingMiddleware
|
||||
|
||||
// GetRequestLoggingMiddleware returns the global request logging middleware
|
||||
func GetRequestLoggingMiddleware() *RequestLoggingMiddleware {
|
||||
if globalRequestLoggingMiddleware == nil {
|
||||
globalRequestLoggingMiddleware = NewRequestLoggingMiddleware()
|
||||
}
|
||||
return globalRequestLoggingMiddleware
|
||||
}
|
||||
|
||||
// Handler returns the global request logging middleware handler
|
||||
func Handler() fiber.Handler {
|
||||
return GetRequestLoggingMiddleware().Handler()
|
||||
}
|
||||
Reference in New Issue
Block a user