2019-04-03 17:00:46 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2019-04-03 17:00:46 +03:00
|
|
|
*
|
|
|
|
* @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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2019-04-03 17:00:46 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Authentication\Token;
|
|
|
|
|
2020-10-12 18:14:25 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2019-07-03 11:10:56 +03:00
|
|
|
use function array_filter;
|
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\EventDispatcher\IEventDispatcher;
|
2019-07-03 10:44:37 +03:00
|
|
|
use OCP\IUser;
|
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
|
|
|
|
2020-10-12 18:14:25 +03:00
|
|
|
/** @var LoggerInterface */
|
2019-04-03 17:00:46 +03:00
|
|
|
private $logger;
|
|
|
|
|
|
|
|
public function __construct(IProvider $tokenProvider,
|
2019-05-28 18:56:01 +03:00
|
|
|
IEventDispatcher $eventDispatcher,
|
2020-10-12 18:14:25 +03:00
|
|
|
LoggerInterface $logger) {
|
2019-04-03 17:00:46 +03:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2019-07-03 11:10:56 +03:00
|
|
|
/**
|
2020-03-16 10:52:46 +03:00
|
|
|
* @param IToken $token
|
2019-07-03 11:10:56 +03:00
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* @throws InvalidTokenException
|
|
|
|
* @throws WipeTokenException
|
|
|
|
*/
|
2020-03-16 10:52:46 +03:00
|
|
|
public function markTokenForWipe(IToken $token): bool {
|
|
|
|
if (!$token instanceof IWipeableToken) {
|
2019-07-03 10:44:37 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$token->wipe();
|
|
|
|
$this->tokenProvider->updateToken($token);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-03 11:10:56 +03:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
*
|
|
|
|
* @return bool true if any tokens have been marked for remote wipe
|
|
|
|
*/
|
|
|
|
public function markAllTokensForWipe(IUser $user): bool {
|
|
|
|
$tokens = $this->tokenProvider->getTokenByUser($user->getUID());
|
|
|
|
|
|
|
|
/** @var IWipeableToken[] $wipeable */
|
|
|
|
$wipeable = array_filter($tokens, function (IToken $token) {
|
|
|
|
return $token instanceof IWipeableToken;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (empty($wipeable)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($wipeable as $token) {
|
|
|
|
$token->wipe();
|
|
|
|
$this->tokenProvider->updateToken($token);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-03 17:00:46 +03:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
}
|