Skip to content

Commit

Permalink
Remember last used key within SigningDialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
DolphFlynn committed Dec 30, 2024
1 parent 70dff0c commit 8add11f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -72,6 +73,7 @@ public EditorPresenter(
this.keysRepository = keysRepository;
this.model = new EditorModel();
this.messageDialogFactory = new MessageDialogFactory(view.uiComponent());
this.lastSigningKeys = new LastSigningKeys();
}

/**
Expand Down Expand Up @@ -271,7 +273,8 @@ private void signingDialog(SigningDialog.Mode mode) {
logging,
keysRepository.getSigningKeys(),
getJWS(),
mode
mode,
lastSigningKeys
);

showDialogAndUpdateJWS(signDialog);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Mode, Key> lastKeyMap = new HashMap<>();

Optional<Key> lastKeyFor(Mode mode) {
return Optional.ofNullable(lastKeyMap.get(mode));
}

void recordKeyUse(Mode mode, Key key) {
lastKeyMap.put(mode, key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Key> 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);

Expand All @@ -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) {
Expand All @@ -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;

Expand Down

0 comments on commit 8add11f

Please sign in to comment.