diff --git a/src/main/java/com/jmpeax/ssltoolbox/jks/JKSView.java b/src/main/java/com/jmpeax/ssltoolbox/jks/JKSView.java index 1dcf5c5..5ee6215 100644 --- a/src/main/java/com/jmpeax/ssltoolbox/jks/JKSView.java +++ b/src/main/java/com/jmpeax/ssltoolbox/jks/JKSView.java @@ -104,7 +104,7 @@ private JPanel createUnlockButton() { this.passwordField = new JBPasswordField(); panel.setBorder(JBUI.Borders.customLineBottom(JBUI.CurrentTheme.Toolbar.SEPARATOR_COLOR)); JButton unlockButton = getButton(passwordField, panel); - JLabel passwordLabel = new JBLabel("Enter password: "); + JBLabel passwordLabel = new JBLabel("Enter password: "); passwordField.requestFocusInWindow(); // Set focus traversal keys for the password field to move to the unlock button @@ -211,6 +211,7 @@ public void addCertificate(String alias, X509Certificate certificate) { public String getSelectedCertificate() { return list.getSelectedValue(); } + public void removeCertificate(String alias) { listModel.remove(listModel.indexOf(alias)); certs.remove(alias); diff --git a/src/main/java/com/jmpeax/ssltoolbox/jks/actions/DeletetCert.java b/src/main/java/com/jmpeax/ssltoolbox/jks/actions/DeletetCert.java new file mode 100644 index 0000000..c005777 --- /dev/null +++ b/src/main/java/com/jmpeax/ssltoolbox/jks/actions/DeletetCert.java @@ -0,0 +1,47 @@ +package com.jmpeax.ssltoolbox.jks.actions; + + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.actionSystem.PlatformDataKeys; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.ui.Messages; +import com.jmpeax.ssltoolbox.jks.JKSView; +import com.jmpeax.ssltoolbox.svc.CertificateHelper; +import org.jetbrains.annotations.NotNull; + +public class DeletetCert extends AnAction { + + @Override + public void actionPerformed(@NotNull AnActionEvent e) { + + var certificateHelper = ApplicationManager.getApplication().getService(CertificateHelper.class); + var ksVirtualFile = e.getData(CommonDataKeys.VIRTUAL_FILE); + var view = getViewComponent(e); + if (view == null) { + Messages.showErrorDialog("No JKS view found", "No JKS View"); + return; + } + var selectedAlias = view.getSelectedCertificate(); + if (selectedAlias == null) { + Messages.showErrorDialog("No certificate selected", "No Certificate Selected"); + return; + } + var pwd = Messages.showPasswordDialog("Keystore password", "KeyStore Password"); + if (pwd != null && !pwd.isBlank()) { + certificateHelper.removeCertificate(ksVirtualFile, selectedAlias,pwd.toCharArray()); + view.removeCertificate(selectedAlias); + Messages.showInfoMessage("Certificate removed", "Certificate Removed"); + } + } + + + + private JKSView getViewComponent(@NotNull AnActionEvent e) { + // Retrieve your view component from context, e.g., using the data context from the event + return e.getData(PlatformDataKeys.CONTEXT_COMPONENT) instanceof JKSView + ? (JKSView) e.getData(PlatformDataKeys.CONTEXT_COMPONENT) + : null; + } +} diff --git a/src/main/java/com/jmpeax/ssltoolbox/jks/actions/ExportCert.java b/src/main/java/com/jmpeax/ssltoolbox/jks/actions/ExportCert.java index ffc95e6..8a4a4b6 100644 --- a/src/main/java/com/jmpeax/ssltoolbox/jks/actions/ExportCert.java +++ b/src/main/java/com/jmpeax/ssltoolbox/jks/actions/ExportCert.java @@ -6,19 +6,23 @@ import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.fileChooser.FileChooser; -import com.intellij.openapi.fileChooser.FileChooserDescriptor; +import com.intellij.openapi.fileChooser.FileChooserFactory; +import com.intellij.openapi.fileChooser.FileSaverDescriptor; import com.intellij.openapi.ui.Messages; -import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.VirtualFileWrapper; import com.jmpeax.ssltoolbox.jks.JKSView; import com.jmpeax.ssltoolbox.svc.CertificateHelper; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Objects; public class ExportCert extends AnAction { + private static final Logger LOGGER = LoggerFactory.getLogger(ExportCert.class); @Override public void actionPerformed(@NotNull AnActionEvent e) { @@ -28,24 +32,37 @@ public void actionPerformed(@NotNull AnActionEvent e) { var view = getViewComponent(e); if (view == null) { Messages.showErrorDialog("No JKS view found", "No JKS View"); + return; } var selectedAlias = view.getSelectedCertificate(); if (selectedAlias == null) { Messages.showErrorDialog("No certificate selected", "No Certificate Selected"); + return; } - var descriptor = new FileChooserDescriptor( - false, // Choose Files - true, - false, - false, - false, - false - ); + var descriptor = new FileSaverDescriptor("Export Certificate", "Export certificate", "cer"); + var pwd = Messages.showPasswordDialog("Keystore password", "KeyStore Password"); if (pwd != null && !pwd.isBlank()) { - certificateHelper.exportCertificate(ksVirtualFile, selectedAlias, pwd.toCharArray()); - Messages.showInfoMessage("Certificate exported", "Certificate Exported"); - view.removeCertificate(selectedAlias); + var f = FileChooserFactory.getInstance().createSaveFileDialog(descriptor, view).save(selectedAlias + ".cer"); + var cert = certificateHelper.exportCertificateToByte(ksVirtualFile, selectedAlias, pwd.toCharArray()); + if (f != null) { + ApplicationManager.getApplication().runWriteAction(() -> writeFile(f, cert)); + Messages.showInfoMessage("Certificate exported", "Certificate Exported"); + } else { + Messages.showErrorDialog("No file selected", "No File Selected"); + } + } + } + + private void writeFile(VirtualFileWrapper f, ByteArrayOutputStream cert) { + try { + var file = f.getVirtualFile(true); + if (file != null) { + file.setBinaryContent(cert.toByteArray()); + } + } catch (IOException e) { + LOGGER.error("Error writing file", e); + Messages.showErrorDialog("Error writing file", "Error Writing File"); } } diff --git a/src/main/java/com/jmpeax/ssltoolbox/pem/PemView.java b/src/main/java/com/jmpeax/ssltoolbox/pem/PemView.java index 749cb7f..78b704c 100644 --- a/src/main/java/com/jmpeax/ssltoolbox/pem/PemView.java +++ b/src/main/java/com/jmpeax/ssltoolbox/pem/PemView.java @@ -1,6 +1,7 @@ package com.jmpeax.ssltoolbox.pem; import com.intellij.ui.components.JBPanel; +import com.intellij.ui.components.JBTextField; import com.intellij.util.ui.JBUI; import com.jmpeax.ssltoolbox.utils.Messages; import org.jetbrains.annotations.NotNull; @@ -35,7 +36,7 @@ * This method is used to create the text fields displaying the certificate details in the panel. *
* The `PemView` class does not provide any public methods or properties other than the constructor.*/
-public class PemView extends JBPanel {
+public class PemView extends JBPanel