Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/private/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,6 @@ private function checkTokenCredentials(IToken $dbToken, $token) {
}

$dbToken->setLastCheck($now);
$this->tokenProvider->updateToken($dbToken);
return true;
}

Expand All @@ -608,7 +607,6 @@ private function checkTokenCredentials(IToken $dbToken, $token) {
return false;
}
$dbToken->setLastCheck($now);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that one then required at all?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, otherwise the checks are performed on every request, see above

if ($lastCheck > ($now - 60 * 5)) {
    // Checked performed recently, nothing to do now
    return true;
}

$this->tokenProvider->updateToken($dbToken);
return true;
}

Expand Down
155 changes: 128 additions & 27 deletions tests/lib/User/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Test\User;

use OC\Authentication\Token\DefaultTokenMapper;
use OC\Authentication\Token\DefaultTokenProvider;
use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
use OC\Security\Bruteforce\Throttler;
Expand All @@ -18,10 +20,12 @@
use OC\User\User;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;

/**
Expand Down Expand Up @@ -167,16 +171,16 @@ public function testLoginValidPasswordEnabled() {
$session->expects($this->exactly(2))
->method('set')
->with($this->callback(function ($key) {
switch ($key) {
case 'user_id':
case 'loginname':
return true;
break;
default:
return false;
break;
}
}, 'foo'));
switch ($key) {
case 'user_id':
case 'loginname':
return true;
break;
default:
return false;
break;
}
}, 'foo'));

$managerMethods = get_class_methods('\OC\User\Manager');
//keep following methods intact in order to ensure hooks are
Expand Down Expand Up @@ -490,13 +494,13 @@ public function testRememberLoginValidToken() {
$session->expects($this->exactly(1))
->method('set')
->with($this->callback(function ($key) {
switch ($key) {
case 'user_id':
return true;
default:
return false;
}
}, 'foo'));
switch ($key) {
case 'user_id':
return true;
default:
return false;
}
}, 'foo'));
$session->expects($this->once())
->method('regenerateId');

Expand Down Expand Up @@ -643,8 +647,8 @@ public function testActiveUserAfterSetSession() {
$manager->expects($this->any())
->method('get')
->will($this->returnCallback(function ($uid) use ($users) {
return $users[$uid];
}));
return $users[$uid];
}));

$session = new Memory('');
$session->set('user_id', 'foo');
Expand Down Expand Up @@ -699,7 +703,7 @@ public function testCreateSessionToken() {
->method('getToken')
->with($password)
->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException()));

$this->tokenProvider->expects($this->once())
->method('generateToken')
->with($sessionId, $uid, $loginName, $password, 'Firefox');
Expand Down Expand Up @@ -748,7 +752,7 @@ public function testCreateSessionTokenWithTokenPassword() {
->method('getPassword')
->with($token, $password)
->will($this->returnValue($realPassword));

$this->tokenProvider->expects($this->once())
->method('generateToken')
->with($sessionId, $uid, $loginName, $realPassword, 'Firefox');
Expand All @@ -772,7 +776,7 @@ public function testCreateSessionTokenWithNonExistentUser() {
->method('get')
->with($uid)
->will($this->returnValue(null));

$this->assertFalse($userSession->createSessionToken($request, $uid, $loginName, $password));
}

Expand Down Expand Up @@ -890,9 +894,6 @@ public function testValidateSessionNoPassword() {
->method('getPassword')
->with($token, 'APP-PASSWORD')
->will($this->throwException(new \OC\Authentication\Exceptions\PasswordlessTokenException()));
$tokenProvider->expects($this->once())
->method('updateToken')
->with($token);

$this->invokePrivate($userSession, 'validateSession', [$user]);

Expand All @@ -907,7 +908,7 @@ public function testUpdateSessionTokenPassword() {
$userSession = new \OC\User\Session($userManager, $session, $timeFactory, $tokenProvider, $this->config);

$password = '123456';
$sessionId ='session1234';
$sessionId = 'session1234';
$token = new \OC\Authentication\Token\DefaultToken();

$session->expects($this->once())
Expand Down Expand Up @@ -946,7 +947,7 @@ public function testUpdateSessionTokenPasswordInvalidTokenException() {
$userSession = new \OC\User\Session($userManager, $session, $timeFactory, $tokenProvider, $this->config);

$password = '123456';
$sessionId ='session1234';
$sessionId = 'session1234';
$token = new \OC\Authentication\Token\DefaultToken();

$session->expects($this->once())
Expand All @@ -964,4 +965,104 @@ public function testUpdateSessionTokenPasswordInvalidTokenException() {
$userSession->updateSessionTokenPassword($password);
}

public function testUpdateAuthTokenLastCheck() {
$manager = $this->getMockBuilder('\OC\User\Manager')
->disableOriginalConstructor()
->getMock();
$session = $this->createMock(ISession::class);
$request = $this->createMock(IRequest::class);

$token = new \OC\Authentication\Token\DefaultToken();
$token->setUid('john');
$token->setLoginName('john');
$token->setLastActivity(100);
$token->setLastCheck(100);

$mapper = $this->getMockBuilder(DefaultTokenMapper::class)
->disableOriginalConstructor()
->getMock();
$crypto = $this->getMock(ICrypto::class);
$logger = $this->getMock(ILogger::class);
$tokenProvider = new DefaultTokenProvider($mapper, $crypto, $this->config, $logger, $this->timeFactory);

/** @var \OC\User\Session $userSession */
$userSession = new Session($manager, $session, $this->timeFactory, $tokenProvider, $this->config);

