Skip to content

Commit 41b45b3

Browse files
authored
Merge pull request #34 from xEdziu/bug-fixes-v2.3.2
Update to 2.3.2
2 parents fd0e4cd + 68aff4f commit 41b45b3

File tree

5 files changed

+180
-29
lines changed

5 files changed

+180
-29
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>me.goral</groupId>
88
<artifactId>KeepMyPassword-Desktop</artifactId>
9-
<version>2.3.1</version>
9+
<version>2.3.2</version>
1010
<name>KeepMyPassword Desktop</name>
1111
<packaging>jar</packaging>
1212

src/main/java/me/goral/keepmypassworddesktop/controllers/MainAppController.java

Lines changed: 170 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,160 @@ protected void onLoginButtonClick() {
9393
dialog.setResultConverter(dialogButton -> {
9494
if (dialogButton == registerButtonType){
9595

96-
Pair<String, Color> checker = PasswordGeneratorUtil.checkPasswordComplexity(password.getText());
96+
String newUname = username.getText();
97+
String newPwd = password.getText();
98+
99+
Pair<String, Color> checker = PasswordGeneratorUtil.checkPasswordComplexity(newPwd);
100+
101+
if (password.getText().isEmpty() && !login){
102+
AlertsUtil.showErrorDialog("Error", "There is a problem.", "You can't register with empty password.");
103+
return new Pair<>("err"+newUname, newPwd);
104+
} else if ((!checker.getKey().equals("Strong password") && !checker.getKey().equals("Medium password")) && !login){
105+
AlertsUtil.showErrorDialog("Error", "There is a problem.", "Password is not strong enough.");
106+
return new Pair<>("err"+newUname, newPwd);
107+
} else {
108+
return new Pair<>(newUname, newPwd);
109+
}
110+
}
111+
return null;
112+
});
113+
114+
Optional<Pair<String, String>> res = dialog.showAndWait();
115+
res.ifPresent(result -> {
116+
117+
String uname = result.getKey();
118+
String plain = result.getValue();
119+
120+
if (uname.startsWith("err")){
121+
restartLoginForm(uname.substring(3), plain);
122+
return;
123+
}
124+
125+
if (login){
126+
//login
127+
try {
128+
String config = ConfUtil.readConfigFile();
129+
String[] configArr = config.split(":");
130+
String unameFromString = configArr[0];
131+
String encryptedInitial = configArr[1];
132+
String ivString = configArr[2];
133+
String salt = configArr[3];
134+
String argon = ArgonUtil.encrypt(plain, salt);
135+
SecretKey key = AESUtil.generateKey(argon);
136+
137+
if (SHAUtil.hashSHA(uname).equals(unameFromString)){
138+
boolean authorized = AuthUtil.authorize(encryptedInitial, ivString, key);
139+
if (!authorized){
140+
showErrorDialog("Error", "Invalid username or password",
141+
"Please provide correct credentials.");
142+
onLoginButtonClick();
143+
} else {
144+
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("layouts/logged.fxml"));
145+
Parent root = loader.load();
146+
147+
LoggedController loggedController = loader.getController();
148+
loggedController.setSecretKey(key);
149+
loggedController.setUnameLabel(uname);
150+
151+
Scene sc = new Scene(root);
152+
String css = MainApp.class.getResource("styles/main.css").toExternalForm();
153+
sc.getStylesheets().add(css);
154+
MainApp.getStage().setScene(sc);
155+
}
156+
} else {
157+
showErrorDialog("Error", "Invalid username or password",
158+
"Please provide correct credentials.");
159+
onLoginButtonClick();
160+
}
161+
} catch (Exception e) {
162+
AlertsUtil.showExceptionStackTraceDialog(e);
163+
}
164+
} else {
165+
166+
//register
167+
try {
168+
IvParameterSpec iv = AESUtil.generateIv();
169+
String salt = Base64.getEncoder().encodeToString(ArgonUtil.generateSalt());
170+
String argon = ArgonUtil.encrypt(plain, salt);
171+
SecretKey key = AESUtil.generateKey(argon);
172+
String init = AuthUtil.encryptInitial(key, iv);
173+
174+
String output = SHAUtil.hashSHA(uname) + ":" + init + ":" + salt;
175+
createConfFiles(output);
176+
login = true;
177+
handleAppRun();
178+
onLoginButtonClick();
179+
180+
} catch (Exception e){
181+
AlertsUtil.showExceptionStackTraceDialog(e);
182+
}
183+
}
184+
});
185+
}
186+
187+
protected void restartLoginForm(String u, String p){
188+
onLoginButtonClick(u, p);
189+
}
190+
191+
@FXML
192+
protected void onLoginButtonClick(String unameString, String passwordString) {
193+
Dialog<Pair<String, String>> dialog = new Dialog<>();
194+
String dialogType = login ? "Login" : "Register";
195+
dialog.setTitle(dialogType + " Dialog");
196+
dialog.setHeaderText(login ? "Log in to your account" : "Set up your account");
197+
dialog.setGraphic(new ImageView(MainApp.class.getResource("/me/goral/keepmypassworddesktop/images/login-64.png").toString()));
198+
dialog.getDialogPane().getStylesheets().add(MainApp.class.getResource("styles/dialog.css").toExternalForm());
199+
200+
ButtonType registerButtonType = new ButtonType(dialogType, ButtonBar.ButtonData.OK_DONE);
201+
ButtonType cancelButtonType = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
202+
dialog.getDialogPane().getButtonTypes().setAll(registerButtonType, cancelButtonType);
203+
Node regBtn = dialog.getDialogPane().lookupButton(registerButtonType);
204+
Node canBtn = dialog.getDialogPane().lookupButton(cancelButtonType);
205+
regBtn.getStyleClass().add("btn");
206+
canBtn.getStyleClass().add("btn");
207+
regBtn.setDisable(true);
208+
209+
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
210+
stage.getIcons().add(new Image(MainApp.class.getResourceAsStream("/me/goral/keepmypassworddesktop/images/access-32.png")));
211+
212+
GridPane grid = new GridPane();
213+
grid.setHgap(10);
214+
grid.setVgap(10);
215+
grid.setPadding(new Insets(20,150,10,10));
216+
217+
TextField username = new TextField();
218+
username.setText(unameString);
219+
PasswordField password = new PasswordField();
220+
password.setText(passwordString);
221+
222+
grid.add(new Label("Username"), 0, 0);
223+
grid.add(username, 1, 0);
224+
grid.add(new Label("Password"), 0, 1);
225+
grid.add(password, 1, 1);
226+
227+
username.textProperty().addListener(((observableValue, oldV, newV) -> regBtn.setDisable(newV.trim().isEmpty())));
228+
regBtn.setDisable(unameString.trim().isEmpty());
229+
230+
dialog.getDialogPane().setContent(grid);
231+
Platform.runLater(username::requestFocus);
232+
233+
dialog.setResultConverter(dialogButton -> {
234+
if (dialogButton == registerButtonType){
235+
236+
String newUname = username.getText();
237+
String newPwd = password.getText();
238+
239+
Pair<String, Color> checker = PasswordGeneratorUtil.checkPasswordComplexity(newPwd);
97240

98241
if (password.getText().isEmpty() && !login){
99242
AlertsUtil.showErrorDialog("Error", "There is a problem.", "You can't register with empty password.");
100-
return null;
243+
return new Pair<>("err"+newUname, newPwd);
101244
} else if ((!checker.getKey().equals("Strong password") && !checker.getKey().equals("Medium password")) && !login){
102245
AlertsUtil.showErrorDialog("Error", "There is a problem.", "Password is not strong enough.");
103-
return null;
246+
return new Pair<>("err"+newUname, newPwd);
247+
} else {
248+
return new Pair<>(newUname, newPwd);
104249
}
105-
return new Pair<>(username.getText(), password.getText());
106250
}
107251
return null;
108252
});
@@ -113,6 +257,11 @@ protected void onLoginButtonClick() {
113257
String uname = result.getKey();
114258
String plain = result.getValue();
115259

260+
if (uname.startsWith("err")){
261+
restartLoginForm(uname.substring(3), plain);
262+
return;
263+
}
264+
116265
if (login){
117266
//login
118267
try {
@@ -179,26 +328,24 @@ public void setIsLogged() {
179328
login = true;
180329
}
181330

182-
/**
183-
* If the database and config files don't exist, then the user is
184-
* registering. If the database exists but the config file doesn't, then the user is registering. If the config file
185-
* exists but the database doesn't, then the user is registering. If the config file and database exist, then the user is
186-
* logging in
187-
*/
331+
188332
public void handleAppRun() {
189-
if (!ConfUtil.checkIfConfigExists() && !ConfUtil.checkIfDatabaseExists()) {
190-
btnLogin.setText("Register");
191-
} else if (ConfUtil.checkIfDatabaseExists() && !ConfUtil.checkIfConfigExists()) {
192-
File db = new File("database.db");
193-
db.delete();
194-
btnLogin.setText("Register");
195-
} else if (ConfUtil.checkIfConfigExists() && !ConfUtil.checkIfDatabaseExists()){
196-
DatabaseHandler.createDatabase();
197-
btnLogin.setText("Log in");
198-
login = true;
199-
} else {
200-
btnLogin.setText("Log in");
201-
login = true;
333+
try {
334+
if (!ConfUtil.checkIfConfigExists() && !ConfUtil.checkIfDatabaseExists()) {
335+
btnLogin.setText("Register");
336+
} else if (ConfUtil.checkIfDatabaseExists() && !ConfUtil.checkIfConfigExists()) {
337+
ConfUtil.deleteConfFiles();
338+
btnLogin.setText("Register");
339+
} else if (ConfUtil.checkIfConfigExists() && !ConfUtil.checkIfDatabaseExists()){
340+
DatabaseHandler.createDatabase();
341+
btnLogin.setText("Log in");
342+
login = true;
343+
} else {
344+
btnLogin.setText("Log in");
345+
login = true;
346+
}
347+
} catch (Exception e){
348+
AlertsUtil.showExceptionStackTraceDialog(e);
202349
}
203350
}
204351
}

src/main/java/me/goral/keepmypassworddesktop/util/AlertsUtil.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,10 @@ public static void showGeneratePasswordDialog(){
404404
showErrorDialog("Error", "Invalid input", "Length parameter can't be empty!");
405405
return;
406406
}
407+
if (len.length() >= 6) {
408+
showErrorDialog("Error", "You can't generate that long password.", "Why would you need 6-digits long password anyway?");
409+
return;
410+
}
407411
String lower = result.get(1);
408412
if (lower.length() == 0) {
409413
showErrorDialog("Error", "Invalid input", "Lowe case number parameter can't be empty!");
@@ -491,14 +495,14 @@ public static void showGeneratedPasswordDialog(String pwd){
491495
TextArea textArea = new TextArea(pwd);
492496
textArea.setEditable(false);
493497
textArea.setWrapText(true);
494-
textArea.setMaxHeight(new Text(pwd).getLayoutBounds().getHeight());
498+
textArea.setMaxHeight(Double.MAX_VALUE);
495499

496500
GridPane.setVgrow(textArea, Priority.ALWAYS);
497501
GridPane.setHgrow(textArea, Priority.ALWAYS);
498502

499503
GridPane expContent = new GridPane();
500504
expContent.setMaxWidth(Double.MAX_VALUE);
501-
expContent.setMaxHeight(new Text(pwd).getLayoutBounds().getHeight());
505+
expContent.setMaxHeight(Double.MAX_VALUE);
502506
expContent.add(new Label("Your new password:"), 0, 0);
503507
expContent.add(textArea, 0, 1);
504508

src/main/java/me/goral/keepmypassworddesktop/util/ConfUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ public static void deleteConfFiles() {
123123
File f = new File(workingDirectory + confFileName);
124124
if (f.delete()){
125125
File db = new File(workingDirectory + databaseFileName);
126-
if (db.delete()){
127-
showInformationDialog("Information Dialog", "Your account is now deleted", "Have a great day!");
126+
if (!db.delete()){
127+
throw new Exception("Database could not be deleted");
128128
}
129129
} else showErrorDialog("Something went wrong", "Whoops!", "Sorry, but something went wrong. " +
130130
"Please, raise an issue on github and describe what happened.");

src/main/resources/me/goral/keepmypassworddesktop/layouts/logged.fxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
<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" />
2323
<Button fx:id="showBtn" layoutX="442.0" layoutY="109.0" maxWidth="16" mnemonicParsing="false" onAction="#onShowBtnClick" prefHeight="16" prefWidth="16" />
2424
<Button fx:id="settingsButton" layoutX="625.0" layoutY="5.0" maxHeight="128" maxWidth="128" mnemonicParsing="false" onAction="#onSettingsButtonClick" prefHeight="64" prefWidth="64" />
25-
<Label layoutY="75.0" prefHeight="17.0" prefWidth="750.0" text="v2.3.1" textAlignment="CENTER" />
25+
<Label layoutY="75.0" prefHeight="17.0" prefWidth="750.0" text="v2.3.2" textAlignment="CENTER" />
2626
</children>
2727
</AnchorPane>

0 commit comments

Comments
 (0)