@Test
  public void testMyLdifFileWasLoaded() throws Exception {
    LdapTemplate t = new LdapTemplate();
    LdapContextSource s = new LdapContextSource();
    s.setPassword(LdapConfiguration.DEFAULT_PASSWORD);
    s.setUserDn(LdapConfiguration.DEFAULT_BIND_DN);
    s.setUrl(String.format("ldap://*****:*****@SuppressWarnings("unchecked")
    List<String> dns =
        t.search(
            "",
            filter.encode(),
            new ContextMapper() {
              public Object mapFromContext(Object ctx) {
                DirContextAdapter context = (DirContextAdapter) ctx;
                return context.getDn().toString();
              }
            });

    assertEquals(2, dns.size());
    assertTrue(dns.contains("dc=root"));
    assertTrue(dns.contains("dc=child,dc=root"));
  }
Example #2
0
  protected Fixture createFixture(File ctxFile) throws Exception {
    Fixture fixture = new Fixture();
    fixture.ctx = new FileSystemXmlApplicationContext("file:" + ctxFile.getAbsolutePath());
    fixture.config = (LdapConfig) fixture.ctx.getBean("config");

    Map<String, LdapContextSource> sources = fixture.ctx.getBeansOfType(LdapContextSource.class);

    LdapContextSource source = sources.values().iterator().next();
    String[] urls = source.getUrls();
    assertEquals(1, urls.length);

    fixture.template = new LdapTemplate(source);

    fixture.role = mock(RoleProvider.class);
    RoleProvider provider = (RoleProvider) fixture.role.proxy();

    fixture.sql = mock(SqlAction.class);
    SqlAction sql = (SqlAction) fixture.sql.proxy();

    fixture.queryMock = mock(LocalQuery.class);
    fixture.query = (LocalQuery) fixture.queryMock.proxy();
    fixture.queryMock.expects(once()).method("findByString").will(returnValue(null));

    fixture.ldap =
        new LdapImpl(source, fixture.template, new Roles(), fixture.config, provider, sql);
    fixture.ldap.setQueryService(fixture.query);

    fixture.provider = new LdapPasswordProvider(new PasswordUtil(sql), fixture.ldap);

    return fixture;
  }
 public static LdapContextSource contextSourceTarget() {
   LdapContextSource ldapContextSource = new LdapContextSource();
   ldapContextSource.setUrl("ldap://Alenas-iMac:1389");
   ldapContextSource.setBase("ou=metis_authentication,dc=europeana,dc=eu");
   ldapContextSource.setUserDn("cn=Metis Authentication");
   ldapContextSource.setPassword("secret");
   return ldapContextSource;
 }
 /**
  * Provides a configured LdapTemplate instance that can be used to perform any ldap based
  * operations against the Senate LDAP. This should typically be autowired into DAO layer classes.
  */
 @Bean(name = "ldapTemplate")
 public LdapTemplate ldapTemplate() {
   if (ldapUrl == null || ldapUrl.isEmpty()) {
     throw new BeanInitializationException(
         "Cannot instantiate LDAP Template because ldap.url in the properties file is not set.");
   }
   logger.info("Configuring ldap template with url {}", ldapUrl);
   LdapContextSource ldapContextSource = new LdapContextSource();
   ldapContextSource.setUrl(ldapUrl);
   ldapContextSource.afterPropertiesSet();
   ldapContextSource.setAnonymousReadOnly(true);
   return new LdapTemplate(ldapContextSource);
 }
 @Bean
 public LdapContextSource contextSource() {
   /*
    * We need to statically set these values, since we cannot use the
    * real contextSource in LdapConfig because is retrieves values
    * from the Play! application.conf.  Play! is not running in a test
    * environment, so we set these here for the tests.
    */
   LdapContextSource contextSource = new LdapContextSource();
   contextSource.setUrl("ldap://localhost:389");
   // contextSource.setBase("dc=example,dc=com");
   contextSource.setUserDn("cn=Directory Manager");
   contextSource.setPassword("password");
   return contextSource;
 }
 // @see: http://stackoverflow.com/questions/26004062/ldap-query-configuration-using-spring-boot
 @Bean
 @ConfigurationProperties(prefix = "ldap")
 public LdapContextSource contextSource() {
   LdapContextSource contextSource = new LdapContextSource();
   contextSource.setUrl(this.getUrl());
   contextSource.setUserDn(this.getUserOn());
   contextSource.setPassword(this.getPassword());
   try {
     contextSource.afterPropertiesSet();
   } catch (Exception e) {
     e.printStackTrace();
   }
   LOG.debug("LdapContextSource:" + this.toString());
   // LdapContextSource contextSource = new LdapContextSource();
   return contextSource;
 }
  @Before
  public void setUp() throws Exception {
    lctxs = new LdapContextSource();
    lctxs.setBase(baseDn);
    lctxs.setUrl(ldapUrl);
    lctxs.setUserDn(bindDn);
    lctxs.setPassword(bindPassword);
    lctxs.afterPropertiesSet();
    ltmpl = new LdapTemplate();
    ltmpl.setContextSource(lctxs);
    ltmpl.afterPropertiesSet();

    // removing users
    try {
      ltmpl.unbind("ou=users", true);
    } catch (NamingException e) {
    }

    // removing roles
    try {
      ltmpl.unbind("ou=roles", true);
    } catch (NamingException e) {
    }

    // recreating ou=users
    try {
      DirContextOperations ctx = new DirContextAdapter("ou=users");
      ctx.addAttributeValue("objectClass", "top");
      ctx.addAttributeValue("objectClass", "organizationalUnit");
      ctx.addAttributeValue("ou", "users");
      ltmpl.bind(ctx);
    } catch (Exception e) {
    }
    ;

    // recreating ou=roles
    try {
      DirContextOperations ctx = new DirContextAdapter("ou=roles");
      ctx.addAttributeValue("objectClass", "top");
      ctx.addAttributeValue("objectClass", "organizationalUnit");
      ctx.addAttributeValue("ou", "roles");
      ltmpl.bind(ctx);
    } catch (Exception e) {
    }
    ;
  }
 private LdapContextSource getLdapContextSource() throws Exception {
   LdapContextSource ldapContextSource = new DefaultSpringSecurityContextSource(ldapURL);
   ldapContextSource.setUserDn(ldapBindDN);
   ldapContextSource.setPassword(ldapBindPassword);
   ldapContextSource.setReferral(ldapReferral);
   ldapContextSource.setCacheEnvironmentProperties(false);
   ldapContextSource.setAnonymousReadOnly(false);
   ldapContextSource.setPooled(true);
   ldapContextSource.afterPropertiesSet();
   return ldapContextSource;
 }
  @Before
  public void setUp() throws Exception {
    // Bind to the directory
    LdapContextSource contextSource = new LdapContextSource();
    contextSource.setUrl("ldap://127.0.0.1:" + PORT);
    contextSource.setUserDn("");
    contextSource.setPassword("");
    contextSource.setPooled(false);
    contextSource.afterPropertiesSet();

    // Create the Sprint LDAP template
    LdapTemplate template = new LdapTemplate(contextSource);

    // Clear out any old data - and load the test data
    LdapTestUtils.cleanAndSetup(
        template.getContextSource(), baseName, new ClassPathResource("testdata.ldif"));
  }