@@ -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}
0 commit comments