-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Login Page with registering new user.
Salting Added. Account Page still not done yet. Tests Written. Handle different users + account setting page #33 Co-authored-by: rhit-norflwe <rhit-norflwe@users.noreply.github.com>
- Loading branch information
1 parent
d23ee1d
commit 4eb2468
Showing
15 changed files
with
216 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
backend/omniplanner/src/main/java/com/main/omniplanner/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.main.omniplanner; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
|
||
@Configuration | ||
public class SecurityConfig { | ||
|
||
@Bean | ||
public BCryptPasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
backend/omniplanner/src/main/java/com/main/omniplanner/user/PasswordService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.main.omniplanner.user; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.security.SecureRandom; | ||
import java.util.Base64; | ||
|
||
@Service | ||
public class PasswordService { | ||
|
||
private final BCryptPasswordEncoder passwordEncoder; | ||
|
||
@Autowired | ||
public PasswordService(BCryptPasswordEncoder passwordEncoder) { | ||
this.passwordEncoder = passwordEncoder; | ||
} | ||
|
||
public String generateSalt() { | ||
SecureRandom random = new SecureRandom(); | ||
byte[] salt = new byte[16]; | ||
random.nextBytes(salt); | ||
return Base64.getEncoder().encodeToString(salt); | ||
} | ||
|
||
public String hashPassword(String password, String salt) { | ||
return passwordEncoder.encode(password + salt); | ||
} | ||
|
||
public boolean verifyPassword(String password, String salt, String hashedPassword) { | ||
return passwordEncoder.matches(password + salt, hashedPassword); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
backend/omniplanner/src/test/java/com/main/omniplanner/UserTests/PasswordServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.main.omniplanner.UserTests; | ||
|
||
import com.main.omniplanner.user.PasswordService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class PasswordServiceTest { | ||
|
||
@Mock | ||
private BCryptPasswordEncoder passwordEncoder; | ||
|
||
private PasswordService passwordService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
passwordService = new PasswordService(passwordEncoder); | ||
} | ||
|
||
@Test | ||
public void testGenerateSaltLength() { | ||
String salt = passwordService.generateSalt(); | ||
assertEquals(24, salt.length()); | ||
} | ||
|
||
@Test | ||
public void testGenerateSaltUnique() { | ||
String salt1 = passwordService.generateSalt(); | ||
String salt2 = passwordService.generateSalt(); | ||
assertNotEquals(salt1, salt2); | ||
} | ||
|
||
@Test | ||
public void hashPasswordLength() { | ||
String password = "mySecurePassword"; | ||
String salt = "randomSalt"; | ||
|
||
String hashedPassword = "hashedPasswordExample"; | ||
when(passwordEncoder.encode(password + salt)).thenReturn(hashedPassword); | ||
|
||
assertEquals(hashedPassword, passwordService.hashPassword(password, salt)); | ||
} | ||
|
||
@Test | ||
public void hashPasswordNotSame() { | ||
String password = "mySecurePassword"; | ||
String salt = "randomSalt"; | ||
|
||
when(passwordEncoder.encode(password + salt)).thenReturn("hashedPassword1", "hashedPassword2"); | ||
|
||
assertNotEquals(passwordService.hashPassword(password, salt), passwordService.hashPassword(password, salt)); | ||
} | ||
|
||
@Test | ||
public void verifyPassword() { | ||
String password = "mySecurePassword"; | ||
String salt = "randomSalt"; | ||
String hashedPassword = "hashedPassword"; | ||
|
||
when(passwordEncoder.matches(password + salt, hashedPassword)).thenReturn(true); | ||
|
||
assertTrue(passwordService.verifyPassword(password, salt, hashedPassword)); | ||
|
||
verify(passwordEncoder).matches(password + salt, hashedPassword); | ||
} | ||
} |
Oops, something went wrong.