Skip to content

Commit

Permalink
Make use of CredentialAttributes #152
Browse files Browse the repository at this point in the history
  • Loading branch information
lorriborri committed Aug 12, 2024
1 parent b21d808 commit 3d520a1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 36 deletions.
5 changes: 0 additions & 5 deletions src/main/java/org/intellij/sdk/settings/AppSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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;
import com.intellij.util.ui.FormBuilder;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.Arrays;

/**
* Supports creating and managing a {@link JPanel} for the Settings Dialog.
Expand All @@ -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();
}
Expand All @@ -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) {
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand All @@ -43,48 +47,64 @@ 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);
}

@Override
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
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)
);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<applicationConfigurable parentId="tools" instance="org.intellij.sdk.settings.AppSettingsConfigurable"
id="org.intellij.sdk.settings.AppSettingsConfigurable"
displayName="SecHub Configuration"/>
displayName="SecHub"/>
</extensions>

<actions>
Expand Down

0 comments on commit 3d520a1

Please sign in to comment.