@Test public void testAuthenticateBadPassword() throws Exception { String basePathString = "dc=apache,dc=org"; String ldapUserRelativeDNString = String.format("uid=%s,ou=people,ou=dev", "ldapUsername"); LdapName ldapUserRelativeDN = new LdapName(ldapUserRelativeDNString); String ldapUserDNString = String.format("%s,%s", ldapUserRelativeDNString, basePathString); LdapName basePath = LdapUtils.newLdapName(basePathString); LdapContextSource ldapCtxSource = createMock(LdapContextSource.class); expect(ldapCtxSource.getBaseLdapName()) .andReturn(basePath) .atLeastOnce(); expect(ldapCtxSource.getContext(ldapUserDNString, "password")) .andThrow(new org.springframework.ldap.AuthenticationException(null)) .once(); DirContextOperations searchedUserContext = createMock(DirContextOperations.class); expect(searchedUserContext.getDn()) .andReturn(ldapUserRelativeDN) .atLeastOnce(); FilterBasedLdapUserSearch userSearch = createMock(FilterBasedLdapUserSearch.class); expect(userSearch.searchForUser(anyString())).andReturn(searchedUserContext).once(); replayAll(); Configuration configuration = new Configuration(); AmbariLdapBindAuthenticator bindAuthenticator = new AmbariLdapBindAuthenticator(ldapCtxSource, configuration); bindAuthenticator.setUserSearch(userSearch); try { bindAuthenticator.authenticate(new UsernamePasswordAuthenticationToken("username", "password")); fail("Expected thrown exception: org.springframework.security.authentication.BadCredentialsException"); } catch (org.springframework.security.authentication.BadCredentialsException e) { } catch (Throwable t) { fail("Expected thrown exception: org.springframework.security.authentication.BadCredentialsException\nEncountered thrown exception " + t.getClass().getName()); } verifyAll(); }
@Override public DirContextOperations authenticate(Authentication authentication) { if (!(authentication instanceof UsernamePasswordAuthenticationToken)) { LOG.info("Unexpected authentication token type encountered ({}) - failing authentication.", authentication.getClass().getName()); throw new BadCredentialsException("Unexpected authentication token type encountered."); } DirContextOperations user = authenticate((UsernamePasswordAuthenticationToken) authentication); LdapServerProperties ldapServerProperties = configuration.getLdapServerProperties(); if (StringUtils.isNotEmpty(ldapServerProperties.getAdminGroupMappingRules())) { setAmbariAdminAttr(user, ldapServerProperties); } String ldapUserName = user.getStringAttribute(ldapServerProperties.getUsernameAttribute()); String loginName = authentication.getName(); if (ldapUserName == null) { LOG.warn("The user data does not contain a value for {}.", ldapServerProperties.getUsernameAttribute()); } else if (ldapUserName.isEmpty()) { LOG.warn("The user data contains an empty value for {}.", ldapServerProperties.getUsernameAttribute()); } else if (!ldapUserName.equals(loginName)) { LOG.info("User with {}='{}' logged in with login alias '{}'", ldapServerProperties.getUsernameAttribute(), ldapUserName, loginName); String processedLdapUserName; if (ldapServerProperties.isForceUsernameToLowercase()) { processedLdapUserName = ldapUserName.toLowerCase(); LOG.info("Forcing ldap username to be lowercase characters: {} ==> {}", ldapUserName, processedLdapUserName); } else { processedLdapUserName = ldapUserName; } AuthorizationHelper.addLoginNameAlias(processedLdapUserName, loginName); } return user; }
AmbariLdapBindAuthenticator extends AbstractLdapAuthenticator { @Override public DirContextOperations authenticate(Authentication authentication) { if (!(authentication instanceof UsernamePasswordAuthenticationToken)) { LOG.info("Unexpected authentication token type encountered ({}) - failing authentication.", authentication.getClass().getName()); throw new BadCredentialsException("Unexpected authentication token type encountered."); } DirContextOperations user = authenticate((UsernamePasswordAuthenticationToken) authentication); LdapServerProperties ldapServerProperties = configuration.getLdapServerProperties(); if (StringUtils.isNotEmpty(ldapServerProperties.getAdminGroupMappingRules())) { setAmbariAdminAttr(user, ldapServerProperties); } String ldapUserName = user.getStringAttribute(ldapServerProperties.getUsernameAttribute()); String loginName = authentication.getName(); if (ldapUserName == null) { LOG.warn("The user data does not contain a value for {}.", ldapServerProperties.getUsernameAttribute()); } else if (ldapUserName.isEmpty()) { LOG.warn("The user data contains an empty value for {}.", ldapServerProperties.getUsernameAttribute()); } else if (!ldapUserName.equals(loginName)) { LOG.info("User with {}='{}' logged in with login alias '{}'", ldapServerProperties.getUsernameAttribute(), ldapUserName, loginName); String processedLdapUserName; if (ldapServerProperties.isForceUsernameToLowercase()) { processedLdapUserName = ldapUserName.toLowerCase(); LOG.info("Forcing ldap username to be lowercase characters: {} ==> {}", ldapUserName, processedLdapUserName); } else { processedLdapUserName = ldapUserName; } AuthorizationHelper.addLoginNameAlias(processedLdapUserName, loginName); } return user; } }
AmbariLdapBindAuthenticator extends AbstractLdapAuthenticator { @Override public DirContextOperations authenticate(Authentication authentication) { if (!(authentication instanceof UsernamePasswordAuthenticationToken)) { LOG.info("Unexpected authentication token type encountered ({}) - failing authentication.", authentication.getClass().getName()); throw new BadCredentialsException("Unexpected authentication token type encountered."); } DirContextOperations user = authenticate((UsernamePasswordAuthenticationToken) authentication); LdapServerProperties ldapServerProperties = configuration.getLdapServerProperties(); if (StringUtils.isNotEmpty(ldapServerProperties.getAdminGroupMappingRules())) { setAmbariAdminAttr(user, ldapServerProperties); } String ldapUserName = user.getStringAttribute(ldapServerProperties.getUsernameAttribute()); String loginName = authentication.getName(); if (ldapUserName == null) { LOG.warn("The user data does not contain a value for {}.", ldapServerProperties.getUsernameAttribute()); } else if (ldapUserName.isEmpty()) { LOG.warn("The user data contains an empty value for {}.", ldapServerProperties.getUsernameAttribute()); } else if (!ldapUserName.equals(loginName)) { LOG.info("User with {}='{}' logged in with login alias '{}'", ldapServerProperties.getUsernameAttribute(), ldapUserName, loginName); String processedLdapUserName; if (ldapServerProperties.isForceUsernameToLowercase()) { processedLdapUserName = ldapUserName.toLowerCase(); LOG.info("Forcing ldap username to be lowercase characters: {} ==> {}", ldapUserName, processedLdapUserName); } else { processedLdapUserName = ldapUserName; } AuthorizationHelper.addLoginNameAlias(processedLdapUserName, loginName); } return user; } AmbariLdapBindAuthenticator(BaseLdapPathContextSource contextSource,
                                     Configuration configuration); }
