diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5d50917a..a0a6b733 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,8 +3,8 @@ name: CI on: push: branches: - - 2.x - - 2.next + - 3.x + - 3.next pull_request: branches: - '*' diff --git a/src/Middleware/AuthenticationMiddleware.php b/src/Middleware/AuthenticationMiddleware.php index b6049900..1de93ddc 100644 --- a/src/Middleware/AuthenticationMiddleware.php +++ b/src/Middleware/AuthenticationMiddleware.php @@ -16,11 +16,13 @@ */ namespace Authentication\Middleware; +use Authentication\AuthenticationService; use Authentication\AuthenticationServiceInterface; use Authentication\AuthenticationServiceProviderInterface; use Authentication\Authenticator\AuthenticationRequiredException; use Authentication\Authenticator\StatelessInterface; use Authentication\Authenticator\UnauthenticatedException; +use Cake\Core\ContainerApplicationInterface; use Laminas\Diactoros\Response; use Laminas\Diactoros\Response\RedirectResponse; use Laminas\Diactoros\Stream; @@ -64,6 +66,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) { diff --git a/tests/TestCase/Authenticator/HttpBasicAuthenticatorTest.php b/tests/TestCase/Authenticator/HttpBasicAuthenticatorTest.php index 3b868e84..b600a764 100644 --- a/tests/TestCase/Authenticator/HttpBasicAuthenticatorTest.php +++ b/tests/TestCase/Authenticator/HttpBasicAuthenticatorTest.php @@ -37,6 +37,16 @@ class HttpBasicAuthenticatorTest extends TestCase 'core.Users', ]; + /** + * @var \Authentication\IdentifierCollection + */ + protected $identifiers; + + /** + * @var \Autnentication\Authenticator\HttpBasicAuthenticator + */ + protected $auth; + /** * @inheritDoc */ diff --git a/tests/TestCase/Authenticator/HttpDigestAuthenticatorTest.php b/tests/TestCase/Authenticator/HttpDigestAuthenticatorTest.php index c76c4083..602d4c1b 100644 --- a/tests/TestCase/Authenticator/HttpDigestAuthenticatorTest.php +++ b/tests/TestCase/Authenticator/HttpDigestAuthenticatorTest.php @@ -43,6 +43,16 @@ class HttpDigestAuthenticatorTest extends TestCase 'core.Users', ]; + /** + * @var \Authentication\IdentifierCollection + */ + protected $identifiers; + + /** + * @var \Autnentication\Authenticator\HttpDigestAuthenticator + */ + protected $auth; + /** * setup * diff --git a/tests/TestCase/Authenticator/JwtAuthenticatorTest.php b/tests/TestCase/Authenticator/JwtAuthenticatorTest.php index 155d4100..e10dd65a 100644 --- a/tests/TestCase/Authenticator/JwtAuthenticatorTest.php +++ b/tests/TestCase/Authenticator/JwtAuthenticatorTest.php @@ -60,6 +60,11 @@ class JwtAuthenticatorTest extends TestCase */ public $identifiers; + /** + * @var \Cake\Http\ServerRequest + */ + protected $request; + /** * @inheritDoc */ diff --git a/tests/TestCase/Authenticator/SessionAuthenticatorTest.php b/tests/TestCase/Authenticator/SessionAuthenticatorTest.php index f13fa7db..95cb711b 100644 --- a/tests/TestCase/Authenticator/SessionAuthenticatorTest.php +++ b/tests/TestCase/Authenticator/SessionAuthenticatorTest.php @@ -40,6 +40,13 @@ class SessionAuthenticatorTest extends TestCase 'core.Users', ]; + /** + * @var \Authentication\IdentifierCollection + */ + protected $identifiers; + + protected $sessionMock; + /** * @inheritDoc */ diff --git a/tests/TestCase/Authenticator/TokenAuthenticatorTest.php b/tests/TestCase/Authenticator/TokenAuthenticatorTest.php index 323e6c39..9b67fa5f 100644 --- a/tests/TestCase/Authenticator/TokenAuthenticatorTest.php +++ b/tests/TestCase/Authenticator/TokenAuthenticatorTest.php @@ -34,6 +34,16 @@ class TokenAuthenticatorTest extends TestCase 'core.Users', ]; + /** + * @var \Authentication\IdentifierCollection + */ + protected $identifiers; + + /** + * @var \Cake\Http\ServerRequest + */ + protected $request; + /** * @inheritDoc */ diff --git a/tests/TestCase/Middleware/AuthenticationMiddlewareTest.php b/tests/TestCase/Middleware/AuthenticationMiddlewareTest.php index b4de8f13..1627cd6f 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\TestSuite\ContainerStubTrait; use Cake\Http\Response; use Cake\Http\ServerRequestFactory; use Firebase\JWT\JWT; @@ -32,6 +33,18 @@ class AuthenticationMiddlewareTest extends TestCase { + use ContainerStubTrait; + + /** + * @var \Authentication\AuthenticationService + */ + protected $service; + + /** + * @var \TestApp\Application + */ + protected $application; + /** * Fixtures */ @@ -116,7 +129,7 @@ public function testApplicationAuthenticationRequestResponse() $service->method('getIdentityAttribute')->willReturn('identity'); $application = $this->getMockBuilder(Application::class) - ->disableOriginalConstructor() + ->setConstructorArgs(['config']) ->onlyMethods(['getAuthenticationService', 'middleware']) ->getMock(); @@ -638,4 +651,20 @@ public function testCookieAuthorizationThroughTheMiddlewareStack() $this->assertStringContainsString('CookieAuth=%5B%22mariano%22', $response->getHeaderLine('Set-Cookie')); } + + 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)); + } }