@Test public void testNoTwoUsersWithSameId(){ String user = getUniqueId(); boolean created = userService.createUser(user,"a"); assertTrue(created); created = userService.createUser(user,"b"); assertFalse(created); }
public boolean createUser(String userId, String password) { if (userId == null) { return false; } User userDetails = getUser(userId); if (userDetails != null) { return false; } userDetails = new User(); userDetails.setUserId(userId); String salt = getRandomSalt(); String hash = computeSHA256(password, ITERATIONS, salt, PEPPER); String hashedPassword = "SHA-256"+SEPARATOR+ITERATIONS+SEPARATOR+salt+SEPARATOR+hash; userDetails.setPassword(hashedPassword); em.persist(userDetails); return true; }
UserService { public boolean createUser(String userId, String password) { if (userId == null) { return false; } User userDetails = getUser(userId); if (userDetails != null) { return false; } userDetails = new User(); userDetails.setUserId(userId); String salt = getRandomSalt(); String hash = computeSHA256(password, ITERATIONS, salt, PEPPER); String hashedPassword = "SHA-256"+SEPARATOR+ITERATIONS+SEPARATOR+salt+SEPARATOR+hash; userDetails.setPassword(hashedPassword); em.persist(userDetails); return true; } }
UserService { public boolean createUser(String userId, String password) { if (userId == null) { return false; } User userDetails = getUser(userId); if (userDetails != null) { return false; } userDetails = new User(); userDetails.setUserId(userId); String salt = getRandomSalt(); String hash = computeSHA256(password, ITERATIONS, salt, PEPPER); String hashedPassword = "SHA-256"+SEPARATOR+ITERATIONS+SEPARATOR+salt+SEPARATOR+hash; userDetails.setPassword(hashedPassword); em.persist(userDetails); return true; }  }
UserService { public boolean createUser(String userId, String password) { if (userId == null) { return false; } User userDetails = getUser(userId); if (userDetails != null) { return false; } userDetails = new User(); userDetails.setUserId(userId); String salt = getRandomSalt(); String hash = computeSHA256(password, ITERATIONS, salt, PEPPER); String hashedPassword = "SHA-256"+SEPARATOR+ITERATIONS+SEPARATOR+salt+SEPARATOR+hash; userDetails.setPassword(hashedPassword); em.persist(userDetails); return true; }  boolean createUser(String userId, String password); boolean login(String userId, String password); User getUser(String userId); }
UserService { public boolean createUser(String userId, String password) { if (userId == null) { return false; } User userDetails = getUser(userId); if (userDetails != null) { return false; } userDetails = new User(); userDetails.setUserId(userId); String salt = getRandomSalt(); String hash = computeSHA256(password, ITERATIONS, salt, PEPPER); String hashedPassword = "SHA-256"+SEPARATOR+ITERATIONS+SEPARATOR+salt+SEPARATOR+hash; userDetails.setPassword(hashedPassword); em.persist(userDetails); return true; }  boolean createUser(String userId, String password); boolean login(String userId, String password); User getUser(String userId);  }