AmbariLdapBindAuthenticator extends AbstractLdapAuthenticator { @Override public DirContextOperations authenticate(Authentication authentication) { if (!(authentication instanceof UsernamePasswordAuthenticationToken)) { LOG.info("Unexpected authentication token type encountered ({}) - failing authentication.", authentication.getClass().getName()); throw new BadCredentialsException("Unexpected authentication token type encountered."); } DirContextOperations user = authenticate((UsernamePasswordAuthenticationToken) authentication); LdapServerProperties ldapServerProperties = configuration.getLdapServerProperties(); if (StringUtils.isNotEmpty(ldapServerProperties.getAdminGroupMappingRules())) { setAmbariAdminAttr(user, ldapServerProperties); } String ldapUserName = user.getStringAttribute(ldapServerProperties.getUsernameAttribute()); String loginName = authentication.getName(); if (ldapUserName == null) { LOG.warn("The user data does not contain a value for {}.", ldapServerProperties.getUsernameAttribute()); } else if (ldapUserName.isEmpty()) { LOG.warn("The user data contains an empty value for {}.", ldapServerProperties.getUsernameAttribute()); } else if (!ldapUserName.equals(loginName)) { LOG.info("User with {}='{}' logged in with login alias '{}'", ldapServerProperties.getUsernameAttribute(), ldapUserName, loginName); String processedLdapUserName; if (ldapServerProperties.isForceUsernameToLowercase()) { processedLdapUserName = ldapUserName.toLowerCase(); LOG.info("Forcing ldap username to be lowercase characters: {} ==> {}", ldapUserName, processedLdapUserName); } else { processedLdapUserName = ldapUserName; } AuthorizationHelper.addLoginNameAlias(processedLdapUserName, loginName); } return user; } AmbariLdapBindAuthenticator(BaseLdapPathContextSource contextSource,
                                     Configuration configuration); @Override DirContextOperations authenticate(Authentication authentication); }
AmbariLdapBindAuthenticator extends AbstractLdapAuthenticator { @Override public DirContextOperations authenticate(Authentication authentication) { if (!(authentication instanceof UsernamePasswordAuthenticationToken)) { LOG.info("Unexpected authentication token type encountered ({}) - failing authentication.", authentication.getClass().getName()); throw new BadCredentialsException("Unexpected authentication token type encountered."); } DirContextOperations user = authenticate((UsernamePasswordAuthenticationToken) authentication); LdapServerProperties ldapServerProperties = configuration.getLdapServerProperties(); if (StringUtils.isNotEmpty(ldapServerProperties.getAdminGroupMappingRules())) { setAmbariAdminAttr(user, ldapServerProperties); } String ldapUserName = user.getStringAttribute(ldapServerProperties.getUsernameAttribute()); String loginName = authentication.getName(); if (ldapUserName == null) { LOG.warn("The user data does not contain a value for {}.", ldapServerProperties.getUsernameAttribute()); } else if (ldapUserName.isEmpty()) { LOG.warn("The user data contains an empty value for {}.", ldapServerProperties.getUsernameAttribute()); } else if (!ldapUserName.equals(loginName)) { LOG.info("User with {}='{}' logged in with login alias '{}'", ldapServerProperties.getUsernameAttribute(), ldapUserName, loginName); String processedLdapUserName; if (ldapServerProperties.isForceUsernameToLowercase()) { processedLdapUserName = ldapUserName.toLowerCase(); LOG.info("Forcing ldap username to be lowercase characters: {} ==> {}", ldapUserName, processedLdapUserName); } else { processedLdapUserName = ldapUserName; } AuthorizationHelper.addLoginNameAlias(processedLdapUserName, loginName); } return user; } AmbariLdapBindAuthenticator(BaseLdapPathContextSource contextSource,
                                     Configuration configuration); @Override DirContextOperations authenticate(Authentication authentication);  }