diff --git a/docs/en/identity-object.rst b/docs/en/identity-object.rst index 5dacb394..f606ea22 100644 --- a/docs/en/identity-object.rst +++ b/docs/en/identity-object.rst @@ -108,10 +108,9 @@ that implements the required interface:: // Then pass it to the service configuration $service = new AuthenticationService([ 'identityClass' => $identityResolver, - 'identifiers' => [ - 'Authentication.Password' - ], 'authenticators' => [ - 'Authentication.Form' + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ] ]); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 45ff80a6..b1a2a3c8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,6 +3,8 @@ colors="true" processIsolation="false" stopOnFailure="false" + failOnDeprecation="true" + displayDetailsOnTestsThatTriggerDeprecations="true" cacheDirectory=".phpunit.cache" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"> diff --git a/src/AuthenticationService.php b/src/AuthenticationService.php index 656d2844..e4a08276 100644 --- a/src/AuthenticationService.php +++ b/src/AuthenticationService.php @@ -89,11 +89,10 @@ class AuthenticationService implements AuthenticationServiceInterface, Impersona * ``` * $service = new AuthenticationService([ * 'authenticators' => [ - * 'Authentication.Form + * 'Authentication.Form' => [ + * 'identifier' => 'Authentication.Password', + * ], * ], - * 'identifiers' => [ - * 'Authentication.Password' - * ] * ]); * ``` * @@ -172,7 +171,7 @@ public function loadIdentifier(string $name, array $config = []): IdentifierInte { deprecationWarning( '3.3.0', - 'loadIdentifier() usage is deprecated. Directly pass Identifier to Authenticator.', + 'loadIdentifier() usage is deprecated. Directly pass `\'identifier\'` config to the Authenticator.', ); return $this->identifiers()->load($name, $config); diff --git a/src/Authenticator/AuthenticatorCollection.php b/src/Authenticator/AuthenticatorCollection.php index 8036eee3..1796f87b 100644 --- a/src/Authenticator/AuthenticatorCollection.php +++ b/src/Authenticator/AuthenticatorCollection.php @@ -46,7 +46,7 @@ public function __construct(IdentifierCollection $identifiers, array $config = [ if ($identifiers->count() > 0) { deprecationWarning( '3.3.0', - 'loadIdentifier() usage is deprecated. Directly pass Identifier to Authenticator.', + 'loadIdentifier() usage is deprecated. Directly pass `\'identifier\'` config to the Authenticator.', ); } @@ -70,7 +70,7 @@ protected function _create(object|string $class, string $alias, array $config): } else { deprecationWarning( '3.3.0', - 'loadIdentifier() usage is deprecated. Directly pass `\'identifier\'` config to Authenticator.', + 'loadIdentifier() usage is deprecated. Directly pass `\'identifier\'` config to the Authenticator.', ); } diff --git a/tests/TestCase/AuthenticationServiceTest.php b/tests/TestCase/AuthenticationServiceTest.php index 757d7953..a551e543 100644 --- a/tests/TestCase/AuthenticationServiceTest.php +++ b/tests/TestCase/AuthenticationServiceTest.php @@ -34,6 +34,7 @@ use Cake\I18n\DateTime; use Cake\Routing\Router; use InvalidArgumentException; +use PHPUnit\Runner\Version; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -55,11 +56,10 @@ public function testAuthenticateDeprecated() ); $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Form', + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); @@ -121,11 +121,10 @@ public function testAuthenticateWithChallenge() ]); $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.HttpBasic', + 'Authentication.HttpBasic' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); @@ -155,12 +154,10 @@ public function testAuthenticateWithChallengeDisabled() ]); $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ 'Authentication.HttpBasic' => [ 'skipChallenge' => true, + 'identifier' => 'Authentication.Password', ], ], ]); @@ -190,12 +187,10 @@ public function testAuthenticationWithSessionIdentify() $factory = function () { return new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ 'Authentication.Session' => [ 'identify' => true, + 'identifier' => 'Authentication.Password', ], ], ]); @@ -238,9 +233,16 @@ public function testLoadAuthenticatorException() */ public function testLoadIdentifier() { - $service = new AuthenticationService(); - $result = $service->loadIdentifier('Authentication.Password'); - $this->assertInstanceOf(PasswordIdentifier::class, $result); + $this->skipIf( + version_compare(Version::id(), '11.0', '<'), + 'For some reason PHPUnit doesn\'t pick up the deprecation on v10', + ); + + $this->deprecated(function () { + $service = new AuthenticationService(); + $result = $service->loadIdentifier('Authentication.Password'); + $this->assertInstanceOf(PasswordIdentifier::class, $result); + }); } /** @@ -263,11 +265,10 @@ public function testIdentifiers() public function testClearIdentity() { $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Form', + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); @@ -293,11 +294,10 @@ public function testClearIdentity() public function testClearIdentityWithCustomIdentityAttribute() { $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Form', + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], 'identityAttribute' => 'customIdentity', ]); @@ -324,11 +324,10 @@ public function testClearIdentityWithCustomIdentityAttribute() public function testClearIdentityWithCustomIdentityAttributeShouldPreserveDefault() { $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Form', + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], 'identityAttribute' => 'customIdentity', ]); @@ -360,11 +359,10 @@ public function testClearIdentityWithCustomIdentityAttributeShouldPreserveDefaul public function testClearIdentityWithImpersonation() { $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Session', + 'Authentication.Session' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); @@ -394,12 +392,13 @@ public function testClearIdentityWithImpersonation() public function testPersistIdentity() { $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Session', - 'Authentication.Form', + 'Authentication.Session' => [ + 'identifier' => 'Authentication.Password', + ], + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); @@ -438,12 +437,13 @@ public function testPersistIdentity() public function testPersistIdentityWithCustomIdentityAttribute() { $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Session', - 'Authentication.Form', + 'Authentication.Session' => [ + 'identifier' => 'Authentication.Password', + ], + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], 'identityAttribute' => 'customIdentity', ]); @@ -485,12 +485,13 @@ public function testPersistIdentityWithCustomIdentityAttribute() public function testPersistIdentityWithCustomIdentityAttributeShouldPreserveDefault() { $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Session', - 'Authentication.Form', + 'Authentication.Session' => [ + 'identifier' => 'Authentication.Password', + ], + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], 'identityAttribute' => 'customIdentity', ]); @@ -600,12 +601,13 @@ public function testGetResult() ); $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Session', - 'Authentication.Form', + 'Authentication.Session' => [ + 'identifier' => 'Authentication.Password', + ], + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); @@ -632,11 +634,7 @@ public function testNoAuthenticatorsLoadedException() ['username' => 'mariano', 'password' => 'password'], ); - $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], - ]); + $service = new AuthenticationService(); $service->authenticate($request); } @@ -716,11 +714,10 @@ public function testCallableIdentityProvider() $service = new AuthenticationService([ 'identityClass' => $callable, - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Form', + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); @@ -744,11 +741,10 @@ public function testGetIdentity() ); $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Form', + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); @@ -954,7 +950,9 @@ public function testImpersonate() $service = new AuthenticationService([ 'authenticators' => [ - 'Authentication.Session', + 'Authentication.Session' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); $service->authenticate($request); @@ -988,7 +986,9 @@ public function testImpersonateAlreadyImpersonating() $service = new AuthenticationService([ 'authenticators' => [ - 'Authentication.Session', + 'Authentication.Session' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); $service->authenticate($request); @@ -1013,11 +1013,10 @@ public function testImpersonateWrongProvider() $impersonator = new ArrayObject(['username' => 'mariano']); $impersonated = new ArrayObject(['username' => 'larry']); $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Form', + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); @@ -1047,7 +1046,9 @@ public function testStopImpersonating() $service = new AuthenticationService([ 'authenticators' => [ - 'Authentication.Session', + 'Authentication.Session' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); $service->authenticate($request); @@ -1076,11 +1077,10 @@ public function testStopImpersonatingWrongProvider() $response = new Response(); $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Form', + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); @@ -1108,8 +1108,9 @@ public function testIsImpersonatingImpersonating() $request->getSession()->write('AuthImpersonate', $impersonator); $service = new AuthenticationService([ 'authenticators' => [ - 'Authentication.Session', - + 'Authentication.Session' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); $service->authenticate($request); @@ -1135,8 +1136,9 @@ public function testIsImpersonatingNotImpersonating() $service = new AuthenticationService([ 'authenticators' => [ - 'Authentication.Session', - + 'Authentication.Session' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); $service->authenticate($request); @@ -1158,11 +1160,10 @@ public function testIsImpersonatingWrongProvider() ); $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Form', + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); diff --git a/tests/TestCase/Authenticator/AuthenticatorCollectionTest.php b/tests/TestCase/Authenticator/AuthenticatorCollectionTest.php index 7e3a6f60..c1565955 100644 --- a/tests/TestCase/Authenticator/AuthenticatorCollectionTest.php +++ b/tests/TestCase/Authenticator/AuthenticatorCollectionTest.php @@ -33,7 +33,9 @@ public function testConstruct() { $identifiers = $this->createMock(IdentifierCollection::class); $collection = new AuthenticatorCollection($identifiers, [ - 'Authentication.Form', + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ]); $result = $collection->get('Form'); $this->assertInstanceOf(FormAuthenticator::class, $result); @@ -48,7 +50,9 @@ public function testLoad() { $identifiers = $this->createMock(IdentifierCollection::class); $collection = new AuthenticatorCollection($identifiers); - $result = $collection->load('Authentication.Form'); + $result = $collection->load('Authentication.Form', [ + 'identifier' => 'Authentication.Password', + ]); $this->assertInstanceOf(FormAuthenticator::class, $result); } @@ -87,7 +91,9 @@ public function testIsEmpty() $collection = new AuthenticatorCollection($identifiers); $this->assertTrue($collection->isEmpty()); - $collection->load('Authentication.Form'); + $collection->load('Authentication.Form', [ + 'identifier' => 'Authentication.Password', + ]); $this->assertFalse($collection->isEmpty()); } diff --git a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php index 43ffdbff..45cbb23c 100644 --- a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php +++ b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php @@ -75,12 +75,13 @@ public function setUp(): void $this->identity = new Identity($this->identityData); $this->service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Session', - 'Authentication.Form', + 'Authentication.Session' => [ + 'identifier' => 'Authentication.Password', + ], + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); diff --git a/tests/TestCase/Middleware/AuthenticationMiddlewareTest.php b/tests/TestCase/Middleware/AuthenticationMiddlewareTest.php index c6eb4369..8c11ef61 100644 --- a/tests/TestCase/Middleware/AuthenticationMiddlewareTest.php +++ b/tests/TestCase/Middleware/AuthenticationMiddlewareTest.php @@ -61,11 +61,10 @@ public function setUp(): void { parent::setUp(); $this->service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Form', + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); $this->application = new Application('config'); @@ -87,7 +86,6 @@ public function testApplicationAuthentication() $service = $handler->request->getAttribute('authentication'); $this->assertInstanceOf(AuthenticationService::class, $service); - $this->assertTrue($service->identifiers()->has('Password')); $this->assertTrue($service->authenticators()->has('Form')); } @@ -114,7 +112,6 @@ public function testProviderAuthentication() $this->assertSame($this->service, $service); $this->assertSame('identity', $service->getConfig('identityAttribute')); - $this->assertTrue($service->identifiers()->has('Password')); $this->assertTrue($service->authenticators()->has('Form')); } @@ -180,11 +177,10 @@ public function testAuthenticationAndClearIdentityInteraction() // Setup the request with a session so we can test it being cleared $request->getSession()->write('Auth', ['username' => 'mariano']); $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Session', + 'Authentication.Session' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); $handler = new TestRequestHandler(function ($request) { @@ -269,12 +265,9 @@ public function testSuccessfulAuthenticationPersistIdentity() ['username' => 'mariano', 'password' => 'password'], ); $this->service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Form', - 'Authentication.Session', + 'Authentication.Form' => ['identifier' => 'Authentication.Password'], + 'Authentication.Session' => ['identifier' => 'Authentication.Password'], ], ]); $middleware = new AuthenticationMiddleware($this->service); @@ -333,11 +326,8 @@ public function testNonSuccessfulAuthenticationWithChallenge() $handler = new TestRequestHandler(); $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.HttpBasic', + 'Authentication.HttpBasic' => ['identifier' => 'Authentication.Password'], ], ]); @@ -589,14 +579,11 @@ public function testJwtTokenAuthorizationThroughTheMiddlewareStack() $token = JWT::encode($data, 'secretKey', 'HS256'); $this->service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - 'Authentication.JwtSubject', - ], 'authenticators' => [ - 'Authentication.Form', + 'Authentication.Form' => ['identifier' => 'Authentication.Password'], 'Authentication.Jwt' => [ 'secretKey' => 'secretKey', + 'identifier' => 'Authentication.JwtSubject', ], ], ]); @@ -626,12 +613,9 @@ public function testJwtTokenAuthorizationThroughTheMiddlewareStack() public function testCookieAuthorizationThroughTheMiddlewareStack() { $this->service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Form', - 'Authentication.Cookie', + 'Authentication.Form' => ['identifier' => 'Authentication.Password'], + 'Authentication.Cookie' => ['identifier' => 'Authentication.Password'], ], ]); diff --git a/tests/test_app/TestApp/Application.php b/tests/test_app/TestApp/Application.php index 6e068dbc..9dbe9f36 100644 --- a/tests/test_app/TestApp/Application.php +++ b/tests/test_app/TestApp/Application.php @@ -29,16 +29,18 @@ public function middleware(MiddlewareQueue $middleware): MiddlewareQueue public function authentication(AuthenticationServiceInterface $service) { - $service->loadIdentifier('Authentication.Password'); - $service->loadAuthenticator('Authentication.Form'); + $service->loadAuthenticator('Authentication.Form', [ + 'identifier' => 'Authentication.Password', + ]); return $service; } public function authenticationApi(AuthenticationServiceInterface $service) { - $service->loadIdentifier('Authentication.Token'); - $service->loadAuthenticator('Authentication.Token'); + $service->loadAuthenticator('Authentication.Token', [ + 'identifier' => 'Authentication.Token', + ]); return $service; } @@ -52,8 +54,9 @@ public function authenticationApi(AuthenticationServiceInterface $service) public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface { $service = new AuthenticationService(); - $service->loadIdentifier('Authentication.Password'); - $service->loadAuthenticator('Authentication.Form'); + $service->loadAuthenticator('Authentication.Form', [ + 'identifier' => 'Authentication.Password', + ]); return $service; }