Skip to content

Commit db34671

Browse files
check login name when authenticating with client token
1 parent 04e3da0 commit db34671

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

lib/private/User/Session.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public function getLoginName() {
280280
*/
281281
public function login($uid, $password) {
282282
$this->session->regenerateId();
283-
if ($this->validateToken($password)) {
283+
if ($this->validateToken($password, $uid)) {
284284
// When logging in with token, the password must be decrypted first before passing to login hook
285285
try {
286286
$token = $this->tokenProvider->getToken($password);
@@ -584,15 +584,24 @@ private function checkTokenCredentials(IToken $dbToken, $token) {
584584
* Invalidates the token if checks fail
585585
*
586586
* @param string $token
587+
* @param string $user login name
587588
* @return boolean
588589
*/
589-
private function validateToken($token) {
590+
private function validateToken($token, $user = null) {
590591
try {
591592
$dbToken = $this->tokenProvider->getToken($token);
592593
} catch (InvalidTokenException $ex) {
593594
return false;
594595
}
595596

597+
// Check if login names match
598+
if (!is_null($user) && $dbToken->getLoginName() !== $user) {
599+
// TODO: this makes it imposssible to use different login names on browser and client
600+
// e.g. login by e-mail 'user@example.com' on browser for generating the token will not
601+
// allow to use the client token with the login name 'user'.
602+
return false;
603+
}
604+
596605
if (!$this->checkTokenCredentials($dbToken, $token)) {
597606
return false;
598607
}

tests/lib/User/SessionTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,36 @@ public function testLoginNonExisting() {
314314
$userSession->login('foo', 'bar');
315315
}
316316

317+
/**
318+
* When using a device token, the loginname must match the one that was used
319+
* when generating the token on the browser.
320+
*/
321+
public function testLoginWithDifferentTokenLoginName() {
322+
$session = $this->getMock('\OC\Session\Memory', array(), array(''));
323+
$manager = $this->getMock('\OC\User\Manager');
324+
$backend = $this->getMock('\Test\Util\User\Dummy');
325+
$userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
326+
$username = 'user123';
327+
$token = new \OC\Authentication\Token\DefaultToken();
328+
$token->setLoginName($username);
329+
330+
$session->expects($this->never())
331+
->method('set');
332+
$session->expects($this->once())
333+
->method('regenerateId');
334+
$this->tokenProvider->expects($this->once())
335+
->method('getToken')
336+
->with('bar')
337+
->will($this->returnValue($token));
338+
339+
$manager->expects($this->once())
340+
->method('checkPassword')
341+
->with('foo', 'bar')
342+
->will($this->returnValue(false));
343+
344+
$userSession->login('foo', 'bar');
345+
}
346+
317347
/**
318348
* @expectedException \OC\Authentication\Exceptions\PasswordLoginForbiddenException
319349
*/

0 commit comments

Comments
 (0)