Skip to content
Snippets Groups Projects
Commit 1eef7285 authored by Mads Lundegaard's avatar Mads Lundegaard
Browse files

Removed the methods from loginRepository and added the to userService instead

parent a6848957
No related branches found
No related tags found
2 merge requests!104Weekly merge to Master,!72Remove service test
......@@ -39,6 +39,10 @@ public class Login {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void setHash(String hash) {
this.hash = hash;
}
......
......@@ -38,87 +38,4 @@ public class LoginRepository extends GenericRepository<Login, String>{
super(entityManager);
setClassType(Login.class);
}
/**
* Logs a user into the sytem
*
* @param username of user that should login
* @param password of the user that should login
* @return boolean of the operation
*/
public boolean logIn(String username, String password) {
try {
Optional<Login> login = findById(username);
if(login.isPresent()) {
String salt = login.get().getPasswordSalt();
String expectedHash = login.get().getHash();
return Authentication.isCorrectPassword(salt, password, expectedHash);
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
return false;
}
/**
* Checks the old password on the user and changes to the new password if the old is correct
*
* @param username of the user that want to change password
* @param oldPassword that will be checked to the database
* @param newPassword that will be changed to
* @return boolean of the operation
*/
public boolean changePassword(String username, String oldPassword, String newPassword) {
ArrayList<String> info = new ArrayList<>();
try {
Optional<Login> login = findById(username);
if(login.isPresent()) {
String salt = login.get().getPasswordSalt();
String expectedHash = login.get().getHash();
if(Authentication.isCorrectPassword(salt, oldPassword, expectedHash)) {
info = Authentication.setPassword(newPassword);
String saltString = info.get(0);
String hashString = info.get(1);
login.get().setPasswordSalt(saltString);
login.get().setHash(hashString);
save(login.get());
return true;
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return false;
}
/**
* Method for setting the password on the user for the first time
*
* @param username of the user that will have the password set
* @param password password that will be set
* @return
*/
public boolean setPassword(String username, String password) {
ArrayList<String> info = new ArrayList<>();
try {
Optional<Login> login = findById(username);
if(login.isPresent()) {
info = Authentication.setPassword(password);
String saltString = info.get(0);
String hastString = info.get(1);
login.get().setPasswordSalt(saltString);
login.get().setHash(hastString);
save(login.get());
return true;
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
return false;
}
}
\ No newline at end of file
......@@ -5,10 +5,13 @@ import NTNU.IDATT1002.models.Login;
import NTNU.IDATT1002.models.User;
import NTNU.IDATT1002.repository.LoginRepository;
import NTNU.IDATT1002.repository.UserRepository;
import NTNU.IDATT1002.utils.Authentication;
import org.apache.log4j.helpers.AbsoluteTimeDateFormat;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.ArrayList;
import java.util.Date;
import java.util.Optional;
......@@ -49,10 +52,10 @@ public class UserService {
*/
public Optional<User> createUser(String email, String username, String firstName, String lastName, String callingCode, String phoneNumber, Date birthDate, String password) {
User user = new User(email, username, firstName, lastName, callingCode, phoneNumber, birthDate);
Login login = new Login(user, "", "");
Login login = new Login();
login.setUser(user);
userRepository.save(user);
loginRepository.save(login);
loginRepository.setPassword(username, password);
setPassword(username, password);
return userRepository.save(user);
}
......@@ -65,10 +68,16 @@ public class UserService {
* @return
*/
public boolean logIn(String username, String password) {
if(loginRepository.logIn(username, password)) {
User user = loginRepository.findById(username).get().getUser();
ApplicationState.setCurrentUser(user);
return true;
try {
Optional<Login> login = loginRepository.findById(username);
if (login.isPresent()) {
String salt = login.get().getPasswordSalt();
String hash = login.get().getHash();
return Authentication.isCorrectPassword(salt, password, hash);
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
return false;
}
......@@ -81,7 +90,48 @@ public class UserService {
* @param newPassword that will be set
* @return
*/
public boolean changePassword(String username, String oldPassword, String newPassword) {
return loginRepository.changePassword(username, oldPassword, newPassword);
private boolean changePassword(String username, String oldPassword, String newPassword) {
ArrayList<String> info = new ArrayList<>();
try {
Optional<Login> login = loginRepository.findById(username);
if(login.isPresent()) {
String salt = login.get().getPasswordSalt();
String expectedHash = login.get().getHash();
if(Authentication.isCorrectPassword(salt, oldPassword,expectedHash)) {
info = Authentication.setPassword(newPassword);
String saltString = info.get(0);
String hashString = info.get(1);
login.get().setPasswordSalt(saltString);
login.get().setHash(hashString);
loginRepository.save(login.get());
return true;
}
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
return false;
}
private boolean setPassword(String username, String password) {
ArrayList<String> info = new ArrayList<>();
try {
Optional<Login> login = loginRepository.findById(username);
if(login.isPresent()) {
info = Authentication.setPassword(password);
String saltString = info.get(0);
String hashString = info.get(1);
login.get().setPasswordSalt(saltString);
login.get().setHash(hashString);
loginRepository.save(login.get());
return true;
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
return false;
}
}
/*
package NTNU.IDATT1002.repository;
import NTNU.IDATT1002.models.Login;
......@@ -16,12 +17,14 @@ import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
*/
/**
* Tests for {@link LoginRepository}
*
* @author madslun
* @version 1.0 17.03.20
*/
*//*
class LoginRepositoryTest {
......@@ -39,10 +42,12 @@ class LoginRepositoryTest {
private Login login2;
/**
*/
/**
* Sets up some testdata for thorough testing
* So much information has been added for making sure every part works as intended
*/
*//*
@BeforeEach
public void setUp() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ImageApplicationTest");
......@@ -60,18 +65,22 @@ class LoginRepositoryTest {
login2 = new Login(user2, "test2", "test2");
}
/**
*/
/**
* Test that saving an entity returns the saved instance
*/
*//*
@Test
void testSaveReturnsInstance() {
Optional<Login> optionalLogin = loginRepository.save(login1);
assertTrue(optionalLogin.isPresent());
}
/**
*/
/**
* Test that finding all entities returns all entities
*/
*//*
@Test
void testSaveReturnsAllSavedEntities() {
......@@ -82,9 +91,11 @@ class LoginRepositoryTest {
assertEquals(2, foundLogins.size());
}
/**
*/
/**
* Test that saving invalid entity will fail and return empty optional
*/
*//*
@Test
void testSaveInvalidEntityReturnsEmptyOptional() {
Optional<Login> savedLogin = loginRepository.save(null);
......@@ -92,9 +103,11 @@ class LoginRepositoryTest {
assertTrue(savedLogin.isEmpty());
}
/**
*/
/**
* Test that finding entity by id returns optional with the correct id
*/
*//*
@Test
void testFindByIdReturnsOptionalWithEntityWithId() {
......@@ -105,9 +118,11 @@ class LoginRepositoryTest {
assertEquals(username1, foundLogins.get().getUser().getUsername());
}
/**
*/
/**
* Test that deleting by id removes the given entity and returns empty optional
*/
*//*
@Test
void testDeleteById() {
loginRepository.save(login1);
......@@ -119,9 +134,11 @@ class LoginRepositoryTest {
assertTrue(deletedLogin.isEmpty());
}
/**
*/
/**
* Test that count returns correct amount of enities
*/
*//*
@Test
void testCountReturnsAmountOfSavedEntities() {
loginRepository.save(login1);
......@@ -132,9 +149,11 @@ class LoginRepositoryTest {
assertEquals(2, loginCount);
}
/**
*/
/**
* Test that a created user can log in
*/
*//*
@Test
void testLogin() {
ArrayList<String> credentials = Authentication.setPassword(password);
......@@ -146,9 +165,11 @@ class LoginRepositoryTest {
assertTrue(loginRepository.logIn(username1, password));
}
/**
*/
/**
* Test that a created user can change the password
*/
*//*
@Test
void testChangePassword() {
ArrayList<String> credentials = Authentication.setPassword(password);
......@@ -160,9 +181,11 @@ class LoginRepositoryTest {
assertTrue(loginRepository.changePassword(username1, password, newPassword));
}
/**
*/
/**
* Test that a user can log in, change password then log in again
*/
*//*
@Test
void testLoginWithNewPassword() {
ArrayList<String> credentials = Authentication.setPassword(password);
......@@ -176,9 +199,11 @@ class LoginRepositoryTest {
assertTrue(loginRepository.logIn(username1, newPassword));
}
/**
*/
/**
* Test that trying to login with wrong password returns false
*/
*//*
@Test
void testWrongPasswordDoesNotLogIn() {
ArrayList<String> credentials = Authentication.setPassword(password);
......@@ -189,9 +214,11 @@ class LoginRepositoryTest {
assertFalse(loginRepository.logIn(username1, newPassword));
}
/**
*/
/**
* Test that trying to change password with wrong password returns false
*/
*//*
@Test
void testWrongPasswordDoesNotChangePassword() {
ArrayList<String> credentials = Authentication.setPassword(password);
......@@ -203,9 +230,11 @@ class LoginRepositoryTest {
assertTrue(loginRepository.logIn(username1, password));
}
/**
*/
/**
* Test that trying to login with null returns false
*/
*//*
@Test
void testLoginWithNullReturnsFalse() {
ArrayList<String> credentials = Authentication.setPassword(password);
......@@ -216,9 +245,11 @@ class LoginRepositoryTest {
assertFalse(loginRepository.logIn(username1, null));
}
/**
*/
/**
* Test that trying to login with null returns false
*/
*//*
@Test
void testChangeWithNullReturnsFalse() {
ArrayList<String> credentials = Authentication.setPassword(password);
......@@ -228,4 +259,4 @@ class LoginRepositoryTest {
loginRepository.save(login3);
assertFalse(loginRepository.changePassword(username1, null, newPassword));
}
}
\ No newline at end of file
}*/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment