Java Code Examples for org.springframework.ldap.core.AttributesMapper

The following examples show how to use org.springframework.ldap.core.AttributesMapper. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ctsms   Source File: LdapService.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
private AttributesMapper getAttributeMapper(final Object... baseArgs) {
	return new AttributesMapper() {

		@Override
		public Object mapFromAttributes(Attributes attrs) throws NamingException {
			LdapEntryVO entry = new LdapEntryVO();
			entry.setUsername((String) attrs.get(USERNAME_ATTRIBUTE_ID).get());
			DistinguishedName dn = new DistinguishedName(getBase(baseArgs));
			dn.add(USERNAME_ATTRIBUTE_ID, entry.getUsername());
			entry.setAbsoluteDn(dn.encode());
			Map<String, Object> attributes = new LinkedHashMap<String, Object>();
			if (searchResultAttributes != null) {
				for (int i = 0; i < searchResultAttributes.length; i++) {
					Attribute attr = attrs.get(searchResultAttributes[i]);
					if (attr != null) {
						attributes.put(searchResultAttributes[i], attr.get());
					}
				}
			}
			entry.setAttributes(attributes);
			return entry;
		}
	};
}
 
Example 2
Source Project: metron   Source File: KnoxSSOAuthenticationFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Builds the Spring Authentication object using the supplied user name and groups looked up from LDAP.  Groups are currently
 * mapped directly to Spring roles by converting to upper case and prepending the name with "ROLE_".
 * @param userName The username to build the Authentication object with.
 * @param httpRequest HttpServletRequest
 * @return Authentication object for the given user.
 */
protected Authentication getAuthentication(String userName, HttpServletRequest httpRequest) {
  String ldapName = LdapNameBuilder.newInstance().add(userSearchBase).add("uid", userName).build().toString();

  // Search ldap for a user's groups and convert to a Spring role
  List<GrantedAuthority> grantedAuths = ldapTemplate.search(query()
          .where("objectclass")
          .is("groupOfNames")
          .and("member")
          .is(ldapName), (AttributesMapper<String>) attrs -> (String) attrs.get("cn").get())
          .stream()
          .map(group -> String.format("%s%s", SECURITY_ROLE_PREFIX, group.toUpperCase()))
          .map(SimpleGrantedAuthority::new).collect(Collectors.toList());

  final UserDetails principal = new User(userName, "", grantedAuths);
  final UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
          principal, "", grantedAuths);
  WebAuthenticationDetails webDetails = new WebAuthenticationDetails(httpRequest);
  authentication.setDetails(webDetails);
  return authentication;
}
 
Example 3
Source Project: metron   Source File: KnoxSSOAuthenticationFilterTest.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void getAuthenticationShouldProperlyPopulateAuthentication() {
  LdapTemplate ldapTemplate = mock(LdapTemplate.class);
  KnoxSSOAuthenticationFilter knoxSSOAuthenticationFilter = spy(new KnoxSSOAuthenticationFilter("ou=people,dc=hadoop,dc=apache,dc=org",
          mock(Path.class),
          "knoxKeyString",
          "knoxCookie",
          ldapTemplate
  ));

  HttpServletRequest request = mock(HttpServletRequest.class);

  when(ldapTemplate.search(any(LdapQuery.class), any(AttributesMapper.class))).thenReturn(Arrays.asList("USER", "ADMIN"));

  Authentication authentication = knoxSSOAuthenticationFilter.getAuthentication("userName", request);
  Object[] grantedAuthorities = authentication.getAuthorities().toArray();
  assertEquals("ROLE_USER", grantedAuthorities[0].toString());
  assertEquals("ROLE_ADMIN", grantedAuthorities[1].toString());
  assertEquals("userName", authentication.getName());
}
 
Example 4
Source Project: geofence   Source File: LdapUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates and LDAP filter from the DAO search filter. Currently only "property = value" filters are supported.
 *
 * @param filter
 * @return
 */
