use of org.springframework.ldap.core.DirContextOperations in project gocd by gocd.
the class LdapAuthenticator method authenticate.
public DirContextOperations authenticate(Authentication authentication) {
DirContextOperations contextOperations = super.authenticate(authentication);
contextOperations.setAttributeValue(AUTHENTICATION_KEY, authentication);
return contextOperations;
}
use of org.springframework.ldap.core.DirContextOperations in project gocd by gocd.
the class LdapUserSearchTest method shouldNotLogWhenUserNameNotFoundExceptionIsThrown.
@Test
public void shouldNotLogWhenUserNameNotFoundExceptionIsThrown() {
final FilterBasedLdapUserSearch filter1 = mock(FilterBasedLdapUserSearch.class);
final FilterBasedLdapUserSearch filter2 = mock(FilterBasedLdapUserSearch.class);
LdapConfig ldapConfig = setLdapConfig(new BasesConfig(new BaseConfig("base1"), new BaseConfig("base2")));
DirContextOperations foundUser = mock(DirContextOperations.class);
doReturn(filter1).when(spy).getFilterBasedLdapUserSearch(ldapConfig.getBasesConfig().get(0).getValue(), ldapConfig.searchFilter());
doReturn(filter2).when(spy).getFilterBasedLdapUserSearch(ldapConfig.getBasesConfig().get(1).getValue(), ldapConfig.searchFilter());
when(filter1.searchForUser("username")).thenThrow(new UsernameNotFoundException("User username not found in directory."));
when(filter2.searchForUser("username")).thenReturn(foundUser);
spy.searchForUser("username");
verify(logger, never()).warn(Matchers.<Object>any(), Matchers.<Throwable>any());
}
use of org.springframework.ldap.core.DirContextOperations in project gocd by gocd.
the class LdapUserSearchTest method shouldSkipInvalidSearchBaseWhenAuthenticating.
@Test
public void shouldSkipInvalidSearchBaseWhenAuthenticating() {
final FilterBasedLdapUserSearch filter1 = mock(FilterBasedLdapUserSearch.class);
final FilterBasedLdapUserSearch filter2 = mock(FilterBasedLdapUserSearch.class);
LdapConfig ldapConfig = setLdapConfig(new BasesConfig(new BaseConfig("base1"), new BaseConfig("base2")));
doReturn(filter1).when(spy).getFilterBasedLdapUserSearch(ldapConfig.getBasesConfig().get(0).getValue(), ldapConfig.searchFilter());
doReturn(filter2).when(spy).getFilterBasedLdapUserSearch(ldapConfig.getBasesConfig().get(1).getValue(), ldapConfig.searchFilter());
DirContextOperations foundUser = mock(DirContextOperations.class);
when(filter1.searchForUser("username")).thenThrow(new RuntimeException("Invalid search base"));
when(filter2.searchForUser("username")).thenReturn(foundUser);
assertThat(spy.searchForUser("username"), is(foundUser));
verify(filter1).searchForUser("username");
verify(filter2).searchForUser("username");
}
use of org.springframework.ldap.core.DirContextOperations in project gocd by gocd.
the class LdapUserSearchTest method shouldLogErrorsForAllInvalidSearchBaseWhenAuthenticating.
@Test
public void shouldLogErrorsForAllInvalidSearchBaseWhenAuthenticating() {
final FilterBasedLdapUserSearch filter1 = mock(FilterBasedLdapUserSearch.class);
final FilterBasedLdapUserSearch filter2 = mock(FilterBasedLdapUserSearch.class);
final FilterBasedLdapUserSearch filter3 = mock(FilterBasedLdapUserSearch.class);
final FilterBasedLdapUserSearch filter4 = mock(FilterBasedLdapUserSearch.class);
LdapConfig ldapConfig = setLdapConfig(new BasesConfig(new BaseConfig("base1"), new BaseConfig("base2"), new BaseConfig("base3"), new BaseConfig("base4")));
doReturn(filter1).when(spy).getFilterBasedLdapUserSearch(ldapConfig.getBasesConfig().get(0).getValue(), ldapConfig.searchFilter());
doReturn(filter2).when(spy).getFilterBasedLdapUserSearch(ldapConfig.getBasesConfig().get(1).getValue(), ldapConfig.searchFilter());
doReturn(filter3).when(spy).getFilterBasedLdapUserSearch(ldapConfig.getBasesConfig().get(2).getValue(), ldapConfig.searchFilter());
doReturn(filter4).when(spy).getFilterBasedLdapUserSearch(ldapConfig.getBasesConfig().get(3).getValue(), ldapConfig.searchFilter());
DirContextOperations foundUser = mock(DirContextOperations.class);
RuntimeException runtimeException = new RuntimeException("Invalid search base");
when(filter1.searchForUser("username")).thenThrow(runtimeException);
when(filter2.searchForUser("username")).thenThrow(new UsernameNotFoundException("User not found"));
when(filter3.searchForUser("username")).thenThrow(runtimeException);
when(filter4.searchForUser("username")).thenReturn(foundUser);
assertThat(spy.searchForUser("username"), is(foundUser));
verify(logger, times(1)).warn("The ldap configuration for search base 'base1' is invalid", runtimeException);
verify(logger, times(1)).warn("The ldap configuration for search base 'base3' is invalid", runtimeException);
}
use of org.springframework.ldap.core.DirContextOperations in project gocd by gocd.
the class LdapUserSearchTest method shouldNotProceedWithTheNextSearchBaseWhenUserIsFoundInOne.
@Test
public void shouldNotProceedWithTheNextSearchBaseWhenUserIsFoundInOne() {
final FilterBasedLdapUserSearch filter1 = mock(FilterBasedLdapUserSearch.class);
final FilterBasedLdapUserSearch filter2 = mock(FilterBasedLdapUserSearch.class);
LdapConfig ldapConfig = setLdapConfig(new BasesConfig(new BaseConfig("base1"), new BaseConfig("base2")));
doReturn(filter1).when(spy).getFilterBasedLdapUserSearch(ldapConfig.getBasesConfig().get(0).getValue(), ldapConfig.searchFilter());
doReturn(filter2).when(spy).getFilterBasedLdapUserSearch(ldapConfig.getBasesConfig().get(1).getValue(), ldapConfig.searchFilter());
DirContextOperations foundUser = mock(DirContextOperations.class);
when(filter1.searchForUser("username")).thenReturn(foundUser);
assertThat(spy.searchForUser("username"), is(foundUser));
verify(filter1).searchForUser("username");
verify(filter2, never()).searchForUser("username");
}
Aggregations