-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from cortiz/jks-import/export
Jks import/export
- Loading branch information
Showing
10 changed files
with
396 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/com/jmpeax/ssltoolbox/jks/actions/DeletetCert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/com/jmpeax/ssltoolbox/jks/actions/ExportCert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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.fileChooser.FileChooserFactory; | ||
import com.intellij.openapi.fileChooser.FileSaverDescriptor; | ||
import com.intellij.openapi.ui.Messages; | ||
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) { | ||
|
||
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 descriptor = new FileSaverDescriptor("Export Certificate", "Export certificate", "cer"); | ||
|
||
var pwd = Messages.showPasswordDialog("Keystore password", "KeyStore Password"); | ||
if (pwd != null && !pwd.isBlank()) { | ||
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"); | ||
} | ||
} | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/com/jmpeax/ssltoolbox/jks/actions/ListenerDataContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.jmpeax.ssltoolbox.jks.actions; | ||
|
||
import com.intellij.openapi.actionSystem.DataContext; | ||
import com.intellij.openapi.actionSystem.DataKey; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.security.cert.X509Certificate; | ||
|
||
public class ListenerDataContext implements DataContext { | ||
private final VirtualFile file; | ||
private final OnImport onImport; | ||
|
||
public ListenerDataContext(VirtualFile file,OnImport onImport) { | ||
this.file = file; | ||
this.onImport = onImport; | ||
} | ||
|
||
@Override | ||
public @Nullable Object getData(@NotNull String dataId) { | ||
return file; | ||
} | ||
|
||
@Override | ||
public <T> @Nullable T getData(@NotNull DataKey<T> key) { | ||
return DataContext.super.getData(key); | ||
} | ||
|
||
public void update(String alias, X509Certificate certificate){ | ||
onImport.imported(alias,certificate); | ||
} | ||
|
||
interface OnImport { | ||
void imported(String alias, X509Certificate cert); | ||
} | ||
} |
Oops, something went wrong.