RdfEngine implements Engine { @Override public boolean checkPassword(String username, String password) { Optional<User> userOptional = retrieveUser(username); if (!userOptional.isPresent()) { logger.debug("User with " + username + " username does not exist"); return false; } User user = userOptional.get(); if (!user.getPassword().isPresent()) { throw new IllegalStateException("Error retrieving user info"); } String savedPassword = user.getPassword().get().stringValue(); Encryption encryption = encryptionSupport.getEncryption(); if (encryption == null) { return savedPassword.equals(password); } else { String encryptionPrefix = encryptionSupport.getEncryptionPrefix(); String encryptionSuffix = encryptionSupport.getEncryptionSuffix(); boolean prefix = encryptionPrefix == null || savedPassword.startsWith(encryptionPrefix); boolean suffix = encryptionSuffix == null || savedPassword.endsWith(encryptionSuffix); if (prefix && suffix) { savedPassword = savedPassword.substring(encryptionPrefix != null ? encryptionPrefix.length() : 0, savedPassword.length() - (encryptionSuffix != null ? encryptionSuffix.length() : 0)); return encryption.checkPassword(password, savedPassword); } else { return password.equals(savedPassword); } } }  @Override Optional<Role> getRole(String roleName); @Override Set<User> getUsers(); @Override User createUser(UserConfig userConfig); @Override void storeUser(User user); @Override boolean userExists(String username); @Override boolean userExists(Resource userId); @Override Optional<User> retrieveUser(String username); @Override void updateUser(User newUser); @Override void deleteUser(String username); @Override Set<Group> getGroups(); @Override Group createGroup(GroupConfig groupConfig); @Override void storeGroup(Group group); @Override boolean groupExists(String groupTitle); @Override boolean groupExists(Resource groupId); @Override Optional<Group> retrieveGroup(String groupTitle); @Override void updateGroup(Group newGroup); @Override void deleteGroup(String groupTitle); @Override Set<Role> getUserRoles(String username); @Override boolean checkPassword(String username, String password); @Override String getEngineName(); static final String ENGINE_NAME; }
@Test public void testCheckPasswordWithoutEncryption() { when(encryptionSupport.getEncryption()).thenReturn(null); boolean result = engine.checkPassword(username, password); assertTrue(result); result = engine.checkPassword(username, "password"); assertFalse(result); }
@Test public void testCheckPasswordWithEncryption() { boolean result = engine.checkPassword(username, password); assertTrue(result); when(encryption.checkPassword(anyString(), anyString())).thenReturn(false); result = engine.checkPassword(username, "password"); assertFalse(result); }
@Test public void testCheckPasswordUserDoesNotExist() { boolean result = engine.checkPassword("error", password); assertFalse(result); }