AmbariLocalUserProvider extends AbstractUserDetailsAuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String userName = authentication.getName().trim(); LOG.info("Loading user by name: " + userName); UserEntity userEntity = userDAO.findLocalUserByName(userName); if (userEntity == null) { LOG.info("user not found"); throw new InvalidUsernamePasswordCombinationException(); } if (!userEntity.getActive()) { logger.debug("User account is disabled"); throw new InvalidUsernamePasswordCombinationException(); } if (authentication.getCredentials() == null) { logger.debug("Authentication failed: no credentials provided"); throw new InvalidUsernamePasswordCombinationException(); } String password = userEntity.getUserPassword(); String presentedPassword = authentication.getCredentials().toString(); if (!passwordEncoder.matches(presentedPassword, password)) { logger.debug("Authentication failed: password does not match stored value"); throw new InvalidUsernamePasswordCombinationException(); } Collection<AmbariGrantedAuthority> userAuthorities = users.getUserAuthorities(userEntity.getUserName(), userEntity.getUserType()); User user = new User(userEntity); Authentication auth = new AmbariUserAuthentication(userEntity.getUserPassword(), user, userAuthorities); auth.setAuthenticated(true); return auth; } @Inject  AmbariLocalUserProvider(UserDAO userDAO, Users users, PasswordEncoder passwordEncoder); @Override Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> authentication);  }
@Test public void testSuccessfulAuth() { Users users = createMock(Users.class); UserDAO userDAO = createMock(UserDAO.class); Authentication authentication = createMock(Authentication.class); UserEntity userEntity = combineUserEntity(); expect(authentication.getName()).andReturn(TEST_USER_NAME); expect(userDAO.findLocalUserByName(TEST_USER_NAME)).andReturn(userEntity); expect(authentication.getCredentials()).andReturn(TEST_USER_PASS).anyTimes(); expect(users.getUserAuthorities(userEntity.getUserName(), userEntity.getUserType())).andReturn(null); replay(users, userDAO, authentication); AmbariLocalUserProvider ambariLocalUserProvider = new AmbariLocalUserProvider(userDAO, users, passwordEncoder); Authentication resultedAuth = ambariLocalUserProvider.authenticate(authentication); verify(users, userDAO, authentication); assertNotNull(resultedAuth); assertEquals(true, resultedAuth.isAuthenticated()); assertTrue(resultedAuth instanceof AmbariUserAuthentication); assertEquals(1, ((User) resultedAuth.getPrincipal()).getUserId()); }
@Test(expected = InvalidUsernamePasswordCombinationException.class) public void testAuthWithIncorrectName() { Users users = createMock(Users.class); UserDAO userDAO = createMock(UserDAO.class); Authentication authentication = createMock(Authentication.class); expect(authentication.getName()).andReturn(TEST_USER_NAME); expect(userDAO.findLocalUserByName(TEST_USER_NAME)).andReturn(null); replay(users, userDAO, authentication); AmbariLocalUserProvider ambariLocalUserProvider = new AmbariLocalUserProvider(userDAO, users, passwordEncoder); ambariLocalUserProvider.authenticate(authentication); }
@Test(expected = InvalidUsernamePasswordCombinationException.class) public void testAuthWithoutPass() { Users users = createMock(Users.class); UserDAO userDAO = createMock(UserDAO.class); Authentication authentication = createMock(Authentication.class); UserEntity userEntity = combineUserEntity(); expect(authentication.getName()).andReturn(TEST_USER_NAME); expect(userDAO.findLocalUserByName(TEST_USER_NAME)).andReturn(userEntity); expect(authentication.getCredentials()).andReturn(null); replay(users, userDAO, authentication); AmbariLocalUserProvider ambariLocalUserProvider = new AmbariLocalUserProvider(userDAO, users, passwordEncoder); ambariLocalUserProvider.authenticate(authentication); }
@Test(expected = InvalidUsernamePasswordCombinationException.class) public void testAuthWithIncorrectPass() { Users users = createMock(Users.class); UserDAO userDAO = createMock(UserDAO.class); Authentication authentication = createMock(Authentication.class); UserEntity userEntity = combineUserEntity(); expect(authentication.getName()).andReturn(TEST_USER_NAME); expect(userDAO.findLocalUserByName(TEST_USER_NAME)).andReturn(userEntity); expect(authentication.getCredentials()).andReturn(TEST_USER_INCORRECT_PASS).anyTimes(); replay(users, userDAO, authentication); AmbariLocalUserProvider ambariLocalUserProvider = new AmbariLocalUserProvider(userDAO, users, passwordEncoder); ambariLocalUserProvider.authenticate(authentication); }