From b3924c313156f1b5f006560f445eb34fd2bd61ff Mon Sep 17 00:00:00 2001 From: Chris Nizzardini Date: Sun, 22 Nov 2026 15:33:11 -0500 Subject: [PATCH] Support cake authn 4 --- composer.json | 2 +- plugins/jwt-auth/composer.json | 2 +- .../tests/test_app/src/Application.php | 59 ++++++++++++------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/composer.json b/composer.json index 55b0cda..d6f2267 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "require-dev": { "ext-mysqli": "*", "ext-sqlite3": "*", - "cakephp/authentication": "^3.0", + "cakephp/authentication": "^3.0 || ^4.0", "cakephp/bake": "^3.0", "cakephp/cakephp-codesniffer": "^4.2", "cakephp/debug_kit": "^5.0", diff --git a/plugins/jwt-auth/composer.json b/plugins/jwt-auth/composer.json index 1834f5b..43ab59b 100755 --- a/plugins/jwt-auth/composer.json +++ b/plugins/jwt-auth/composer.json @@ -7,7 +7,7 @@ "require": { "ext-openssl": "*", "php": "^8.1", - "cakephp/authentication": "^3.0", + "cakephp/authentication": "^3.0 || ^4.0", "cakephp/cakephp": "^5.0", "firebase/php-jwt": "^7.0" }, diff --git a/plugins/jwt-auth/tests/test_app/src/Application.php b/plugins/jwt-auth/tests/test_app/src/Application.php index 23dc1f6..ccb23c4 100644 --- a/plugins/jwt-auth/tests/test_app/src/Application.php +++ b/plugins/jwt-auth/tests/test_app/src/Application.php @@ -6,7 +6,8 @@ use Authentication\AuthenticationService; use Authentication\AuthenticationServiceInterface; use Authentication\AuthenticationServiceProviderInterface; -use Authentication\Identifier\AbstractIdentifier; +use Authentication\Identifier\PasswordIdentifier; +use Authentication\Identifier\IdentifierFactory; use Authentication\Middleware\AuthenticationMiddleware; use Cake\Core\Configure; use Cake\Core\ContainerInterface; @@ -90,32 +91,30 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe $service = new AuthenticationService(); $service->loadAuthenticator('Authentication.Form', [ 'fields' => [ - AbstractIdentifier::CREDENTIAL_USERNAME => 'email', - AbstractIdentifier::CREDENTIAL_PASSWORD => 'password', + PasswordIdentifier::CREDENTIAL_USERNAME => 'email', + PasswordIdentifier::CREDENTIAL_PASSWORD => 'password', ], 'loginUrl' => '/test/login.json', - 'identifier' => [ - 'Authentication.Password' => [ - 'fields' => [ - AbstractIdentifier::CREDENTIAL_USERNAME => 'email', - AbstractIdentifier::CREDENTIAL_PASSWORD => 'password', - ], - 'resolver' => [ - 'className' => 'Authentication.Orm', - 'userModel' => 'Users', - ], - 'passwordHasher' => [ - 'className' => 'Authentication.Fallback', - 'hashers' => [ - 'Authentication.Default', - [ - 'className' => 'Authentication.Legacy', - 'hashType' => 'md5', - ], + 'identifier' => $this->identifierConfig('Authentication.Password', [ + 'fields' => [ + PasswordIdentifier::CREDENTIAL_USERNAME => 'email', + PasswordIdentifier::CREDENTIAL_PASSWORD => 'password', + ], + 'resolver' => [ + 'className' => 'Authentication.Orm', + 'userModel' => 'Users', + ], + 'passwordHasher' => [ + 'className' => 'Authentication.Fallback', + 'hashers' => [ + 'Authentication.Default', + [ + 'className' => 'Authentication.Legacy', + 'hashType' => 'md5', ], ], ], - ], + ]), ]); if (str_starts_with(haystack: $config->getAlg(), needle: 'HS')) { @@ -134,4 +133,20 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe return $service; } + + /** + * Builds identifier config compatible with both cakephp/authentication 3.x (nested) and 4.x (flat). + * + * @param string $className Identifier class name + * @param array $config Identifier config + * @return array + */ + private function identifierConfig(string $className, array $config): array + { + if (class_exists(IdentifierFactory::class)) { + return ['className' => $className] + $config; + } + + return [$className => $config]; + } }