Skip to content

Commit 03774ad

Browse files
Merge pull request #42971 from nextcloud/fix/auth/login-email-password-login-name-mismatch
fix(auth): Fix logging in with email and app password
2 parents 90a551d + 7f2fdd8 commit 03774ad

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

lib/private/User/Session.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ public function logClientIn($user,
460460
if ($isTokenPassword) {
461461
$dbToken = $this->tokenProvider->getToken($password);
462462
$userFromToken = $this->manager->get($dbToken->getUID());
463-
$isValidEmailLogin = $userFromToken->getEMailAddress() === $user;
463+
$isValidEmailLogin = $userFromToken->getEMailAddress() === $user
464+
&& $this->validateTokenLoginName($userFromToken->getEMailAddress(), $dbToken);
464465
} else {
465466
$users = $this->manager->getByEmail($user);
466467
$isValidEmailLogin = (\count($users) === 1 && $this->login($users[0]->getUID(), $password));
@@ -800,18 +801,7 @@ private function validateToken($token, $user = null) {
800801
return false;
801802
}
802803

803-
// Check if login names match
804-
if (!is_null($user) && $dbToken->getLoginName() !== $user) {
805-
// TODO: this makes it impossible to use different login names on browser and client
806-
// e.g. login by e-mail 'user@example.com' on browser for generating the token will not
807-
// allow to use the client token with the login name 'user'.
808-
$this->logger->error('App token login name does not match', [
809-
'tokenLoginName' => $dbToken->getLoginName(),
810-
'sessionLoginName' => $user,
811-
'app' => 'core',
812-
'user' => $dbToken->getUID(),
813-
]);
814-
804+
if (!is_null($user) && !$this->validateTokenLoginName($user, $dbToken)) {
815805
return false;
816806
}
817807

@@ -831,6 +821,27 @@ private function validateToken($token, $user = null) {
831821
return true;
832822
}
833823

824+
/**
825+
* Check if login names match
826+
*/
827+
private function validateTokenLoginName(?string $loginName, IToken $token): bool {
828+
if ($token->getLoginName() !== $loginName) {
829+
// TODO: this makes it impossible to use different login names on browser and client
830+
// e.g. login by e-mail 'user@example.com' on browser for generating the token will not
831+
// allow to use the client token with the login name 'user'.
832+
$this->logger->error('App token login name does not match', [
833+
'tokenLoginName' => $token->getLoginName(),
834+
'sessionLoginName' => $loginName,
835+
'app' => 'core',
836+
'user' => $token->getUID(),
837+
]);
838+
839+
return false;
840+
}
841+
842+
return true;
843+
}
844+
834845
/**
835846
* Tries to login the user with auth token header
836847
*

0 commit comments

Comments
 (0)