Skip to content

Commit

Permalink
feat(user): switch password handling to char[] and enforce BCrypt 72-…
Browse files Browse the repository at this point in the history
…char limit
  • Loading branch information
Fredrik Marjoni committed Mar 10, 2026
1 parent 446438a commit 9b9e9db
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
10 changes: 7 additions & 3 deletions src/main/java/edu/group5/app/model/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ public String getPasswordHash() {
* This method uses BCrypt to compare the plaintext password with the hashed password.
* @param password the plaintext password to verify
* @return true if the password is correct, false otherwise
* @throws IllegalArgumentException if the password is null, empty, or longer than 72 characters (BCrypt limit)
*/
public boolean verifyPassword(String password) {
if (password == null || password.isEmpty()) {
public boolean verifyPassword(char[] password) {
if (password == null || password.length == 0) {
return false;
}
if (password.length > 72) { // BCrypt has a maximum password length of 72 bytes
throw new IllegalArgumentException("Password cannot be longer than 72 characters");
}
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder.matches(password, this.passwordHash);
return encoder.matches(new String(password), this.passwordHash);
}
}
4 changes: 2 additions & 2 deletions src/main/java/edu/group5/app/model/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public boolean registerUser(String role, String firstName, String lastName,
* (i.e., the user exists and the password is correct), false otherwise
* @throws IllegalArgumentException if email is null or empty, or if password is null or empty
*/
public boolean login(String email, String password) {
if (email == null || email.trim().isEmpty() || password == null || password.trim().isEmpty()) {
public boolean login(String email, char[] password) {
if (email == null || email.trim().isEmpty() || password == null || password.length == 0) {
return false;
}
User user = this.userRepository.findUserByEmail(email);
Expand Down
18 changes: 15 additions & 3 deletions src/test/java/edu/group5/app/model/user/CustomerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ void verifyPasswordReturnsTrueForCorrectPassword() {
User user = new Customer(testUserId, testFirstName,
testLastName, testEmail, testPasswordHash);

assertTrue(user.verifyPassword(testPassword));
assertTrue(user.verifyPassword(testPassword.toCharArray()));
}

@Test
void verifyPasswordReturnsFalseForIncorrectPassword() {
User user = new Customer(testUserId, testFirstName,
testLastName, testEmail, testPasswordHash);

assertFalse(user.verifyPassword("wrongPassword"));
assertFalse(user.verifyPassword("wrongPassword".toCharArray()));
}

@Test
Expand All @@ -140,7 +140,19 @@ void verifyPasswordReturnsFalseForEmptyPassword() {
User user = new Customer(testUserId, testFirstName,
testLastName, testEmail, testPasswordHash);

assertFalse(user.verifyPassword(""));
assertFalse(user.verifyPassword("".toCharArray()));
}

@Test
void verifyPasswordThrowsExceptionForTooLongPassword() {
User user = new Customer(testUserId, testFirstName,
testLastName, testEmail, testPasswordHash);
char[] longPassword = new char[73]; // 73 characters, exceeding BCrypt limit
IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class,
() -> user.verifyPassword(longPassword)
);
assertEquals("Password cannot be longer than 72 characters", exception.getMessage());
}

@Test
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/edu/group5/app/model/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void loginValidPassword() {
User testUser = new Customer(10, "Test", "User", "test@example.com", hashedPassword);
repo.addContent(testUser);

boolean result = service.login("test@example.com", plainPassword);
boolean result = service.login("test@example.com", plainPassword.toCharArray());
assertTrue(result);
}

Expand All @@ -107,19 +107,19 @@ void loginInvalidPassword() {
User testUser = new Customer(10, "Test", "User", "test@example.com", hashedPassword);
repo.addContent(testUser);

boolean result = service.login("test@example.com", "wrongpass");
boolean result = service.login("test@example.com", "wrongpass".toCharArray());
boolean result2 = service.login("test@example.com", null);
boolean result3 = service.login("test@example.com", " ");
boolean result3 = service.login("test@example.com", " ".toCharArray());
assertFalse(result);
assertFalse(result2);
assertFalse(result3);
}

@Test
void loginInvalidEmail() {
boolean result = service.login("nonexist@example.com", "password");
boolean result2 = service.login(null, "password");
boolean result3 = service.login(" ", "password");
boolean result = service.login("nonexist@example.com", "password".toCharArray());
boolean result2 = service.login(null, "password".toCharArray());
boolean result3 = service.login(" ", "password".toCharArray());
assertFalse(result);
assertFalse(result2);
assertFalse(result3);
Expand Down

0 comments on commit 9b9e9db

Please sign in to comment.