private LdapNetworkConnection bindAs(LdapNetworkConnection ldapConnection,
            LdapNetworkConnection useCredentialsFrom) {

        // Copy bind username and password from original config
        LdapConnectionConfig ldapConfig = useCredentialsFrom.getConfig();
        String username = ldapConfig.getName();
        String password = ldapConfig.getCredentials();

        // Parse bind username as an LDAP DN
        Dn userDN;
        try {
            userDN = new Dn(username);
        }
        catch (LdapInvalidDnException e) {
            logger.error("Credentials of existing connection cannot be used. "
                    + "The username used (\"{}\") is not a valid DN.", username);
            logger.debug("Cannot bind using invalid DN.", e);
            ldapConnection.close();
            return null;
        }

        // Bind using username/password from existing connection
        return bindAs(ldapConnection, userDN.getName(), password);

    }
Binds to the LDAP server indicated by a given LdapNetworkConnection
using the credentials that were used to bind another
LdapNetworkConnection. If the LdapNetworkConnection about to be bound is
not yet connected, an LDAP connection is first established. The
credentials from the other LdapNetworkConnection will be stored within
the LdapConnectionConfig of the given LdapNetworkConnection. If the bind
operation fails, the given LdapNetworkConnection is automatically
closed.

@param ldapConnection
    The LdapNetworkConnection describing the connection to the LDAP
    server. This LdapNetworkConnection is modified as a result of this
    call and will be automatically closed if this call fails.

@param useCredentialsFrom
    A bound LdapNetworkConnection whose bind credentials should be
    copied for use within this bind operation.

@return
    A bound LDAP connection, or null if the connection could not be
    bound.
extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPConnectionService.java