private Map<String, String> entryToAttributes(final String[] attributes, final SearchResult result) { final LdapEntry entry = result.getEntry(); return Stream.of(attributes == null || attributes.length == 0 ? entry.getAttributeNames() : attributes) .collect(toMap(identity(), a -> readAttribute(entry, a))); }
/** * Creates a CAS principal with attributes if the LDAP entry contains principal attributes. * * @param username Username that was successfully authenticated which is used for principal ID when principal id is not specified. * @param ldapEntry LDAP entry that may contain principal attributes. * @return Principal if the LDAP entry contains at least a principal ID attribute value, null otherwise. * @throws LoginException On security policy errors related to principal creation. */ protected Principal createPrincipal(final String username, final LdapEntry ldapEntry) throws LoginException { LOGGER.debug("Creating LDAP principal for [{}] based on [{}] and attributes [{}]", username, ldapEntry.getDn(), ldapEntry.getAttributeNames()); val id = getLdapPrincipalIdentifier(username, ldapEntry); LOGGER.debug("LDAP principal identifier created is [{}]", id); val attributeMap = collectAttributesForLdapEntry(ldapEntry, id); LOGGER.debug("Created LDAP principal for id [{}] and [{}] attributes", id, attributeMap.size()); return this.principalFactory.createPrincipal(id, attributeMap); }
/** * Executes a {@link MergeOperation} with the supplied entry. * * @param entry to merge * * @return response for the merge operation * * @throws LdapException if the operation fails */ @Nonnull private Response<Void> merge(@Nonnull final LdapEntry entry) throws LdapException { Connection conn = null; try { conn = connectionFactory.getConnection(); final MergeOperation merge = new MergeOperation(conn); final MergeRequest request = new MergeRequest(entry); request.setIncludeAttributes(entry.getAttributeNames()); return merge.execute(request); } finally { conn.close(); } }
/** * Tests create with a collection. * * @param attr1 ldap attribute * @param attr2 ldap attribute */ @Test(groups = {"bean"}, dataProvider = "attributes") public void createCollection(final LdapAttribute attr1, final LdapAttribute attr2) { final Set<LdapAttribute> s = new HashSet<>(); s.add(attr1); final LdapEntry le = new LdapEntry("uid=1", s); le.addAttribute(attr2); AssertJUnit.assertEquals(attr1, le.getAttribute("GIVENNAME")); AssertJUnit.assertEquals(attr2, le.getAttribute("sn")); AssertJUnit.assertEquals(2, le.getAttributeNames().length); AssertJUnit.assertEquals(2, le.size()); AssertJUnit.assertEquals(le, new LdapEntry("uid=1", attr1, attr2)); le.removeAttribute("GIVENNAME"); AssertJUnit.assertEquals(1, le.size()); le.clear(); AssertJUnit.assertEquals(0, le.size()); }
/** * Tests create with one entry. * * @param attr1 ldap attribute * @param attr2 ldap attribute */ @Test(groups = {"bean"}, dataProvider = "attributes") public void createOne(final LdapAttribute attr1, final LdapAttribute attr2) { final LdapEntry le = new LdapEntry("uid=1", attr1); AssertJUnit.assertEquals(attr1, le.getAttribute()); AssertJUnit.assertEquals(attr1, le.getAttribute("givenName")); AssertJUnit.assertEquals(attr1, le.getAttribute("givenname")); AssertJUnit.assertEquals("givenName", le.getAttributeNames()[0]); AssertJUnit.assertEquals(1, le.size()); AssertJUnit.assertEquals(le, new LdapEntry("uid=1", attr1)); le.clear(); AssertJUnit.assertEquals(0, le.size()); }
/** * Tests create with two entries. * * @param attr1 ldap attribute * @param attr2 ldap attribute */ @Test(groups = {"bean"}, dataProvider = "attributes") public void createTwo(final LdapAttribute attr1, final LdapAttribute attr2) { final LdapEntry le = new LdapEntry("uid=1", attr2, attr1); AssertJUnit.assertEquals(attr1, le.getAttribute("givenName")); AssertJUnit.assertEquals(attr2, le.getAttribute("SN")); AssertJUnit.assertEquals(2, le.getAttributeNames().length); AssertJUnit.assertEquals(2, le.size()); AssertJUnit.assertEquals(le, new LdapEntry("uid=1", attr1, attr2)); le.removeAttribute(attr2); AssertJUnit.assertEquals(1, le.size()); le.clear(); AssertJUnit.assertEquals(0, le.size()); }