Search in sources :

Example 1 with Attribute

use of com.unboundid.ldap.sdk.Attribute in project gocd by gocd.

the class InMemoryLdapServerForTests method addOrganizationalUnit.

public LDIFRecord addOrganizationalUnit(String nameOfOU, String dnOfOU) throws LDAPException {
    LDIFAddChangeRecord record = new LDIFAddChangeRecord(dnOfOU, new Attribute("objectClass", "top", "organizationalUnit"), new Attribute("ou", nameOfOU));
    record.processChange(server);
    return record;
}
Also used : Attribute(com.unboundid.ldap.sdk.Attribute) LDIFAddChangeRecord(com.unboundid.ldif.LDIFAddChangeRecord)

Example 2 with Attribute

use of com.unboundid.ldap.sdk.Attribute in project oxCore by GluuFederation.

the class LdapEntryManager method persist.

@Override
protected void persist(String dn, List<AttributeData> attributes) {
    List<Attribute> ldapAttributes = new ArrayList<Attribute>(attributes.size());
    for (AttributeData attribute : attributes) {
        String attributeName = attribute.getName();
        String[] attributeValues = attribute.getValues();
        if (ArrayHelper.isNotEmpty(attributeValues) && StringHelper.isNotEmpty(attributeValues[0])) {
            if (ldapOperationService.isCertificateAttribute(attributeName)) {
                byte[][] binaryValues = toBinaryValues(attributeValues);
                ldapAttributes.add(new Attribute(attributeName + ";binary", binaryValues));
            } else {
                ldapAttributes.add(new Attribute(attributeName, attributeValues));
            }
        }
    }
    // Persist entry
    try {
        boolean result = this.ldapOperationService.addEntry(dn, ldapAttributes);
        if (!result) {
            throw new EntryPersistenceException(String.format("Failed to persist entry: %s", dn));
        }
    } catch (ConnectionException ex) {
        throw new EntryPersistenceException(String.format("Failed to persist entry: %s", dn), ex.getCause());
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to persist entry: %s", dn), ex);
    }
}
Also used : Attribute(com.unboundid.ldap.sdk.Attribute) ArrayList(java.util.ArrayList) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) AttributeData(org.gluu.persist.model.AttributeData) ConnectionException(org.gluu.persist.exception.operation.ConnectionException) SearchException(org.gluu.persist.exception.operation.SearchException) AuthenticationException(org.gluu.persist.exception.operation.AuthenticationException) MappingException(org.gluu.persist.exception.mapping.MappingException) SearchScopeException(org.gluu.persist.exception.operation.SearchScopeException) ParseException(java.text.ParseException) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) ConnectionException(org.gluu.persist.exception.operation.ConnectionException)

Example 3 with Attribute

use of com.unboundid.ldap.sdk.Attribute in project oxCore by GluuFederation.

the class LdapEntryManager method getAttributeDataList.

private List<AttributeData> getAttributeDataList(SearchResultEntry entry) {
    if (entry == null) {
        return null;
    }
    List<AttributeData> result = new ArrayList<AttributeData>();
    for (Attribute attribute : entry.getAttributes()) {
        String[] attributeValueStrings = NO_STRINGS;
        String attributeName = attribute.getName();
        if (LOG.isTraceEnabled()) {
            if (attribute.needsBase64Encoding()) {
                LOG.trace("Found binary attribute: " + attributeName + ". Is defined in LDAP config: " + ldapOperationService.isBinaryAttribute(attributeName));
            }
        }
        attributeValueStrings = attribute.getValues();
        if (attribute.needsBase64Encoding()) {
            boolean binaryAttribute = ldapOperationService.isBinaryAttribute(attributeName);
            boolean certificateAttribute = ldapOperationService.isCertificateAttribute(attributeName);
            if (binaryAttribute || certificateAttribute) {
                byte[][] attributeValues = attribute.getValueByteArrays();
                if (attributeValues != null) {
                    attributeValueStrings = new String[attributeValues.length];
                    for (int i = 0; i < attributeValues.length; i++) {
                        attributeValueStrings[i] = Base64.encodeBase64String(attributeValues[i]);
                        LOG.trace("Binary attribute: " + attribute.getName() + " value (hex): " + org.apache.commons.codec.binary.Hex.encodeHexString(attributeValues[i]) + " value (base64): " + attributeValueStrings[i]);
                    }
                }
            }
            if (certificateAttribute) {
                attributeName = ldapOperationService.getCertificateAttributeName(attributeName);
            }
        }
        AttributeData tmpAttribute = new AttributeData(attributeName, attributeValueStrings);
        result.add(tmpAttribute);
    }
    return result;
}
Also used : Attribute(com.unboundid.ldap.sdk.Attribute) ArrayList(java.util.ArrayList) AttributeData(org.gluu.persist.model.AttributeData)

