extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPConnectionService.java
/**
     * Binds to the LDAP server using the provided user DN and password.
     *
     * @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.
     *
     * @throws GuacamoleException
     *     If an error occurs while binding to the LDAP server.
     */
Binds to the LDAP server using the provided user DN and password.
@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.

@throws GuacamoleException
If an error occurs while binding to the LDAP server.
Binds to the LDAP server using the provided user DN and password . @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 . @throws GuacamoleException If an error occurs while binding to the LDAP server .
public LDAPConnection bindAs(String userDN, String password)
            throws GuacamoleException {
        LDAPConnection ldapConnection = createLDAPConnection();
        LDAPConstraints ldapConstraints = ldapConnection.getConstraints();
        if (ldapConstraints == null)
          ldapConstraints = new LDAPConstraints();
        ldapConstraints.setReferralFollowing(confService.getFollowReferrals());
        if (userDN != null && !userDN.isEmpty())
            ldapConstraints.setReferralHandler(new ReferralAuthHandler(userDN, password));
        ldapConstraints.setHopLimit(confService.getMaxReferralHops());
        ldapConstraints.setTimeLimit(confService.getOperationTimeout() * 1000);
        ldapConnection.setConstraints(ldapConstraints);
        try {
            ldapConnection.connect(
                confService.getServerHostname(),
                confService.getServerPort()
            );
            if (confService.getEncryptionMethod() == EncryptionMethod.STARTTLS)
                ldapConnection.startTLS();
        }
        catch (LDAPException e) {
            logger.error("Unable to connect to LDAP server: {}", e.getMessage());
            logger.debug("Failed to connect to LDAP server.", e);
            return null;
        }
        try {
            byte[] passwordBytes;
            try {
                if (password != null)
                    passwordBytes = password.getBytes("UTF-8");
                else
                    passwordBytes = null;
            }
            catch (UnsupportedEncodingException e) {
                logger.error("Unexpected lack of support for UTF-8: {}", e.getMessage());
                logger.debug("Support for UTF-8 (as required by Java spec) not found.", e);
                disconnect(ldapConnection);
                return null;
            }
            ldapConnection.bind(LDAPConnection.LDAP_V3, userDN, passwordBytes);
        }
        catch (LDAPException e) {
            logger.debug("LDAP bind failed.", e);
            disconnect(ldapConnection);
            return null;
        }
        return ldapConnection;
    }
public LDAPConnection bindAs ( String userDN , String password ) throws GuacamoleException { LDAPConnection ldapConnection = createLDAPConnection ( ) ; LDAPConstraints ldapConstraints = ldapConnection . getConstraints ( ) ; if ( ldapConstraints == null ) ldapConstraints = new LDAPConstraints ( ) ; ldapConstraints . setReferralFollowing ( confService . getFollowReferrals ( ) ) ; if ( userDN != null && ! userDN . isEmpty ( ) ) ldapConstraints . setReferralHandler ( new ReferralAuthHandler ( userDN , password ) ) ; ldapConstraints . setHopLimit ( confService . getMaxReferralHops ( ) ) ; ldapConstraints . setTimeLimit ( confService . getOperationTimeout ( ) * 1000 ) ; ldapConnection . setConstraints ( ldapConstraints ) ; try { ldapConnection . connect ( confService . getServerHostname ( ) , confService . getServerPort ( ) ) ; if ( confService . getEncryptionMethod ( ) == EncryptionMethod . STARTTLS ) ldapConnection . startTLS ( ) ; } catch ( LDAPException e ) { logger . error ( "Unable to connect to LDAP server: {}" , e . getMessage ( ) ) ; logger . debug ( "Failed to connect to LDAP server." , e ) ; return null ; } try { byte [ ] passwordBytes ; try { if ( password != null ) passwordBytes = password . getBytes ( "UTF-8" ) ; else passwordBytes = null ; } catch ( UnsupportedEncodingException e ) { logger . error ( "Unexpected lack of support for UTF-8: {}" , e . getMessage ( ) ) ; logger . debug ( "Support for UTF-8 (as required by Java spec) not found." , e ) ; disconnect ( ldapConnection ) ; return null ; } ldapConnection . bind ( LDAPConnection . LDAP_V3 , userDN , passwordBytes ) ; } catch ( LDAPException e ) { logger . debug ( "LDAP bind failed." , e ) ; disconnect ( ldapConnection ) ; return null ; } return ldapConnection ; }
Binds to the LDAP server using the provided user DN and password.
Binds to the LDAP server using the provided user DN and password .
// Obtain appropriately-configured LDAPConnection instance // Configure LDAP connection constraints // Set whether or not we follow referrals // Set referral authentication to use the provided credentials. // Set the maximum number of referrals we follow // Set timelimit to wait for LDAP operations, converting to ms // Apply the constraints to the connection // Connect to LDAP server // Explicitly start TLS if requested // Bind using provided credentials // Convert password into corresponding byte array // Bind as user // Disconnect if an error occurs during bind