2025-12-22 21:56:37 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-29 18:23:23 +00:00
|
|
|
"context"
|
2025-12-22 21:56:37 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
|
|
"veza-backend-api/internal/middleware"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// CSRFHandler gère les handlers pour la protection CSRF
|
|
|
|
|
type CSRFHandler struct {
|
2025-12-29 18:23:23 +00:00
|
|
|
csrfMiddleware CSRFMiddlewareInterface
|
2025-12-22 21:56:37 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-29 18:23:23 +00:00
|
|
|
// CSRFMiddlewareInterface defines methods needed for CSRF handler
|
|
|
|
|
type CSRFMiddlewareInterface interface {
|
|
|
|
|
GetToken(ctx context.Context, userID uuid.UUID) (string, error)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-22 21:56:37 +00:00
|
|
|
// NewCSRFHandler crée un nouveau handler CSRF
|
|
|
|
|
func NewCSRFHandler(csrfMiddleware *middleware.CSRFMiddleware, logger *zap.Logger) *CSRFHandler {
|
|
|
|
|
return &CSRFHandler{
|
|
|
|
|
csrfMiddleware: csrfMiddleware,
|
|
|
|
|
logger: logger,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-29 18:23:23 +00:00
|
|
|
// NewCSRFHandlerWithInterface creates a new CSRF handler with interface (for testing)
|
|
|
|
|
func NewCSRFHandlerWithInterface(csrfMiddleware CSRFMiddlewareInterface, logger *zap.Logger) *CSRFHandler {
|
|
|
|
|
return &CSRFHandler{
|
|
|
|
|
csrfMiddleware: csrfMiddleware,
|
feat: Visual masterpiece - true light mode & premium UI
🎨 **True Light/Dark Mode**
- Implemented proper light mode with inverted color scheme
- Smooth theme transitions (0.3s ease)
- Light mode colors: white backgrounds, dark text, vibrant accents
- System theme detection with proper class application
🌈 **Enhanced Theme System**
- 4 color themes work in both light and dark modes
- Cyber (cyan/magenta), Ocean (blue/teal), Forest (green/lime), Sunset (orange/purple)
- Theme-specific glassmorphism effects
- Proper contrast in light mode
✨ **Premium Animations**
- Float, glow-pulse, slide-in, scale-in, rotate-in animations
- Smooth page transitions
- Hover effects with depth (lift, glow, scale)
- Micro-interactions on all interactive elements
🎯 **Visual Polish**
- Enhanced glassmorphism for light/dark modes
- Custom scrollbar with theme colors
- Beautiful text selection
- Focus indicators for accessibility
- Premium utility classes
🔧 **Technical Improvements**
- Updated UIStore to properly apply light/dark classes
- Added data-theme attribute for CSS targeting
- Smooth scroll behavior
- Optimized transitions
The app is now a visual masterpiece with perfect light/dark mode support!
2026-01-11 01:32:21 +00:00
|
|
|
logger: logger,
|
2025-12-29 18:23:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-22 21:56:37 +00:00
|
|
|
// GetCSRFToken retourne un token CSRF pour l'utilisateur authentifié
|
|
|
|
|
// GET /api/v1/csrf-token
|
|
|
|
|
func (h *CSRFHandler) GetCSRFToken() gin.HandlerFunc {
|
|
|
|
|
return func(c *gin.Context) {
|
feat: Visual masterpiece - true light mode & premium UI
🎨 **True Light/Dark Mode**
- Implemented proper light mode with inverted color scheme
- Smooth theme transitions (0.3s ease)
- Light mode colors: white backgrounds, dark text, vibrant accents
- System theme detection with proper class application
🌈 **Enhanced Theme System**
- 4 color themes work in both light and dark modes
- Cyber (cyan/magenta), Ocean (blue/teal), Forest (green/lime), Sunset (orange/purple)
- Theme-specific glassmorphism effects
- Proper contrast in light mode
✨ **Premium Animations**
- Float, glow-pulse, slide-in, scale-in, rotate-in animations
- Smooth page transitions
- Hover effects with depth (lift, glow, scale)
- Micro-interactions on all interactive elements
🎯 **Visual Polish**
- Enhanced glassmorphism for light/dark modes
- Custom scrollbar with theme colors
- Beautiful text selection
- Focus indicators for accessibility
- Premium utility classes
🔧 **Technical Improvements**
- Updated UIStore to properly apply light/dark classes
- Added data-theme attribute for CSS targeting
- Smooth scroll behavior
- Optimized transitions
The app is now a visual masterpiece with perfect light/dark mode support!
2026-01-11 01:32:21 +00:00
|
|
|
// Récupérer le userID depuis le contexte (défini par AuthMiddleware.OptionalAuth)
|
2025-12-22 21:56:37 +00:00
|
|
|
userIDInterface, exists := c.Get("user_id")
|
|
|
|
|
if !exists {
|
2026-01-15 18:26:53 +00:00
|
|
|
// Si pas d'utilisateur authentifié, on retourne un token public/anonyme
|
|
|
|
|
// Le middleware CSRF côté serveur ignore la validation si user_id est absent du contexte
|
|
|
|
|
// Action 1.3.2.1: Use wrapped format helper
|
|
|
|
|
RespondSuccess(c, http.StatusOK, gin.H{
|
|
|
|
|
"csrf_token": "public-anonymous-token",
|
|
|
|
|
})
|
2025-12-22 21:56:37 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userID, ok := userIDInterface.(uuid.UUID)
|
|
|
|
|
if !ok {
|
|
|
|
|
h.logger.Error("Invalid user_id type in context")
|
2026-01-15 16:32:02 +00:00
|
|
|
// Action 1.3.2.1: Use wrapped format helper for errors
|
|
|
|
|
RespondWithError(c, 500, "Internal server error")
|
2025-12-22 21:56:37 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Générer ou récupérer le token CSRF
|
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
token, err := h.csrfMiddleware.GetToken(ctx, userID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.logger.Error("Failed to get CSRF token",
|
|
|
|
|
zap.Error(err),
|
|
|
|
|
zap.String("user_id", userID.String()),
|
|
|
|
|
)
|
2026-01-15 16:32:02 +00:00
|
|
|
// Action 1.3.2.1: Use wrapped format helper for errors
|
|
|
|
|
RespondWithError(c, 500, "Failed to generate CSRF token")
|
2025-12-22 21:56:37 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Retourner le token
|
2026-01-15 16:32:02 +00:00
|
|
|
// Action 1.3.2.1: Use wrapped format helper
|
|
|
|
|
RespondSuccess(c, http.StatusOK, gin.H{
|
|
|
|
|
"csrf_token": token,
|
2025-12-22 21:56:37 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|