Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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));
}
}