Skip to content

3.1.0 #668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 28, 2024
2 changes: 1 addition & 1 deletion src/Authenticator/ImpersonationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

interface ImpersonationInterface extends PersistenceInterface
interface ImpersonationInterface
{
/**
* Impersonates a user
Expand Down
10 changes: 10 additions & 0 deletions src/Controller/Component/AuthenticationComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
15 changes: 14 additions & 1 deletion src/Middleware/AuthenticationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand All @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions tests/TestCase/Authenticator/CookieAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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')
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/Middleware/AuthenticationMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
6 changes: 5 additions & 1 deletion tests/TestCase/PasswordHasher/LegacyPasswordHasherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

/**
Expand Down
Loading