Skip to content

Commit

Permalink
Merge pull request #34 from xEdziu/bug-fixes-v2.3.2
Browse files Browse the repository at this point in the history
Update to 2.3.2
  • Loading branch information
xEdziu authored Mar 22, 2022
2 parents fd0e4cd + 68aff4f commit 41b45b3
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.goral</groupId>
<artifactId>KeepMyPassword-Desktop</artifactId>
<version>2.3.1</version>
<version>2.3.2</version>
<name>KeepMyPassword Desktop</name>
<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,160 @@ protected void onLoginButtonClick() {
dialog.setResultConverter(dialogButton -> {
if (dialogButton == registerButtonType){

Pair<String, Color> checker = PasswordGeneratorUtil.checkPasswordComplexity(password.getText());
String newUname = username.getText();
String newPwd = password.getText();

Pair<String, Color> checker = PasswordGeneratorUtil.checkPasswordComplexity(newPwd);

if (password.getText().isEmpty() && !login){
AlertsUtil.showErrorDialog("Error", "There is a problem.", "You can't register with empty password.");
return new Pair<>("err"+newUname, newPwd);
} else if ((!checker.getKey().equals("Strong password") && !checker.getKey().equals("Medium password")) && !login){
AlertsUtil.showErrorDialog("Error", "There is a problem.", "Password is not strong enough.");
return new Pair<>("err"+newUname, newPwd);
} else {
return new Pair<>(newUname, newPwd);
}
}
return null;
});

Optional<Pair<String, String>> res = dialog.showAndWait();
res.ifPresent(result -> {

String uname = result.getKey();
String plain = result.getValue();

if (uname.startsWith("err")){
restartLoginForm(uname.substring(3), plain);
return;
}

if (login){
//login
try {
String config = ConfUtil.readConfigFile();
String[] configArr = config.split(":");
String unameFromString = configArr[0];
String encryptedInitial = configArr[1];
String ivString = configArr[2];
String salt = configArr[3];
String argon = ArgonUtil.encrypt(plain, salt);
SecretKey key = AESUtil.generateKey(argon);

if (SHAUtil.hashSHA(uname).equals(unameFromString)){
boolean authorized = AuthUtil.authorize(encryptedInitial, ivString, key);
if (!authorized){
showErrorDialog("Error", "Invalid username or password",
"Please provide correct credentials.");
onLoginButtonClick();
} else {
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("layouts/logged.fxml"));
Parent root = loader.load();

LoggedController loggedController = loader.getController();
loggedController.setSecretKey(key);
loggedController.setUnameLabel(uname);

Scene sc = new Scene(root);
String css = MainApp.class.getResource("styles/main.css").toExternalForm();
sc.getStylesheets().add(css);
MainApp.getStage().setScene(sc);
}
} else {
showErrorDialog("Error", "Invalid username or password",
"Please provide correct credentials.");
onLoginButtonClick();
}
} catch (Exception e) {
AlertsUtil.showExceptionStackTraceDialog(e);
}
} else {

//register
try {
IvParameterSpec iv = AESUtil.generateIv();
String salt = Base64.getEncoder().encodeToString(ArgonUtil.generateSalt());
String argon = ArgonUtil.encrypt(plain, salt);
SecretKey key = AESUtil.generateKey(argon);
String init = AuthUtil.encryptInitial(key, iv);

String output = SHAUtil.hashSHA(uname) + ":" + init + ":" + salt;
createConfFiles(output);
login = true;
handleAppRun();
onLoginButtonClick();

} catch (Exception e){
AlertsUtil.showExceptionStackTraceDialog(e);
}
}
});
}

protected void restartLoginForm(String u, String p){
onLoginButtonClick(u, p);
}

@FXML
protected void onLoginButtonClick(String unameString, String passwordString) {
Dialog<Pair<String, String>> dialog = new Dialog<>();
String dialogType = login ? "Login" : "Register";
dialog.setTitle(dialogType + " Dialog");
dialog.setHeaderText(login ? "Log in to your account" : "Set up your account");
dialog.setGraphic(new ImageView(MainApp.class.getResource("/me/goral/keepmypassworddesktop/images/login-64.png").toString()));
dialog.getDialogPane().getStylesheets().add(MainApp.class.getResource("styles/dialog.css").toExternalForm());

ButtonType registerButtonType = new ButtonType(dialogType, ButtonBar.ButtonData.OK_DONE);
ButtonType cancelButtonType = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
dialog.getDialogPane().getButtonTypes().setAll(registerButtonType, cancelButtonType);
Node regBtn = dialog.getDialogPane().lookupButton(registerButtonType);
Node canBtn = dialog.getDialogPane().lookupButton(cancelButtonType);
regBtn.getStyleClass().add("btn");
canBtn.getStyleClass().add("btn");
regBtn.setDisable(true);

Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(MainApp.class.getResourceAsStream("/me/goral/keepmypassworddesktop/images/access-32.png")));

GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20,150,10,10));

TextField username = new TextField();
username.setText(unameString);
PasswordField password = new PasswordField();
password.setText(passwordString);

grid.add(new Label("Username"), 0, 0);
grid.add(username, 1, 0);
grid.add(new Label("Password"), 0, 1);
grid.add(password, 1, 1);

username.textProperty().addListener(((observableValue, oldV, newV) -> regBtn.setDisable(newV.trim().isEmpty())));
regBtn.setDisable(unameString.trim().isEmpty());

dialog.getDialogPane().setContent(grid);
Platform.runLater(username::requestFocus);

