Refine search
private Attributes buildAttributes( String dn, String[] attributes, String[] values, String multValuedSeparator ) { Attributes attrs = new javax.naming.directory.BasicAttributes( true ); int nrAttributes = attributes.length; for ( int i = 0; i < nrAttributes; i++ ) { if ( !Utils.isEmpty( values[i] ) ) { // We have a value String value = values[i].trim(); if ( multValuedSeparator != null && value.indexOf( multValuedSeparator ) > 0 ) { Attribute attr = new javax.naming.directory.BasicAttribute( attributes[i] ); for ( String attribute : value.split( multValuedSeparator ) ) { attr.add( attribute ); } attrs.put( attr ); } else { attrs.put( attributes[i], value ); } } } return attrs; }
@Test public void ldapCompareOperationIsUsedWhenPasswordIsNotRetrieved() throws Exception { final DirContext dirCtx = mock(DirContext.class); final BaseLdapPathContextSource source = mock(BaseLdapPathContextSource.class); final BasicAttributes attrs = new BasicAttributes(); attrs.put(new BasicAttribute("uid", "bob")); PasswordComparisonAuthenticator authenticator = new PasswordComparisonAuthenticator( source); authenticator.setUserDnPatterns(new String[] { "cn={0},ou=people" }); // Get the mock to return an empty attribute set when(source.getReadOnlyContext()).thenReturn(dirCtx); when(dirCtx.getAttributes(eq("cn=Bob,ou=people"), any(String[].class))) .thenReturn(attrs); when(dirCtx.getNameInNamespace()).thenReturn("dc=springframework,dc=org"); // Setup a single return value (i.e. success) final NamingEnumeration searchResults = new BasicAttributes("", null).getAll(); when( dirCtx.search(eq("cn=Bob,ou=people"), eq("(userPassword={0})"), any(Object[].class), any(SearchControls.class))).thenReturn( searchResults); authenticator.authenticate(new UsernamePasswordAuthenticationToken("Bob", "bobspassword")); } }
private BasicAttributes createSrvRecords() { BasicAttributes records = new BasicAttributes(); BasicAttribute record = new BasicAttribute("SRV"); // the structure of the service records is: // priority weight port hostname // for more information: http://en.wikipedia.org/wiki/SRV_record record.add("20 80 389 kdc3.springsource.com."); record.add("10 70 389 kdc.springsource.com."); record.add("20 20 389 kdc4.springsource.com."); record.add("10 30 389 kdc2.springsource.com"); records.put(record); return records; } }
@Test public void testPasswordAttributeIsMappedCorrectly() throws Exception { LdapUserDetailsMapper mapper = new LdapUserDetailsMapper(); mapper.setPasswordAttributeName("myappsPassword"); BasicAttributes attrs = new BasicAttributes(); attrs.put(new BasicAttribute("myappsPassword", "mypassword".getBytes())); DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName")); ctx.setAttributeValue("uid", "ani"); LdapUserDetails user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani", AuthorityUtils.NO_AUTHORITIES); assertThat(user.getPassword()).isEqualTo("mypassword"); }
@Override public void clearCredentials() throws RealmUnavailableException { try { Attributes attributes = new BasicAttributes(); attributes.put(new BasicAttribute(userPasswordAttributeName)); context.modifyAttributes(distinguishedName, DirContext.REMOVE_ATTRIBUTE, attributes); } catch (NoSuchAttributeException e) { // ignore if already clear } catch (NamingException e) { throw log.ldapRealmCredentialClearingFailed(distinguishedName, e); } } }
@Test public void testGetCompensatingModificationItem_RemoveFullExistingAttribute() throws NamingException { BasicAttribute attribute = new BasicAttribute("someattr"); attribute.add("value1"); attribute.add("value2"); Attributes attributes = new BasicAttributes(); attributes.put(attribute); ModificationItem originalItem = new ModificationItem( DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("someattr")); // Perform test ModificationItem result = tested.getCompensatingModificationItem( attributes, originalItem); // Verify result assertThat(result.getModificationOp()).isEqualTo(DirContext.ADD_ATTRIBUTE); Attribute resultAttribute = result.getAttribute(); assertThat(resultAttribute.getID()).isEqualTo("someattr"); Object object = resultAttribute.get(0); assertThat(object).isEqualTo("value1"); assertThat(resultAttribute.get(1)).isEqualTo("value2"); }
/** * Get all user names. */ public synchronized List getAllUserNames() { ArrayList allUsers = new ArrayList(); try { Attributes matchAttrs = new BasicAttributes(true); matchAttrs.put(mObjClassAttr); NamingEnumeration answers = mAdminContext.search(mstRoot, matchAttrs, UID_ATTRS); while (answers.hasMore()) { SearchResult sr = (SearchResult)answers.next(); String uid = sr.getAttributes().get(User.ATTR_LOGIN).get().toString(); allUsers.add(uid); } } catch(Exception ex) { getLogger().error("LdapUserManager.getAllUserNames()", ex); } Collections.sort(allUsers); return allUsers; }
private Attributes createRoleAttributes(TgwRole role){ Attributes attrs = new BasicAttributes(); attrs.put(roleObjectClass); attrs.put(new BasicAttribute(rolePrefix,role.getName())); attrs.put(new BasicAttribute(descriptionAttribute,role.getDescription())); return attrs; }
@Test public void testRemoveOneOfSeveralDnAttributeSyntacticallyEqual() throws NamingException { BasicAttributes attributes = new BasicAttributes(); BasicAttribute attribute = new BasicAttribute("uniqueMember", "cn=john doe,OU=company"); attribute.add("cn=jane doe, ou=company"); attributes.put(attribute); DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups")); tested.setUpdateMode(true); tested.removeAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=john doe, ou=company")); ModificationItem[] modificationItems = tested.getModificationItems(); assertThat(modificationItems.length).isEqualTo(1); ModificationItem modificationItem = modificationItems[0]; assertThat(modificationItem.getModificationOp()).isEqualTo(DirContext.REMOVE_ATTRIBUTE); assertThat(modificationItem.getAttribute().getID()).isEqualTo("uniqueMember"); assertThat(modificationItem.getAttribute().get()).isEqualTo("cn=john doe,OU=company"); }
@Test public void testLookup_String_AttributesMapper() throws Exception { expectGetReadOnlyContext(); BasicAttributes expectedAttributes = new BasicAttributes(); when(dirContextMock.getAttributes(DEFAULT_BASE_STRING)).thenReturn(expectedAttributes); Object expected = new Object(); when(attributesMapperMock.mapFromAttributes(expectedAttributes)).thenReturn(expected); Object actual = tested .lookup(DEFAULT_BASE_STRING, attributesMapperMock); verify(dirContextMock).close(); assertThat(actual).isSameAs(expected); }
@Override public void persistCredential(final Credential credential) throws RealmUnavailableException { // TODO - We probably need some better resolution here of the existing attributes - i.e. different types we would want to add, same type we would want to replace. try { byte[] composedPassword = UserPasswordPasswordUtil.composeUserPassword(credential.castAndApply(PasswordCredential.class, PasswordCredential::getPassword)); Assert.assertNotNull(composedPassword); Attributes attributes = new BasicAttributes(); attributes.put(userPasswordAttributeName, composedPassword); context.modifyAttributes(distinguishedName, DirContext.REPLACE_ATTRIBUTE, attributes); } catch (NamingException | IOException e) { throw log.ldapRealmCredentialPersistingFailed(credential.toString(), distinguishedName, e); } }
@Test public void testSearch_StringBase_CallbackHandler() throws Exception { expectGetReadOnlyContext(); SearchControls controls = searchControlsOneLevel(); SearchResult searchResult = new SearchResult("", new Object(), new BasicAttributes()); singleSearchResultWithStringBase(controls, searchResult); tested.search(DEFAULT_BASE_STRING, "(ou=somevalue)", 1, true, handlerMock); verify(handlerMock).handleNameClassPair(searchResult); verify(dirContextMock).close(); }
private Attributes setupAttributes() { Attributes attributes = new BasicAttributes(); BasicAttribute ocattr = new BasicAttribute("objectclass"); ocattr.add("top"); ocattr.add("person"); attributes.put(ocattr); attributes.put("cn", "Some Person4"); attributes.put("sn", "Person4"); return attributes; }
public void updateAttribute(Name dn, Attribute a) throws NamingException { BasicAttributes atts = new BasicAttributes(); atts.put(a); modifyAttributes(dn, 2, atts); }
@Test public void testAddDnAttributeNewValue() throws NamingException { BasicAttributes attributes = new BasicAttributes(); attributes.put("uniqueMember", "cn=john doe, ou=company"); DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups")); tested.setUpdateMode(true); tested.addAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=jane doe, ou=company")); ModificationItem[] modificationItems = tested.getModificationItems(); assertThat(modificationItems.length).isEqualTo(1); ModificationItem modificationItem = modificationItems[0]; assertThat(modificationItem.getModificationOp()).isEqualTo(DirContext.ADD_ATTRIBUTE); assertThat(modificationItem.getAttribute().getID()).isEqualTo("uniqueMember"); assertThat(modificationItem.getAttribute().get()).isEqualTo("cn=jane doe, ou=company"); }
/** * see if a user is in a group * @param context * @param groupName * @return the list * @throws NamingException */ public static List<String> groupList(DirContext context, String groupName) throws NamingException { Attributes searchAttributes = new BasicAttributes(); searchAttributes.put(new BasicAttribute("cn", groupName)); NamingEnumeration namingEnumeration = context.search( "ou=groups", searchAttributes, new String[]{"hasMember"}); List<String> members = retrieveAttributeStringListValue(namingEnumeration, "hasMember"); return members; }
/** * This method performs a simple DNS lookup using JNDI * @param queryInput String containing the query body itself (e.g. 4.3.3.1.in-addr.arpa); * @param queryType String containing the query type (e.g. TXT); */ protected Attributes doLookup(String queryInput, String queryType) throws NamingException { // This is a simple DNS lookup attempt Attributes attrs; try { // Uses pre-existing context to resolve attrs = ictx.getAttributes(queryInput, new String[]{queryType}); return attrs; } catch ( NameNotFoundException e) { getLogger().debug("Resolution for domain {} failed due to {}", new Object[]{queryInput, e}); attrs = new BasicAttributes(queryType, "NXDOMAIN",true); return attrs; } }
@Test public void testBind() throws Exception { expectGetReadWriteContext(); Object expectedObject = new Object(); BasicAttributes expectedAttributes = new BasicAttributes(); tested.bind(nameMock, expectedObject, expectedAttributes); verify(dirContextMock).bind(nameMock, expectedObject, expectedAttributes); verify(dirContextMock).close(); }
public Attributes getAttributes(Name name, String[] attrIds) throws NamingException { Attributes attrs; if (attrIds == null) { attrs = getAttributes(name); } else { attrs = new BasicAttributes(); Set atns = new HashSet(Arrays.asList(attrIds)); for (NamingEnumeration e = getAttributes(name).getAll(); e.hasMore();) { Attribute attr = (Attribute) e.next(); if (atns.contains(attr.getID())) attrs.put(attr); } } return attrs; }
@Test public void testRebind_String() throws Exception { expectGetReadWriteContext(); Object expectedObject = new Object(); BasicAttributes expectedAttributes = new BasicAttributes(); tested.rebind(DEFAULT_BASE_STRING, expectedObject, expectedAttributes); verify(dirContextMock).rebind(DEFAULT_BASE_STRING, expectedObject, expectedAttributes); verify(dirContextMock).close(); }