public static String createLDAPFilter(Filter filter, AttributesMapper mapper)
{
    // TODO add other filter types
    if (filter.getOperator() == Filter.OP_EQUAL) {
        String propertyName = filter.getProperty();
        if (mapper instanceof LdapAttributesMapper) {
            propertyName = ((LdapAttributesMapper) mapper)
                    .getLdapAttribute(propertyName);
        }
        return propertyName + "=" + filter.getValue().toString();
    } else {
        LOGGER.error("MISSING IMPLEMENTATION FOR " + filter);
    }
    return null;
}
 
Example 5
@Test
public void testServerStartup() throws Exception {
    ctx = new ClassPathXmlApplicationContext("/applicationContext-testContextSource.xml");
    LdapTemplate ldapTemplate = ctx.getBean(LdapTemplate.class);
    assertThat(ldapTemplate).isNotNull();

    List<String> list = ldapTemplate.search(
            LdapQueryBuilder.query().where("objectclass").is("person"),
            new AttributesMapper<String>() {
                public String mapFromAttributes(Attributes attrs)
                        throws NamingException {
                    return (String) attrs.get("cn").get();
                }
            });
    assertThat(list.size()).isEqualTo(5);
}
 
Example 6
@Test
public void testServerStartup() throws Exception {
    ctx = new ClassPathXmlApplicationContext("/applicationContext-ldifPopulator.xml");
    LdapTemplate ldapTemplate = ctx.getBean(LdapTemplate.class);
    assertThat(ldapTemplate).isNotNull();

    List<String> list = ldapTemplate.search(
            LdapQueryBuilder.query().where("objectclass").is("person"),
            new AttributesMapper<String>() {
                public String mapFromAttributes(Attributes attrs)
                        throws NamingException {
                    return (String) attrs.get("cn").get();
                }
            });
    assertThat(list.size()).isEqualTo(5);
}
 
Example 7
@Test
public void testUpdateWithException() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	try {
		dummyDao.updateWithException(dn, "Some Person", "Updated Person", "Updated description");
		fail("DummyException expected");
	}
	catch (DummyException expected) {
		assertThat(true).isTrue();
	}

	log.debug("Verifying result");

	Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Person");
			assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person");
			return new Object();
		}
	});

	assertThat(ldapResult).isNotNull();
}
 
Example 8
@Test
public void testUpdate() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	dummyDao.update(dn, "Some Person", "Updated Person", "Updated description");

	log.debug("Verifying result");
	Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Updated Person");
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	assertThat(ldapResult).isNotNull();

	dummyDao.update(dn, "Some Person", "Person", "Sweden, Company1, Some Person");
}
 
Example 9
@Test
public void testUpdateAndRename() {
	String dn = "cn=Some Person2,ou=company1,ou=Sweden";
	String newDn = "cn=Some Person2,ou=company2,ou=Sweden";
	// Perform test
	dummyDao.updateAndRename(dn, newDn, "Updated description");

	// Verify that entry was moved and updated.
	Object object = ldapTemplate.lookup(newDn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	assertThat(object).isNotNull();
	dummyDao.updateAndRename(newDn, dn, "Sweden, Company1, Some Person2");
}
 
Example 10
@Test
public void testModifyAttributesWithException() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	try {
		// Perform test
		dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description");
		fail("DummyException expected");
	}
	catch (DummyException expected) {
		assertThat(true).isTrue();
	}

	// Verify result - check that the operation was properly rolled back
	Object result = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Person");
			assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person");
			return new Object();
		}
	});

	assertThat(result).isNotNull();
}
 
Example 11
@Test
public void testModifyAttributes() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	// Perform test
	dummyDao.modifyAttributes(dn, "Updated lastname", "Updated description");

	// Verify result - check that the operation was not rolled back
	Object result = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Updated lastname");
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	assertThat(result).isNotNull();
}
 
