- Added a reset of the models.Setting struct before querying for settings in both the Manager and Cerberus components to avoid ID leakage from previous queries. - Introduced new functions in Cerberus for checking admin authentication and admin whitelist status. - Enhanced middleware logic to allow admin users to bypass ACL checks if their IP is whitelisted. - Added tests to verify the behavior of the middleware with respect to ACLs and admin whitelisting. - Created a new utility for checking if an IP is in a CIDR list. - Updated various services to use `Where` clause for fetching records by ID instead of directly passing the ID to `First`, ensuring consistency in query patterns. - Added comprehensive tests for settings queries to demonstrate and verify the fix for ID leakage issues.
45 lines
856 B
Go
45 lines
856 B
Go
package middleware
|
|
|
|
import (
|
|
"github.com/Wikid82/charon/backend/internal/services"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// OptionalAuth applies best-effort authentication for downstream middleware without blocking requests.
|
|
func OptionalAuth(authService *services.AuthService) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if authService == nil {
|
|
c.Next()
|
|
return
|
|
}
|
|
|
|
if bypass, exists := c.Get("emergency_bypass"); exists {
|
|
if bypassActive, ok := bypass.(bool); ok && bypassActive {
|
|
c.Next()
|
|
return
|
|
}
|
|
}
|
|
|
|
if _, exists := c.Get("role"); exists {
|
|
c.Next()
|
|
return
|
|
}
|
|
|
|
tokenString, ok := extractAuthToken(c)
|
|
if !ok {
|
|
c.Next()
|
|
return
|
|
}
|
|
|
|
claims, err := authService.ValidateToken(tokenString)
|
|
if err != nil {
|
|
c.Next()
|
|
return
|
|
}
|
|
|
|
c.Set("userID", claims.UserID)
|
|
c.Set("role", claims.Role)
|
|
c.Next()
|
|
}
|
|
}
|