Invalid credentials; getLastError: could not call ldap_get_option because LDAP resource was not of type resource

Hi,

I would like to set up a simple Ldap authentication system using, as indicated in the doc :

use Laminas\Authentication\AuthenticationService;
use Laminas\Authentication\Adapter\Ldap as LdapAdapter;

public function checkIfAuthentifGood()
{		
	$username = 'usertoto';
	$password = 'pwdtoto';
	$config = [
		'server1' => [
			'host'              => 'ldap.univdomain.fr',
			'port'              => '389',
			'accountCanonicalForm' => 2,
			'username'          => 'CN=useradm,DC=univdomain,DC=fr',
			'password'          => 'useradmpwd',
			'baseDn'            => 'OU=people,DC=univdomain,DC=fr',
			'bindRequiresDn'    => true,
		],
	];
	$auth = new AuthenticationService();
	$adapter = new LdapAdapter($config, $username, $password);
	$result = $auth->authenticate($adapter);
	foreach ($result->getMessages() as $i => $message) {
		var_dump($message);
	}
	var_dump(__LINE__);die;
}

Everything works perfectly when the identifiers are correct. However, if the password is wrong, ldap_bind desperately generates a php_warning (please see below).

I naturally want to get a “false” if the authentication is no good, nothing else…
I’ve tried several solutions to avoid this error, but I can’t manage it except by using native PHP Ldap functions and adding an “@” to the ldap_bind() :

$ds=ldap_connect($config['server1']['host']);
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
$b = @ldap_bind($ds, 'uid=' . $username . ',' . $config['server1']['baseDn'], $password);

Could you suggest some solutions ? Knowing that I’m not at ease with handling \Exception with Laminas.
Many thanks in advance.

My environment :

Linux 6.1.0-13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.55-1 (2023-09-29) x86_64
Debian GNU/Linux 12 (bookworm) (kernel 6.1.0-13-amd64)

ldap-utils/stable,now 2.5.13+dfsg-5 amd64 [installé]
libaprutil1-ldap/stable,now 1.6.3-1 amd64 [installé, automatique]
libldap-2.5-0/stable,now 2.5.13+dfsg-5 amd64 [installé, automatique]
libldap-common/stable,now 2.5.13+dfsg-5 all [installé, automatique]
libnet-ldap-perl/stable,now 1:0.6800+dfsg-1 all [installé]
php8.1-ldap/bookworm,now 8.1.25-1+0~20231027.56+debian12~1.gbpf5dd08 amd64 [installé]
apache2-bin/stable,now 2.4.57-2 amd64 [installé, automatique]
apache2-data/stable,now 2.4.57-2 all [installé, automatique]
apache2-utils/stable,now 2.4.57-2 amd64 [installé, automatique]
apache2/stable,now 2.4.57-2 amd64 [installé, automatique]
libapache2-mod-php8.1/bookworm,now 8.1.25-1+0~20231027.56+debian12~1.gbpf5dd08 amd64 [installé, automatique]

composer show :

laminas/laminas-authentication 2.16.0
laminas/laminas-component-installer 3.4.0
laminas/laminas-eventmanager 3.12.0
laminas/laminas-ldap 2.17.0
laminas/laminas-modulemanager 2.15.0
laminas/laminas-servicemanager 3.22.1

and my PHP Warning :

tail -f /var/log/apache2/error.log
	[Sat Nov 25 08:46:25.654940 2023] [mpm_prefork:notice] [pid 1911] AH00163: Apache/2.4.57 (Debian) configured -- resuming normal operations
	[Sat Nov 25 08:46:25.655015 2023] [mpm_prefork:info] [pid 1911] AH00164: Server built: 2023-04-13T03:26:51
	[Sat Nov 25 08:46:25.655033 2023] [core:notice] [pid 1911] AH00094: Command line: '/usr/sbin/apache2'
[Sat Nov 25 08:47:24.316931 2023] [php:notice] [pid 1916] [client 192.168.90.1:43164]
PHP Warning:  Uncaught Laminas\\Ldap\\Exception\\LdapException: 0x31 (Invalid credentials; getLastError:
could not call ldap_get_option because LDAP resource was not of type resource): uid=usertoto,ou=people,dc=univdomain,dc=fr in /opt/projetZend/myappli/vendor/laminas/laminas-ldap/src/Ldap.php:1037

Stack trace:
#0 /opt/projetZend/myappli/vendor/laminas/laminas-authentication/src/Adapter/Ldap.php(254): Laminas\\Ldap\\Ldap->bind()
#1 /opt/projetZend/myappli/vendor/laminas/laminas-authentication/src/AuthenticationService.php(105): Laminas\\Authentication\\Adapter\\Ldap->authenticate()
#2 /opt/projetZend/myappli/module/Application/src/Model/ManageAuthentif.php(466): Laminas\\Authentication\\AuthenticationService->authenticate()
#3 /opt/projetZend/myappli/module/Application/src/Controller/AuthController.php(127): Application\\Model\\ManageAuthentif->checkIfAuthentifGood()
#4 /opt/projetZend/myappli/vendor/laminas/laminas-mvc/src/Controller/AbstractActionController.php(72): Application\\Controller\\AuthController->loginAction()
#5 /opt/projetZend/myappli/vendor/laminas/laminas-eventmanager/src/EventManager.php(320): Laminas\\Mvc\\Controller\\AbstractActionController->onDispatch()
#6 /opt/projetZend/myappli/vendor/laminas/laminas-eventmanager/src/EventManager.php(178): Laminas\\EventManager\\EventManager->triggerListeners()
#7 /opt/projetZend/myappli/vendor/laminas/laminas-mvc/src/Controller/AbstractController.php(105): Laminas\\EventManager\\EventManager->triggerEventUntil()
#8 /opt/projetZend/myappli/vendor/laminas/laminas-mvc/src/DispatchListener.php(117): Laminas\\Mvc\\Controller\\AbstractController->dispatch()
#9 /opt/projetZend/myappli/vendor/laminas/laminas-eventmanager/src/EventManager.php(320): Laminas\\Mvc\\DispatchListener->onDispatch()
#10 /opt/projetZend/myappli/vendor/laminas/laminas-eventmanager/src/EventManager.php(178): Laminas\\EventManager\\EventManager->triggerListeners()
#11 /opt/projetZend/myappli/vendor/laminas/laminas-mvc/src/Application.php(319): Laminas\\EventManager\\EventManager->triggerEventUntil()
#12 /opt/projetZend/myappli/public/index.php(46): Laminas\\Mvc\\Application->run()
#13 {main}\n  thrown in /opt/projetZend/myappli/vendor/laminas/laminas-ldap/src/Ldap.php on line 1037, referer: http://myappli.univdomain.fr/myappli/
...

Sorry, unfortunately I can’t really help as I don’t use laminas-ldap.

If I interpret the code correctly, this error message should be handled accordingly. I would recommend that you try to reproduce the problem with laminas-ldap only. You can then open a ticket in the corresponding bug tracker.

Laminas\Authentication\Adapter\Ldap uses Laminas\Ldap\Ldap and its bind() function but I tried directly with Laminas-Ldap and it’s the same problem.
Thanks for your feedback.