@@ -39,261 +39,261 @@ |
||
| 39 | 39 | |
| 40 | 40 | class VerifyUserData extends Job { |
| 41 | 41 | |
| 42 | - /** @var bool */ |
|
| 43 | - private $retainJob = true; |
|
| 44 | - |
|
| 45 | - /** @var int max number of attempts to send the request */ |
|
| 46 | - private $maxTry = 24; |
|
| 47 | - |
|
| 48 | - /** @var int how much time should be between two tries (1 hour) */ |
|
| 49 | - private $interval = 3600; |
|
| 50 | - |
|
| 51 | - /** @var AccountManager */ |
|
| 52 | - private $accountManager; |
|
| 53 | - |
|
| 54 | - /** @var IUserManager */ |
|
| 55 | - private $userManager; |
|
| 56 | - |
|
| 57 | - /** @var IClientService */ |
|
| 58 | - private $httpClientService; |
|
| 59 | - |
|
| 60 | - /** @var ILogger */ |
|
| 61 | - private $logger; |
|
| 62 | - |
|
| 63 | - /** @var string */ |
|
| 64 | - private $lookupServerUrl; |
|
| 65 | - |
|
| 66 | - /** @var IConfig */ |
|
| 67 | - private $config; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * VerifyUserData constructor. |
|
| 71 | - * |
|
| 72 | - * @param AccountManager $accountManager |
|
| 73 | - * @param IUserManager $userManager |
|
| 74 | - * @param IClientService $clientService |
|
| 75 | - * @param ILogger $logger |
|
| 76 | - * @param IConfig $config |
|
| 77 | - */ |
|
| 78 | - public function __construct(AccountManager $accountManager, |
|
| 79 | - IUserManager $userManager, |
|
| 80 | - IClientService $clientService, |
|
| 81 | - ILogger $logger, |
|
| 82 | - IConfig $config |
|
| 83 | - ) { |
|
| 84 | - $this->accountManager = $accountManager; |
|
| 85 | - $this->userManager = $userManager; |
|
| 86 | - $this->httpClientService = $clientService; |
|
| 87 | - $this->logger = $logger; |
|
| 88 | - |
|
| 89 | - $lookupServerUrl = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com'); |
|
| 90 | - $this->lookupServerUrl = rtrim($lookupServerUrl, '/'); |
|
| 91 | - $this->config = $config; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * run the job, then remove it from the jobList |
|
| 96 | - * |
|
| 97 | - * @param JobList $jobList |
|
| 98 | - * @param ILogger|null $logger |
|
| 99 | - */ |
|
| 100 | - public function execute($jobList, ILogger $logger = null) { |
|
| 101 | - |
|
| 102 | - if ($this->shouldRun($this->argument)) { |
|
| 103 | - parent::execute($jobList, $logger); |
|
| 104 | - $jobList->remove($this, $this->argument); |
|
| 105 | - if ($this->retainJob) { |
|
| 106 | - $this->reAddJob($jobList, $this->argument); |
|
| 107 | - } else { |
|
| 108 | - $this->resetVerificationState(); |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - protected function run($argument) { |
|
| 115 | - |
|
| 116 | - $try = (int)$argument['try'] + 1; |
|
| 117 | - |
|
| 118 | - switch($argument['type']) { |
|
| 119 | - case AccountManager::PROPERTY_WEBSITE: |
|
| 120 | - $result = $this->verifyWebsite($argument); |
|
| 121 | - break; |
|
| 122 | - case AccountManager::PROPERTY_TWITTER: |
|
| 123 | - case AccountManager::PROPERTY_EMAIL: |
|
| 124 | - $result = $this->verifyViaLookupServer($argument, $argument['type']); |
|
| 125 | - break; |
|
| 126 | - default: |
|
| 127 | - // no valid type given, no need to retry |
|
| 128 | - $this->logger->error($argument['type'] . ' is no valid type for user account data.'); |
|
| 129 | - $result = true; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - if ($result === true || $try > $this->maxTry) { |
|
| 133 | - $this->retainJob = false; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * verify web page |
|
| 139 | - * |
|
| 140 | - * @param array $argument |
|
| 141 | - * @return bool true if we could check the verification code, otherwise false |
|
| 142 | - */ |
|
| 143 | - protected function verifyWebsite(array $argument) { |
|
| 144 | - |
|
| 145 | - $result = false; |
|
| 146 | - |
|
| 147 | - $url = rtrim($argument['data'], '/') . '/.well-known/' . 'CloudIdVerificationCode.txt'; |
|
| 148 | - |
|
| 149 | - $client = $this->httpClientService->newClient(); |
|
| 150 | - try { |
|
| 151 | - $response = $client->get($url); |
|
| 152 | - } catch (\Exception $e) { |
|
| 153 | - return false; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - if ($response->getStatusCode() === Http::STATUS_OK) { |
|
| 157 | - $result = true; |
|
| 158 | - $publishedCode = $response->getBody(); |
|
| 159 | - // remove new lines and spaces |
|
| 160 | - $publishedCodeSanitized = trim(preg_replace('/\s\s+/', ' ', $publishedCode)); |
|
| 161 | - $user = $this->userManager->get($argument['uid']); |
|
| 162 | - // we don't check a valid user -> give up |
|
| 163 | - if ($user === null) { |
|
| 164 | - $this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.'); |
|
| 165 | - return $result; |
|
| 166 | - } |
|
| 167 | - $userData = $this->accountManager->getUser($user); |
|
| 168 | - |
|
| 169 | - if ($publishedCodeSanitized === $argument['verificationCode']) { |
|
| 170 | - $userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFIED; |
|
| 171 | - } else { |
|
| 172 | - $userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::NOT_VERIFIED; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - $this->accountManager->updateUser($user, $userData); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - return $result; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * verify email address |
|
| 183 | - * |
|
| 184 | - * @param array $argument |
|
| 185 | - * @param string $dataType |
|
| 186 | - * @return bool true if we could check the verification code, otherwise false |
|
| 187 | - */ |
|
| 188 | - protected function verifyViaLookupServer(array $argument, $dataType) { |
|
| 189 | - if(empty($this->lookupServerUrl) || $this->config->getSystemValue('has_internet_connection', true) === false) { |
|
| 190 | - return false; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - $user = $this->userManager->get($argument['uid']); |
|
| 194 | - |
|
| 195 | - // we don't check a valid user -> give up |
|
| 196 | - if ($user === null) { |
|
| 197 | - $this->logger->info($argument['uid'] . ' doesn\'t exist, can\'t verify user data.'); |
|
| 198 | - return true; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $localUserData = $this->accountManager->getUser($user); |
|
| 202 | - $cloudId = $user->getCloudId(); |
|
| 203 | - |
|
| 204 | - // ask lookup-server for user data |
|
| 205 | - $lookupServerData = $this->queryLookupServer($cloudId); |
|
| 206 | - |
|
| 207 | - // for some reasons we couldn't read any data from the lookup server, try again later |
|
| 208 | - if (empty($lookupServerData) || empty($lookupServerData[$dataType])) { |
|
| 209 | - return false; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - // lookup server has verification data for wrong user data (e.g. email address), try again later |
|
| 213 | - if ($lookupServerData[$dataType]['value'] !== $argument['data']) { |
|
| 214 | - return false; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - // lookup server hasn't verified the email address so far, try again later |
|
| 218 | - if ($lookupServerData[$dataType]['verified'] === AccountManager::NOT_VERIFIED) { |
|
| 219 | - return false; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - $localUserData[$dataType]['verified'] = AccountManager::VERIFIED; |
|
| 223 | - $this->accountManager->updateUser($user, $localUserData); |
|
| 224 | - |
|
| 225 | - return true; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * @param string $cloudId |
|
| 230 | - * @return array |
|
| 231 | - */ |
|
| 232 | - protected function queryLookupServer($cloudId) { |
|
| 233 | - try { |
|
| 234 | - $client = $this->httpClientService->newClient(); |
|
| 235 | - $response = $client->get( |
|
| 236 | - $this->lookupServerUrl . '/users?search=' . urlencode($cloudId) . '&exactCloudId=1', |
|
| 237 | - [ |
|
| 238 | - 'timeout' => 10, |
|
| 239 | - 'connect_timeout' => 3, |
|
| 240 | - ] |
|
| 241 | - ); |
|
| 242 | - |
|
| 243 | - $body = json_decode($response->getBody(), true); |
|
| 244 | - |
|
| 245 | - if (is_array($body) && isset($body['federationId']) && $body['federationId'] === $cloudId) { |
|
| 246 | - return $body; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - } catch (\Exception $e) { |
|
| 250 | - // do nothing, we will just re-try later |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - return []; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * re-add background job with new arguments |
|
| 258 | - * |
|
| 259 | - * @param IJobList $jobList |
|
| 260 | - * @param array $argument |
|
| 261 | - */ |
|
| 262 | - protected function reAddJob(IJobList $jobList, array $argument) { |
|
| 263 | - $jobList->add(VerifyUserData::class, |
|
| 264 | - [ |
|
| 265 | - 'verificationCode' => $argument['verificationCode'], |
|
| 266 | - 'data' => $argument['data'], |
|
| 267 | - 'type' => $argument['type'], |
|
| 268 | - 'uid' => $argument['uid'], |
|
| 269 | - 'try' => (int)$argument['try'] + 1, |
|
| 270 | - 'lastRun' => time() |
|
| 271 | - ] |
|
| 272 | - ); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * test if it is time for the next run |
|
| 277 | - * |
|
| 278 | - * @param array $argument |
|
| 279 | - * @return bool |
|
| 280 | - */ |
|
| 281 | - protected function shouldRun(array $argument) { |
|
| 282 | - $lastRun = (int)$argument['lastRun']; |
|
| 283 | - return ((time() - $lastRun) > $this->interval); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * reset verification state after max tries are reached |
|
| 289 | - */ |
|
| 290 | - protected function resetVerificationState() { |
|
| 291 | - $user = $this->userManager->get($this->argument['uid']); |
|
| 292 | - if ($user !== null) { |
|
| 293 | - $accountData = $this->accountManager->getUser($user); |
|
| 294 | - $accountData[$this->argument['type']]['verified'] = AccountManager::NOT_VERIFIED; |
|
| 295 | - $this->accountManager->updateUser($user, $accountData); |
|
| 296 | - } |
|
| 297 | - } |
|
| 42 | + /** @var bool */ |
|
| 43 | + private $retainJob = true; |
|
| 44 | + |
|
| 45 | + /** @var int max number of attempts to send the request */ |
|
| 46 | + private $maxTry = 24; |
|
| 47 | + |
|
| 48 | + /** @var int how much time should be between two tries (1 hour) */ |
|
| 49 | + private $interval = 3600; |
|
| 50 | + |
|
| 51 | + /** @var AccountManager */ |
|
| 52 | + private $accountManager; |
|
| 53 | + |
|
| 54 | + /** @var IUserManager */ |
|
| 55 | + private $userManager; |
|
| 56 | + |
|
| 57 | + /** @var IClientService */ |
|
| 58 | + private $httpClientService; |
|
| 59 | + |
|
| 60 | + /** @var ILogger */ |
|
| 61 | + private $logger; |
|
| 62 | + |
|
| 63 | + /** @var string */ |
|
| 64 | + private $lookupServerUrl; |
|
| 65 | + |
|
| 66 | + /** @var IConfig */ |
|
| 67 | + private $config; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * VerifyUserData constructor. |
|
| 71 | + * |
|
| 72 | + * @param AccountManager $accountManager |
|
| 73 | + * @param IUserManager $userManager |
|
| 74 | + * @param IClientService $clientService |
|
| 75 | + * @param ILogger $logger |
|
| 76 | + * @param IConfig $config |
|
| 77 | + */ |
|
| 78 | + public function __construct(AccountManager $accountManager, |
|
| 79 | + IUserManager $userManager, |
|
| 80 | + IClientService $clientService, |
|
| 81 | + ILogger $logger, |
|
| 82 | + IConfig $config |
|
| 83 | + ) { |
|
| 84 | + $this->accountManager = $accountManager; |
|
| 85 | + $this->userManager = $userManager; |
|
| 86 | + $this->httpClientService = $clientService; |
|
| 87 | + $this->logger = $logger; |
|
| 88 | + |
|
| 89 | + $lookupServerUrl = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com'); |
|
| 90 | + $this->lookupServerUrl = rtrim($lookupServerUrl, '/'); |
|
| 91 | + $this->config = $config; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * run the job, then remove it from the jobList |
|
| 96 | + * |
|
| 97 | + * @param JobList $jobList |
|
| 98 | + * @param ILogger|null $logger |
|
| 99 | + */ |
|
| 100 | + public function execute($jobList, ILogger $logger = null) { |
|
| 101 | + |
|
| 102 | + if ($this->shouldRun($this->argument)) { |
|
| 103 | + parent::execute($jobList, $logger); |
|
| 104 | + $jobList->remove($this, $this->argument); |
|
| 105 | + if ($this->retainJob) { |
|
| 106 | + $this->reAddJob($jobList, $this->argument); |
|
| 107 | + } else { |
|
| 108 | + $this->resetVerificationState(); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + protected function run($argument) { |
|
| 115 | + |
|
| 116 | + $try = (int)$argument['try'] + 1; |
|
| 117 | + |
|
| 118 | + switch($argument['type']) { |
|
| 119 | + case AccountManager::PROPERTY_WEBSITE: |
|
| 120 | + $result = $this->verifyWebsite($argument); |
|
| 121 | + break; |
|
| 122 | + case AccountManager::PROPERTY_TWITTER: |
|
| 123 | + case AccountManager::PROPERTY_EMAIL: |
|
| 124 | + $result = $this->verifyViaLookupServer($argument, $argument['type']); |
|
| 125 | + break; |
|
| 126 | + default: |
|
| 127 | + // no valid type given, no need to retry |
|
| 128 | + $this->logger->error($argument['type'] . ' is no valid type for user account data.'); |
|
| 129 | + $result = true; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + if ($result === true || $try > $this->maxTry) { |
|
| 133 | + $this->retainJob = false; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * verify web page |
|
| 139 | + * |
|
| 140 | + * @param array $argument |
|
| 141 | + * @return bool true if we could check the verification code, otherwise false |
|
| 142 | + */ |
|
| 143 | + protected function verifyWebsite(array $argument) { |
|
| 144 | + |
|
| 145 | + $result = false; |
|
| 146 | + |
|
| 147 | + $url = rtrim($argument['data'], '/') . '/.well-known/' . 'CloudIdVerificationCode.txt'; |
|
| 148 | + |
|
| 149 | + $client = $this->httpClientService->newClient(); |
|
| 150 | + try { |
|
| 151 | + $response = $client->get($url); |
|
| 152 | + } catch (\Exception $e) { |
|
| 153 | + return false; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + if ($response->getStatusCode() === Http::STATUS_OK) { |
|
| 157 | + $result = true; |
|
| 158 | + $publishedCode = $response->getBody(); |
|
| 159 | + // remove new lines and spaces |
|
| 160 | + $publishedCodeSanitized = trim(preg_replace('/\s\s+/', ' ', $publishedCode)); |
|
| 161 | + $user = $this->userManager->get($argument['uid']); |
|
| 162 | + // we don't check a valid user -> give up |
|
| 163 | + if ($user === null) { |
|
| 164 | + $this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.'); |
|
| 165 | + return $result; |
|
| 166 | + } |
|
| 167 | + $userData = $this->accountManager->getUser($user); |
|
| 168 | + |
|
| 169 | + if ($publishedCodeSanitized === $argument['verificationCode']) { |
|
| 170 | + $userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFIED; |
|
| 171 | + } else { |
|
| 172 | + $userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::NOT_VERIFIED; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + $this->accountManager->updateUser($user, $userData); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + return $result; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * verify email address |
|
| 183 | + * |
|
| 184 | + * @param array $argument |
|
| 185 | + * @param string $dataType |
|
| 186 | + * @return bool true if we could check the verification code, otherwise false |
|
| 187 | + */ |
|
| 188 | + protected function verifyViaLookupServer(array $argument, $dataType) { |
|
| 189 | + if(empty($this->lookupServerUrl) || $this->config->getSystemValue('has_internet_connection', true) === false) { |
|
| 190 | + return false; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + $user = $this->userManager->get($argument['uid']); |
|
| 194 | + |
|
| 195 | + // we don't check a valid user -> give up |
|
| 196 | + if ($user === null) { |
|
| 197 | + $this->logger->info($argument['uid'] . ' doesn\'t exist, can\'t verify user data.'); |
|
| 198 | + return true; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $localUserData = $this->accountManager->getUser($user); |
|
| 202 | + $cloudId = $user->getCloudId(); |
|
| 203 | + |
|
| 204 | + // ask lookup-server for user data |
|
| 205 | + $lookupServerData = $this->queryLookupServer($cloudId); |
|
| 206 | + |
|
| 207 | + // for some reasons we couldn't read any data from the lookup server, try again later |
|
| 208 | + if (empty($lookupServerData) || empty($lookupServerData[$dataType])) { |
|
| 209 | + return false; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + // lookup server has verification data for wrong user data (e.g. email address), try again later |
|
| 213 | + if ($lookupServerData[$dataType]['value'] !== $argument['data']) { |
|
| 214 | + return false; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + // lookup server hasn't verified the email address so far, try again later |
|
| 218 | + if ($lookupServerData[$dataType]['verified'] === AccountManager::NOT_VERIFIED) { |
|
| 219 | + return false; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + $localUserData[$dataType]['verified'] = AccountManager::VERIFIED; |
|
| 223 | + $this->accountManager->updateUser($user, $localUserData); |
|
| 224 | + |
|
| 225 | + return true; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * @param string $cloudId |
|
| 230 | + * @return array |
|
| 231 | + */ |
|
| 232 | + protected function queryLookupServer($cloudId) { |
|
| 233 | + try { |
|
| 234 | + $client = $this->httpClientService->newClient(); |
|
| 235 | + $response = $client->get( |
|
| 236 | + $this->lookupServerUrl . '/users?search=' . urlencode($cloudId) . '&exactCloudId=1', |
|
| 237 | + [ |
|
| 238 | + 'timeout' => 10, |
|
| 239 | + 'connect_timeout' => 3, |
|
| 240 | + ] |
|
| 241 | + ); |
|
| 242 | + |
|
| 243 | + $body = json_decode($response->getBody(), true); |
|
| 244 | + |
|
| 245 | + if (is_array($body) && isset($body['federationId']) && $body['federationId'] === $cloudId) { |
|
| 246 | + return $body; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + } catch (\Exception $e) { |
|
| 250 | + // do nothing, we will just re-try later |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + return []; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * re-add background job with new arguments |
|
| 258 | + * |
|
| 259 | + * @param IJobList $jobList |
|
| 260 | + * @param array $argument |
|
| 261 | + */ |
|
| 262 | + protected function reAddJob(IJobList $jobList, array $argument) { |
|
| 263 | + $jobList->add(VerifyUserData::class, |
|
| 264 | + [ |
|
| 265 | + 'verificationCode' => $argument['verificationCode'], |
|
| 266 | + 'data' => $argument['data'], |
|
| 267 | + 'type' => $argument['type'], |
|
| 268 | + 'uid' => $argument['uid'], |
|
| 269 | + 'try' => (int)$argument['try'] + 1, |
|
| 270 | + 'lastRun' => time() |
|
| 271 | + ] |
|
| 272 | + ); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * test if it is time for the next run |
|
| 277 | + * |
|
| 278 | + * @param array $argument |
|
| 279 | + * @return bool |
|
| 280 | + */ |
|
| 281 | + protected function shouldRun(array $argument) { |
|
| 282 | + $lastRun = (int)$argument['lastRun']; |
|
| 283 | + return ((time() - $lastRun) > $this->interval); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * reset verification state after max tries are reached |
|
| 289 | + */ |
|
| 290 | + protected function resetVerificationState() { |
|
| 291 | + $user = $this->userManager->get($this->argument['uid']); |
|
| 292 | + if ($user !== null) { |
|
| 293 | + $accountData = $this->accountManager->getUser($user); |
|
| 294 | + $accountData[$this->argument['type']]['verified'] = AccountManager::NOT_VERIFIED; |
|
| 295 | + $this->accountManager->updateUser($user, $accountData); |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | 298 | |
| 299 | 299 | } |