Commit a694133d authored by Olaf Rosendahl's avatar Olaf Rosendahl 💻
Browse files

BEAUTIFY!

parent c25079ee
......@@ -12,55 +12,55 @@ import javafx.scene.layout.HBox;
*/
public class AlbumContainer {
private final Album ALBUM;
private CheckBox checkBox;
private Button albumButton;
private HBox albumContainerHBox;
private final Album ALBUM;
private CheckBox checkBox;
private Button albumButton;
private HBox albumContainerHBox;
/**
* Constructor that takes a album object and initializes the album container
*
* @param album a album object
*/
public AlbumContainer(Album album) {
this.ALBUM = album;
setupAlbumContainer(album);
}
/**
* Constructor that takes a album object and initializes the album container
*
* @param album a album object
*/
public AlbumContainer(Album album) {
this.ALBUM = album;
setupAlbumContainer(album);
}
public Album getALBUM() {
return ALBUM;
}
public Album getALBUM() {
return ALBUM;
}
public Button getAlbumButton() {
return albumButton;
}
public Button getAlbumButton() {
return albumButton;
}
public HBox getAlbumContainerHBox() {
return albumContainerHBox;
}
public HBox getAlbumContainerHBox() {
return albumContainerHBox;
}
public CheckBox getCheckBox() {
return checkBox;
}
public CheckBox getCheckBox() {
return checkBox;
}
public Button getPhotoButton() {
return albumButton;
}
public Button getPhotoButton() {
return albumButton;
}
/**
* Makes a album container that is a button that contains an album title and a checkbox
*
* @param album a album object
*/
private void setupAlbumContainer(Album album) {
albumButton = new Button(album.getName());
checkBox = new CheckBox();
checkBox.getStyleClass().add("check-box");
albumContainerHBox = new HBox(albumButton, checkBox);
albumContainerHBox.getStylesheets().add("file:src/main/App/Css/CheckBoxStyle.css");
albumContainerHBox.setSpacing(10);
albumContainerHBox.setAlignment(Pos.CENTER_LEFT);
Css.setContainer(albumButton, albumContainerHBox, 80,160);
albumButton.setStyle("-fx-font-size: 28");
}
/**
* Makes a album container that is a button that contains an album title and a checkbox
*
* @param album a album object
*/
private void setupAlbumContainer(Album album) {
albumButton = new Button(album.getName());
checkBox = new CheckBox();
checkBox.getStyleClass().add("check-box");
albumContainerHBox = new HBox(albumButton, checkBox);
albumContainerHBox.getStylesheets().add("file:src/main/App/Css/CheckBoxStyle.css");
albumContainerHBox.setSpacing(10);
albumContainerHBox.setAlignment(Pos.CENTER_LEFT);
Css.setContainer(albumButton, albumContainerHBox, 80, 160);
albumButton.setStyle("-fx-font-size: 28");
}
}
package Components;
import Database.Hibernate;
import Roots.LoginRoot;
import Main.ApplicationManager;
import java.util.logging.Level;
import Roots.LoginRoot;
import javax.persistence.PersistenceException;
import java.util.logging.Level;
/**
* Authentication class for authenticating username and password for each user
*/
public final class Authentication {
/**
* Private constructor to hinder creation of utility class
*/
private Authentication() {
throw new IllegalStateException("Can not make instance of utility class");
}
/**
* Private constructor to hinder creation of utility class
*/
private Authentication() {
throw new IllegalStateException("Can not make instance of utility class");
}
/**
* Register a new user
* It will encrypt the password before inserting into database
*
* @param username the username
* @param password the password
* @return if method was successful
*/
public static boolean register(String username, String password) {
try {
// Encrypt password
String encrypter = Encrypter.encrypt(password, null);
// Get salt and hash
String hash = Encrypter.getHash(encrypter);
String salt = Encrypter.getSalt(encrypter);
/**
* Register a new user
* It will encrypt the password before inserting into database
*
* @param username the username
* @param password the password
* @return if method was successful
*/
public static boolean register(String username, String password) {
try {
// Encrypt password
String encrypter = Encrypter.encrypt(password, null);
// Get salt and hash
String hash = Encrypter.getHash(encrypter);
String salt = Encrypter.getSalt(encrypter);
return Hibernate.registerUser(username, hash, salt);
} catch (ExceptionInInitializerError | NoClassDefFoundError e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
throw e;
} catch (IllegalArgumentException e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
return false;
}
}
return Hibernate.registerUser(username, hash, salt);
} catch (ExceptionInInitializerError | NoClassDefFoundError e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
throw e;
} catch (IllegalArgumentException e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
return false;
}
}
/**
* LogIn method is used when the user is trying to log in
* will compare password from db to the entered password
*
* @param username of user
* @param password of user
* @return if login was successful
*/
public static boolean logIn(String username, String password) {
try {
// Getting salt from db using username
String salt = Hibernate.getSalt(username);
// Generating hash using salt
String encrypter = Encrypter.encrypt(password, salt);
String hash = Encrypter.getHash(encrypter);
/**
* LogIn method is used when the user is trying to log in
* will compare password from db to the entered password
*
* @param username of user
* @param password of user
* @return if login was successful
*/
public static boolean logIn(String username, String password) {
try {
// Getting salt from db using username
String salt = Hibernate.getSalt(username);
// Generating hash using salt
String encrypter = Encrypter.encrypt(password, salt);
String hash = Encrypter.getHash(encrypter);
// Try to login
if (Hibernate.login(username, hash)) {
UserInfo.initializeUser(Hibernate.getUser(username));
return true;
} else {
return false;
}
} catch (ExceptionInInitializerError | NoClassDefFoundError | PersistenceException e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
throw e;
}
}
// Try to login
if (Hibernate.login(username, hash)) {
UserInfo.initializeUser(Hibernate.getUser(username));
return true;
} else {
return false;
}
} catch (ExceptionInInitializerError | NoClassDefFoundError | PersistenceException e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
throw e;
}
}
/**
* Checks if a user with a username and password is the current user logged in
*
* @param username of user
* @param password of user
* @return if this user is the current user
*/
public static boolean isCurrentUser(String username, String password){
try {
// Getting salt from db using username
String salt = Hibernate.getSalt(username);
// Generating hash using salt
String encrypter = Encrypter.encrypt(password, salt);
String hash = Encrypter.getHash(encrypter);
/**
* Checks if a user with a username and password is the current user logged in
*
* @param username of user
* @param password of user
* @return if this user is the current user
*/
public static boolean isCurrentUser(String username, String password) {
try {
// Getting salt from db using username
String salt = Hibernate.getSalt(username);
// Generating hash using salt
String encrypter = Encrypter.encrypt(password, salt);
String hash = Encrypter.getHash(encrypter);
// Checks if the current user's hash is equal to the hash from the password entered
return UserInfo.getUser().getHash().equals(hash);
} catch (ExceptionInInitializerError | NoClassDefFoundError | PersistenceException e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
throw e;
}
}
// Checks if the current user's hash is equal to the hash from the password entered
return UserInfo.getUser().getHash().equals(hash);
} catch (ExceptionInInitializerError | NoClassDefFoundError | PersistenceException e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
throw e;
}
}
/**
* Deletes the current logged in user
* will compare password from db to the entered password
*
* @param username of user
* @param password of user
* @param confirmation if the user confirmed to delete the user
* @return if deleting the user was successful
*/
public static boolean deleteUser(String username, String password, boolean confirmation){
try {
// Checks if this is the current user and the user has given consent
if (isCurrentUser(username, password) && confirmation) {
Hibernate.deleteUser(username);
return true;
} else {
return false;
}
} catch (ExceptionInInitializerError | NoClassDefFoundError | PersistenceException e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
throw e;
}
}
/**
* Deletes the current logged in user
* will compare password from db to the entered password
*
* @param username of user
* @param password of user
* @param confirmation if the user confirmed to delete the user
* @return if deleting the user was successful
*/
public static boolean deleteUser(String username, String password, boolean confirmation) {
try {
// Checks if this is the current user and the user has given consent
if (isCurrentUser(username, password) && confirmation) {
Hibernate.deleteUser(username);
return true;
} else {
return false;
}
} catch (ExceptionInInitializerError | NoClassDefFoundError | PersistenceException e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
throw e;
}
}
/**
* Log out the user and redirect to login scene
*
* @return loginRoot the root the user is sent to
*/
public static LoginRoot logout() {
UserInfo.logOut();
Hibernate.getEm().clear();
LoginRoot loginRoot = new LoginRoot();
ApplicationManager.setRoot(loginRoot);
// Returning loginRoot allows adding a message when logging out the user
return loginRoot;
}
/**
* Log out the user and redirect to login scene
*
* @return loginRoot the root the user is sent to
*/
public static LoginRoot logout() {
UserInfo.logOut();
Hibernate.getEm().clear();
LoginRoot loginRoot = new LoginRoot();
ApplicationManager.setRoot(loginRoot);
// Returning loginRoot allows adding a message when logging out the user
return loginRoot;
}
}
......@@ -9,14 +9,14 @@ import javafx.scene.control.Button;
*/
public final class ConfirmationBox {
private static boolean answer;
private static boolean answer;
/**
* Private constructor to hinder the creation of the utility class
*/
private ConfirmationBox() {
throw new IllegalStateException("Utility class");
}
/**
* Private constructor to hinder the creation of the utility class
*/
private ConfirmationBox() {
throw new IllegalStateException("Utility class");
}
/**
* Creates the confirmation box and displays it
......@@ -32,7 +32,7 @@ public final class ConfirmationBox {
if (width < 200) width = 200;
if (height < 100) height = 100;
PopUpWindow dialogWindow = new PopUpWindow(ApplicationManager.getStage(), width, height);
PopUpWindow dialogWindow = new PopUpWindow(ApplicationManager.getStage(), width, height);
dialogWindow.getDialogWindow().close();
Button yesButton = new Button("Yes");
......@@ -47,14 +47,14 @@ public final class ConfirmationBox {
dialogWindow.getDialogWindow().close();
});
Css.setButton(width - 100, 30, 15, yesButton, noButton);
Css.setButton(width - 100, 30, 15, yesButton, noButton);
dialogWindow.getDialogWindow().setTitle(title);
dialogWindow.getDialogText().setText(message);
dialogWindow.getDialogVBox().getChildren().addAll(yesButton, noButton);
dialogWindow.getDialogWindow().setTitle(title);
dialogWindow.getDialogText().setText(message);
dialogWindow.getDialogVBox().getChildren().addAll(yesButton, noButton);
dialogWindow.getDialogWindow().showAndWait();
dialogWindow.getDialogWindow().showAndWait();
return answer;
}
return answer;
}
}
......@@ -12,109 +12,109 @@ import java.util.logging.Level;
public final class Encrypter {
/**
* Private constructor to hinder creation of utility class
*/
private Encrypter() {
throw new IllegalStateException("Can not make instance of utility class");
}
/**
* Private constructor to hinder creation of utility class
*/
private Encrypter() {
throw new IllegalStateException("Can not make instance of utility class");
}
/**
* Encryption used for the password, method is used for both making and verifying an encrypted password.
*
* @param password String password that the user har entered
* @param saltest String hex values for the salt
* @return String combination of both "hash|salt", uses splitters to get either
*/
/**
* Encryption used for the password, method is used for both making and verifying an encrypted password.
*
* @param password String password that the user har entered
* @param saltest String hex values for the salt
* @return String combination of both "hash|salt", uses splitters to get either
*/
public static String encrypt(String password, String saltest) {
if (password == null || password.trim().isEmpty()) {
throw new IllegalArgumentException("Password cannot be null");
}
try {
// Select the message digest for the hash computation -> SHA-256
MessageDigest md = MessageDigest.getInstance("SHA-256");
// Generate the random salt
byte[] salt;
if (saltest == null) {
SecureRandom random = new SecureRandom();
salt = new byte[16];
random.nextBytes(salt);
} else {
salt = buildBytes(saltest);
}
// Passing the salt to the digest for the computation
md.update(salt);
String salted = buildHexString(salt);
// Generate the salted hash
byte[] hashedPassword = md.digest(password.getBytes(StandardCharsets.UTF_8));
//Hashed password
String hashed = buildHexString(hashedPassword);
return hashed + "|" + salted;
} catch (NoSuchAlgorithmException e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
}
return null;
}
public static String encrypt(String password, String saltest) {
if (password == null || password.trim().isEmpty()) {
throw new IllegalArgumentException("Password cannot be null");
}
try {
// Select the message digest for the hash computation -> SHA-256
MessageDigest md = MessageDigest.getInstance("SHA-256");
// Generate the random salt
byte[] salt;
if (saltest == null) {
SecureRandom random = new SecureRandom();
salt = new byte[16];
random.nextBytes(salt);
} else {
salt = buildBytes(saltest);
}
// Passing the salt to the digest for the computation
md.update(salt);
String salted = buildHexString(salt);
// Generate the salted hash
byte[] hashedPassword = md.digest(password.getBytes(StandardCharsets.UTF_8));
//Hashed password
String hashed = buildHexString(hashedPassword);
return hashed + "|" + salted;
} catch (NoSuchAlgorithmException e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
}
return null;
}
/**
* Byte-to-Hex converter
*
* @param bytes is an array of byte
* @return String bytes in hex
*/
private static String buildHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
// Convert from byte to hex
sb.append(String.format("%02x", b));
}
return sb.toString();
}
/**
* Byte-to-Hex converter
*
* @param bytes is an array of byte
* @return String bytes in hex
*/
private static String buildHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
// Convert from byte to hex
sb.append(String.format("%02x", b));
}
return sb.toString();
}
/**
* Hex-to-byte converter
*
* @param hex input to get an byte array
* @return byte[] converted from hex
*/
private static byte[] buildBytes(String hex) {
byte[] b = new byte[hex.length() / 2];
for (int i = 0; i < hex.length(); i += 2) {
int v = Integer.parseInt(hex.substring(i, i + 2), 16);
b[i / 2] = (byte) v;
}
return b;
}
/**
* Hex-to-byte converter
*
* @param hex input to get an byte array
* @return byte[] converted from hex
*/
private static byte[] buildBytes(String hex) {
byte[] b = new byte[hex.length() / 2];
for (int i = 0; i < hex.length(); i += 2) {
int v = Integer.parseInt(hex.substring(i, i + 2), 16);
b[i / 2] = (byte) v;
}
return b;
}
/**
* Used to get the hash string from the encrypt function
*
* @param s the output from encrypt
* @return String hash
*/
// Split string from the encryption to Hash using the splitter
public static String getHash(String s) {
int index = s.indexOf('|');
if (index < 0) {
return null;
}
return s.substring(0, index);
}
/**
* Used to get the hash string from the encrypt function
*
* @param s the output from encrypt
* @return String hash
*/