]> source.dussan.org Git - nextcloud-server.git/commitdiff
Refactor: move remote wipe token logic to RW service
authorChristoph Wurst <christoph@winzerhof-wurst.at>
Wed, 3 Jul 2019 07:44:37 +0000 (09:44 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Tue, 9 Jul 2019 11:39:27 +0000 (13:39 +0200)
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
lib/private/Authentication/Token/IWipeableToken.php
lib/private/Authentication/Token/RemoteWipe.php
settings/Controller/AuthSettingsController.php
tests/Settings/Controller/AuthSettingsControllerTest.php
tests/lib/Authentication/Token/RemoteWipeTest.php

index 8d4d3a60781b347f0268e142bc0a3bc51c45ce2a..f0777bf4a24bf7df57e04b3c723102192f0c1cc2 100644 (file)
@@ -24,6 +24,11 @@ declare(strict_types=1);
 
 namespace OC\Authentication\Token;
 
-interface IWipeableToken {
+interface IWipeableToken extends IToken {
+
+       /**
+        * Mark the token for remote wipe
+        */
        public function wipe(): void;
+
 }
index 5534ff1cba186c275a1038f6532dccaa8a8237ad..38f1f439e8e847ca5542d9d9800e0532a84d375a 100644 (file)
@@ -35,6 +35,7 @@ use OCP\Activity\IManager as IActivityManager;
 use OCP\AppFramework\Utility\ITimeFactory;
 use OCP\EventDispatcher\IEventDispatcher;
 use OCP\ILogger;
+use OCP\IUser;
 use OCP\Notification\IManager as INotificationManager;
 use Symfony\Component\EventDispatcher\EventDispatcher;
 
@@ -57,6 +58,19 @@ class RemoteWipe {
                $this->logger = $logger;
        }
 
+       public function markTokenForWipe(int $id): bool {
+               $token = $this->tokenProvider->getTokenById($id);
+
+               if (!($token instanceof IWipeableToken)) {
+                       return false;
+               }
+
+               $token->wipe();
+               $this->tokenProvider->updateToken($token);
+
+               return true;
+       }
+
        /**
         * @param string $token
         *
index be497315ed4864e7536ba53f79cc00f86be5bb2c..da9414dcb104ad81ab8af13765830b17365f9d85 100644 (file)
@@ -35,6 +35,7 @@ use OC\Authentication\Token\INamedToken;
 use OC\Authentication\Token\IProvider;
 use OC\Authentication\Token\IToken;
 use OC\Authentication\Token\IWipeableToken;
+use OC\Authentication\Token\RemoteWipe;
 use OC\Settings\Activity\Provider;
 use OCP\Activity\IManager;
 use OCP\AppFramework\Controller;
@@ -63,6 +64,9 @@ class AuthSettingsController extends Controller {
        /** @var IManager */
        private $activityManager;
 
+       /** @var RemoteWipe */
+       private $remoteWipe;
+
        /** @var ILogger */
        private $logger;
 
@@ -74,6 +78,7 @@ class AuthSettingsController extends Controller {
         * @param ISecureRandom $random
         * @param string|null $userId
         * @param IManager $activityManager
+        * @param RemoteWipe $remoteWipe
         * @param ILogger $logger
         */
        public function __construct(string $appName,
@@ -83,6 +88,7 @@ class AuthSettingsController extends Controller {
                                                                ISecureRandom $random,
                                                                ?string $userId,
                                                                IManager $activityManager,
+                                                               RemoteWipe $remoteWipe,
                                                                ILogger $logger) {
                parent::__construct($appName, $request);
                $this->tokenProvider = $tokenProvider;
@@ -90,6 +96,7 @@ class AuthSettingsController extends Controller {
                $this->session = $session;
                $this->random = $random;
                $this->activityManager = $activityManager;
+               $this->remoteWipe = $remoteWipe;
                $this->logger = $logger;
        }
 
@@ -262,15 +269,10 @@ class AuthSettingsController extends Controller {
         * @throws \OC\Authentication\Exceptions\ExpiredTokenException
         */
        public function wipe(int $id): JSONResponse {
-               $token = $this->tokenProvider->getTokenById($id);
-
-               if (!($token instanceof IWipeableToken)) {
+               if (!$this->remoteWipe->markTokenForWipe($id)) {
                        return new JSONResponse([], Http::STATUS_BAD_REQUEST);
                }
 
-               $token->wipe();
-               $this->tokenProvider->updateToken($token);
-
                return new JSONResponse([]);
        }
 }
index 198b3a72c3326003e4fa033d6efca7f2b4375ca1..d335abc98a3dc246003c3c224421c6b9da845561 100644 (file)
@@ -26,6 +26,7 @@ use OC\Authentication\Exceptions\InvalidTokenException;
 use OC\Authentication\Token\DefaultToken;
 use OC\Authentication\Token\IProvider;
 use OC\Authentication\Token\IToken;
+use OC\Authentication\Token\RemoteWipe;
 use OC\Settings\Controller\AuthSettingsController;
 use OCP\Activity\IEvent;
 use OCP\Activity\IManager;
@@ -35,22 +36,25 @@ use OCP\IRequest;
 use OCP\ISession;
 use OCP\Security\ISecureRandom;
 use OCP\Session\Exceptions\SessionNotAvailableException;
+use PHPUnit\Framework\MockObject\MockObject;
 use Test\TestCase;
 
 class AuthSettingsControllerTest extends TestCase {
 
        /** @var AuthSettingsController */
        private $controller;
-       /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
+       /** @var IRequest|MockObject */
        private $request;
-       /** @var IProvider|\PHPUnit_Framework_MockObject_MockObject */
+       /** @var IProvider|MockObject */
        private $tokenProvider;
-       /** @var ISession|\PHPUnit_Framework_MockObject_MockObject */
+       /** @var ISession|MockObject */
        private $session;
-       /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
+       /** @var ISecureRandom|MockObject */
        private $secureRandom;
-       /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
+       /** @var IManager|MockObject */
        private $activityManager;
+       /** @var RemoteWipe|MockObject */
+       private $remoteWipe;
        private $uid = 'jane';
 
        protected function setUp() {
@@ -61,7 +65,8 @@ class AuthSettingsControllerTest extends TestCase {
                $this->session = $this->createMock(ISession::class);
                $this->secureRandom = $this->createMock(ISecureRandom::class);
                $this->activityManager = $this->createMock(IManager::class);
-               /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject $logger */
+               $this->remoteWipe = $this->createMock(RemoteWipe::class);
+               /** @var ILogger|MockObject $logger */
                $logger = $this->createMock(ILogger::class);
 
                $this->controller = new AuthSettingsController(
@@ -72,6 +77,7 @@ class AuthSettingsControllerTest extends TestCase {
                        $this->secureRandom,
                        $this->uid,
                        $this->activityManager,
+                       $this->remoteWipe,
                        $logger
                );
        }
@@ -201,6 +207,7 @@ class AuthSettingsControllerTest extends TestCase {
 
        /**
         * @dataProvider dataRenameToken
+        *
         * @param string $name
         * @param string $newName
         */
@@ -243,6 +250,7 @@ class AuthSettingsControllerTest extends TestCase {
 
        /**
         * @dataProvider dataUpdateFilesystemScope
+        *
         * @param bool $filesystem
         * @param bool $newFilesystem
         */
@@ -359,4 +367,29 @@ class AuthSettingsControllerTest extends TestCase {
                        ->with($this->equalTo($tokenId))
                        ->willReturn($token);
        }
+
+       public function testRemoteWipeNotSuccessful(): void {
+               $this->remoteWipe->expects($this->once())
+                       ->method('markTokenForWipe')
+                       ->with(123)
+                       ->willReturn(false);
+
+               $response = $this->controller->wipe(123);
+
+               $expected = new JSONResponse([], Http::STATUS_BAD_REQUEST);
+               $this->assertEquals($expected, $response);
+       }
+
+       public function testRemoteWipeSuccessful(): void {
+               $this->remoteWipe->expects($this->once())
+                       ->method('markTokenForWipe')
+                       ->with(123)
+                       ->willReturn(true);
+
+               $response = $this->controller->wipe(123);
+
+               $expected = new JSONResponse([]);
+               $this->assertEquals($expected, $response);
+       }
+
 }
index e0b3e9fcae9c6d823c1bdf04454ecf5b4e1c48d0..d5d63b2fb4036115e54a3c1447b5d06c5ffb434b 100644 (file)
@@ -29,6 +29,7 @@ use OC\Authentication\Exceptions\WipeTokenException;
 use OC\Authentication\Token\IProvider as ITokenProvider;
 use OC\Authentication\Token\IProvider;
 use OC\Authentication\Token\IToken;
+use OC\Authentication\Token\IWipeableToken;
 use OC\Authentication\Token\RemoteWipe;
 use OCP\EventDispatcher\IEventDispatcher;
 use OCP\ILogger;
@@ -63,6 +64,35 @@ class RemoteWipeTest extends TestCase {
                );
        }
 
+       public function testMarkNonWipableTokenForWipe(): void {
+               $token = $this->createMock(IToken::class);
+               $this->tokenProvider->expects($this->once())
+                       ->method('getTokenById')
+                       ->with(123)
+                       ->willReturn($token);
+
+               $result = $this->remoteWipe->markTokenForWipe(123);
+
+               $this->assertFalse($result);
+       }
+
+       public function testMarkTokenForWipe(): void {
+               $token = $this->createMock(IWipeableToken::class);
+               $this->tokenProvider->expects($this->once())
+                       ->method('getTokenById')
+                       ->with(123)
+                       ->willReturn($token);
+               $token->expects($this->once())
+                       ->method('wipe');
+               $this->tokenProvider->expects($this->once())
+                       ->method('updateToken')
+                       ->with($token);
+
+               $result = $this->remoteWipe->markTokenForWipe(123);
+
+               $this->assertTrue($result);
+       }
+
        public function testStartWipingNotAWipeToken() {
                $token = $this->createMock(IToken::class);
                $this->tokenProvider->expects($this->once())