diff --git a/checkmarx-ast-eclipse-plugin-tests/src/test/java/checkmarx/ast/eclipse/plugin/tests/integration/AuthenticatorIntegrationTest.java b/checkmarx-ast-eclipse-plugin-tests/src/test/java/checkmarx/ast/eclipse/plugin/tests/integration/AuthenticatorIntegrationTest.java index 5be35e98..4e7d263c 100644 --- a/checkmarx-ast-eclipse-plugin-tests/src/test/java/checkmarx/ast/eclipse/plugin/tests/integration/AuthenticatorIntegrationTest.java +++ b/checkmarx-ast-eclipse-plugin-tests/src/test/java/checkmarx/ast/eclipse/plugin/tests/integration/AuthenticatorIntegrationTest.java @@ -18,7 +18,7 @@ public class AuthenticatorIntegrationTest extends BaseIntegrationTest { @Test public void testSuccessfulAuthentication() { - authenticator = new Authenticator(LoggerFactory.getLogger(Authenticator.class)); + authenticator = new Authenticator(); System.out.println("\n=== Starting Authentication Test ==="); System.out.println("Current directory: " + new File(".").getAbsolutePath()); System.out.println("API Key available: " + (VALID_API_KEY != null)); @@ -31,7 +31,7 @@ public void testSuccessfulAuthentication() { @Test public void testInvalidApiKeyAuthentication() { - authenticator = new Authenticator(LoggerFactory.getLogger(Authenticator.class)); + authenticator = new Authenticator(); System.out.println("\n=== Starting Invalid API Key Test ==="); String invalidApiKey = "invalid-api-key"; String result = authenticator.doAuthentication(invalidApiKey, ""); diff --git a/common-lib/src/com/checkmarx/eclipse/common/runner/Authenticator.java b/common-lib/src/com/checkmarx/eclipse/common/runner/Authenticator.java index d9a9f7b8..c1dcb21d 100644 --- a/common-lib/src/com/checkmarx/eclipse/common/runner/Authenticator.java +++ b/common-lib/src/com/checkmarx/eclipse/common/runner/Authenticator.java @@ -6,7 +6,7 @@ public class Authenticator { - private Authenticator() { + public Authenticator() { // Private constructor to prevent instantiation } diff --git a/devassist-lib/src/com/checkmarx/eclipse/devassist/backend/AuthenticationStateListener.java b/devassist-lib/src/com/checkmarx/eclipse/devassist/backend/AuthenticationStateListener.java new file mode 100644 index 00000000..244f071a --- /dev/null +++ b/devassist-lib/src/com/checkmarx/eclipse/devassist/backend/AuthenticationStateListener.java @@ -0,0 +1,73 @@ +package com.checkmarx.eclipse.devassist.backend; + +import org.eclipse.jface.util.IPropertyChangeListener; +import org.eclipse.jface.util.PropertyChangeEvent; + +import com.checkmarx.eclipse.common.preferences.Preferences; +import com.checkmarx.eclipse.common.listener.IWorkspaceScanService; +import com.checkmarx.eclipse.common.utils.CxLogger; +import com.checkmarx.eclipse.devassist.backend.ScannerRegistry.ScannerType; +import java.util.EnumSet; + +/** + * Listens for authentication state changes (CREDENTIALS_VALIDATED flag) and triggers + * workspace scan when user logs in. + * + * Problem it solves: + * - When user logs in, no preferences change, so ScannerPreferencesListener doesn't trigger scan + * - But we still need to scan projects that were opened before authentication + * + * Solution: + * - Listen to CREDENTIALS_VALIDATED changes + * - When it becomes true (login), trigger workspace scan immediately + * - ScannerPreferencesListener handles preference changes separately + */ +public class AuthenticationStateListener implements IPropertyChangeListener { + + private static final String LOG_TAG = "[AUTH-STATE-LISTENER]"; + + @Override + public void propertyChange(PropertyChangeEvent event) { + if (event == null || event.getProperty() == null) { + return; + } + + // Only respond to authentication state changes + if (!Preferences.CREDENTIALS_VALIDATED.equals(event.getProperty())) { + return; + } + + Object newValue = event.getNewValue(); + boolean nowAuthenticated = newValue instanceof Boolean && (Boolean) newValue; + + // Only trigger scan on login (true), not on logout (false) + if (nowAuthenticated) { + CxLogger.info(LOG_TAG + " User authenticated - clearing scan cache and triggering workspace scan..."); + + // CRITICAL: Clear scan state cache so files that were never scanned (before authentication) + // are not treated as "unchanged" and skipped. Without this, files show as "cached/unchanged" + // and the scan is skipped even though they were never actually scanned before. + try { + CxLogger.info(LOG_TAG + " Clearing scan state cache for all scanners..."); + EnumSet allScanners = EnumSet.allOf(ScannerType.class); + ScanStateCacheClearer.clearForScanners(allScanners); + CxLogger.info(LOG_TAG + " ✓ Scan cache cleared"); + } catch (Exception e) { + CxLogger.warning(LOG_TAG + " Error clearing scan cache: " + e.getMessage()); + } + + // Now trigger workspace scan with cleared cache + IWorkspaceScanService scanService = Preferences.getWorkspaceScanService(); + if (scanService != null) { + try { + scanService.scanWorkspace(); + CxLogger.info(LOG_TAG + " ✓ Workspace scan triggered on login"); + } catch (Exception e) { + CxLogger.error(LOG_TAG + " Error triggering workspace scan: " + e.getMessage(), e); + } + } else { + CxLogger.warning(LOG_TAG + " Workspace scan service not available"); + } + } + } +} diff --git a/devassist-lib/src/com/checkmarx/eclipse/devassist/configuration/McpInstallService.java b/devassist-lib/src/com/checkmarx/eclipse/devassist/configuration/McpInstallService.java index 3e45ca04..c0def2c4 100644 --- a/devassist-lib/src/com/checkmarx/eclipse/devassist/configuration/McpInstallService.java +++ b/devassist-lib/src/com/checkmarx/eclipse/devassist/configuration/McpInstallService.java @@ -40,6 +40,10 @@ private static void registerAuthenticationHandlers() { // Register listener for MCP auto-install on API key change Preferences.STORE.addPropertyChangeListener(new AuthenticationListener()); + // Register listener for workspace scan trigger on authentication state change + // (when user logs in, preferences may not change, but we still need to scan) + Preferences.STORE.addPropertyChangeListener(new com.checkmarx.eclipse.devassist.backend.AuthenticationStateListener()); + // Register handler for post-authentication UI (welcome dialog, workspace scan) Preferences.setAuthenticationSuccessHandler(new AuthenticationSuccessHandler()); diff --git a/devassist-lib/src/com/checkmarx/eclipse/devassist/utils/DevAssistUtils.java b/devassist-lib/src/com/checkmarx/eclipse/devassist/utils/DevAssistUtils.java index 08df5ee5..7b843f29 100644 --- a/devassist-lib/src/com/checkmarx/eclipse/devassist/utils/DevAssistUtils.java +++ b/devassist-lib/src/com/checkmarx/eclipse/devassist/utils/DevAssistUtils.java @@ -431,7 +431,14 @@ private static ITheme getActiveTheme() { * on the Display: approximate dark mode from the widget background luminance. */ private static boolean isDarkByBackgroundLuminance() { - Color background = Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); + Display display = Display.getCurrent(); + if (display == null) { + display = Display.getDefault(); + } + if (display == null) { + return false; + } + Color background = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); double luminance = (0.299 * background.getRed() + 0.587 * background.getGreen() + 0.114 * background.getBlue()) / 255.0; return luminance < 0.5;