From d28c56d9a5babcb0cf0d2433529db60d9db59a48 Mon Sep 17 00:00:00 2001 From: Tomas Saghy Date: Wed, 29 Nov 2023 11:43:43 +0100 Subject: [PATCH 1/5] Remove PersistenceInterface inheritance from ImpersonationInterface solves https://github.com/cakephp/authentication/issues/648 --- src/Authenticator/ImpersonationInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Authenticator/ImpersonationInterface.php b/src/Authenticator/ImpersonationInterface.php index b8094878..99fafc58 100644 --- a/src/Authenticator/ImpersonationInterface.php +++ b/src/Authenticator/ImpersonationInterface.php @@ -20,7 +20,7 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -interface ImpersonationInterface extends PersistenceInterface +interface ImpersonationInterface { /** * Impersonates a user From abb32a20be8acb195af305b23ded944a7eb84432 Mon Sep 17 00:00:00 2001 From: Jamison Bryant Date: Mon, 25 Mar 2024 09:57:02 -0400 Subject: [PATCH 2/5] Add the container to the middleware --- src/Middleware/AuthenticationMiddleware.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Middleware/AuthenticationMiddleware.php b/src/Middleware/AuthenticationMiddleware.php index 1de93ddc..79616eaf 100644 --- a/src/Middleware/AuthenticationMiddleware.php +++ b/src/Middleware/AuthenticationMiddleware.php @@ -23,6 +23,7 @@ use Authentication\Authenticator\StatelessInterface; use Authentication\Authenticator\UnauthenticatedException; use Cake\Core\ContainerApplicationInterface; +use Cake\Core\ContainerInterface; use Laminas\Diactoros\Response; use Laminas\Diactoros\Response\RedirectResponse; use Laminas\Diactoros\Stream; @@ -43,16 +44,26 @@ class AuthenticationMiddleware implements MiddlewareInterface */ protected AuthenticationServiceInterface|AuthenticationServiceProviderInterface $subject; + /** + * The container instance from the application + * + * @var \Cake\Core\ContainerInterface|null + */ + protected ?ContainerInterface $container; + /** * Constructor * * @param \Authentication\AuthenticationServiceInterface|\Authentication\AuthenticationServiceProviderInterface $subject Authentication service or application instance. + * @param \Cake\Core\ContainerInterface|null $container The container instance from the application. * @throws \InvalidArgumentException When invalid subject has been passed. */ public function __construct( - AuthenticationServiceInterface|AuthenticationServiceProviderInterface $subject + AuthenticationServiceInterface|AuthenticationServiceProviderInterface $subject, + ?ContainerInterface $container = null ) { $this->subject = $subject; + $this->container = $container; } /** @@ -69,6 +80,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface if ($this->subject instanceof ContainerApplicationInterface) { $container = $this->subject->getContainer(); $container->add(AuthenticationService::class, $service); + } elseif ($this->container) { + $this->container->add(AuthenticationService::class, $service); } try { From 67d3f82b94865334dde1bafaafb3dbaf99fb5819 Mon Sep 17 00:00:00 2001 From: Jamison Bryant Date: Mon, 25 Mar 2024 09:57:13 -0400 Subject: [PATCH 3/5] Add a new test --- .../AuthenticationMiddlewareTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/TestCase/Middleware/AuthenticationMiddlewareTest.php b/tests/TestCase/Middleware/AuthenticationMiddlewareTest.php index 1627cd6f..4d760f67 100644 --- a/tests/TestCase/Middleware/AuthenticationMiddlewareTest.php +++ b/tests/TestCase/Middleware/AuthenticationMiddlewareTest.php @@ -24,6 +24,7 @@ use Authentication\IdentityInterface; use Authentication\Middleware\AuthenticationMiddleware; use Authentication\Test\TestCase\AuthenticationTestCase as TestCase; +use Cake\Core\Container; use Cake\Core\TestSuite\ContainerStubTrait; use Cake\Http\Response; use Cake\Http\ServerRequestFactory; @@ -667,4 +668,25 @@ public function testMiddlewareInjectsServiceIntoDIC(): void $container = $this->application->getContainer(); $this->assertInstanceOf(AuthenticationService::class, $container->get(AuthenticationService::class)); } + + public function testMiddlewareInjectsServiceIntoDICCustomContainerInstance(): void + { + $request = ServerRequestFactory::fromGlobals( + ['REQUEST_URI' => '/testpath'], + [], + ['username' => 'mariano', 'password' => 'password'] + ); + $handler = new TestRequestHandler(); + + $provider = $this->createMock(AuthenticationServiceProviderInterface::class); + $provider + ->method('getAuthenticationService') + ->willReturn($this->service); + $container = new Container(); + + $middleware = new AuthenticationMiddleware($provider, $container); + $middleware->process($request, $handler); + + $this->assertInstanceOf(AuthenticationService::class, $container->get(AuthenticationService::class)); + } } From 09aae2c7a0b1ed4e318198c7d6ff970fc008a5a0 Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 25 Jul 2024 18:17:21 +0530 Subject: [PATCH 4/5] Fix test on PHP 8.4 --- tests/TestCase/Authenticator/CookieAuthenticatorTest.php | 8 ++++++-- .../TestCase/PasswordHasher/LegacyPasswordHasherTest.php | 6 +++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/TestCase/Authenticator/CookieAuthenticatorTest.php b/tests/TestCase/Authenticator/CookieAuthenticatorTest.php index a876888b..e26ad062 100644 --- a/tests/TestCase/Authenticator/CookieAuthenticatorTest.php +++ b/tests/TestCase/Authenticator/CookieAuthenticatorTest.php @@ -301,8 +301,12 @@ public function testPersistIdentity() $this->assertArrayHasKey('response', $result); $this->assertInstanceOf(RequestInterface::class, $result['request']); $this->assertInstanceOf(ResponseInterface::class, $result['response']); + $hashCost = '10'; + if (PHP_VERSION_ID >= 80400) { + $hashCost = '12'; + } $this->assertStringContainsString( - 'CookieAuth=%5B%22mariano%22%2C%22%242y%2410%24', // `CookieAuth=["mariano","$2y$10$` + 'CookieAuth=%5B%22mariano%22%2C%22%242y%24' . $hashCost . '%24', // `CookieAuth=["mariano","$2y$10$` $result['response']->getHeaderLine('Set-Cookie') ); $this->assertStringContainsString( @@ -333,7 +337,7 @@ public function testPersistIdentity() ]); $result = $authenticator->persistIdentity($request, $response, $identity); $this->assertStringContainsString( - 'CookieAuth=%5B%22mariano%22%2C%22%242y%2410%24', + 'CookieAuth=%5B%22mariano%22%2C%22%242y%24' . $hashCost . '%24', $result['response']->getHeaderLine('Set-Cookie') ); } diff --git a/tests/TestCase/PasswordHasher/LegacyPasswordHasherTest.php b/tests/TestCase/PasswordHasher/LegacyPasswordHasherTest.php index 85225edd..b11b7796 100644 --- a/tests/TestCase/PasswordHasher/LegacyPasswordHasherTest.php +++ b/tests/TestCase/PasswordHasher/LegacyPasswordHasherTest.php @@ -47,7 +47,11 @@ public function testNeedsRehash() $hasher = new LegacyPasswordHasher(); $this->assertTrue($hasher->needsRehash(md5('foo'))); $this->assertTrue($hasher->needsRehash('bar')); - $this->assertFalse($hasher->needsRehash('$2y$10$juOA0XVFpvZa0KTxRxEYVuX5kIS7U1fKDRcxyYhhUQECN1oHYnBMy')); + $hashCost = '10'; + if (PHP_VERSION_ID >= 80400) { + $hashCost = '12'; + } + $this->assertFalse($hasher->needsRehash('$2y$' . $hashCost . '$juOA0XVFpvZa0KTxRxEYVuX5kIS7U1fKDRcxyYhhUQECN1oHYnBMy')); } /** From 110b35c0642f0b2bc45d633db5d59279f26b893d Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 26 Jul 2024 12:15:41 +0530 Subject: [PATCH 5/5] Add convenience method to get the identifier. --- .../Component/AuthenticationComponent.php | 10 ++++++++++ .../Component/AuthenticationComponentTest.php | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Controller/Component/AuthenticationComponent.php b/src/Controller/Component/AuthenticationComponent.php index d01f93b8..398e24d5 100644 --- a/src/Controller/Component/AuthenticationComponent.php +++ b/src/Controller/Component/AuthenticationComponent.php @@ -240,6 +240,16 @@ public function getResult(): ?ResultInterface return $this->getAuthenticationService()->getResult(); } + /** + * Get the identifier (primary key) of the identity. + * + * @return array|string|int|null + */ + public function getIdentifier(): array|string|int|null + { + return $this->getIdentity()?->getIdentifier(); + } + /** * Returns the identity used in the authentication attempt. * diff --git a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php index 45d6b4c6..abe5d9aa 100644 --- a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php +++ b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php @@ -143,6 +143,22 @@ public function testGetAuthenticationServiceInvalidServiceObject() $component->getAuthenticationService(); } + public function testGetId(): void + { + $component = new AuthenticationComponent(new ComponentRegistry(new Controller($this->request))); + $this->assertNull($component->getIdentifier()); + + $request = $this->request + ->withAttribute('identity', $this->identity) + ->withAttribute('authentication', $this->service); + + $controller = new Controller($request); + $registry = new ComponentRegistry($controller); + $component = new AuthenticationComponent($registry); + + $this->assertSame($component->getIdentifier(), $this->identity->getIdentifier()); + } + /** * testGetIdentity *