public function perform()
{
$hash = Request::getString($_REQUEST['x']);
if (!strlen($hash) || base64_decode($hash, true) === false) {
$this->req->setResult(['success' => false, 'error' => 'Wrong format of extTokenHash']);
return;
}
$this->appInstance->externalAuthTokens->findByExtTokenHash($hash, function ($result) use($hash) {
if ($result) {
$this->req->setResult(['success' => false, 'error' => 'This token was already used.']);
return;
}
$ip = $this->req->getIp();
$intToken = Crypt::hash(Daemon::uniqid() . "" . $ip . "" . Crypt::randomString());
$this->appInstance->externalAuthTokens->save(['extTokenHash' => $hash, 'intToken' => $intToken, 'ip' => $ip, 'useragent' => Request::getString($_SERVER['HTTP_USER_AGENT']), 'ctime' => microtime(true), 'status' => 'new'], function ($lastError) use($intToken) {
if (!isset($lastError['n']) || $lastError['n'] === 0) {
$this->req->setResult(['success' => false, 'errors' => ['code' => 'Sorry, internal error.']]);
return;
}
$type = Request::getString($_REQUEST['type']);
if ($type === 'email') {
// send email....
} elseif ($type === 'redirect') {
$this->req->redirectTo(HTTPClient::buildUrl(['/' . $this->req->locale . '/account/extauth', 'i' => $intToken]), false);
}
$this->req->setResult(['success' => true, 'intToken' => $intToken]);
});
});
}