66use Authentication \AuthenticationService ;
77use Authentication \AuthenticationServiceInterface ;
88use Authentication \AuthenticationServiceProviderInterface ;
9- use Authentication \Identifier \AbstractIdentifier ;
9+ use Authentication \Identifier \PasswordIdentifier ;
10+ use Authentication \Identifier \IdentifierFactory ;
1011use Authentication \Middleware \AuthenticationMiddleware ;
1112use Cake \Core \Configure ;
1213use 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