
org.connid.bundles.ldap.LdapConnection Maven / Gradle / Ivy
The newest version!
/**
* ====================
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright 2011-2013 Tirasa. All rights reserved.
*
* The contents of this file are subject to the terms of the Common Development
* and Distribution License("CDDL") (the "License"). You may not use this file
* except in compliance with the License.
*
* You can obtain a copy of the License at https://oss.oracle.com/licenses/CDDL
* See the License for the specific language governing permissions and limitations
* under the License.
*
* When distributing the Covered Code, include this CDDL Header Notice in each file
* and include the License file at https://oss.oracle.com/licenses/CDDL.
* If applicable, add the following below this CDDL Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
* ====================
*/
package org.connid.bundles.ldap;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableSet;
import static org.connid.bundles.ldap.commons.LdapUtil.getStringAttrValue;
import static org.connid.bundles.ldap.commons.LdapUtil.getStringAttrValues;
import static org.connid.bundles.ldap.commons.LdapUtil.nullAsEmpty;
import static org.identityconnectors.common.CollectionUtil.newCaseInsensitiveSet;
import static org.identityconnectors.common.StringUtil.isNotBlank;
import com.sun.jndi.ldap.ctl.PasswordExpiredResponseControl;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import org.connid.bundles.ldap.commons.LdapNativeSchema;
import org.connid.bundles.ldap.commons.ServerNativeSchema;
import org.connid.bundles.ldap.commons.StaticNativeSchema;
import org.connid.bundles.ldap.schema.LdapSchemaMapping;
import org.identityconnectors.common.Pair;
import org.identityconnectors.common.logging.Log;
import org.identityconnectors.common.security.GuardedString;
import org.identityconnectors.common.security.GuardedString.Accessor;
import org.identityconnectors.framework.common.exceptions.ConnectorException;
import org.identityconnectors.framework.common.exceptions.ConnectorSecurityException;
import org.identityconnectors.framework.common.exceptions.PasswordExpiredException;
public class LdapConnection {
// TODO: SASL authentication, "dn:entryDN" user name.
// The LDAP attributes with a byte array syntax.
private static final Set LDAP_BINARY_SYNTAX_ATTRS;
// The LDAP attributes which require the binary option for transfer.
private static final Set LDAP_BINARY_OPTION_ATTRS;
static {
// Cf. http://java.sun.com/products/jndi/tutorial/ldap/misc/attrs.html.
LDAP_BINARY_SYNTAX_ATTRS = newCaseInsensitiveSet();
LDAP_BINARY_SYNTAX_ATTRS.add("audio");
LDAP_BINARY_SYNTAX_ATTRS.add("jpegPhoto");
LDAP_BINARY_SYNTAX_ATTRS.add("photo");
LDAP_BINARY_SYNTAX_ATTRS.add("personalSignature");
LDAP_BINARY_SYNTAX_ATTRS.add("userPassword");
LDAP_BINARY_SYNTAX_ATTRS.add("userCertificate");
LDAP_BINARY_SYNTAX_ATTRS.add("caCertificate");
LDAP_BINARY_SYNTAX_ATTRS.add("authorityRevocationList");
LDAP_BINARY_SYNTAX_ATTRS.add("deltaRevocationList");
LDAP_BINARY_SYNTAX_ATTRS.add("certificateRevocationList");
LDAP_BINARY_SYNTAX_ATTRS.add("crossCertificatePair");
LDAP_BINARY_SYNTAX_ATTRS.add("x500UniqueIdentifier");
LDAP_BINARY_SYNTAX_ATTRS.add("supportedAlgorithms");
// Java serialized objects.
LDAP_BINARY_SYNTAX_ATTRS.add("javaSerializedData");
// These seem to only be present in Active Directory.
LDAP_BINARY_SYNTAX_ATTRS.add("thumbnailPhoto");
LDAP_BINARY_SYNTAX_ATTRS.add("thumbnailLogo");
// Cf. RFC 4522 and RFC 4523.
LDAP_BINARY_OPTION_ATTRS = newCaseInsensitiveSet();
LDAP_BINARY_OPTION_ATTRS.add("userCertificate");
LDAP_BINARY_OPTION_ATTRS.add("caCertificate");
LDAP_BINARY_OPTION_ATTRS.add("authorityRevocationList");
LDAP_BINARY_OPTION_ATTRS.add("deltaRevocationList");
LDAP_BINARY_OPTION_ATTRS.add("certificateRevocationList");
LDAP_BINARY_OPTION_ATTRS.add("crossCertificatePair");
LDAP_BINARY_OPTION_ATTRS.add("supportedAlgorithms");
}
private static final String LDAP_CTX_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
private static final Log LOG = Log.getLog(LdapConnection.class);
private final LdapConfiguration config;
private final LdapSchemaMapping schemaMapping;
private LdapContext initCtx;
private Set supportedControls;
private ServerType serverType;
public LdapConnection(LdapConfiguration config) {
this.config = config;
schemaMapping = new LdapSchemaMapping(this);
}
public String format(String key, String dflt, Object... args) {
return config.getConnectorMessages().format(key, dflt, args);
}
public LdapConfiguration getConfiguration() {
return config;
}
public LdapContext getInitialContext() {
if (initCtx != null) {
return initCtx;
}
initCtx = connect(config.getPrincipal(), config.getCredentials());
return initCtx;
}
private LdapContext connect(String principal, GuardedString credentials) {
Pair pair = createContext(principal, credentials);
if (pair.first.getType().equals(AuthenticationResultType.SUCCESS)) {
return pair.second;
}
pair.first.propagate();
throw new IllegalStateException("Should never get here");
}
private Pair createContext(String principal, GuardedString credentials) {
final List> result =
new ArrayList>(1);
final Hashtable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy