private LdapNetworkConnection bindAs(LdapNetworkConnection ldapConnection,
            String bindUser, String password) {

        // Add credentials to existing config
        LdapConnectionConfig config = ldapConnection.getConfig();
        config.setName(bindUser);
        config.setCredentials(password);

        try {
            // Connect and bind using provided credentials
            ldapConnection.bind();
        }

        // Disconnect if an authentication error occurs, but log that failure
        // only at the debug level (such failures are expected)
        catch (LdapAuthenticationException e) {
            ldapConnection.close();
            logger.debug("Bind attempt with LDAP server as user \"{}\" failed.",
                    bindUser, e);
            return null;
        }

        // Disconnect for all other bind failures, as well, logging those at
        // the error level
        catch (LdapException e) {
            ldapConnection.close();
            logger.error("Binding with the LDAP server at \"{}\" as user "
                    + "\"{}\" failed: {}", config.getLdapHost(), bindUser,
                    e.getMessage());
            logger.debug("Unable to bind to LDAP server.", e);
            return null;
        }

        return ldapConnection;

    }
Binds to the LDAP server indicated by the given LdapNetworkConnection
using the given credentials. If the LdapNetworkConnection is not yet
connected, an LDAP connection is first established. The provided
credentials 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 userDN
    The DN of the user to bind as, or null to bind anonymously.

@param password
    The password to use when binding as the specified user, or null to
    attempt to bind without a password.

@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