Skip to content

Commit

Permalink
feat(login): improve new login mechanic
Browse files Browse the repository at this point in the history
  • Loading branch information
teebow1e committed Jun 15, 2024
1 parent 568a596 commit 83b2c32
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 36 deletions.
8 changes: 8 additions & 0 deletions .config/accounts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"users": [
{
"username": "a",
"password_hash": "$2a$10$t.g7znEK3FTRtqNZSHb4c.D.IdnUm8SBobTV/xjq6uvs7iB7qng/m"
}
]
}
5 changes: 5 additions & 0 deletions .config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"DEFAULT_APACHE_LOG_LOCATION": "",
"DEFAULT_MODSECURITY_LOG_LOCATION": "",
"GEOLITE_DB_LOCATION": ""
}
11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
<artifactId>exec-maven-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<mainClass>ui.Main</mainClass>
<mainClass>entrypoint.Main</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>ui.Main</mainClass>
<mainClass>entrypoint.Main</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>ui.Main</mainClass>
<mainClass>entrypoint.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
Expand Down Expand Up @@ -136,5 +136,10 @@
<artifactId>maxmind-db</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>0.4</version>
</dependency>
</dependencies>
</project>
4 changes: 2 additions & 2 deletions src/main/java/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public void init(Stage stage) {
// CONSTANT VALUE HERE
String dbFilePath = System.getProperty("user.dir")
+ File.separator
+ "credentials"
+ ".config"
+ File.separator
+ "cred.txt";
+ "accounts.json";
File dbFile = new File(dbFilePath);
if (dbFile.exists()) {
userLists = UserManagement.readUserFile(dbFilePath);
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/entrypoint/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package entrypoint;

import javafx.application.Application;
import ui.LoginForm;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main {

private static final String configDirPath =
System.getProperty("user.dir") + File.separator + ".config";
private static final String accountsFilePath =
configDirPath + File.separator + "accounts.json";
private static final String configFilePath =
configDirPath + File.separator + "config.json";
public static void main(String[] args) {
File configDir = new File(configDirPath);
File accountsFile = new File(accountsFilePath);
File configFile = new File(configFilePath);
if (!configDir.exists()) {
configDir.mkdir();
}
Application.launch(LoginForm.class, args);
}

// private static void createFileIfNotExists(String filePath) {
// File file = new File(filePath);
// if (!file.exists()) {
// try {
// Files.createFile(Paths.get(filePath));
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
}
8 changes: 0 additions & 8 deletions src/main/java/ui/Main.java

This file was deleted.

15 changes: 5 additions & 10 deletions src/main/java/usermanagement/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@

public class User {
private final String username;
private final String password;
private final String role;
private final String passwordHash;

public User(String username, String password, String role) {
public User(String username, String passwordHash) {
this.username = username;
this.password = password;
this.role = role;
this.passwordHash = passwordHash;
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
public String getPasswordHash() {
return passwordHash;
}

public String getRole() {
return role;
}
}
42 changes: 29 additions & 13 deletions src/main/java/usermanagement/UserManagement.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package usermanagement;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.mindrot.jbcrypt.BCrypt;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
Expand All @@ -15,15 +17,15 @@ private UserManagement() {
}
public static List<User> readUserFile(String fileName) {
List<User> userList = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3) {
String username = parts[0];
String password = parts[1];
String role = parts[2];
userList.add(new User(username, password, role));
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode rootNode = objectMapper.readTree(new File(fileName));
JsonNode usersNode = rootNode.path("users");
if (usersNode.isArray()) {
for (JsonNode userNode : usersNode) {
String username = userNode.path("username").asText();
String passwordHash = userNode.path("password_hash").asText();
userList.add(new User(username, passwordHash));
}
}
} catch (IOException e) {
Expand All @@ -34,10 +36,24 @@ public static List<User> readUserFile(String fileName) {

public static boolean authenticateUser(List<User> userList, String username, String password) {
for (User user : userList) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
if (user.getUsername().equals(username) && BCrypt.checkpw(password, user.getPasswordHash())) {
return true;
}
}
return false;
}

public static void addUser(String fileName, User user) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true))) {
String hashedPassword = BCrypt.hashpw(user.getPasswordHash(), BCrypt.gensalt());
writer.write(user.getUsername() + "," + hashedPassword + "\n");
} catch (IOException e) {
logger.log(Level.SEVERE, "An error occurred while writing to the user file", e);
}
}

public static void main(String[] args) {
System.out.println(BCrypt.hashpw("a", BCrypt.gensalt()));
System.out.println(BCrypt.checkpw("a", "$2a$10$t.g7znEK3FTRtqNZSHb4c.D.IdnUm8SBobTV/xjq6uvs7iB7qng/m"));
}
}

0 comments on commit 83b2c32

Please sign in to comment.