dialog.setResultConverter(dialogButton -> {
if (dialogButton == registerButtonType){

String newUname = username.getText();
String newPwd = password.getText();

Pair<String, Color> checker = PasswordGeneratorUtil.checkPasswordComplexity(newPwd);

if (password.getText().isEmpty() && !login){
AlertsUtil.showErrorDialog("Error", "There is a problem.", "You can't register with empty password.");
return null;
return new Pair<>("err"+newUname, newPwd);
} else if ((!checker.getKey().equals("Strong password") && !checker.getKey().equals("Medium password")) && !login){
AlertsUtil.showErrorDialog("Error", "There is a problem.", "Password is not strong enough.");
return null;
return new Pair<>("err"+newUname, newPwd);
} else {
return new Pair<>(newUname, newPwd);
}
return new Pair<>(username.getText(), password.getText());
}
return null;
});
Expand All @@ -113,6 +257,11 @@ protected void onLoginButtonClick() {
String uname = result.getKey();
String plain = result.getValue();

if (uname.startsWith("err")){
restartLoginForm(uname.substring(3), plain);
return;
}

if (login){
//login
try {
Expand Down Expand Up @@ -179,26 +328,24 @@ public void setIsLogged() {
login = true;
}

/**
* If the database and config files don't exist, then the user is
* registering. If the database exists but the config file doesn't, then the user is registering. If the config file
* exists but the database doesn't, then the user is registering. If the config file and database exist, then the user is
* logging in
*/

public void handleAppRun() {
if (!ConfUtil.checkIfConfigExists() && !ConfUtil.checkIfDatabaseExists()) {
btnLogin.setText("Register");
} else if (ConfUtil.checkIfDatabaseExists() && !ConfUtil.checkIfConfigExists()) {
File db = new File("database.db");
db.delete();
btnLogin.setText("Register");
} else if (ConfUtil.checkIfConfigExists() && !ConfUtil.checkIfDatabaseExists()){
DatabaseHandler.createDatabase();
btnLogin.setText("Log in");
login = true;
} else {
btnLogin.setText("Log in");
login = true;
try {
if (!ConfUtil.checkIfConfigExists() && !ConfUtil.checkIfDatabaseExists()) {
btnLogin.setText("Register");
} else if (ConfUtil.checkIfDatabaseExists() && !ConfUtil.checkIfConfigExists()) {
ConfUtil.deleteConfFiles();
btnLogin.setText("Register");
} else if (ConfUtil.checkIfConfigExists() && !ConfUtil.checkIfDatabaseExists()){
DatabaseHandler.createDatabase();
btnLogin.setText("Log in");
login = true;
} else {
btnLogin.setText("Log in");
login = true;
}
} catch (Exception e){
AlertsUtil.showExceptionStackTraceDialog(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ public static void showGeneratePasswordDialog(){
showErrorDialog("Error", "Invalid input", "Length parameter can't be empty!");
return;
}
if (len.length() >= 6) {
showErrorDialog("Error", "You can't generate that long password.", "Why would you need 6-digits long password anyway?");
return;
}
String lower = result.get(1);
if (lower.length() == 0) {
showErrorDialog("Error", "Invalid input", "Lowe case number parameter can't be empty!");
Expand Down Expand Up @@ -491,14 +495,14 @@ public static void showGeneratedPasswordDialog(String pwd){
TextArea textArea = new TextArea(pwd);
textArea.setEditable(false);
textArea.setWrapText(true);
textArea.setMaxHeight(new Text(pwd).getLayoutBounds().getHeight());
textArea.setMaxHeight(Double.MAX_VALUE);

GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS);

GridPane expContent = new GridPane();
expContent.setMaxWidth(Double.MAX_VALUE);
expContent.setMaxHeight(new Text(pwd).getLayoutBounds().getHeight());
expContent.setMaxHeight(Double.MAX_VALUE);
expContent.add(new Label("Your new password:"), 0, 0);
expContent.add(textArea, 0, 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public static void deleteConfFiles() {
File f = new File(workingDirectory + confFileName);
if (f.delete()){
File db = new File(workingDirectory + databaseFileName);
if (db.delete()){
showInformationDialog("Information Dialog", "Your account is now deleted", "Have a great day!");
if (!db.delete()){
throw new Exception("Database could not be deleted");
}
} else showErrorDialog("Something went wrong", "Whoops!", "Sorry, but something went wrong. " +
"Please, raise an issue on github and describe what happened.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
<Button fx:id="genPwd" layoutX="509.0" layoutY="175.0" mnemonicParsing="false" onAction="#onGenPwdClick" prefHeight="25.0" prefWidth="195.0" text="Generate Password" wrapText="true" />
<Button fx:id="showBtn" layoutX="442.0" layoutY="109.0" maxWidth="16" mnemonicParsing="false" onAction="#onShowBtnClick" prefHeight="16" prefWidth="16" />
<Button fx:id="settingsButton" layoutX="625.0" layoutY="5.0" maxHeight="128" maxWidth="128" mnemonicParsing="false" onAction="#onSettingsButtonClick" prefHeight="64" prefWidth="64" />
<Label layoutY="75.0" prefHeight="17.0" prefWidth="750.0" text="v2.3.1" textAlignment="CENTER" />
<Label layoutY="75.0" prefHeight="17.0" prefWidth="750.0" text="v2.3.2" textAlignment="CENTER" />
</children>
</AnchorPane>

0 comments on commit 41b45b3

Please sign in to comment.