From c00e58ceec72aed0d8c524f32ce4bce7ecb080e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Deruss=C3=A9?= Date: Mon, 21 Mar 2022 20:06:53 +0100 Subject: [PATCH] Fix compatibility of ldap 6.0 with security 5.x --- .../Ldap/Security/LdapAuthenticator.php | 9 ++++ .../Tests/Security/LdapAuthenticatorTest.php | 45 +++++++++++++++++++ src/Symfony/Component/Ldap/composer.json | 3 +- 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/Ldap/Tests/Security/LdapAuthenticatorTest.php diff --git a/src/Symfony/Component/Ldap/Security/LdapAuthenticator.php b/src/Symfony/Component/Ldap/Security/LdapAuthenticator.php index 02a423c239ce5..ac915c0c47d43 100644 --- a/src/Symfony/Component/Ldap/Security/LdapAuthenticator.php +++ b/src/Symfony/Component/Ldap/Security/LdapAuthenticator.php @@ -18,6 +18,7 @@ use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface; use Symfony\Component\Security\Http\Authenticator\InteractiveAuthenticatorInterface; use Symfony\Component\Security\Http\Authenticator\Passport\Passport; +use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface; use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; use Symfony\Component\Security\Http\EntryPoint\Exception\NotAnEntryPointException; @@ -64,6 +65,14 @@ public function authenticate(Request $request): Passport return $passport; } + /** + * @internal + */ + public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface + { + throw new \BadMethodCallException(sprintf('The "%s()" method cannot be called.', __METHOD__)); + } + public function createToken(Passport $passport, string $firewallName): TokenInterface { return $this->authenticator->createToken($passport, $firewallName); diff --git a/src/Symfony/Component/Ldap/Tests/Security/LdapAuthenticatorTest.php b/src/Symfony/Component/Ldap/Tests/Security/LdapAuthenticatorTest.php new file mode 100644 index 0000000000000..52857cc496792 --- /dev/null +++ b/src/Symfony/Component/Ldap/Tests/Security/LdapAuthenticatorTest.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Ldap\Tests\Security; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Ldap\Security\LdapAuthenticator; +use Symfony\Component\Ldap\Security\LdapBadge; +use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface; +use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; +use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; +use Symfony\Component\Security\Http\Authenticator\Passport\Passport; + +class LdapAuthenticatorTest extends TestCase +{ + public function testAuthenticate() + { + $decorated = $this->createMock(AuthenticatorInterface::class); + $passport = new Passport(new UserBadge('test'), new PasswordCredentials('s3cret')); + $decorated + ->expects($this->once()) + ->method('authenticate') + ->willReturn($passport) + ; + + $authenticator = new LdapAuthenticator($decorated, 'serviceId'); + $request = new Request(); + + $authenticator->authenticate($request); + + /** @var LdapBadge $badge */ + $badge = $passport->getBadge(LdapBadge::class); + $this->assertNotNull($badge); + $this->assertSame('serviceId', $badge->getLdapServiceId()); + } +} diff --git a/src/Symfony/Component/Ldap/composer.json b/src/Symfony/Component/Ldap/composer.json index 51b2570a1870c..073c7c8ff692a 100644 --- a/src/Symfony/Component/Ldap/composer.json +++ b/src/Symfony/Component/Ldap/composer.json @@ -21,7 +21,8 @@ "symfony/options-resolver": "^5.4|^6.0" }, "require-dev": { - "symfony/security-core": "^5.4|^6.0" + "symfony/security-core": "^5.4|^6.0", + "symfony/security-http": "^5.4|^6.0" }, "conflict": { "symfony/options-resolver": "<5.4",