]> source.dussan.org Git - nextcloud-server.git/commitdiff
use token last_activity instead of session value
authorChristoph Wurst <christoph@owncloud.com>
Fri, 17 Jun 2016 10:08:48 +0000 (12:08 +0200)
committerChristoph Wurst <christoph@owncloud.com>
Fri, 17 Jun 2016 13:42:28 +0000 (15:42 +0200)
lib/private/Authentication/Token/DefaultTokenProvider.php
lib/private/Authentication/Token/IProvider.php
lib/private/User/Session.php
tests/lib/Authentication/Token/DefaultTokenProviderTest.php

index 84effc5f87556daa2831f134eabb19212754f5f5..03b8bb5da2877e1582a976250c5e43e1a4e1fafc 100644 (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);
+               }
        }
 
        /**
index fece7dcb5676dc7ae1b996de0bdead33d8850865..e79ba8b30e5521d7b21c755da506b38652e82048 100644 (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
index 0cebb3e0613ee7b2647524c8cff7b7904f8c31fe..89148dcf8ecc7ac8ceb806665ae31dda0976fdae 100644 (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
         *
index 98cee208065e934760aee2ec7a29c40e22232c94..86f4842bbc36c9e63b62f57ce17985befd39dfe6 100644 (file)
@@ -97,14 +97,25 @@ 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');