Skip to content
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

merge 2.next -> 3.x #655

Merged
merged 11 commits into from
Dec 26, 2023
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ name: CI
on:
push:
branches:
- 2.x
- 2.next
- 3.x
- 3.next
pull_request:
branches:
- '*'
Expand Down
7 changes: 7 additions & 0 deletions src/Middleware/AuthenticationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions tests/TestCase/Authenticator/HttpBasicAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ class HttpBasicAuthenticatorTest extends TestCase
'core.Users',
];

/**
* @var \Authentication\IdentifierCollection
*/
protected $identifiers;

/**
* @var \Autnentication\Authenticator\HttpBasicAuthenticator
*/
protected $auth;

/**
* @inheritDoc
*/
Expand Down
10 changes: 10 additions & 0 deletions tests/TestCase/Authenticator/HttpDigestAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ class HttpDigestAuthenticatorTest extends TestCase
'core.Users',
];

/**
* @var \Authentication\IdentifierCollection
*/
protected $identifiers;

/**
* @var \Autnentication\Authenticator\HttpDigestAuthenticator
*/
protected $auth;

/**
* setup
*
Expand Down
5 changes: 5 additions & 0 deletions tests/TestCase/Authenticator/JwtAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class JwtAuthenticatorTest extends TestCase
*/
public $identifiers;

/**
* @var \Cake\Http\ServerRequest
*/
protected $request;

/**
* @inheritDoc
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/TestCase/Authenticator/SessionAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class SessionAuthenticatorTest extends TestCase
'core.Users',
];

/**
* @var \Authentication\IdentifierCollection
*/
protected $identifiers;

protected $sessionMock;

/**
* @inheritDoc
*/
Expand Down
10 changes: 10 additions & 0 deletions tests/TestCase/Authenticator/TokenAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ class TokenAuthenticatorTest extends TestCase
'core.Users',
];

/**
* @var \Authentication\IdentifierCollection
*/
protected $identifiers;

/**
* @var \Cake\Http\ServerRequest
*/
protected $request;

/**
* @inheritDoc
*/
Expand Down
31 changes: 30 additions & 1 deletion 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\TestSuite\ContainerStubTrait;
use Cake\Http\Response;
use Cake\Http\ServerRequestFactory;
use Firebase\JWT\JWT;
Expand All @@ -32,6 +33,18 @@

class AuthenticationMiddlewareTest extends TestCase
{
use ContainerStubTrait;

/**
* @var \Authentication\AuthenticationService
*/
protected $service;

/**
* @var \TestApp\Application
*/
protected $application;

/**
* Fixtures
*/
Expand Down Expand Up @@ -116,7 +129,7 @@ public function testApplicationAuthenticationRequestResponse()
$service->method('getIdentityAttribute')->willReturn('identity');

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

Expand Down Expand Up @@ -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));
}
}