@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); }
@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; }
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; } }
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); }
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); }
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);  }