Pulse/internal/api/auth.go
Pulse Monitor decce4f39f feat: implement secure registration token system with fixes
- Add registration token system for secure node auto-registration
- Implement token generation, validation, and revocation APIs
- Add frontend UI for managing registration tokens
- Fix polling interval hot-reload to work without restart
- Fix environment variable persistence for system settings
- Optimize monitor reload to avoid 'no nodes configured' message
- Fix goroutine leak in token manager cleanup
- Fix context propagation in reload logic
- Fix AUTO_UPDATE_ENABLED persistence bug
- Add proper error handling and security validation
- Ensure all resources properly cleaned up with defer statements
2025-08-09 11:54:26 +00:00

32 lines
No EOL
771 B
Go

package api
import (
"net/http"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
"github.com/rs/zerolog/log"
)
// RequireAuth middleware checks for API token authentication
func RequireAuth(cfg *config.Config, handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// If no API token is configured, allow access
if cfg.APIToken == "" {
handler(w, r)
return
}
// Check for API token in header
apiToken := r.Header.Get("X-API-Token")
if apiToken == "" || apiToken != cfg.APIToken {
log.Warn().
Str("ip", r.RemoteAddr).
Str("path", r.URL.Path).
Msg("Unauthorized API access attempt")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
handler(w, r)
}
}