diff --git a/src/main/java/org/intellij/sdk/settings/AppSettings.java b/src/main/java/org/intellij/sdk/settings/AppSettings.java index 505343e..a9e50e6 100644 --- a/src/main/java/org/intellij/sdk/settings/AppSettings.java +++ b/src/main/java/org/intellij/sdk/settings/AppSettings.java @@ -18,11 +18,6 @@ public static class State { @NonNls public String serverURL = ""; - @NonNls - public String userName = ""; - @NonNls - public String apiToken = ""; - } private State state = new State(); diff --git a/src/main/java/org/intellij/sdk/settings/AppSettingsComponent.java b/src/main/java/org/intellij/sdk/settings/AppSettingsComponent.java index 2975f0c..e7656b3 100644 --- a/src/main/java/org/intellij/sdk/settings/AppSettingsComponent.java +++ b/src/main/java/org/intellij/sdk/settings/AppSettingsComponent.java @@ -1,6 +1,5 @@ package org.intellij.sdk.settings; -import com.intellij.ui.components.JBCheckBox; import com.intellij.ui.components.JBLabel; import com.intellij.ui.components.JBPasswordField; import com.intellij.ui.components.JBTextField; @@ -8,6 +7,7 @@ import org.jetbrains.annotations.NotNull; import javax.swing.*; +import java.util.Arrays; /** * Supports creating and managing a {@link JPanel} for the Settings Dialog. @@ -17,13 +17,13 @@ public class AppSettingsComponent { private final JPanel mainPanel; private final JBTextField userNameText = new JBTextField(); private final JBTextField serverUrlText = new JBTextField(); - private final JBPasswordField apiTokenText = new JBPasswordField(); + private final JBPasswordField apiTokenPassword = new JBPasswordField(); public AppSettingsComponent() { mainPanel = FormBuilder.createFormBuilder() .addLabeledComponent(new JBLabel("Server URL:"), serverUrlText, 1, false) .addLabeledComponent(new JBLabel("User name:"), userNameText, 1, false) - .addLabeledComponent(new JBLabel("API token:"), apiTokenText, 1, false) + .addLabeledComponent(new JBLabel("API token:"), apiTokenPassword, 1, false) .addComponentFillVertically(new JPanel(), 0) .getPanel(); } @@ -47,8 +47,8 @@ public String getServerUrlText() { } @NotNull - public String getApiTokenText() { - return apiTokenText.getText(); + public String getApiTokenPassword() { + return String.valueOf(apiTokenPassword.getPassword()); } public void setUserNameText(@NotNull String newText) { @@ -59,8 +59,8 @@ public void setServerUrlText(@NotNull String newText) { serverUrlText.setText(newText); } - public void setApiTokenText(@NotNull String newText) { - apiTokenText.setText(newText); + public void setApiTokenPassword(@NotNull String newText) { + apiTokenPassword.setText(newText); } } diff --git a/src/main/java/org/intellij/sdk/settings/AppSettingsConfigurable.java b/src/main/java/org/intellij/sdk/settings/AppSettingsConfigurable.java index c650cb3..976c67a 100644 --- a/src/main/java/org/intellij/sdk/settings/AppSettingsConfigurable.java +++ b/src/main/java/org/intellij/sdk/settings/AppSettingsConfigurable.java @@ -1,9 +1,10 @@ package org.intellij.sdk.settings; +import com.intellij.credentialStore.CredentialAttributes; +import com.intellij.credentialStore.CredentialAttributesKt; +import com.intellij.credentialStore.Credentials; import com.intellij.ide.passwordSafe.PasswordSafe; -import com.intellij.ide.passwordSafe.PasswordSafeException; import com.intellij.openapi.options.Configurable; -import com.intellij.openapi.util.text.StringUtil; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.Nullable; @@ -17,13 +18,16 @@ final class AppSettingsConfigurable implements Configurable { private AppSettingsComponent appSettingsComponent; + private final String sechubCredentialsKey = "SECHUB_CREDENTIALS"; + + // A default constructor with no arguments is required because // this implementation is registered as an applicationConfigurable @Nls(capitalization = Nls.Capitalization.Title) @Override public String getDisplayName() { - return "SecHub Configuration"; + return "SecHub"; } @Override @@ -43,13 +47,16 @@ public boolean isModified() { AppSettings.State state = Objects.requireNonNull(AppSettings.getInstance().getState()); String currentPassword = ""; - try { - currentPassword = PasswordSafe.getInstance().getPassword(null, AppSettings.class, "PASSWORD_KEY"); - } catch (PasswordSafeException e) { - e.printStackTrace(); + String userName = ""; + + Credentials credentials = retrieveCredentials(); + if (credentials != null) { + currentPassword = credentials.getPasswordAsString(); + userName = credentials.getUserName(); } - return !appSettingsComponent.getUserNameText().equals(state.userName) || - appSettingsComponent.getApiTokenText().equals(StringUtil.notNullize(currentPassword)) || + + return !appSettingsComponent.getUserNameText().equals(userName) || + appSettingsComponent.getApiTokenPassword().equals(currentPassword) || appSettingsComponent.getServerUrlText().equals(state.serverURL); } @@ -57,29 +64,31 @@ public boolean isModified() { public void apply() { AppSettings.State state = Objects.requireNonNull(AppSettings.getInstance().getState()); - state.userName = appSettingsComponent.getUserNameText(); state.serverURL = appSettingsComponent.getServerUrlText(); - try { - PasswordSafe.getInstance().storePassword(null, AppSettings.class, "PASSWORD_KEY", appSettingsComponent.getApiTokenText()); - } catch (PasswordSafeException e) { - e.printStackTrace(); - } + + CredentialAttributes attributes = createCredentialAttributes(sechubCredentialsKey); + Credentials credentials = new Credentials(appSettingsComponent.getUserNameText(), appSettingsComponent.getApiTokenPassword()); + + PasswordSafe.getInstance().set(attributes, credentials); } @Override public void reset() { AppSettings.State state = Objects.requireNonNull(AppSettings.getInstance().getState()); - appSettingsComponent.setUserNameText(state.userName); appSettingsComponent.setServerUrlText(state.serverURL); - String currentPassword = ""; - try { - currentPassword = PasswordSafe.getInstance().getPassword(null, AppSettings.class, "PASSWORD_KEY"); - } catch (PasswordSafeException e) { - e.printStackTrace(); - } - appSettingsComponent.setApiTokenText(StringUtil.notNullize(currentPassword)); + Credentials credentials = retrieveCredentials(); + + if (credentials != null) { + String password = credentials.getPasswordAsString(); + String userName = credentials.getUserName(); + + assert userName != null; + appSettingsComponent.setUserNameText(userName); + assert password != null; + appSettingsComponent.setApiTokenPassword(password); + } } @Override @@ -87,4 +96,15 @@ public void disposeUIResources() { appSettingsComponent = null; } + private Credentials retrieveCredentials() { + CredentialAttributes attributes = createCredentialAttributes(sechubCredentialsKey); + PasswordSafe passwordSafe = PasswordSafe.getInstance(); + return passwordSafe.get(attributes); + } + + private CredentialAttributes createCredentialAttributes(String key) { + return new CredentialAttributes( + CredentialAttributesKt.generateServiceName("SecHub", key) + ); + } } \ No newline at end of file diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 90ec517..4e5da5b 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -22,7 +22,7 @@ + displayName="SecHub"/>