@@ -121,6 +121,12 @@ public function index() {
121121 }
122122
123123 private function login_user ($ authuser ) {
124+ if ($ authuser instanceof \App \Model \Entity \User) {
125+ $ this ->Authentication ->setIdentity ($ authuser );
126+ Log::write ('debug ' , 'Logged in local user ' . $ authuser ['username ' ]);
127+ return ;
128+ }
129+
124130 // Try to find the user in the database
125131 $ user = $ this ->Users ->find ()
126132 ->where ([ 'username ' => $ authuser ['username ' ] ])
@@ -181,6 +187,7 @@ public function login() {
181187
182188 if ($ this ->request ->is ('post ' )) {
183189 $ authuser = $ this ->Authentication ->getIdentity ();
190+ $ this ->debugLocalAuthenticationAttempt ($ authuser );
184191
185192 if (! $ authuser ) {
186193 $ this ->Flash ->error ('Username o password non corretti ' );
@@ -216,6 +223,42 @@ public function login() {
216223 $ this ->set ('redirect ' , $ this ->request ->getQuery (('redirect ' )));
217224 }
218225
226+ private function debugLocalAuthenticationAttempt ($ authuser ): void
227+ {
228+ $ username = (string )$ this ->request ->getData ('username ' , '' );
229+ $ password = (string )$ this ->request ->getData ('password ' , '' );
230+ $ result = $ this ->Authentication ->getResult ();
231+ $ user = $ username === ''
232+ ? null
233+ : $ this ->Users ->find ()
234+ ->where (['username ' => $ username ])
235+ ->first ();
236+
237+ $ debug = [
238+ 'username ' => $ username ,
239+ 'has_password_in_request ' => $ password !== '' ,
240+ 'authentication_result_status ' => $ result ? $ result ->getStatus () : null ,
241+ 'authentication_result_valid ' => $ result ? $ result ->isValid () : null ,
242+ 'authentication_result_errors ' => $ result ? $ result ->getErrors () : null ,
243+ 'identity_class ' => is_object ($ authuser ) ? get_class ($ authuser ) : null ,
244+ 'identity_username ' => $ authuser ? ($ authuser ['username ' ] ?? null ) : null ,
245+ 'local_user_found ' => $ user !== null ,
246+ 'local_user_id ' => $ user ? $ user ['id ' ] : null ,
247+ 'local_user_admin ' => $ user ? (bool )$ user ['admin ' ] : null ,
248+ 'local_password_hash_prefix ' => ($ user && $ user ['password ' ])
249+ ? substr ($ user ['password ' ], 0 , 7 )
250+ : null ,
251+ 'local_password_hash_length ' => ($ user && $ user ['password ' ])
252+ ? strlen ($ user ['password ' ])
253+ : null ,
254+ 'local_password_check ' => ($ user && $ password !== '' )
255+ ? $ user ->checkPassword ($ password )
256+ : null ,
257+ ];
258+
259+ Log::debug ('Local authentication debug: ' . json_encode ($ debug ));
260+ }
261+
219262 private function isOAuth2Enabled () {
220263 return !!getenv ('OAUTH2_APPID ' );
221264 }
0 commit comments