use token last_activity instead of session value

This commit is contained in:
Christoph Wurst 2016-06-17 12:08:48 +02:00
parent 491e2654eb
commit c4149c59c2
No known key found for this signature in database
GPG Key ID: FEECD2543CA6EAF0
4 changed files with 22 additions and 22 deletions

View File

@ -97,14 +97,17 @@ class DefaultTokenProvider implements IProvider {
* @throws InvalidTokenException
* @param IToken $token
*/
public function updateToken(IToken $token) {
public function updateTokenActivity(IToken $token) {
if (!($token instanceof DefaultToken)) {
throw new InvalidTokenException();
}
/** @var DefaultToken $token */
$token->setLastActivity($this->time->getTime());
$this->mapper->update($token);
$now = $this->time->getTime();
if ($token->getLastActivity() < ($now - 60)) {
// Update token only once per minute
$token->setLastActivity($now);
$this->mapper->update($token);
}
}
/**

View File

@ -76,7 +76,7 @@ interface IProvider {
*
* @param IToken $token
*/
public function updateToken(IToken $token);
public function updateTokenActivity(IToken $token);
/**
* Get all token of a user

View File

@ -237,8 +237,7 @@ class Session implements IUserSession, Emitter {
$this->session->set('last_login_check', $now);
}
// Session is valid, so the token can be refreshed
$this->updateToken($token);
$this->tokenProvider->updateTokenActivity($token);
}
/**
@ -541,7 +540,7 @@ class Session implements IUserSession, Emitter {
$result = $this->loginWithToken($token->getUID());
if ($result) {
// Login success
$this->updateToken($token);
$this->tokenProvider->updateTokenActivity($token);
return true;
}
}
@ -551,19 +550,6 @@ class Session implements IUserSession, Emitter {
return false;
}
/**
* @param IToken $token
*/
private function updateToken(IToken $token) {
// To save unnecessary DB queries, this is only done once a minute
$lastTokenUpdate = $this->session->get('last_token_update') ? : 0;
$now = $this->timeFacory->getTime();
if ($lastTokenUpdate < ($now - 60)) {
$this->tokenProvider->updateToken($token);
$this->session->set('last_token_update', $now);
}
}
/**
* Tries to login the user with auth token header
*

View File

@ -97,15 +97,26 @@ class DefaultTokenProviderTest extends TestCase {
public function testUpdateToken() {
$tk = new DefaultToken();
$tk->setLastActivity($this->time - 200);
$this->mapper->expects($this->once())
->method('update')
->with($tk);
$this->tokenProvider->updateToken($tk);
$this->tokenProvider->updateTokenActivity($tk);
$this->assertEquals($this->time, $tk->getLastActivity());
}
public function testUpdateTokenDebounce() {
$tk = new DefaultToken();
$tk->setLastActivity($this->time - 30);
$this->mapper->expects($this->never())
->method('update')
->with($tk);
$this->tokenProvider->updateTokenActivity($tk);
}
public function testGetTokenByUser() {
$user = $this->getMock('\OCP\IUser');
$this->mapper->expects($this->once())