Skip to content

Commit

Permalink
Merge pull request #647 from cakephp/2.next-service-di
Browse files Browse the repository at this point in the history
2.next: add authentication service to DIC by default
  • Loading branch information
markstory authored Nov 12, 2023
2 parents 3dfad95 + bdbe04b commit 17d941e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
6 changes: 6 additions & 0 deletions src/Middleware/AuthenticationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Authentication\Authenticator\AuthenticationRequiredException;
use Authentication\Authenticator\StatelessInterface;
use Authentication\Authenticator\UnauthenticatedException;
use Cake\Core\ContainerApplicationInterface;
use Cake\Core\InstanceConfigTrait;
use InvalidArgumentException;
use Laminas\Diactoros\Response;
Expand Down Expand Up @@ -101,6 +102,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
{
$service = $this->getAuthenticationService($request);

if ($this->subject instanceof ContainerApplicationInterface) {
$container = $this->subject->getContainer();
$container->add(AuthenticationService::class, $service);
}

try {
$result = $service->authenticate($request);
} catch (AuthenticationRequiredException $e) {
Expand Down
46 changes: 19 additions & 27 deletions tests/TestCase/Middleware/AuthenticationMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
namespace Authentication\Test\TestCase\Middleware;

use Authentication\AuthenticationService;
use Authentication\AuthenticationServiceInterface;
use Authentication\AuthenticationServiceProviderInterface;
use Authentication\Authenticator\ResultInterface;
use Authentication\Authenticator\UnauthenticatedException;
use Authentication\IdentityInterface;
use Authentication\Middleware\AuthenticationMiddleware;
use Authentication\Test\TestCase\AuthenticationTestCase as TestCase;
use Cake\Core\TestSuite\ContainerStubTrait;
use Cake\Http\Response;
use Cake\Http\ServerRequestFactory;
use Cake\Http\Uri;
Expand All @@ -33,6 +32,8 @@

class AuthenticationMiddlewareTest extends TestCase
{
use ContainerStubTrait;

/**
* Fixtures
*/
Expand Down Expand Up @@ -105,31 +106,6 @@ public function testProviderAuthentication()
$this->assertTrue($service->authenticators()->has('Form'));
}

public function testApplicationAuthenticationRequestResponse()
{
$request = ServerRequestFactory::fromGlobals();
$handler = new TestRequestHandler();

$service = $this->createMock(AuthenticationServiceInterface::class);

$service->method('authenticate')
->willReturn($this->createMock(ResultInterface::class));
$service->method('getIdentityAttribute')->willReturn('identity');

$application = $this->getMockBuilder(Application::class)
->disableOriginalConstructor()
->setMethods(['getAuthenticationService', 'middleware'])
->getMock();

$application->expects($this->once())
->method('getAuthenticationService')
->with($request)
->willReturn($service);

$middleware = new AuthenticationMiddleware($application);
$middleware->process($request, $handler);
}

public function testInvalidSubject()
{
$this->expectException('InvalidArgumentException');
Expand Down Expand Up @@ -770,4 +746,20 @@ public function testServiceConfigurationFallback()
$this->assertSame('redirect', $service->getConfig('queryParam'));
$this->assertSame('/login', $service->getConfig('unauthenticatedRedirect'));
}

public function testMiddlewareInjectsServiceIntoDIC(): void
{
$request = ServerRequestFactory::fromGlobals(
['REQUEST_URI' => '/testpath'],
[],
['username' => 'mariano', 'password' => 'password']
);
$handler = new TestRequestHandler();

$middleware = new AuthenticationMiddleware($this->application);
$middleware->process($request, $handler);

$container = $this->application->getContainer();
$this->assertInstanceOf(AuthenticationService::class, $container->get(AuthenticationService::class));
}
}

0 comments on commit 17d941e

Please sign in to comment.