Skip to content

Commit 73bc93b

Browse files
authored
Support cake authn 4 (#166)
1 parent 5d50ea1 commit 73bc93b

3 files changed

Lines changed: 39 additions & 24 deletions

File tree

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"require-dev": {
2121
"ext-mysqli": "*",
2222
"ext-sqlite3": "*",
23-
"cakephp/authentication": "^3.0",
23+
"cakephp/authentication": "^3.0 || ^4.0",
2424
"cakephp/bake": "^3.0",
2525
"cakephp/cakephp-codesniffer": "^4.2",
2626
"cakephp/debug_kit": "^5.0",

‎plugins/jwt-auth/composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"require": {
88
"ext-openssl": "*",
99
"php": "^8.1",
10-
"cakephp/authentication": "^3.0",
10+
"cakephp/authentication": "^3.0 || ^4.0",
1111
"cakephp/cakephp": "^5.0",
1212
"firebase/php-jwt": "^7.0"
1313
},

‎plugins/jwt-auth/tests/test_app/src/Application.php‎

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
use Authentication\AuthenticationService;
77
use Authentication\AuthenticationServiceInterface;
88
use Authentication\AuthenticationServiceProviderInterface;
9-
use Authentication\Identifier\AbstractIdentifier;
9+
use Authentication\Identifier\PasswordIdentifier;
10+
use Authentication\Identifier\IdentifierFactory;
1011
use Authentication\Middleware\AuthenticationMiddleware;
1112
use Cake\Core\Configure;
1213
use Cake\Core\ContainerInterface;
@@ -90,32 +91,30 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
9091
$service = new AuthenticationService();
9192
$service->loadAuthenticator('Authentication.Form', [
9293
'fields' => [
93-
AbstractIdentifier::CREDENTIAL_USERNAME => 'email',
94-
AbstractIdentifier::CREDENTIAL_PASSWORD => 'password',
94+
PasswordIdentifier::CREDENTIAL_USERNAME => 'email',
95+
PasswordIdentifier::CREDENTIAL_PASSWORD => 'password',
9596
],
9697
'loginUrl' => '/test/login.json',
97-
'identifier' => [
98-
'Authentication.Password' => [
99-
'fields' => [
100-
AbstractIdentifier::CREDENTIAL_USERNAME => 'email',
101-
AbstractIdentifier::CREDENTIAL_PASSWORD => 'password',
102-
],
103-
'resolver' => [
104-
'className' => 'Authentication.Orm',
105-
'userModel' => 'Users',
106-
],
107-
'passwordHasher' => [
108-
'className' => 'Authentication.Fallback',
109-
'hashers' => [
110-
'Authentication.Default',
111-
[
112-
'className' => 'Authentication.Legacy',
113-
'hashType' => 'md5',
114-
],
98+
'identifier' => $this->identifierConfig('Authentication.Password', [
99+
'fields' => [
100+
PasswordIdentifier::CREDENTIAL_USERNAME => 'email',
101+
PasswordIdentifier::CREDENTIAL_PASSWORD => 'password',
102+
],
103+
'resolver' => [
104+
'className' => 'Authentication.Orm',
105+
'userModel' => 'Users',
106+
],
107+
'passwordHasher' => [
108+
'className' => 'Authentication.Fallback',
109+
'hashers' => [
110+
'Authentication.Default',
111+
[
112+
'className' => 'Authentication.Legacy',
113+
'hashType' => 'md5',
115114
],
116115
],
117116
],
118-
],
117+
]),
119118
]);
120119

121120
if (str_starts_with(haystack: $config->getAlg(), needle: 'HS')) {
@@ -134,4 +133,20 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
134133

135134
return $service;
136135
}
136+
137+
/**
138+
* Builds identifier config compatible with both cakephp/authentication 3.x (nested) and 4.x (flat).
139+
*
140+
* @param string $className Identifier class name
141+
* @param array $config Identifier config
142+
* @return array
143+
*/
144+
private function identifierConfig(string $className, array $config): array
145+
{
146+
if (class_exists(IdentifierFactory::class)) {
147+
return ['className' => $className] + $config;
148+
}
149+
150+
return [$className => $config];
151+
}
137152
}

0 commit comments

Comments
 (0)