Example 12
@Test
public void testUnbindWithException() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	try {
		// Perform test
		dummyDao.unbindWithException(dn, "Some Person");
		fail("DummyException expected");
	}
	catch (DummyException expected) {
		assertThat(true).isTrue();
	}

	// Verify result - check that the operation was properly rolled back
	Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			// Just verify that the entry still exists.
			return new Object();
		}
	});

	assertThat(ldapResult).isNotNull();
}
 
Example 13
@Test
public void testUpdateWithException() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	try {
		dummyDao.updateWithException(dn, "Some Person", "Updated Person", "Updated description");
		fail("DummyException expected");
	}
	catch (DummyException expected) {
		assertThat(true).isTrue();
	}

	log.debug("Verifying result");

	Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Person");
			assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person");
			return new Object();
		}
	});

	assertThat(ldapResult).isNotNull();
}
 
Example 14
@Test
public void testUpdate() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	dummyDao.update(dn, "Some Person", "Updated Person", "Updated description");

	log.debug("Verifying result");
	Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Updated Person");
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	assertThat(ldapResult).isNotNull();

	dummyDao.update(dn, "Some Person", "Person", "Sweden, Company1, Some Person");
}
 
Example 15
@Test
public void testUpdateAndRename() {
	String dn = "cn=Some Person2,ou=company1,ou=Sweden";
	String newDn = "cn=Some Person2,ou=company2,ou=Sweden";
	// Perform test
	dummyDao.updateAndRename(dn, newDn, "Updated description");

	// Verify that entry was moved and updated.
	Object object = ldapTemplate.lookup(newDn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	assertThat(object).isNotNull();
	dummyDao.updateAndRename(newDn, dn, "Sweden, Company1, Some Person2");
}
 
Example 16
@Test
public void testModifyAttributesWithException() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	try {
		// Perform test
		dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description");
		fail("DummyException expected");
	}
	catch (DummyException expected) {
		assertThat(true).isTrue();
	}

	// Verify result - check that the operation was properly rolled back
	Object result = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Person");
			assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person");
			return new Object();
		}
	});

	assertThat(result).isNotNull();
}
 
Example 17
@Test
public void testModifyAttributes() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	// Perform test
	dummyDao.modifyAttributes(dn, "Updated lastname", "Updated description");

	// Verify result - check that the operation was not rolled back
	Object result = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Updated lastname");
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	assertThat(result).isNotNull();
}
 
Example 18
@Test
public void testUnbindWithException() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	try {
		// Perform test
		dummyDao.unbindWithException(dn, "Some Person");
		fail("DummyException expected");
	}
	catch (DummyException expected) {
		assertThat(true).isTrue();
	}

	// Verify result - check that the operation was properly rolled back
	Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			// Just verify that the entry still exists.
			return new Object();
		}
	});

	assertThat(ldapResult).isNotNull();
}
 
Example 19
@Test
public void testUpdate() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1));
	person.setLastname("Updated Person");
	person.setDescription("Updated description");

	dummyDao.update(person);

	log.debug("Verifying result");
	Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Updated Person");
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	OrgPerson updatedPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1));
	assertThat(updatedPerson.getLastname()).isEqualTo("Updated Person");
	assertThat(updatedPerson.getDescription()).isEqualTo("Updated description");
	assertThat(ldapResult).isNotNull();
}
 
Example 20
@Test
public void testUpdateAndRename() {
	String dn = "cn=Some Person2,ou=company1,ou=Sweden";
	String newDn = "cn=Some Person2,ou=company2,ou=Sweden";
	// Perform test
	dummyDao.updateAndRename(dn, newDn, "Updated description");

	// Verify that entry was moved and updated.
	Object object = ldapTemplate.lookup(newDn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	assertThat(object).isNotNull();
}
 
Example 21
@Test
public void testModifyAttributesWithException() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	try {
		// Perform test
		dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description");
		fail("DummyException expected");
	}
	catch (DummyException expected) {
		assertThat(true).isTrue();
	}

	// Verify result - check that the operation was properly rolled back
	Object result = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Person");
			assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person");
			return new Object();
		}
	});

	assertThat(result).isNotNull();
}
 
