diff --git a/src/main/java/com/blackberry/jwteditor/presenter/EditorPresenter.java b/src/main/java/com/blackberry/jwteditor/presenter/EditorPresenter.java index 778e086..ebf2808 100644 --- a/src/main/java/com/blackberry/jwteditor/presenter/EditorPresenter.java +++ b/src/main/java/com/blackberry/jwteditor/presenter/EditorPresenter.java @@ -58,6 +58,7 @@ public class EditorPresenter { private final Logging logging; private final MessageDialogFactory messageDialogFactory; private final EditorModel model; + private final LastSigningKeys lastSigningKeys; private boolean selectionChanging; @@ -72,6 +73,7 @@ public EditorPresenter( this.keysRepository = keysRepository; this.model = new EditorModel(); this.messageDialogFactory = new MessageDialogFactory(view.uiComponent()); + this.lastSigningKeys = new LastSigningKeys(); } /** @@ -271,7 +273,8 @@ private void signingDialog(SigningDialog.Mode mode) { logging, keysRepository.getSigningKeys(), getJWS(), - mode + mode, + lastSigningKeys ); showDialogAndUpdateJWS(signDialog); diff --git a/src/main/java/com/blackberry/jwteditor/view/dialog/operations/LastSigningKeys.java b/src/main/java/com/blackberry/jwteditor/view/dialog/operations/LastSigningKeys.java new file mode 100644 index 0000000..f889946 --- /dev/null +++ b/src/main/java/com/blackberry/jwteditor/view/dialog/operations/LastSigningKeys.java @@ -0,0 +1,20 @@ +package com.blackberry.jwteditor.view.dialog.operations; + +import com.blackberry.jwteditor.model.keys.Key; +import com.blackberry.jwteditor.view.dialog.operations.SigningDialog.Mode; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class LastSigningKeys { + private final Map lastKeyMap = new HashMap<>(); + + Optional lastKeyFor(Mode mode) { + return Optional.ofNullable(lastKeyMap.get(mode)); + } + + void recordKeyUse(Mode mode, Key key) { + lastKeyMap.put(mode, key); + } +} diff --git a/src/main/java/com/blackberry/jwteditor/view/dialog/operations/SigningDialog.java b/src/main/java/com/blackberry/jwteditor/view/dialog/operations/SigningDialog.java index 751b0dc..a0544b6 100644 --- a/src/main/java/com/blackberry/jwteditor/view/dialog/operations/SigningDialog.java +++ b/src/main/java/com/blackberry/jwteditor/view/dialog/operations/SigningDialog.java @@ -61,15 +61,18 @@ public enum Mode { private JRadioButton radioButtonUpdateGenerateNone; private final Mode mode; + private final LastSigningKeys lastSigningKeys; public SigningDialog( Window parent, Logging logging, List signingKeys, JWS jws, - Mode mode) { + Mode mode, + LastSigningKeys lastSigningKeys) { super(parent, logging, mode.titleResourceId, jws, "error_title_unable_to_sign"); this.mode = mode; + this.lastSigningKeys = lastSigningKeys; configureUI(contentPane, buttonOK, buttonCancel); @@ -88,8 +91,9 @@ public SigningDialog( buttonOK.setEnabled(true); }); - // Set the signing key to the first entry, also triggering the event handler - comboBoxSigningKey.setSelectedIndex(0); + int lastUsedKeyIndex = lastSigningKeys.lastKeyFor(mode).map(signingKeys::indexOf).orElse(-1); + lastUsedKeyIndex = lastUsedKeyIndex == -1 ? 0 : lastUsedKeyIndex; + comboBoxSigningKey.setSelectedIndex(lastUsedKeyIndex); // If the dialog is being used for the embedded JWK attack, hide the Header Options if (mode != Mode.NORMAL) { @@ -103,6 +107,8 @@ JWS performOperation() throws SigningException, NoSuchFieldException, IllegalAcc JWKKey selectedKey = (JWKKey) comboBoxSigningKey.getSelectedItem(); JWSAlgorithm selectedAlgorithm = (JWSAlgorithm) comboBoxSigningAlgorithm.getSelectedItem(); + lastSigningKeys.recordKeyUse(mode, selectedKey); + // Get the header update mode based on the selected radio button, convert to the associated enum value SigningUpdateMode signingUpdateMode;