DirectoryTree / LdapRecord

A fully-featured LDAP framework.
https://ldaprecord.com
MIT License
502 stars 44 forks source link

[Support] Ldap server connection's response problem #641

Closed enversanli closed 12 months ago

enversanli commented 1 year ago

Hello,

I am having some problems with this package, and I need your support or experience.

I have installed the package (v3) and added the necessary information to connect to the server with the following information.

Note : Username and password are not required to connect to the LDAP server. This is an OpenLDAP server and no authentication is required.

'default' => [ 

            'hosts' => [env('LDAP_HOST', example.ldap-server.com')], 

            'username' => env('LDAP_USERNAME', null), 

            'password' => env('LDAP_PASSWORD', null), 

            'port' => env('LDAP_PORT', 389), 

            'base_dn' => env('LDAP_BASE_DN', 'DC=ldap-server,DC=com'), 

            'timeout' => env('LDAP_TIMEOUT', 10), 

            'use_ssl' => env('LDAP_SSL', false), 

            'use_tls' => env('LDAP_TLS', false), 

            'use_sasl' => env('LDAP_SASL', false), 

            'sasl_options' => [ 

                // 'mech' => 'GSSAPI', 

            ], 

        ], 

When I run the test function in accordance with the above information, I get the following error:

Successful : No 

Error Message : Success. Error Code: [] Diagnostic Message: null 

Vendor -> Ldap.php

 public function bind(string $dn = null, string $password = null, array $controls = null): LdapResultResponse
    {
        /** @var \LDAP\Result $result */
        $result = $this->executeFailableOperation(function () use ($dn, $password, $controls) {
            return ldap_bind_ext($this->connection, $dn, $password ? html_entity_decode($password) : null, $controls);
        });

        $response = $this->parseResult($result);

        $this->bound = $response && $response->successful(); // Here is the response

        return $response;
    }
}

When I debugged the methods executed by the test, I noticed that the successful() function is checking for the errorCode and the errorMessage after ldap_bind_ext(). As you can see in the code above, the errorMessage actually contains a success message ("Bind succeeded"). When I modified the successful() function to return true, everything is working. Please see the example below:

 public function testLdap(){
    $connection = new Connection([
      'hosts' => 'example.ldap.com',
      'username' => null,
      'password' => null
    ]);

    try {
      $connection->connect();

/* Search user in LDAP in order to create DN */
        $search = $connection->query()->where('exampleColumn', '=', 'search parameter')->select('dn')->get(); // Works successfully

      echo "Successfully connected";
    }catch (\LdapRecord\Auth\BindException $e){
      dump($e);
    }
  }

In this case, I get successful results when I connect directly using the Connection object as shown above.

But: If I revert my changes and the successful() function returns false again, the $e->getDetailedError() is null, causing all other function like getErrorCode() / getErrorMessage() / getDiagnosticMessage() to fail.

Do you have any information about this behavior, or do you observe anything wrong with my configuration? Note: I previously used Adldap2 package which is using ldap_bind(). So most probably the error was already present in the past, but was just not visible.

I'm waiting your support for that case. Thank you all.

stevebauman commented 1 year ago

Hi @enversanli, thanks for the detailed issue, I appreciate it!

That's strange, as even the PHP docs for ldap_parse_result display the parameter name as errorMessage:

https://www.php.net/manual/en/function.ldap-parse-result.php

ldap_parse_result(
    [LDAP\Connection](https://www.php.net/manual/en/class.ldap-connection.php) $ldap,
    [LDAP\Result](https://www.php.net/manual/en/class.ldap-result.php) $result,
    int &$error_code,
    string &$matched_dn = null,
    string &$error_message = null,
    array &$referrals = null,
    array &$controls = null
): bool

In your tests, is zero (0) returned as the errorCode? If so, then I think it's safe to remove the empty() check on the error message here:

https://github.com/DirectoryTree/LdapRecord/blob/6827ea17eb473cef9e42603cfb50c3bd430e5a2a/src/LdapResultResponse.php#L24

Let me know and then I can patch this quickly 🙏 .

enversanli commented 12 months ago

Hey @stevebauman , My error code is 0 and error mesage is "Bind succeeded" . Would be good if you remove empty condition as you suggested here.

Thank you for your quick support !

enversanli commented 12 months ago

Hello @stevebauman , I would like to ask about update plan. When do you plan to make the updates for the package ?

Thank you so much for your support !

stevebauman commented 12 months ago

Sorry for the delay @enversanli! I appreciate the ping.

I've just pushed the change. Once tests pass I'll create a new release 🙏

stevebauman commented 12 months ago

Ok I've just released v3.1.3 with this patch. Run composer update and you're all set! 👍

enversanli commented 12 months ago

Hello @stevebauman , Thank you so much for your support. I will update package and test it also on my project.

Again, Thank you for your support.

stevebauman commented 12 months ago

Happy to help @enversanli! Please drop by anytime you need assistance with LdapRecord 👍