2019-04-03 17:00:46 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
|
|
|
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Authentication\Token;
|
|
|
|
|
|
|
|
use BadMethodCallException;
|
2019-05-28 18:56:01 +03:00
|
|
|
use OC\Authentication\Events\RemoteWipeFinished;
|
|
|
|
use OC\Authentication\Events\RemoteWipeStarted;
|
2019-04-03 17:00:46 +03:00
|
|
|
use OC\Authentication\Exceptions\InvalidTokenException;
|
|
|
|
use OC\Authentication\Exceptions\WipeTokenException;
|
2019-05-28 18:56:01 +03:00
|
|
|
use OCP\Activity\IEvent;
|
2019-04-03 17:00:46 +03:00
|
|
|
use OCP\Activity\IManager as IActivityManager;
|
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2019-05-28 18:56:01 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2019-04-03 17:00:46 +03:00
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\Notification\IManager as INotificationManager;
|
2019-05-28 18:56:01 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
2019-04-03 17:00:46 +03:00
|
|
|
|
|
|
|
class RemoteWipe {
|
|
|
|
|
|
|
|
/** @var IProvider */
|
|
|
|
private $tokenProvider;
|
|
|
|
|
2019-05-28 18:56:01 +03:00
|
|
|
/** @var IEventDispatcher */
|
|
|
|
private $eventDispatcher;
|
2019-04-03 17:00:46 +03:00
|
|
|
|
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
public function __construct(IProvider $tokenProvider,
|
2019-05-28 18:56:01 +03:00
|
|
|
IEventDispatcher $eventDispatcher,
|
2019-04-03 17:00:46 +03:00
|
|
|
ILogger $logger) {
|
|
|
|
$this->tokenProvider = $tokenProvider;
|
2019-05-28 18:56:01 +03:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
2019-04-03 17:00:46 +03:00
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $token
|
|
|
|
*
|
|
|
|
* @return bool whether wiping was started
|
|
|
|
* @throws InvalidTokenException
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function start(string $token): bool {
|
|
|
|
try {
|
|
|
|
$this->tokenProvider->getToken($token);
|
|
|
|
|
|
|
|
// We expect a WipedTokenException here. If we reach this point this
|
|
|
|
// is an ordinary token
|
|
|
|
return false;
|
|
|
|
} catch (WipeTokenException $e) {
|
|
|
|
// Expected -> continue below
|
|
|
|
}
|
|
|
|
|
|
|
|
$dbToken = $e->getToken();
|
|
|
|
|
|
|
|
$this->logger->info("user " . $dbToken->getUID() . " started a remote wipe");
|
2019-05-28 18:56:01 +03:00
|
|
|
|
|
|
|
$this->eventDispatcher->dispatch(RemoteWipeStarted::class, new RemoteWipeStarted($dbToken));
|
2019-04-03 17:00:46 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $token
|
|
|
|
*
|
|
|
|
* @return bool whether wiping could be finished
|
|
|
|
* @throws InvalidTokenException
|
|
|
|
*/
|
|
|
|
public function finish(string $token): bool {
|
|
|
|
try {
|
|
|
|
$this->tokenProvider->getToken($token);
|
|
|
|
|
|
|
|
// We expect a WipedTokenException here. If we reach this point this
|
|
|
|
// is an ordinary token
|
|
|
|
return false;
|
|
|
|
} catch (WipeTokenException $e) {
|
|
|
|
// Expected -> continue below
|
|
|
|
}
|
|
|
|
|
|
|
|
$dbToken = $e->getToken();
|
|
|
|
|
|
|
|
$this->tokenProvider->invalidateToken($token);
|
|
|
|
|
|
|
|
$this->logger->info("user " . $dbToken->getUID() . " finished a remote wipe");
|
2019-05-28 18:56:01 +03:00
|
|
|
$this->eventDispatcher->dispatch(RemoteWipeFinished::class, new RemoteWipeFinished($dbToken));
|
2019-04-03 17:00:46 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|