123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <?php
- namespace OCA\Settings\BackgroundJobs;
- use OCP\Accounts\IAccountManager;
- use OCP\Accounts\PropertyDoesNotExistException;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\BackgroundJob\IJobList;
- use OCP\BackgroundJob\Job;
- use OCP\Http\Client\IClientService;
- use OCP\IConfig;
- use OCP\ILogger;
- use OCP\IUserManager;
- class VerifyUserData extends Job {
-
- private $retainJob = true;
-
- private $maxTry = 24;
-
- private $interval = 3600;
-
- private $accountManager;
-
- private $userManager;
-
- private $httpClientService;
-
- private $logger;
-
- private $lookupServerUrl;
-
- private $config;
- public function __construct(IAccountManager $accountManager,
- IUserManager $userManager,
- IClientService $clientService,
- ILogger $logger,
- ITimeFactory $timeFactory,
- IConfig $config
- ) {
- parent::__construct($timeFactory);
- $this->accountManager = $accountManager;
- $this->userManager = $userManager;
- $this->httpClientService = $clientService;
- $this->logger = $logger;
- $lookupServerUrl = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
- $this->lookupServerUrl = rtrim($lookupServerUrl, '/');
- $this->config = $config;
- }
-
- public function execute(IJobList $jobList, ILogger $logger = null) {
- if ($this->shouldRun($this->argument)) {
- parent::execute($jobList, $logger);
- $jobList->remove($this, $this->argument);
- if ($this->retainJob) {
- $this->reAddJob($jobList, $this->argument);
- } else {
- $this->resetVerificationState();
- }
- }
- }
- protected function run($argument) {
- $try = (int)$argument['try'] + 1;
- switch ($argument['type']) {
- case IAccountManager::PROPERTY_WEBSITE:
- $result = $this->verifyWebsite($argument);
- break;
- case IAccountManager::PROPERTY_TWITTER:
- case IAccountManager::PROPERTY_EMAIL:
- $result = $this->verifyViaLookupServer($argument, $argument['type']);
- break;
- default:
-
- $this->logger->error($argument['type'] . ' is no valid type for user account data.');
- $result = true;
- }
- if ($result === true || $try > $this->maxTry) {
- $this->retainJob = false;
- }
- }
-
- protected function verifyWebsite(array $argument) {
- $result = false;
- $url = rtrim($argument['data'], '/') . '/.well-known/' . 'CloudIdVerificationCode.txt';
- $client = $this->httpClientService->newClient();
- try {
- $response = $client->get($url);
- } catch (\Exception $e) {
- return false;
- }
- if ($response->getStatusCode() === Http::STATUS_OK) {
- $result = true;
- $publishedCode = $response->getBody();
-
- $publishedCodeSanitized = trim(preg_replace('/\s\s+/', ' ', $publishedCode));
- $user = $this->userManager->get($argument['uid']);
-
- if ($user === null) {
- $this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.');
- return $result;
- }
- $userAccount = $this->accountManager->getAccount($user);
- $websiteProp = $userAccount->getProperty(IAccountManager::PROPERTY_WEBSITE);
- $websiteProp->setVerified($publishedCodeSanitized === $argument['verificationCode']
- ? IAccountManager::VERIFIED
- : IAccountManager::NOT_VERIFIED
- );
- $this->accountManager->updateAccount($userAccount);
- }
- return $result;
- }
- protected function verifyViaLookupServer(array $argument, string $dataType): bool {
- if (empty($this->lookupServerUrl) ||
- $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') !== 'yes' ||
- $this->config->getSystemValue('has_internet_connection', true) === false) {
- return true;
- }
- $user = $this->userManager->get($argument['uid']);
-
- if ($user === null) {
- $this->logger->info($argument['uid'] . ' doesn\'t exist, can\'t verify user data.');
- return true;
- }
- $cloudId = $user->getCloudId();
- $lookupServerData = $this->queryLookupServer($cloudId);
-
- if (empty($lookupServerData) || empty($lookupServerData[$dataType])) {
- return false;
- }
-
- if ($lookupServerData[$dataType]['value'] !== $argument['data']) {
- return false;
- }
-
- if ($lookupServerData[$dataType]['verified'] === IAccountManager::NOT_VERIFIED) {
- return false;
- }
- try {
- $userAccount = $this->accountManager->getAccount($user);
- $property = $userAccount->getProperty($dataType);
- $property->setVerified(IAccountManager::VERIFIED);
- $this->accountManager->updateAccount($userAccount);
- } catch (PropertyDoesNotExistException $e) {
- return false;
- }
- return true;
- }
-
- protected function queryLookupServer($cloudId) {
- try {
- $client = $this->httpClientService->newClient();
- $response = $client->get(
- $this->lookupServerUrl . '/users?search=' . urlencode($cloudId) . '&exactCloudId=1',
- [
- 'timeout' => 10,
- 'connect_timeout' => 3,
- ]
- );
- $body = json_decode($response->getBody(), true);
- if (is_array($body) && isset($body['federationId']) && $body['federationId'] === $cloudId) {
- return $body;
- }
- } catch (\Exception $e) {
-
- }
- return [];
- }
-
- protected function reAddJob(IJobList $jobList, array $argument) {
- $jobList->add(VerifyUserData::class,
- [
- 'verificationCode' => $argument['verificationCode'],
- 'data' => $argument['data'],
- 'type' => $argument['type'],
- 'uid' => $argument['uid'],
- 'try' => (int)$argument['try'] + 1,
- 'lastRun' => time()
- ]
- );
- }
-
- protected function shouldRun(array $argument) {
- $lastRun = (int)$argument['lastRun'];
- return ((time() - $lastRun) > $this->interval);
- }
-
- protected function resetVerificationState(): void {
- $user = $this->userManager->get($this->argument['uid']);
- if ($user !== null) {
- $userAccount = $this->accountManager->getAccount($user);
- try {
- $property = $userAccount->getProperty($this->argument['type']);
- $property->setVerified(IAccountManager::NOT_VERIFIED);
- $this->accountManager->updateAccount($userAccount);
- } catch (PropertyDoesNotExistException $e) {
- return;
- }
- }
- }
- }
|