Example 4 with Attribute

use of com.unboundid.ldap.sdk.Attribute in project cas by apereo.

the class LdapTestUtils method createLdapEntries.

/**
 * Creates the given LDAP entries.
 *
 * @param connection Open LDAP connection used to connect to directory.
 * @param entries    Collection of LDAP entries.
 */
public static void createLdapEntries(final LDAPConnection connection, final Collection<LdapEntry> entries) {
    try {
        for (final LdapEntry entry : entries) {
            final Collection<Attribute> attrs = new ArrayList<>(entry.getAttributeNames().length);
            attrs.addAll(entry.getAttributes().stream().map(a -> new Attribute(a.getName(), a.getStringValues())).collect(Collectors.toList()));
            final AddRequest ad = new AddRequest(entry.getDn(), attrs);
            connection.add(ad);
        }
    } catch (final Exception e) {
        LOGGER.error(e.getLocalizedMessage());
    }
}
Also used : AddRequest(com.unboundid.ldap.sdk.AddRequest) Attribute(com.unboundid.ldap.sdk.Attribute) LdapAttribute(org.ldaptive.LdapAttribute) ArrayList(java.util.ArrayList) LdapEntry(org.ldaptive.LdapEntry) IOException(java.io.IOException)

Example 5 with Attribute

use of com.unboundid.ldap.sdk.Attribute in project vertx-auth by vert-x3.

the class LDAPShiroAuthProviderTest method insertTestUsers.

/*
   * insert test users (only one currently), if we need more users, it would be
   * better to use a ldif file
   */
private void insertTestUsers() throws LDAPException {
    LDAPConnection connection = null;
    try {
        connection = new LDAPConnection("localhost", 10389);
        // entry tim/sausages
        List<Attribute> addRequest = new ArrayList<>();
        addRequest.add(new Attribute("objectClass", "top"));
        addRequest.add(new Attribute("objectClass", "person"));
        addRequest.add(new Attribute("objectClass", "organizationalPerson"));
        addRequest.add(new Attribute("objectClass", "inetOrgPerson"));
        addRequest.add(new Attribute("cn", "Tim Fox"));
        addRequest.add(new Attribute("sn", "Fox"));
        addRequest.add(new Attribute("mail", "tim@example.com"));
        addRequest.add(new Attribute("uid", "tim"));
        addRequest.add(new Attribute("userPassword", "{ssha}d0M5Z2qjOOCSCQInvZHgVAleCqU5I+ag9ZHXMw=="));
        connection.add("uid=tim,ou=users,dc=foo,dc=com", addRequest);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : Attribute(com.unboundid.ldap.sdk.Attribute) ArrayList(java.util.ArrayList) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection)

Aggregations

Attribute (com.unboundid.ldap.sdk.Attribute)18 ArrayList (java.util.ArrayList)6 LDAPException (com.unboundid.ldap.sdk.LDAPException)4 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)4 Entry (com.unboundid.ldap.sdk.Entry)3 SearchResult (com.unboundid.ldap.sdk.SearchResult)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 AttributeData (org.gluu.persist.model.AttributeData)3 Test (org.junit.Test)3 TeamModel (com.gitblit.models.TeamModel)2 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)2 AddRequest (com.unboundid.ldap.sdk.AddRequest)2 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)2 LDIFAddChangeRecord (com.unboundid.ldif.LDIFAddChangeRecord)2 AttributeManager (com.zimbra.cs.account.AttributeManager)2 MappingException (org.gluu.persist.exception.mapping.MappingException)2 ConnectionException (org.gluu.persist.exception.operation.ConnectionException)2 SearchException (org.gluu.persist.exception.operation.SearchException)2 LdapAttribute (org.ldaptive.LdapAttribute)2