Skip to content

Commit 0fb4ef1

Browse files
authored
Merge pull request #668 from cakephp/3.next
3.1.0
2 parents 3c1ce7f + f58ee8c commit 0fb4ef1

File tree

7 files changed

+74
-5
lines changed

7 files changed

+74
-5
lines changed

src/Authenticator/ImpersonationInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Psr\Http\Message\ResponseInterface;
2121
use Psr\Http\Message\ServerRequestInterface;
2222

23-
interface ImpersonationInterface extends PersistenceInterface
23+
interface ImpersonationInterface
2424
{
2525
/**
2626
* Impersonates a user

src/Controller/Component/AuthenticationComponent.php

+10
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ public function getResult(): ?ResultInterface
240240
return $this->getAuthenticationService()->getResult();
241241
}
242242

243+
/**
244+
* Get the identifier (primary key) of the identity.
245+
*
246+
* @return array|string|int|null
247+
*/
248+
public function getIdentifier(): array|string|int|null
249+
{
250+
return $this->getIdentity()?->getIdentifier();
251+
}
252+
243253
/**
244254
* Returns the identity used in the authentication attempt.
245255
*

src/Middleware/AuthenticationMiddleware.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Authentication\Authenticator\StatelessInterface;
2424
use Authentication\Authenticator\UnauthenticatedException;
2525
use Cake\Core\ContainerApplicationInterface;
26+
use Cake\Core\ContainerInterface;
2627
use Laminas\Diactoros\Response;
2728
use Laminas\Diactoros\Response\RedirectResponse;
2829
use Laminas\Diactoros\Stream;
@@ -43,16 +44,26 @@ class AuthenticationMiddleware implements MiddlewareInterface
4344
*/
4445
protected AuthenticationServiceInterface|AuthenticationServiceProviderInterface $subject;
4546

47+
/**
48+
* The container instance from the application
49+
*
50+
* @var \Cake\Core\ContainerInterface|null
51+
*/
52+
protected ?ContainerInterface $container;
53+
4654
/**
4755
* Constructor
4856
*
4957
* @param \Authentication\AuthenticationServiceInterface|\Authentication\AuthenticationServiceProviderInterface $subject Authentication service or application instance.
58+
* @param \Cake\Core\ContainerInterface|null $container The container instance from the application.
5059
* @throws \InvalidArgumentException When invalid subject has been passed.
5160
*/
5261
public function __construct(
53-
AuthenticationServiceInterface|AuthenticationServiceProviderInterface $subject
62+
AuthenticationServiceInterface|AuthenticationServiceProviderInterface $subject,
63+
?ContainerInterface $container = null
5464
) {
5565
$this->subject = $subject;
66+
$this->container = $container;
5667
}
5768

5869
/**
@@ -69,6 +80,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
6980
if ($this->subject instanceof ContainerApplicationInterface) {
7081
$container = $this->subject->getContainer();
7182
$container->add(AuthenticationService::class, $service);
83+
} elseif ($this->container) {
84+
$this->container->add(AuthenticationService::class, $service);
7285
}
7386

7487
try {

tests/TestCase/Authenticator/CookieAuthenticatorTest.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,12 @@ public function testPersistIdentity()
301301
$this->assertArrayHasKey('response', $result);
302302
$this->assertInstanceOf(RequestInterface::class, $result['request']);
303303
$this->assertInstanceOf(ResponseInterface::class, $result['response']);
304+
$hashCost = '10';
305+
if (PHP_VERSION_ID >= 80400) {
306+
$hashCost = '12';
307+
}
304308
$this->assertStringContainsString(
305-
'CookieAuth=%5B%22mariano%22%2C%22%242y%2410%24', // `CookieAuth=["mariano","$2y$10$`
309+
'CookieAuth=%5B%22mariano%22%2C%22%242y%24' . $hashCost . '%24', // `CookieAuth=["mariano","$2y$10$`
306310
$result['response']->getHeaderLine('Set-Cookie')
307311
);
308312
$this->assertStringContainsString(
@@ -333,7 +337,7 @@ public function testPersistIdentity()
333337
]);
334338
$result = $authenticator->persistIdentity($request, $response, $identity);
335339
$this->assertStringContainsString(
336-
'CookieAuth=%5B%22mariano%22%2C%22%242y%2410%24',
340+
'CookieAuth=%5B%22mariano%22%2C%22%242y%24' . $hashCost . '%24',
337341
$result['response']->getHeaderLine('Set-Cookie')
338342
);
339343
}

tests/TestCase/Controller/Component/AuthenticationComponentTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ public function testGetAuthenticationServiceInvalidServiceObject()
143143
$component->getAuthenticationService();
144144
}
145145

146+
public function testGetId(): void
147+
{
148+
$component = new AuthenticationComponent(new ComponentRegistry(new Controller($this->request)));
149+
$this->assertNull($component->getIdentifier());
150+
151+
$request = $this->request
152+
->withAttribute('identity', $this->identity)
153+
->withAttribute('authentication', $this->service);
154+
155+
$controller = new Controller($request);
156+
$registry = new ComponentRegistry($controller);
157+
$component = new AuthenticationComponent($registry);
158+
159+
$this->assertSame($component->getIdentifier(), $this->identity->getIdentifier());
160+
}
161+
146162
/**
147163
* testGetIdentity
148164
*

tests/TestCase/Middleware/AuthenticationMiddlewareTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Authentication\IdentityInterface;
2525
use Authentication\Middleware\AuthenticationMiddleware;
2626
use Authentication\Test\TestCase\AuthenticationTestCase as TestCase;
27+
use Cake\Core\Container;
2728
use Cake\Core\TestSuite\ContainerStubTrait;
2829
use Cake\Http\Response;
2930
use Cake\Http\ServerRequestFactory;
@@ -667,4 +668,25 @@ public function testMiddlewareInjectsServiceIntoDIC(): void
667668
$container = $this->application->getContainer();
668669
$this->assertInstanceOf(AuthenticationService::class, $container->get(AuthenticationService::class));
669670
}
671+
672+
public function testMiddlewareInjectsServiceIntoDICCustomContainerInstance(): void
673+
{
674+
$request = ServerRequestFactory::fromGlobals(
675+
['REQUEST_URI' => '/testpath'],
676+
[],
677+
['username' => 'mariano', 'password' => 'password']
678+
);
679+
$handler = new TestRequestHandler();
680+
681+
$provider = $this->createMock(AuthenticationServiceProviderInterface::class);
682+
$provider
683+
->method('getAuthenticationService')
684+
->willReturn($this->service);
685+
$container = new Container();
686+
687+
$middleware = new AuthenticationMiddleware($provider, $container);
688+
$middleware->process($request, $handler);
689+
690+
$this->assertInstanceOf(AuthenticationService::class, $container->get(AuthenticationService::class));
691+
}
670692
}

tests/TestCase/PasswordHasher/LegacyPasswordHasherTest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ public function testNeedsRehash()
4747
$hasher = new LegacyPasswordHasher();
4848
$this->assertTrue($hasher->needsRehash(md5('foo')));
4949
$this->assertTrue($hasher->needsRehash('bar'));
50-
$this->assertFalse($hasher->needsRehash('$2y$10$juOA0XVFpvZa0KTxRxEYVuX5kIS7U1fKDRcxyYhhUQECN1oHYnBMy'));
50+
$hashCost = '10';
51+
if (PHP_VERSION_ID >= 80400) {
52+
$hashCost = '12';
53+
}
54+
$this->assertFalse($hasher->needsRehash('$2y$' . $hashCost . '$juOA0XVFpvZa0KTxRxEYVuX5kIS7U1fKDRcxyYhhUQECN1oHYnBMy'));
5155
}
5256

5357
/**

0 commit comments

Comments
 (0)