package auth_middleware import ( "net/http" "agentdeck-whatsapp-service/pkg/config" instance_service "agentdeck-whatsapp-service/pkg/instance/service" "github.com/gin-gonic/gin" ) type Middleware interface { Auth(ctx *gin.Context) AuthAdmin(ctx *gin.Context) } type middleware struct { config *config.Config instanceService instance_service.InstanceService } func (m middleware) Auth(ctx *gin.Context) { token := ctx.GetHeader("apikey") if token == "" { ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "not authorized"}) return } instance, err := m.instanceService.GetInstanceByToken(token) if err != nil { ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "not authorized"}) return } ctx.Set("instance", instance) ctx.Next() } func (m middleware) AuthAdmin(ctx *gin.Context) { token := ctx.GetHeader("apikey") if token == "" { ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "not authorized"}) return } if token != m.config.GlobalApiKey { ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "not authorized"}) return } ctx.Next() } func NewMiddleware(config *config.Config, instanceService instance_service.InstanceService) *middleware { return &middleware{config: config, instanceService: instanceService} }