Skip to content

Commit

Permalink
Added classes for prefs
Browse files Browse the repository at this point in the history
Improved gui layout consistency
Remove unused exception from shared db
  • Loading branch information
Siedlerchr committed Oct 22, 2017
1 parent 418aaad commit 74ce5b9
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 64 deletions.
1 change: 0 additions & 1 deletion src/main/java/org/jabref/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public static synchronized KeyBindingRepository getKeyPrefs() {
return keyBindingRepository;
}


// Background tasks
public static void startBackgroundTasks() {
Globals.focusListener = new GlobalFocusListener();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -253,7 +252,7 @@ private void applyPreferences() {
try {
passwordField.setText(
new Password(sharedDatabasePassword.get().toCharArray(), sharedDatabaseUser.get()).decrypt());
} catch (GeneralSecurityException | UnsupportedEncodingException e) {
} catch (GeneralSecurityException e) {
LOGGER.error("Could not read the password due to decryption problems.", e);
}
}
Expand Down Expand Up @@ -401,7 +400,7 @@ private void setPreferences() {
if (rememberPassword.isSelected()) {
try {
prefs.setPassword(new Password(passwordField.getPassword(), userField.getText()).encrypt());
} catch (GeneralSecurityException | UnsupportedEncodingException e) {
} catch (GeneralSecurityException e) {
LOGGER.error("Could not store the password due to encryption problems.", e);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jabref.gui.FXDialog;
import org.jabref.gui.FXDialogService;
import org.jabref.logic.sharelatex.ShareLatexManager;
import org.jabref.logic.sharelatex.SharelatexConnectionProperties;

public class ShareLatexLoginDialogController extends AbstractController<ShareLatexLoginDialogViewModel> {

Expand All @@ -20,6 +21,7 @@ public class ShareLatexLoginDialogController extends AbstractController<ShareLat
@FXML private PasswordField pfPassword;
@Inject private ShareLatexManager manager;

private SharelatexConnectionProperties props;
@FXML
private void initialize() {
viewModel = new ShareLatexLoginDialogViewModel();
Expand All @@ -40,6 +42,8 @@ private void signIn() {
dlg.setContentText("Your email or password is incorrect. Please try again");
dlg.showAndWait();
} else {
//TODO: Wait until pdf + injection stuff gets merged
props = new SharelatexConnectionProperties(tbAddress.getText(), tbUsername.getText(), pfPassword.getText(), "default");
ShareLatexProjectDialogView dlgprojects = new ShareLatexProjectDialogView();
dlgprojects.show();
closeDialog();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.jabref.logic.sharelatex;

import java.security.GeneralSecurityException;
import java.util.Objects;

import org.jabref.shared.security.Password;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SharelatexConnectionProperties {

private static final Log LOGGER = LogFactory.getLog(SharelatexConnectionProperties.class);

private String user;
private String password;
private String url;
private String project;

public SharelatexConnectionProperties() {
// no data
}

public SharelatexConnectionProperties(SharelatexPreferences prefs) {
setFromPreferences(prefs);
}

public SharelatexConnectionProperties(String url, String user, String password, String project) {
this.url = url;
this.user = user;
this.password = password;
this.project = project;
}

public String getUser() {
return user;
}

public String getPassword() {
return password;
}

public String getUrl() {
return url;
}

public String getProject() {
return project;
}

public boolean isValid() {
return Objects.nonNull(url)
&& Objects.nonNull(user)
&& Objects.nonNull(password)
&& Objects.nonNull(project);
}

private void setFromPreferences(SharelatexPreferences prefs) {

this.url = prefs.getSharelatexUrl();
prefs.getDefaultProject().ifPresent(proj -> this.project = proj);

if (prefs.getUser().isPresent()) {
this.user = prefs.getUser().get();
if (prefs.getPassword().isPresent()) {
try {
this.password = new Password(prefs.getPassword().get().toCharArray(), prefs.getUser().get()).decrypt();
} catch (GeneralSecurityException e) {
LOGGER.error("Could not decrypt password", e);
}
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.jabref.logic.sharelatex;

import java.util.Optional;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import org.jabref.JabRefMain;

public class SharelatexPreferences {

private static final String DEFAULT_NODE = "default";
private static final String PARENT_NODE = "jabref-sharelatex";

private static final String SHARELATEX_URL = "sharelatexUrl";
private static final String SHARELATEX_USER = "sharelatexUser";
private static final String SHARELATEX_PASSWORD = "sharelatexPassword";
private static final String SHARELATEX_REMEMBER_PASSWORD = "sharelatexRememberPassword";
private static final String SHARELATEX_PROJECT = "sharelatexProject";

// This {@link Preferences} is used only for things which should not appear in real JabRefPreferences due to security reasons.
private final Preferences internalPrefs;

public SharelatexPreferences() {
this(DEFAULT_NODE);

}

public SharelatexPreferences(String sharelatexId) {
internalPrefs = Preferences.userNodeForPackage(JabRefMain.class).parent().node(PARENT_NODE).node(sharelatexId);
}

public String getSharelatexUrl() {
return getOptionalValue(SHARELATEX_URL).orElse("https://www.sharelatex.com");
}

public Optional<String> getUser() {
return getOptionalValue(SHARELATEX_USER);
}

public Optional<String> getPassword() {
return getOptionalValue(SHARELATEX_PASSWORD);
}

public Optional<String> getDefaultProject() {
return getOptionalValue(SHARELATEX_PROJECT);
}

public void setSharelatexUrl(String url) {
internalPrefs.put(SHARELATEX_URL, url);
}

public void setSharelatexUser(String user) {
internalPrefs.put(SHARELATEX_USER, user);
}

public void setSharelatexPassword(String pwd) {
internalPrefs.put(SHARELATEX_PASSWORD, pwd);
}

public void setSharelatexProject(String project) {
internalPrefs.put(SHARELATEX_PROJECT, project);
}

public void setRememberPassword(boolean rememberPassword) {
internalPrefs.putBoolean(SHARELATEX_REMEMBER_PASSWORD, rememberPassword);
}

public void clearPassword() {
internalPrefs.remove(SHARELATEX_PASSWORD);
}

public void clear() throws BackingStoreException {
internalPrefs.clear();
}

private Optional<String> getOptionalValue(String key) {
return Optional.ofNullable(internalPrefs.get(key, null));
}

public static void clearAll() throws BackingStoreException {
Preferences.userNodeForPackage(JabRefMain.class).parent().node(PARENT_NODE).clear();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jabref.shared;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -123,7 +122,7 @@ private void setFromPreferences(SharedDatabasePreferences prefs) {
if (prefs.getPassword().isPresent()) {
try {
this.password = new Password(prefs.getPassword().get().toCharArray(), prefs.getUser().get()).decrypt();
} catch (UnsupportedEncodingException | GeneralSecurityException e) {
} catch (GeneralSecurityException e) {
LOGGER.error("Could not decrypt password", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jabref.shared.prefs;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Optional;
import java.util.prefs.BackingStoreException;
Expand Down Expand Up @@ -127,7 +126,7 @@ public void putAllDBMSConnectionProperties(DBMSConnectionProperties properties)

try {
setPassword(new Password(properties.getPassword().toCharArray(), properties.getUser()).encrypt());
} catch (GeneralSecurityException | UnsupportedEncodingException e) {
} catch (GeneralSecurityException e) {
LOGGER.error("Could not store the password due to encryption problems.", e);
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/jabref/shared/security/Password.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jabref.shared.security;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
Expand Down Expand Up @@ -40,7 +39,7 @@ public Password(char[] phrase, String key) throws NoSuchAlgorithmException, NoSu
*
* @return Encrypted phrase/password
*/
public String encrypt() throws GeneralSecurityException, UnsupportedEncodingException {
public String encrypt() throws GeneralSecurityException {
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
return new String(Base64.getEncoder().encode(cipher.doFinal(phrase)), StandardCharsets.UTF_8);
}
Expand All @@ -50,7 +49,7 @@ public String encrypt() throws GeneralSecurityException, UnsupportedEncodingExce
*
* @return Decrypted phrase/password
*/
public String decrypt() throws GeneralSecurityException, UnsupportedEncodingException {
public String decrypt() throws GeneralSecurityException {
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
return new String(cipher.doFinal(Base64.getDecoder().decode(phrase)), StandardCharsets.UTF_8);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<DialogPane xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.jabref.gui.sharelatex.ShareLatexLoginDialogController">
<DialogPane prefHeight="220.0" prefWidth="362.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.jabref.gui.sharelatex.ShareLatexLoginDialogController">
<content>
<GridPane alignment="CENTER" hgap="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="140.0" prefWidth="270.0" vgap="10.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="1.7976931348623157E308" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Server Address" />
<TextField id="tbAddress" fx:id="tbAddress" GridPane.columnIndex="1" />
<TextField id="tbUsername" fx:id="tbUsername" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label alignment="CENTER_RIGHT" text="%Username" GridPane.rowIndex="1" />
<Label text="%Password" GridPane.rowIndex="2" />
<PasswordField id="tbPassword" fx:id="pfPassword" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<ButtonBar prefHeight="40.0" prefWidth="200.0" GridPane.columnSpan="2" GridPane.rowIndex="3">
<BorderPane prefHeight="200.0" prefWidth="402.0">
<center>
<GridPane alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="140.0" prefWidth="270.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="1.7976931348623157E308" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Server Address" />
<TextField id="tbAddress" fx:id="tbAddress" prefHeight="25.0" prefWidth="174.0" text="https://www.sharelatex.com" GridPane.columnIndex="1" />
<TextField id="tbUsername" fx:id="tbUsername" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label alignment="CENTER_RIGHT" text="%Username" GridPane.rowIndex="1" />
<Label text="%Password" GridPane.rowIndex="2" />
<PasswordField id="tbPassword" fx:id="pfPassword" GridPane.columnIndex="1" GridPane.rowIndex="2" />

</children>
</GridPane>
</center>
<bottom>
<ButtonBar prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<buttons>
<Button id="btnCancel" cancelButton="true" mnemonicParsing="false" onAction="#closeDialog" text="%Cancel" />
<Button id="btnLogin" mnemonicParsing="false" onAction="#signIn" text="Login" />
<Button id="btnLogin" mnemonicParsing="false" onAction="#signIn" text="Login" ButtonBar.buttonData="OK_DONE" />
<Button id="btnCancel" cancelButton="true" mnemonicParsing="false" onAction="#closeDialog" text="%Cancel" ButtonBar.buttonData="CANCEL_CLOSE" />
</buttons>
</ButtonBar>

</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</GridPane>
</bottom>
</BorderPane>
</content>
</DialogPane>
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.BorderPane?>

<DialogPane xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.jabref.gui.sharelatex.ShareLatexProjectDialogController">
<DialogPane prefHeight="494.0" prefWidth="397.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.jabref.gui.sharelatex.ShareLatexProjectDialogController">
<content>
<VBox>
<children>
<TableView fx:id="tblProjects">
<BorderPane>
<bottom>
<ButtonBar minWidth="-Infinity" BorderPane.alignment="CENTER">
<buttons>
<Button fx:id="btnSync" defaultButton="true" mnemonicParsing="false" onAction="#synchronizeLibrary" text="Synchronize Library" />
<Button fx:id="btnCancel" cancelButton="true" mnemonicParsing="false" onAction="#cancelAndClose" text="Cancel" ButtonBar.buttonData="CANCEL_CLOSE" />
</buttons>
</ButtonBar>
</bottom>
<top>
<TableView fx:id="tblProjects" BorderPane.alignment="CENTER">
<columns>
<TableColumn fx:id="colActive" prefWidth="75.0" text="Active" />
<TableColumn fx:id="colTitle" prefWidth="75.0" text="Title" />
Expand All @@ -20,13 +28,7 @@
<TableColumn fx:id="colLastModified" prefWidth="75.0" text="Last modified" />
</columns>
</TableView>
<ButtonBar minWidth="-Infinity">
<buttons>
<Button fx:id="btnSync" defaultButton="true" mnemonicParsing="false" onAction="#synchronizeLibrary" text="Synchronize Library" />
<Button fx:id="btnCancel" cancelButton="true" mnemonicParsing="false" onAction="#cancelAndClose" text="Cancel" />
</buttons>
</ButtonBar>
</children>
</VBox>
</top>
</BorderPane>
</content>
</DialogPane>
Loading

0 comments on commit 74ce5b9

Please sign in to comment.