SimpleSAML_Auth_LDAP::bind PHP Méthode

bind() public méthode

Bind to LDAP with a specific DN and password. Simple wrapper around ldap_bind() with some additional logging.
public bind ( string $dn, string $password, array $sasl_args = null ) : boolean
$dn string The DN used.
$password string The password used.
$sasl_args array Array of SASL options for SASL bind
Résultat boolean Returns TRUE if successful, FALSE if LDAP_INVALID_CREDENTIALS, LDAP_X_PROXY_AUTHZ_FAILURE, LDAP_INAPPROPRIATE_AUTH, LDAP_INSUFFICIENT_ACCESS
    public function bind($dn, $password, array $sasl_args = null)
    {
        $authz_id = null;
        if ($sasl_args != null) {
            if (!function_exists('ldap_sasl_bind')) {
                $ex_msg = 'Library - missing SASL support';
                throw $this->makeException($ex_msg);
            }
            // SASL Bind, with error handling
            $authz_id = $sasl_args['authz_id'];
            $error = @ldap_sasl_bind($this->ldap, $dn, $password, $sasl_args['mech'], $sasl_args['realm'], $sasl_args['authc_id'], $sasl_args['authz_id'], $sasl_args['props']);
        } else {
            // Simple Bind, with error handling
            $authz_id = $dn;
            $error = @ldap_bind($this->ldap, $dn, $password);
        }
        if ($error === true) {
            // Good
            $this->authz_id = $authz_id;
            SimpleSAML\Logger::debug('Library - LDAP bind(): Bind successful with DN \'' . $dn . '\'');
            return true;
        }
        /* Handle errors
         * LDAP_INVALID_CREDENTIALS
         * LDAP_INSUFFICIENT_ACCESS */
        switch (ldap_errno($this->ldap)) {
            case 32:
                // LDAP_NO_SUCH_OBJECT
                // no break
            // LDAP_NO_SUCH_OBJECT
            // no break
            case 47:
                // LDAP_X_PROXY_AUTHZ_FAILURE
                // no break
            // LDAP_X_PROXY_AUTHZ_FAILURE
            // no break
            case 48:
                // LDAP_INAPPROPRIATE_AUTH
                // no break
            // LDAP_INAPPROPRIATE_AUTH
            // no break
            case 49:
                // LDAP_INVALID_CREDENTIALS
                // no break
            // LDAP_INVALID_CREDENTIALS
            // no break
            case 50:
                // LDAP_INSUFFICIENT_ACCESS
                return false;
            default:
                break;
        }
        // Bad
        throw $this->makeException('Library - LDAP bind(): Bind failed with DN \'' . $dn . '\'');
    }

Usage Example

Exemple #1
0
    SimpleSAML_Utilities::fatalError($session->getTrackID(), 'NORELAYSTATE');
}
if (isset($_POST['username'])) {
    try {
        $ldapconfig = $ldapmulti[$_POST['org']];
        if ($ldapconfig['search.enable'] === TRUE) {
            if (!$ldap->bind($ldapconfig['search.username'], $ldapconfig['search.password'])) {
                throw new Exception('Error authenticating using search username & password.');
            }
            $dn = $ldap->searchfordn($ldapconfig['search.base'], $ldapconfig['search.attributes'], $_POST['username']);
        } else {
            $dn = str_replace('%username%', $_POST['username'], $ldapconfig['dnpattern']);
        }
        $pwd = $_POST['password'];
        $ldap = new SimpleSAML_Auth_LDAP($ldapconfig['hostname'], $ldapconfig['enable_tls']);
        if ($pwd == "" or !$ldap->bind($dn, $pwd)) {
            SimpleSAML_Logger::info('AUTH - ldap-multi: ' . $_POST['username'] . ' failed to authenticate. DN=' . $dn);
            throw new Exception('Wrong username or password');
        }
        $attributes = $ldap->getAttributes($dn, $ldapconfig['attributes']);
        SimpleSAML_Logger::info('AUTH - ldap-multi: ' . $_POST['username'] . ' successfully authenticated');
        $session->doLogin('login-ldapmulti');
        $session->setAttributes($attributes);
        $session->setNameID(array('value' => SimpleSAML_Utilities::generateID(), 'Format' => 'urn:oasis:names:tc:SAML:2.0:nameid-format:transient'));
        /**
         * Create a statistics log entry for every successfull login attempt.
         * Also log a specific attribute as set in the config: statistics.authlogattr
         */
        $authlogattr = $config->getValue('statistics.authlogattr', null);
        if ($authlogattr && array_key_exists($authlogattr, $attributes)) {
            SimpleSAML_Logger::stats('AUTH-login-ldapmulti OK ' . $attributes[$authlogattr][0]);
All Usage Examples Of SimpleSAML_Auth_LDAP::bind