Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

Commit

Permalink
Code refactoring to save Plain Password in XML for email notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Fadi Asbih committed Mar 22, 2016
1 parent e0287d3 commit 797eda1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 23 deletions.
5 changes: 5 additions & 0 deletions src/de/unihannover/elsa/iui/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ public void parseCSV(File file) throws IOException, NoSuchAlgorithmException {
// user.setLogin(nextLine[22]); // The Login is same as the M-nr.

user.setPassword(new Password(PasswordUtility.randomString(5)));
user.setPlainPassword(new Password("PLAIN",user.getPassword().getPlainPassword()));

if(getEmailIndex() > 0) {
user.setEmail(nextLine[getEmailIndex()]);
Expand Down Expand Up @@ -694,6 +695,8 @@ public void parseExcel97(File file) throws IOException, NoSuchAlgorithmException
}

user.setPassword(new Password(PasswordUtility.randomString(5)));
user.setPlainPassword(new Password("PLAIN",user.getPassword().getPlainPassword()));


user.setMatriculation(mnr);
userData.add(user);
Expand Down Expand Up @@ -766,6 +769,8 @@ public void parseExcel(File file) throws IOException, NoSuchAlgorithmException {
}

user.setPassword(new Password(PasswordUtility.randomString(5)));
user.setPlainPassword(new Password("PLAIN",user.getPassword().getPlainPassword()));

user.setMatriculation(mnr);
userData.add(user);

Expand Down
44 changes: 26 additions & 18 deletions src/de/unihannover/elsa/iui/model/Password.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
public class Password {

private String type;
private String passwordToHash;
private String MD5Value;
private String plainPassword;
private String password;


public Password() {
Expand All @@ -41,8 +41,14 @@ public Password() {

public Password(String value) throws NoSuchAlgorithmException {
setType("ILIAS3");
setPasswordToHash(value);
setMD5Value(value);
setPlainPassword(value);
setPassword(value);
}

public Password(String type, String value) throws NoSuchAlgorithmException {
setType(type);
setPlainPassword(value);
setPassword(value);
}
/**
* @return the type
Expand All @@ -64,19 +70,22 @@ public void setType(String type) {
*/
@XmlValue
public String getValue() throws NoSuchAlgorithmException {
setMD5Value(this.getPasswordToHash()); // needed when reloading a xml file.
return this.MD5Value;
setPassword(this.getPlainPassword()); // needed when reloading a xml file.
if(this.getType() == "ILIAS3")
return this.password;
else
return this.plainPassword;
}

/**
* @param passwordToHash
* @param plainPassword
* @throws NoSuchAlgorithmException
*/
public void setMD5Value(String passwordToHash) throws NoSuchAlgorithmException {
public void setPassword(String plainPassword) throws NoSuchAlgorithmException {
// Create MessageDigest instance for MD5
MessageDigest md = MessageDigest.getInstance("MD5");
//Add password bytes to digest
md.update(passwordToHash.getBytes());
md.update(plainPassword.getBytes());
//Get the hash's bytes
byte[] bytes = md.digest();
//This bytes[] has bytes in decimal format;
Expand All @@ -87,22 +96,21 @@ public void setMD5Value(String passwordToHash) throws NoSuchAlgorithmException {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
//Get complete hashed password in hex format
this.MD5Value = sb.toString();
this.password = sb.toString();
}

/**
* @return the passwordToHash
* @return the plainPassword
*/
@XmlAttribute(name="PasswordToHash")
public String getPasswordToHash() {
return passwordToHash;
@XmlAttribute(name="plainPassword")
public String getPlainPassword() {
return plainPassword;
}

/**
* @param passwordToHash the passwordToHash to set
* @param plainPassword the plainPassword to set
*/
public void setPasswordToHash(String passwordToHash) {
this.passwordToHash = passwordToHash;
public void setPlainPassword(String plainPassword) {
this.plainPassword = plainPassword;
}

}
12 changes: 12 additions & 0 deletions src/de/unihannover/elsa/iui/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class User {
private Role localRole;
private StringProperty login;
private Password password;
private Password plainPassword;
private StringProperty firstName;
private StringProperty lastName;
private StringProperty email;
Expand Down Expand Up @@ -63,6 +64,8 @@ public User(String firstName, String lastName) {
this.gender = new SimpleStringProperty("f");
this.login = new SimpleStringProperty("");
this.password = new Password();
this.plainPassword = new Password();
plainPassword.setType("PLAIN");
this.email = new SimpleStringProperty("[email protected]");
this.matriculation = new SimpleStringProperty("");
this.timeLimitUnlimited = "1";
Expand Down Expand Up @@ -282,5 +285,14 @@ public String getAction() {
public String getLanguage() {
return "de";
}

@XmlElement(name = "Password")
public Password getPlainPassword() {
return plainPassword;
}

public void setPlainPassword(Password plainPassword) {
this.plainPassword = plainPassword;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void getSavedSettings() {
if(!mainApp.getUserData().isEmpty()) {
System.out.println(mainApp.getUserData().get(0).getLocalRole().getValue());
localRoleField.setText(mainApp.getUserData().get(0).getLocalRole().getValue());
passwordField.setText(mainApp.getUserData().get(0).getPassword().getPasswordToHash());
passwordField.setText(mainApp.getUserData().get(0).getPassword().getPlainPassword());
timeLimitFromField.setText(mainApp.getUserData().get(0).getTimeLimitFrom());
timeLimitUntilField.setText(mainApp.getUserData().get(0).getTimeLimitUntil());
// limitedButton.setSelected(!Boolean.parseBoolean(mainApp.getUserData().get(0).getTimeLimitUnlimited()));
Expand Down Expand Up @@ -143,6 +143,7 @@ private void handleGenerate() throws NoSuchAlgorithmException {
dummyAccount.setMatriculation("123456");
dummyAccount.setLogin(loginPrefixField.getText()+"_"+i);
dummyAccount.setPassword(new Password(passwordField.getText()));
dummyAccount.setPlainPassword(new Password("PLAIN", passwordField.getText()));

if(limitedButton.isSelected()) {
dummyAccount.setTimeLimitUnlimited("0"); // Time Limit is activated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void getSavedSettings() {
if(!mainApp.getUserData().isEmpty()) {
System.out.println(mainApp.getUserData().get(0).getLocalRole().getValue());
localRoleField.setText(mainApp.getUserData().get(0).getLocalRole().getValue());
passwordField.setText(mainApp.getUserData().get(0).getPassword().getPasswordToHash());
passwordField.setText(mainApp.getUserData().get(0).getPassword().getPlainPassword());
timeLimitFromField.setText(mainApp.getUserData().get(0).getTimeLimitFrom());
timeLimitUntilField.setText(mainApp.getUserData().get(0).getTimeLimitUntil());
// limitedButton.setSelected(!Boolean.parseBoolean(mainApp.getUserData().get(0).getTimeLimitUnlimited()));
Expand All @@ -137,9 +137,13 @@ private void handleOk() throws NoSuchAlgorithmException {
if(generatePassword.isSelected()) {
System.out.println("Password is auto generated. Check the method parseExcel.");
user.setPassword(new Password(PasswordUtility.randomString(5)));
user.setPlainPassword(new Password("PLAIN",user.getPassword().getPlainPassword()));

}
else {
user.setPassword(new Password(passwordField.getText()));
user.setPlainPassword(new Password("PLAIN",user.getPassword().getPlainPassword()));

}

if(limitedButton.isSelected()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void setUser(User user) {
globalRoleField.setText(user.getGlobalRole().getValue());
localRoleField.setText(user.getLocalRole().getValue());
loginField.setText(user.getLogin());
passwordField.setText(user.getPassword().getPasswordToHash());
passwordField.setText(user.getPassword().getPlainPassword());
firstNameField.setText(user.getFirstName());
lastNameField.setText(user.getLastName());
emailField.setText(user.getEmail());
Expand Down Expand Up @@ -109,6 +109,7 @@ private void handleOk() throws NoSuchAlgorithmException {
user.getLocalRole().setId(localRoleField.getText());
user.setLogin(loginField.getText());
user.setPassword(new Password(passwordField.getText()));
user.setPlainPassword(new Password("PLAIN",user.getPassword().getPlainPassword()));
user.setFirstName(firstNameField.getText());
user.setLastName(lastNameField.getText());
user.setEmail(emailField.getText());
Expand Down
4 changes: 2 additions & 2 deletions src/de/unihannover/elsa/iui/view/UserOverviewController.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void showPersonDetails(User user) {
localRoleLabel.setText(user.getLocalRole().getValue());
System.out.println("Local: " + user.getLocalRole().getValue());
loginLabel.setText(user.getLogin());
passwordLabel.setText(user.getPassword().getPasswordToHash());
passwordLabel.setText(user.getPassword().getPlainPassword());
// System.out.println(user.getLogin());
firstNameLabel.setText(user.getFirstName());
lastNameLabel.setText(user.getLastName());
Expand Down Expand Up @@ -118,7 +118,7 @@ private void initialize() {
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
loginColumn.setCellValueFactory(cellData -> cellData.getValue().LoginProperty());
passwordColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getPassword()
.getPasswordToHash()));
.getPlainPassword()));

// Clear person details.
showPersonDetails(null);
Expand Down

0 comments on commit 797eda1

Please sign in to comment.