Skip to content

Commit

Permalink
Merge pull request #281 from solver-it-sro/AG-150/change-signing-flow
Browse files Browse the repository at this point in the history
AG-150 Change signing flow
  • Loading branch information
celuchmarek committed Oct 6, 2023
2 parents de7cf52 + 6b7298e commit 905a6a1
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,20 @@ public class BatchDialogController implements SuppressedFocusController {

@FXML
VBox mainBox;

@FXML
VBox progressBarBox;

@FXML
VBox batchVisualization;

@FXML
Text batchVisualizationCount;

@FXML
public Button chooseKeyButton;
@FXML
public Button signButton;
public Button mainButton;
@FXML
public Button changeKeyButton;

@FXML
public ProgressBar progressBar;

@FXML
public Text progressBarText;

@FXML
public Button cancelBatchButton;

Expand Down Expand Up @@ -76,36 +67,48 @@ public void update() {
}
}

public void onChooseKeyButtonPressed(ActionEvent event) {
autogram.pickSigningKeyAndThen(key -> {
gui.setActiveSigningKey(key);
refreshSigningKey();
enableKeyChange();
});
}

public void onSignButtonPressed(ActionEvent event) {
public void onMainButtonPressed(ActionEvent event) {
var signingKey = gui.getActiveSigningKey();
if (signingKey == null) {
return;
autogram.pickSigningKeyAndThen(key -> {
gui.setActiveSigningKeyAndThen(key, k -> {
hideVisualization();
showProgress();
showCancelButton();
getNodeForLoosingFocus().requestFocus();
gui.disableSigning();
gui.onWorkThreadDo(() -> {
startBatchCallback.accept(k);
});
});
});
} else {
hideVisualization();
showProgress();
showCancelButton();
getNodeForLoosingFocus().requestFocus();
gui.disableSigning();
gui.onWorkThreadDo(() -> {
startBatchCallback.accept(signingKey);
});
}
disableSigning();
disableKeyChange();
hideVisualization();
showProgress();
showCancelButton();
getNodeForLoosingFocus().requestFocus();
gui.onWorkThreadDo(() -> {
startBatchCallback.accept(signingKey);
});
}


public void onChangeKeyButtonPressed(ActionEvent event) {
if (batch.isKeyChangeAllowed()) {
gui.resetSigningKey();
autogram.pickSigningKeyAndThen((key) -> {
gui.setActiveSigningKey(key);
refreshSigningKey();
autogram.pickSigningKeyAndThen(key -> {
gui.setActiveSigningKeyAndThen(key, k -> {
hideVisualization();
showProgress();
showCancelButton();
getNodeForLoosingFocus().requestFocus();
gui.disableSigning();
gui.onWorkThreadDo(() -> {
startBatchCallback.accept(k);
});
});
});
}
}
Expand All @@ -117,32 +120,26 @@ public void onCancelBatchButtonPressed(ActionEvent event) {

public void refreshSigningKey() {
if (batch.isKeyChangeAllowed()) {
SigningKey key = gui.getActiveSigningKey();
if (key == null) {
signButton.setVisible(false);
signButton.setManaged(false);
var key = gui.getActiveSigningKey();

chooseKeyButton.setVisible(true);
chooseKeyButton.setManaged(true);
chooseKeyButton.setDisable(false);
if (key == null) {
mainButton.setText("Podpísať");
changeKeyButton.setVisible(false);

disableKeyChange();
} else {
signButton.setVisible(true);
signButton.setManaged(true);

chooseKeyButton.setVisible(false);
chooseKeyButton.setManaged(false);

signButton.setDisable(false);
signButton.setText("Podpísať ako "
+ DSSUtils.parseCN(key.getCertificate().getSubject().getRFC2253()));

enableKeyChange();
mainButton.setText("Podpísať ako " + DSSUtils.parseCN(key.getCertificate().getSubject().getRFC2253()));
changeKeyButton.setVisible(true);
}
}
}

public void enableSigning() {
mainButton.setDisable(false);
changeKeyButton.setManaged(true);
changeKeyButton.setDisable(false);
changeKeyButton.setVisible(true);
}

public void close() {
var window = mainBox.getScene().getRoot().getScene().getWindow();
if (window instanceof Stage) {
Expand All @@ -167,25 +164,19 @@ public void hideVisualization() {
}

public void disableKeyPicking() {
chooseKeyButton.setText("Načítavam certifikáty…");
chooseKeyButton.setDisable(true);
mainButton.setText("Načítavam certifikáty…");
mainButton.setDisable(true);
changeKeyButton.setDisable(true);
changeKeyButton.setManaged(false);
changeKeyButton.setVisible(false);
}

public void disableSigning() {
signButton.setVisible(false);
signButton.setManaged(false);
// signButton.setText("Prebieha hromadné podpisovanie...");
// signButton.setDisable(true);
}

public void disableKeyChange() {
changeKeyButton.setVisible(false);
mainButton.setText("Prebieha podpisovanie…");
mainButton.setDisable(true);
changeKeyButton.setDisable(true);
changeKeyButton.setManaged(false);
}

public void enableKeyChange() {
changeKeyButton.setVisible(true);
changeKeyButton.setManaged(true);
changeKeyButton.setVisible(false);
}

private void updateVisualizationCount() {
Expand Down
74 changes: 52 additions & 22 deletions src/main/java/digital/slovensko/autogram/ui/gui/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public void cancelBatch(Batch batch) {
batchController.close();
batch.end();
refreshKeyOnAllJobs();
enableSigningOnAllJobs();
}

@Override
Expand Down Expand Up @@ -110,6 +111,7 @@ public void pickTokenDriverAndThen(List<TokenDriver> drivers, Consumer<TokenDriv
if (drivers.isEmpty()) {
showError(new NoDriversDetectedException());
refreshKeyOnAllJobs();
enableSigningOnAllJobs();
} else if (drivers.size() == 1) {
// short-circuit if only one driver present
callback.accept(drivers.get(0));
Expand All @@ -120,8 +122,10 @@ public void pickTokenDriverAndThen(List<TokenDriver> drivers, Consumer<TokenDriv
var stage = new Stage();
stage.setTitle("Výber úložiska certifikátu");
stage.setScene(new Scene(root));
stage.setOnCloseRequest(e -> refreshKeyOnAllJobs());

stage.setOnCloseRequest(e -> {
refreshKeyOnAllJobs();
enableSigningOnAllJobs();
});
stage.sizeToScene();
stage.setResizable(false);
stage.initModality(Modality.APPLICATION_MODAL);
Expand All @@ -142,33 +146,38 @@ public void requestPasswordAndThen(TokenDriver driver, Consumer<char[]> callback
var stage = new Stage();
stage.setTitle("Načítanie klúčov z úložiska");
stage.setScene(new Scene(root));
stage.setOnCloseRequest(e -> refreshKeyOnAllJobs());
stage.setOnCloseRequest(e -> {
refreshKeyOnAllJobs();
enableSigningOnAllJobs();
});
stage.setResizable(false);
stage.initModality(Modality.APPLICATION_MODAL);
stage.show();
}

@Override
public void pickKeyAndThen(List<DSSPrivateKeyEntry> keys,
Consumer<DSSPrivateKeyEntry> callback) {
public void pickKeyAndThen(List<DSSPrivateKeyEntry> keys, Consumer<DSSPrivateKeyEntry> callback) {
if (keys.isEmpty()) {
showError(new NoKeysDetectedException());
refreshKeyOnAllJobs();
} else if (keys.size() == 1) {
// short-circuit if only one key present
callback.accept(keys.get(0));
} else {
var controller = new PickKeyDialogController(keys, callback);
var root = GUIUtils.loadFXML(controller, "pick-key-dialog.fxml");
enableSigningOnAllJobs();

var stage = new Stage();
stage.setTitle("Výber certifikátu");
stage.setScene(new Scene(root));
stage.setOnCloseRequest(e -> refreshKeyOnAllJobs());
stage.setResizable(false);
stage.initModality(Modality.APPLICATION_MODAL);
stage.show();
return;
}

var controller = new PickKeyDialogController(keys, callback);
var root = GUIUtils.loadFXML(controller, "pick-key-dialog.fxml");

var stage = new Stage();
stage.setTitle("Výber certifikátu");
stage.setScene(new Scene(root));
stage.setOnCloseRequest(e -> {
refreshKeyOnAllJobs();
enableSigningOnAllJobs();
});
stage.setResizable(false);
stage.initModality(Modality.APPLICATION_MODAL);
stage.show();
}

public void refreshKeyOnAllJobs() {
Expand All @@ -178,6 +187,12 @@ public void refreshKeyOnAllJobs() {
}
}

public void enableSigningOnAllJobs() {
jobControllers.values().forEach(SigningDialogController::enableSigning);
if (batchController != null)
batchController.enableSigning();
}

@Override
public void showError(AutogramException e) {
logger.debug("GUI showing error", e);
Expand Down Expand Up @@ -295,18 +310,22 @@ public void showIgnorableExceptionDialog(IgnorableException e) {

private void disableKeyPicking() {
jobControllers.values().forEach(SigningDialogController::disableKeyPicking);
if (batchController != null)
batchController.disableKeyPicking();
}

@Override
public void onPickSigningKeyFailed(AutogramException e) {
showError(e);
resetSigningKey();
enableSigningOnAllJobs();
}

@Override
public void onSigningSuccess(SigningJob job) {
jobControllers.get(job).close();
refreshKeyOnAllJobs();
enableSigningOnAllJobs();
updateBatch();
}

Expand All @@ -318,6 +337,7 @@ public void onSigningFailed(AutogramException e) {
} else {
refreshKeyOnAllJobs();
}
enableSigningOnAllJobs();
}

@Override
Expand Down Expand Up @@ -352,6 +372,8 @@ public void onDocumentBatchSaved(BatchUiResult result) {
stage.sizeToScene();
GUIUtils.suppressDefaultFocus(stage, controller);
stage.show();

enableSigningOnAllJobs();
}

@Override
Expand Down Expand Up @@ -386,14 +408,20 @@ public SigningKey getActiveSigningKey() {
return activeKey;
}

public void setActiveSigningKey(SigningKey newKey) {
if (!isActiveSigningKeyChangeAllowed()) {
public void setActiveSigningKeyAndThen(SigningKey newKey, Consumer<SigningKey> callback) {
if (!isActiveSigningKeyChangeAllowed())
throw new RuntimeException("Signing key change is not allowed");
}

if (activeKey != null)
activeKey.close();

activeKey = newKey;
refreshKeyOnAllJobs();

if (callback != null)
callback.accept(newKey);
else
enableSigningOnAllJobs();
}

public boolean isActiveSigningKeyChangeAllowed() {
Expand All @@ -402,10 +430,12 @@ public boolean isActiveSigningKeyChangeAllowed() {

public void disableSigning() {
jobControllers.values().forEach(SigningDialogController::disableSigning);
if (batchController != null)
batchController.disableSigning();
}

public void resetSigningKey() {
setActiveSigningKey(null);
setActiveSigningKeyAndThen(null, null);
}

public void cancelJob(SigningJob job) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public void initialize() {
tooltip.setPrefWidth(400);
radioButton.setTooltip(tooltip);
}

if (keys.size() == 1)
toggleGroup.getToggles().get(0).setSelected(true);
}

public void onPickCertificateButtonAction(ActionEvent actionEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void close() {
if (window instanceof Stage)
((Stage) window).close();

signingDialogController.enableSigning();
signingDialogController.enableSigningOnAllJobs();
}

public void onCancelAction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void close() {
if (window instanceof Stage)
((Stage) window).close();

signigDialogController.enableSigning();
signigDialogController.enableSigningOnAllJobs();
}

public void onCancelAction() {
Expand Down
Loading

0 comments on commit 905a6a1

Please sign in to comment.