Example 22
@Test
public void testModifyAttributes() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	// Perform test
	dummyDao.modifyAttributes(dn, "Updated lastname", "Updated description");

	// Verify result - check that the operation was not rolled back
	Object result = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Updated lastname");
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	assertThat(result).isNotNull();
}
 
Example 23
@Test
public void testUpdate() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1));
	person.setLastname("Updated Person");
	person.setDescription("Updated description");

	dummyDao.update(person);

	log.debug("Verifying result");
	Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Updated Person");
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	OrgPerson updatedPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1));
	assertThat(updatedPerson.getLastname()).isEqualTo("Updated Person");
	assertThat(updatedPerson.getDescription()).isEqualTo("Updated description");
	assertThat(ldapResult).isNotNull();
}
 
Example 24
@Test
public void testUpdateAndRename() {
	String dn = "cn=Some Person2,ou=company1,ou=Sweden";
	String newDn = "cn=Some Person2,ou=company2,ou=Sweden";
	// Perform test
	dummyDao.updateAndRename(dn, newDn, "Updated description");

	// Verify that entry was moved and updated.
	Object object = ldapTemplate.lookup(newDn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	assertThat(object).isNotNull();
}
 
Example 25
@Test
public void testModifyAttributesWithException() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	try {
		// Perform test
		dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description");
		fail("DummyException expected");
	}
	catch (DummyException expected) {
		assertThat(true).isTrue();
	}

	// Verify result - check that the operation was properly rolled back
	Object result = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Person");
			assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person");
			return new Object();
		}
	});

	assertThat(result).isNotNull();
}
 
Example 26
@Test
public void testModifyAttributes() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	// Perform test
	dummyDao.modifyAttributes(dn, "Updated lastname", "Updated description");

	// Verify result - check that the operation was not rolled back
	Object result = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Updated lastname");
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	assertThat(result).isNotNull();
}
 
Example 27
@Test
public void testUpdateAndRename() {
	String dn = "cn=Some Person2,ou=company1,ou=Sweden";
	String newDn = "cn=Some Person2,ou=company2,ou=Sweden";
	// Perform test
	dummyDao.updateAndRename(dn, newDn, "Updated description");

	// Verify that entry was moved and updated.
	Object object = ldapTemplate.lookup(newDn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	assertThat(object).isNotNull();
	dummyDao.updateAndRename(newDn, dn, "Sweden, Company1, Some Person2");
}
 
Example 28
@Test
public void testModifyAttributesWithException() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	try {
		// Perform test
		dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description");
		fail("DummyException expected");
	}
	catch (DummyException expected) {
		assertThat(true).isTrue();
	}

	// Verify result - check that the operation was properly rolled back
	Object result = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Person");
			assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person");
			return new Object();
		}
	});

	assertThat(result).isNotNull();
}
 
Example 29
@Test
public void testModifyAttributes() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	// Perform test
	dummyDao.modifyAttributes(dn, "Updated lastname", "Updated description");

	// Verify result - check that the operation was not rolled back
	Object result = ldapTemplate.lookup(dn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("sn").get()).isEqualTo("Updated lastname");
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	assertThat(result).isNotNull();
	dummyDao.update(dn, "Some Person", "Person", "Sweden, Company1, Some Person");
}
 
Example 30
@Test
public void testUpdateAndRename() {
	String dn = "cn=Some Person2,ou=company1,ou=Sweden";
	String newDn = "cn=Some Person2,ou=company2,ou=Sweden";
	// Perform test
	dummyDao.updateAndRename(dn, newDn, "Updated description");

	// Verify that entry was moved and updated.
	Object object = ldapTemplate.lookup(newDn, new AttributesMapper() {
		public Object mapFromAttributes(Attributes attributes) throws NamingException {
			assertThat(attributes.get("description").get()).isEqualTo("Updated description");
			return new Object();
		}
	});

	assertThat(object).isNotNull();
	dummyDao.updateAndRename(newDn, dn, "Sweden, Company1, Some Person2");
}