$mapper->expects($this->any())
->method('getToken')
->will($this->returnValue($token));
$mapper->expects($this->once())
->method('update');
$request
->expects($this->any())
->method('getRemoteAddress')
->willReturn('192.168.0.1');
$this->throttler
->expects($this->once())
->method('sleepDelay')
->with('192.168.0.1');
$this->throttler
->expects($this->any())
->method('getDelay')
->with('192.168.0.1')
->willReturn(0);
$this->timeFactory
->expects($this->any())
->method('getTime')
->will($this->returnValue(100));

$userSession->logClientIn('john', 'doe', $request, $this->throttler);

$this->assertEquals(10000, $token->getLastActivity());
$this->assertEquals(10000, $token->getLastCheck());
}

public function testNoUpdateAuthTokenLastCheckRecent() {
$manager = $this->getMockBuilder('\OC\User\Manager')
->disableOriginalConstructor()
->getMock();
$session = $this->createMock(ISession::class);
$request = $this->createMock(IRequest::class);

$token = new \OC\Authentication\Token\DefaultToken();
$token->setUid('john');
$token->setLoginName('john');
$token->setLastActivity(10000);
$token->setLastCheck(100);

$mapper = $this->getMockBuilder(DefaultTokenMapper::class)
->disableOriginalConstructor()
->getMock();
$crypto = $this->getMock(ICrypto::class);
$logger = $this->getMock(ILogger::class);
$tokenProvider = new DefaultTokenProvider($mapper, $crypto, $this->config, $logger, $this->timeFactory);

/** @var \OC\User\Session $userSession */
$userSession = new Session($manager, $session, $this->timeFactory, $tokenProvider, $this->config);

$mapper->expects($this->any())
->method('getToken')
->will($this->returnValue($token));
$mapper->expects($this->never())
->method('update');
$request
->expects($this->any())
->method('getRemoteAddress')
->willReturn('192.168.0.1');
$this->throttler
->expects($this->once())
->method('sleepDelay')
->with('192.168.0.1');
$this->throttler
->expects($this->any())
->method('getDelay')
->with('192.168.0.1')
->willReturn(0);
$this->timeFactory
->expects($this->any())
->method('getTime')
->will($this->returnValue(100));

$userSession->logClientIn('john', 'doe', $request, $this->throttler);
}
}