Skip to content

Commit 11e65ad

Browse files
committed
chore: change app into Maven project
1 parent 5c6e78f commit 11e65ad

File tree

6 files changed

+84
-113
lines changed

6 files changed

+84
-113
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.configuration.updateBuildConfiguration": "automatic"
3+
}

run.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mvn compile
2+
mvn exec:java -Dexec.mainClass=com.kub.App
Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.hktransfield;
22
import java.io.Console;
3+
import java.util.Arrays;
34
import java.util.Scanner;
45

56

@@ -13,8 +14,6 @@ public class AuthenticationSystem {
1314
*
1415
*/
1516

16-
17-
1817
/**
1918
* The main menu of the program. Here, users can choose
2019
* to either login, create a new account, or exit the
@@ -28,14 +27,14 @@ public static void startupMenu() {
2827
"3. Exit"
2928
};
3029

31-
AppUtils.clearConsole();
32-
AppUtils.println(AppUtils.ANSI_YELLOW + "***********************" + AppUtils.ANSI_RESET);
33-
AppUtils.println(AppUtils.ANSI_YELLOW + "Password Authentication System" + AppUtils.ANSI_RESET);
34-
AppUtils.println(AppUtils.ANSI_YELLOW + "By Harmon Transfield" + AppUtils.ANSI_RESET);
35-
AppUtils.println(AppUtils.ANSI_YELLOW + "***********************" + AppUtils.ANSI_RESET);
36-
AppUtils.println("\n");
37-
AppUtils.printMenu(options);
38-
AppUtils.print("Choose an option (1/2/3): ");
30+
PrintUtils.clearConsole();
31+
PrintUtils.println(PrintUtils.ANSI_YELLOW + "***********************" + PrintUtils.ANSI_RESET);
32+
PrintUtils.println(PrintUtils.ANSI_YELLOW + "Password Authentication System" + PrintUtils.ANSI_RESET);
33+
PrintUtils.println(PrintUtils.ANSI_YELLOW + "By Harmon Transfield" + PrintUtils.ANSI_RESET);
34+
PrintUtils.println(PrintUtils.ANSI_YELLOW + "***********************" + PrintUtils.ANSI_RESET);
35+
PrintUtils.println("\n");
36+
PrintUtils.printMenu(options);
37+
PrintUtils.print("Choose an option (1/2/3): ");
3938

4039
Scanner s = new Scanner(System.in);
4140
boolean keepRunning = true;
@@ -44,104 +43,97 @@ public static void startupMenu() {
4443
option = s.nextInt();
4544
switch (option) {
4645
case 1: // Sign in
47-
AppUtils.print("You've selected: " + option);
46+
PrintUtils.print("You've selected: " + option);
4847
break;
4948
case 2: // Registration
50-
AppUtils.print("You've selected: " + option);
51-
RegistrationMenu();
49+
PrintUtils.print("You've selected: " + option);
50+
registrationMenu();
5251
break;
5352
case 3: // Exit the application
54-
AppUtils.print("You've selected: " + option);
53+
PrintUtils.print("You've selected: " + option);
5554
keepRunning = false;
5655
break;
5756
default:
5857
System.out.println();
59-
AppUtils.println("Please enter a valid option! (1/2/3)");
58+
PrintUtils.println("Please enter a valid option! (1/2/3)");
6059
break;
6160
}
6261

6362
if(!keepRunning) break;
6463
} catch (Exception e) {
65-
AppUtils.println(AppUtils.ANSI_RED + "Something unexpected happened:" + AppUtils.ANSI_RESET);
64+
PrintUtils.println(PrintUtils.ANSI_RED + "Something unexpected happened:" + PrintUtils.ANSI_RESET);
6665
System.err.println(e);
6766
}
6867
}
6968

7069
s.close();
7170
}
7271

72+
/**
73+
* Opens a menu that allows users to login to an existing username and password.
74+
*/
75+
public static void loginMenu() {
76+
77+
}
78+
7379
/**
7480
* Opens a menu that allows users to create a new username and password.
7581
* The method will check that both username and password meet certain
7682
* criteria,
7783
*/
78-
public static void RegistrationMenu() {
79-
String username;
80-
char[] initPassword, reenteredPassword;
84+
public static void registrationMenu() {
85+
String username = "";
86+
char[] initPassword;
87+
char[] reenteredPassword = {};
8188
boolean usernameValid, passwordValid;
89+
UserDatabase udb;
8290
Console cnsl = System.console();
8391

84-
AppUtils.clearConsole();
85-
AppUtils.println(AppUtils.ANSI_BLUE + "***********************" + AppUtils.ANSI_RESET);
86-
AppUtils.println(AppUtils.ANSI_BLUE + "Registration new user" + AppUtils.ANSI_RESET);
87-
AppUtils.println(AppUtils.ANSI_BLUE + "***********************" + AppUtils.ANSI_RESET);
88-
AppUtils.println("\n");
92+
93+
PrintUtils.clearConsole();
94+
PrintUtils.println(PrintUtils.ANSI_BLUE + "***********************" + PrintUtils.ANSI_RESET);
95+
PrintUtils.println(PrintUtils.ANSI_BLUE + "Registration new user" + PrintUtils.ANSI_RESET);
96+
PrintUtils.println(PrintUtils.ANSI_BLUE + "***********************" + PrintUtils.ANSI_RESET);
97+
PrintUtils.println("\n");
8998

9099
try {
91100
// read username from console
92101
usernameValid = false;
93102

94103
if(cnsl == null) {
95-
AppUtils.println("No console available");
104+
PrintUtils.println("No console available");
96105
return;
97106
}
98107

99108

100109
while(!usernameValid) {
101110

102111
username = cnsl.readLine("Create your username: ");
103-
usernameValid = RegistrationForm.isUsernameValid(username);
112+
usernameValid = RegistrationUtils.isUsernameValid(username);
104113

105114
if(usernameValid)
106115
break;
107116
else
108-
AppUtils.println(AppUtils.ANSI_RED + "Please enter a valid username!" + AppUtils.ANSI_RESET);
117+
PrintUtils.println(PrintUtils.ANSI_RED + "Please enter a valid username!" + PrintUtils.ANSI_RESET);
109118
}
110119

111120
passwordValid = false;
112121

113122
while(!passwordValid) {
114123

115124
initPassword = cnsl.readPassword("Create your password: ");
116-
RegistrationForm.isPasswordValid(initPassword);
125+
reenteredPassword = cnsl.readPassword("Please re-enter your password: ");
126+
127+
if(Arrays.equals(initPassword, reenteredPassword))
128+
passwordValid = RegistrationUtils.isPasswordValid(reenteredPassword);
117129
}
118-
reenteredPassword = cnsl.readPassword("Please re-enter your password: ");
119130

131+
// SUCCESSFUL REGISTRATION
132+
udb = UserDatabase.getInstance();
133+
udb.registerUser(username, reenteredPassword);
120134
} catch (Exception e) {
121135
System.err.println(e);
122136
}
123137
}
124138
}
125139

126-
127-
class User {
128-
private String username;
129-
private String password;
130-
131-
public String getUsername() {
132-
return username;
133-
}
134-
135-
public String getPassword() {
136-
return password;
137-
}
138-
139-
public void setUsername(String un) {
140-
this.username = un;
141-
}
142-
143-
public void setPassword(String pwd) {
144-
this.password = pwd;
145-
}
146-
}
147-

src/main/java/com/hktransfield/AppUtils.java renamed to src/main/java/com/hktransfield/PrintUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.hktransfield;
22

3-
public class AppUtils {
3+
public class PrintUtils {
44
// https://stackoverflow.com/questions/5762491/how-to-print-color-in-console-using-system-out-println
55
public static final String ANSI_RESET = "\u001B[0m";
66
public static final String ANSI_BLACK = "\u001B[30m";

src/main/java/com/hktransfield/RegistrationForm.java renamed to src/main/java/com/hktransfield/RegistrationUtils.java

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/** Represents a Registration form
1111
*
1212
*/
13-
class RegistrationForm {
13+
public class RegistrationUtils {
1414
// declare constants
1515
public static final String REGEX = "^[A-Za-z][A-Za-z0-9_]{7,29}$"; // regex from https://laasyasettyblog.hashnode.dev/validating-username-using-regex
1616
private static final int MIN_PASSWORD_LENGTH = 8;
@@ -21,33 +21,33 @@ class RegistrationForm {
2121
* is valid.
2222
*
2323
* @param username The preliminary username to check
24-
* @return True if the username is valid
24+
* @return True if the username follows all rules
2525
*/
2626
public static boolean isUsernameValid(String username) {
2727
boolean charactersValid = false;
2828
boolean notSwearWord = false;
2929

30-
// ! 1. Case insensitive
30+
// * rule 1) Case insensitive
3131
username = username.toLowerCase();
3232

33-
//! 2. Should only use characters from set [a-zA-z0-9_]
33+
// * rule 2) Should only use characters from set [a-zA-z0-9_]
3434
charactersValid = username.matches(REGEX);
3535
if(!charactersValid)
36-
AppUtils.print(AppUtils.ANSI_RED + "!! Warning: " + AppUtils.ANSI_RESET + "Invalid characters in username, can only have letters [a-z, A-Z], numbers [0-9], and underscore [_].");
36+
PrintUtils.println(PrintUtils.ANSI_RED + "!! Warning: " + PrintUtils.ANSI_RESET + "Invalid characters in username, can only have letters [a-z, A-Z], numbers [0-9], and underscore [_].");
3737

3838

39-
// ! 3. No swear words
39+
// * rule 3-4) No swear words, nor should users bypass with leetspeak
4040
ProfanityFilter.initialise();
4141

42-
// ! 4. Should not be able to bypass rule 3 by substituting numbers for letters
4342
notSwearWord = ProfanityFilter.checkForSwears(username);
4443
if(!notSwearWord)
45-
AppUtils.print(AppUtils.ANSI_RED + "!! Warning: " + AppUtils.ANSI_RESET + " qualified as a bad word in a username");
44+
PrintUtils.println(PrintUtils.ANSI_RED + "!! Warning: " + PrintUtils.ANSI_RESET + " please do not include swear words in username.");
4645

4746
return charactersValid && notSwearWord; // if either are false, then the username is not valid
4847
}
4948

5049
// TODO: Implement the following NIST guidelines
50+
// TODO: Make sure that usernames do not match passwords
5151
/**
5252
* https://blog.netwrix.com/2022/11/14/nist-password-guidelines/
5353
* 8 < length < 64
@@ -61,39 +61,49 @@ public static boolean isPasswordValid(char[] pwd) throws IOException {
6161

6262
// validate the minimum length of the password
6363
if(pwd.length < MIN_PASSWORD_LENGTH) {
64-
AppUtils.println(AppUtils.ANSI_RED + "!! Warning: Password too short! Please create a password of at least 8 characters or more" + AppUtils.ANSI_RESET);
64+
PrintUtils.println(PrintUtils.ANSI_RED + "!! Warning: Password too short! Please create a password of at least 8 characters or more" + PrintUtils.ANSI_RESET);
6565
return false;
6666
}
6767

6868
// validate the maximum length of the password
6969
if(pwd.length > MAX_PASSWORD_LENGTH) {
70-
AppUtils.println(AppUtils.ANSI_RED + "!! Warning: Password too long!" + AppUtils.ANSI_RESET);
70+
PrintUtils.println(PrintUtils.ANSI_RED + "!! Warning: Password too long!" + PrintUtils.ANSI_RESET);
7171
return false;
7272
}
7373

7474
// check if password is a weak password
75-
List<String> listWeakPasswords = textFileToList("res/weakpasswords.txt");
75+
List<String> listWeakPasswords = textFileToList("weakpasswords.txt");
7676
for(String weakPassword : listWeakPasswords) {
7777
if(Arrays.equals(pwd, weakPassword.toCharArray())) {
78-
AppUtils.println(AppUtils.ANSI_RED + "!! Warning: Your password is considered too weak" + AppUtils.ANSI_RESET);
78+
PrintUtils.println(PrintUtils.ANSI_RED + "!! Warning: Your password is considered too weak" + PrintUtils.ANSI_RESET);
7979
return false;
8080
}
8181
}
8282

8383
// check if password is in a breached database
84-
List<String> listBreachedPasswords = textFileToList("res/breachedpasswords.txt");
84+
List<String> listBreachedPasswords = textFileToList("breachedpasswords.txt");
8585
for(String breachedPassword : listBreachedPasswords) {
8686
if(Arrays.equals(pwd, breachedPassword.toCharArray())) {
87-
AppUtils.println(AppUtils.ANSI_RED + "!! Warning: Your password was found in a database of breached passwords" + AppUtils.ANSI_RESET);
87+
PrintUtils.println(PrintUtils.ANSI_RED + "!! Warning: Your password was found in a database of breached passwords" + PrintUtils.ANSI_RESET);
8888
return false;
8989
}
9090
}
9191
return false;
9292
}
9393

94-
private static List<String> textFileToList(String filepath) throws IOException {
94+
/**
95+
* Converts a text file into a list of strings.
96+
*
97+
* @param filename
98+
* @return
99+
* @throws IOException
100+
*/
101+
private static List<String> textFileToList(String filename) throws IOException {
95102
List<String> listStrings = new ArrayList<String>();
96-
BufferedReader br = new BufferedReader(new FileReader(filepath));
103+
ClassLoader cl = Thread.currentThread().getContextClassLoader();
104+
InputStream fis = cl.getResourceAsStream(filename);
105+
InputStreamReader isr = new InputStreamReader(fis);
106+
BufferedReader br = new BufferedReader(isr);
97107
String line = br.readLine();
98108

99109
// checking for end of file
@@ -103,6 +113,8 @@ private static List<String> textFileToList(String filepath) throws IOException {
103113
}
104114

105115
// closing bufferreader object
116+
fis.close();
117+
isr.close();
106118
br.close();
107119

108120
return listStrings;
@@ -111,25 +123,26 @@ private static List<String> textFileToList(String filepath) throws IOException {
111123
/** Represents a profanity filter
112124
* https://gist.github.com/PimDeWitte/c04cc17bc5fa9d7e3aee6670d4105941
113125
*/
114-
protected static class ProfanityFilter {
126+
static class ProfanityFilter {
115127

116128
// declare class scope variables
117129
static Map<String, String[]> words = new HashMap<>();
118130
static int largestWordLength = 0;
119131

132+
/**
133+
* Loads in the CSV file containing all swears and obscenities.
134+
*/
120135
protected static void initialise() {
121136
String line = "";
122-
int counter = 0;
123137

124138
try {
125-
File f = new File("res/Word_Filter.csv");
126-
FileInputStream fis = new FileInputStream(f);
139+
ClassLoader cl = Thread.currentThread().getContextClassLoader();
140+
InputStream fis = cl.getResourceAsStream("Word_Filter.csv");
127141
InputStreamReader isr = new InputStreamReader(fis);
128142
BufferedReader br = new BufferedReader(isr);
129143

130144
while(line != null) {
131145

132-
counter++;
133146
String[] content = null;
134147

135148
try {
@@ -156,8 +169,8 @@ protected static void initialise() {
156169
line = br.readLine();
157170
}
158171
fis.close();
172+
isr.close();
159173
br.close();
160-
161174
// System.out.println("Loaded " + counter + " words to filter out");
162175
} catch (IOException e) {
163176
e.printStackTrace();

src/main/java/com/hktransfield/SecureStorage.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)