Skip to content

Commit 27dba3b

Browse files
manu0401nicolas-grekas
authored andcommitted
-
1 parent 357dc12 commit 27dba3b

File tree

6 files changed

+42
-53
lines changed

6 files changed

+42
-53
lines changed

UPGRADE-7.2.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ FrameworkBundle
2929

3030
* [BC BREAK] The `secrets:decrypt-to-local` command terminates with a non-zero exit code when a secret could not be read
3131

32+
Ldap
33+
----
34+
35+
* Add methods for `saslBind()` and `whoami()` to `ConnectionInterface` and `LdapInterface`
36+
3237
Messenger
3338
---------
3439

src/Symfony/Component/Ldap/Adapter/ConnectionInterface.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
/**
2020
* @author Charles Sarrazin <charles@sarraz.in>
21+
*
22+
* @method void saslBind(?string $dn = null, #[\SensitiveParameter] ?string $password = null, ?string $mech = null, ?string $realm = null, ?string $authcId = null, ?string $authzId = null, ?string $props = null)
23+
* @method string whoami()
2124
*/
2225
interface ConnectionInterface
2326
{
@@ -38,20 +41,16 @@ public function bind(?string $dn = null, #[\SensitiveParameter] ?string $passwor
3841
/**
3942
* Binds the connection against a user's DN and password using SASL
4043
*
41-
* @return void
42-
*
4344
* @throws LdapException When SASL support is not available
4445
* @throws AlreadyExistsException When the connection can't be created because of an LDAP_ALREADY_EXISTS error
4546
* @throws ConnectionTimeoutException When the connection can't be created because of an LDAP_TIMEOUT error
4647
* @throws InvalidCredentialsException When the connection can't be created because of an LDAP_INVALID_CREDENTIALS error
47-
*/
4848
public function saslBind(?string $dn = null, #[\SensitiveParameter] ?string $password = null, ?string $mech = null, ?string $realm = null, ?string $authcId = null, ?string $authzId = null, ?string $props = null): void;
49+
*/
4950

5051
/**
5152
* Return authenticated and authorized (for SASL) DN
52-
*
53-
* @return string
54-
*/
5553
public function whoami(): string;
54+
*/
5655

5756
}

src/Symfony/Component/Ldap/Adapter/ExtLdap/Connection.php

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,14 @@ public function bind(?string $dn = null, #[\SensitiveParameter] ?string $passwor
7070

7171
if (false === @ldap_bind($this->connection, $dn, $password)) {
7272
$error = ldap_error($this->connection);
73-
$errno = ldap_errno($this->connection);
74-
if (self::LDAP_INVALID_CREDENTIALS === $errno) {
75-
throw new InvalidCredentialsException($error);
76-
}
77-
if (self::LDAP_TIMEOUT === $errno) {
78-
throw new ConnectionTimeoutException($error);
79-
}
80-
if (self::LDAP_ALREADY_EXISTS === $errno) {
81-
throw new AlreadyExistsException($error);
82-
}
83-
ldap_get_option($this->connection, LDAP_OPT_DIAGNOSTIC_MESSAGE, $diagnostic_message);
84-
throw new ConnectionException($error.' '.$diagnostic_message);
73+
ldap_get_option($this->connection, LDAP_OPT_DIAGNOSTIC_MESSAGE, $diagnostic);
74+
75+
throw match (ldap_errno($this->connection)) {
76+
self::LDAP_INVALID_CREDENTIALS => new InvalidCredentialsException($error),
77+
self::LDAP_TIMEOUT => new ConnectionTimeoutException($error),
78+
self::LDAP_ALREADY_EXISTS => new AlreadyExistsException($error),
79+
default => new ConnectionException($error.' '.$diagnostic),
80+
};
8581
}
8682

8783
$this->bound = true;
@@ -92,7 +88,7 @@ public function bind(?string $dn = null, #[\SensitiveParameter] ?string $passwor
9288
*/
9389
public function saslBind(?string $dn = null, #[\SensitiveParameter] ?string $password = null, ?string $mech = null, ?string $realm = null, ?string $authcId = null, ?string $authzId = null, ?string $props = null): void
9490
{
95-
if (!function_exists('ldap_sasl_bind')) {
91+
if (!\function_exists('ldap_sasl_bind')) {
9692
throw new LdapException('Library - missing SASL support');
9793
}
9894

@@ -102,46 +98,39 @@ public function saslBind(?string $dn = null, #[\SensitiveParameter] ?string $pas
10298

10399
if (false === @ldap_sasl_bind($this->connection, $dn, $password, $mech, $realm, $authcId, $authzId, $props)) {
104100
$error = ldap_error($this->connection);
105-
$errno = ldap_errno($this->connection);
106-
if (self::LDAP_INVALID_CREDENTIALS === $errno) {
107-
throw new InvalidCredentialsException($error);
108-
}
109-
if (self::LDAP_TIMEOUT === $errno) {
110-
throw new ConnectionTimeoutException($error);
111-
}
112-
if (self::LDAP_ALREADY_EXISTS === $errno) {
113-
throw new AlreadyExistsException($error);
114-
}
115-
throw new ConnectionException($error);
101+
ldap_get_option($this->connection, LDAP_OPT_DIAGNOSTIC_MESSAGE, $diagnostic);
102+
103+
throw match (ldap_errno($this->connection)) {
104+
self::LDAP_INVALID_CREDENTIALS => new InvalidCredentialsException($error),
105+
self::LDAP_TIMEOUT => new ConnectionTimeoutException($error),
106+
self::LDAP_ALREADY_EXISTS => new AlreadyExistsException($error),
107+
default => new ConnectionException($error.' '.$diagnostic),
108+
};
116109
}
117110

118111
$this->bound = true;
119112
}
120113

121-
122114
/**
123115
* ldap_exop_whoami accessor, returns authenticated DN
124116
*/
125117
public function whoami(): string
126118
{
127-
$authzId = ldap_exop_whoami($this->connection);
128-
if ($authzId === false) {
119+
if (false === $authzId = ldap_exop_whoami($this->connection)) {
129120
throw new LdapException(ldap_error($this->connection));
130121
}
131-
132-
$parts = explode(':', $authzId);
133-
if ("dn" === $parts[0]) {
134-
$dn = $parts[1];
135-
} else {
122+
123+
$parts = explode(':', $authzId, 2);
124+
if ('dn' !== $parts[0]) {
136125
/*
137-
* We currently do not handle u:login authzId, which
126+
* We currently do not handle u:login authzId, which
138127
* would require a configuration-dependent LDAP search
139128
* to be turned into a DN
140129
*/
141-
throw new LdapException(sprintf('Unsupported authzId "%s"', $authzId));
130+
throw new LdapException(\sprintf('Unsupported authzId "%s"', $authzId));
142131
}
143-
144-
return $dn;
132+
133+
return $parts[1];
145134
}
146135

147136
/**

src/Symfony/Component/Ldap/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CHANGELOG
44
7.2
55
---
66

7-
* Add support for saslBind and whoami LDAP operations
7+
* Add methods for `saslBind()` and `whoami()` to `ConnectionInterface` and `LdapInterface`
88

99
7.1
1010
---

src/Symfony/Component/Ldap/LdapInterface.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
use Symfony\Component\Ldap\Exception\ConnectionException;
1717

1818
/**
19-
* Ldap interface.
20-
*
2119
* @author Charles Sarrazin <charles@sarraz.in>
20+
*
21+
* @method void saslBind(?string $dn = null, #[\SensitiveParameter] ?string $password = null, ?string $mech = null, ?string $realm = null, ?string $authcId = null, ?string $authzId = null, ?string $props = null)
22+
* @method string whoami()
2223
*/
2324
interface LdapInterface
2425
{
@@ -35,19 +36,14 @@ public function bind(?string $dn = null, #[\SensitiveParameter] ?string $passwor
3536
/**
3637
* Returns a connection bound to the ldap using SASL
3738
*
38-
* @return void
39-
*
4039
* @throws ConnectionException if dn / password could not be bound
41-
*/
4240
public function saslBind(?string $dn = null, #[\SensitiveParameter] ?string $password = null, ?string $mech = null, ?string $realm = null, ?string $authcId = null, ?string $authzId = null, ?string $props = null): void;
43-
41+
*/
4442

4543
/**
4644
* Returns authenticated and authorized (for SASL) DN
47-
*
48-
* @return string
49-
*/
5045
public function whoami(): string;
46+
*/
5147

5248
/**
5349
* Queries a ldap server for entries matching the given criteria.

src/Symfony/Component/Ldap/Tests/Adapter/ExtLdap/AdapterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testSaslBind()
4343

4444
$ldap->getConnection()->saslBind('cn=admin,dc=symfony,dc=com', 'symfony');
4545
$this->assertEquals('cn=admin,dc=symfony,dc=com', $ldap->getConnection()->whoami());
46-
}
46+
}
4747

4848
/**
4949
* @group functional

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy