@Override public void clearCredentials() throws RealmUnavailableException { try { Attributes attributes = new BasicAttributes(); attributes.put(new BasicAttribute(algorithmAttributeName)); attributes.put(new BasicAttribute(hashAttributeName)); attributes.put(new BasicAttribute(seedAttributeName)); attributes.put(new BasicAttribute(sequenceAttributeName)); context.modifyAttributes(distinguishedName, DirContext.REMOVE_ATTRIBUTE, attributes); } catch (NoSuchAttributeException e) { // ignore if already clear } catch (NamingException e) { throw log.ldapRealmCredentialClearingFailed(distinguishedName, e); } } }
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; } }
@Override public void engineSetCertificateEntry(String alias, Certificate cert) throws KeyStoreException { List<ModificationItem> items = new LinkedList<>(); try { BasicAttribute attribute = new BasicAttribute(certificateAttribute); attribute.add(cert.getEncoded()); items.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attribute)); } catch (CertificateEncodingException e) { throw log.ldapKeyStoreFailedToSerializeCertificate(alias, e); } storeAttributes(alias, items); }
@Test public void testRemoveAttributeValueAttributeWithOtherAndSameValueExists() throws NamingException { BasicAttribute basicAttribute = new BasicAttribute("abc"); basicAttribute.add("123"); basicAttribute.add("321"); tested.setAttribute(basicAttribute); // Perform test tested.removeAttributeValue("abc", "123"); Attributes attributes = tested.getAttributes(); Attribute attr = attributes.get("abc"); assertThat(attr).isNotNull(); assertThat(attr.size()).isEqualTo(1); assertThat(attr.get()).isEqualTo("321"); }
private Attributes createUserAttributes(TgwUser user){ Attributes attrs = new BasicAttributes(); attrs.put(userObjectClass); attrs.put(new BasicAttribute(userPrefix,user.getName())); attrs.put(new BasicAttribute(passwordAttribute,user.getPassword())); attrs.put(new BasicAttribute("cn",user.getName())); attrs.put(new BasicAttribute("sn",user.getName())); attrs.put(new BasicAttribute(descriptionAttribute,user.getDescription())); return attrs; }
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")); } }
public Object executeWithContext(DirContext ctx) throws NamingException { for (GrantedAuthority authority : authorities) { String group = convertAuthorityToGroup(authority); DistinguishedName fullDn = LdapUtils.getFullDn(userDn, ctx); ModificationItem addGroup = new ModificationItem(modType, new BasicAttribute(groupMemberAttributeName, fullDn.toUrl())); ctx.modifyAttributes(buildGroupDn(group), new ModificationItem[] { addGroup }); } return null; } });
@Test public void testChangeAttribute() throws Exception { final Attributes fixtureAttrs = new BasicAttributes(); fixtureAttrs.put(new BasicAttribute("abc", "123")); class TestableDirContextAdapter extends DirContextAdapter { public TestableDirContextAdapter() { super(fixtureAttrs, null); setUpdateMode(true); } } tested = new TestableDirContextAdapter(); tested.setAttributeValue("abc", "234"); // change ModificationItem[] mods = tested.getModificationItems(); assertThat(mods.length).isEqualTo(1); assertThat(mods[0].getModificationOp()).isEqualTo(DirContext.REPLACE_ATTRIBUTE); Attribute attr = mods[0].getAttribute(); assertThat((String) attr.getID()).isEqualTo("abc"); assertThat((String) attr.get()).isEqualTo("234"); }
@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 testAddAttributeValueAttributeWithOtherValueExists() throws NamingException { tested.setAttribute(new BasicAttribute("abc", "321")); // Perform test tested.addAttributeValue("abc", "123"); Attributes attrs = tested.getAttributes(); Attribute attr = attrs.get("abc"); assertThat((String) attr.get(0)).isEqualTo("321"); assertThat((String) attr.get(1)).isEqualTo("123"); }
private static Attribute attr(String attrID, byte[]... vals) { Attribute attr = new BasicAttribute(attrID); for (byte[] val : vals) attr.add(val); return attr; }
public void addMember(LDAPEntry childEntry) { Attribute memberAttribute = getLDAPAttributes().get(MEMBER); if (memberAttribute != null) { if (memberAttribute.contains(SPACE_STRING)) { memberAttribute.remove(SPACE_STRING); } } else { memberAttribute = new BasicAttribute(MEMBER); } memberAttribute.add(childEntry.getDN()); getLDAPAttributes().put(memberAttribute); }
private void modifyAuthorities(final DistinguishedName userDn, final Collection<? extends GrantedAuthority> authorities, final int modType) { template.executeReadWrite(new ContextExecutor() { public Object executeWithContext(DirContext ctx) throws NamingException { for (GrantedAuthority authority : authorities) { String group = convertAuthorityToGroup(authority); DistinguishedName fullDn = LdapUtils.getFullDn(userDn, ctx); ModificationItem addGroup = new ModificationItem(modType, new BasicAttribute(groupMemberAttributeName, fullDn.toUrl())); ctx.modifyAttributes(buildGroupDn(group), new ModificationItem[] { addGroup }); } return null; } }); }
public void addChildGroup(LDAPGroup childGroup) { Attribute memberAttribute = getLDAPAttributes().get(MEMBER); if (memberAttribute == null) { memberAttribute = new BasicAttribute(MEMBER); getLDAPAttributes().put(memberAttribute); } memberAttribute.add(getDN(childGroup.getName())); }
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; }
/** * Set the LDAP objectClasses to the attributes. */ public void setLdapObjectClasses(Attributes attributes) { if (ldapObjectclasses != null) { BasicAttribute ocAttrs = new BasicAttribute("objectclass"); for (int i = 0; i < ldapObjectclasses.length; i++) { ocAttrs.add(ldapObjectclasses[i]); } attributes.put(ocAttrs); } }
public LDAPGroup(String groupDNSuffix) { super(groupDNSuffix); Attribute oc = new BasicAttribute(OBJECT_CLASS); oc.add("top"); oc.add("groupOfNames"); getLDAPAttributes().put(oc); }
/** * 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; }
private void collectModifications(int modificationType, Attribute attr, Collection values, Collection c) { if (values.size() > 0) { BasicAttribute modificationAttribute = new BasicAttribute(attr .getID()); for (Iterator iter = values.iterator(); iter.hasNext();) { modificationAttribute.add(iter.next()); } c .add(new ModificationItem(modificationType, modificationAttribute)); } }