X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModule%2FMagic.php;h=7b60299e20ded83dd6aef031283e2745aec0c706;hb=3bca4fe2a64671d09e08346456cdfa6c12f996e9;hp=1012dc72baffdbb57fddcb6e69b2ae50e32b7e19;hpb=7560939d75c6bdc19f4179e0644373ddce0bf915;p=friendica.git diff --git a/src/Module/Magic.php b/src/Module/Magic.php index 1012dc72ba..7b60299e20 100644 --- a/src/Module/Magic.php +++ b/src/Module/Magic.php @@ -1,6 +1,6 @@ logger->info('magic module: invoked'); + if ($_SERVER['REQUEST_METHOD'] == 'HEAD') { + $this->logger->debug('Got a HEAD request'); + System::exit(); + } - $this->logger->debug('args', ['request' => $_REQUEST]); + $this->logger->debug('Invoked', ['request' => $request]); - $addr = $_REQUEST['addr'] ?? ''; - $dest = $_REQUEST['dest'] ?? ''; - $owa = (!empty($_REQUEST['owa']) ? intval($_REQUEST['owa']) : 0); - $cid = 0; + $addr = $request['addr'] ?? ''; + $dest = $request['dest'] ?? ''; + $bdest = $request['bdest'] ?? ''; + $owa = intval($request['owa'] ?? 0); - if (!empty($addr)) { - $cid = Contact::getIdForURL($addr); - } elseif (!empty($dest)) { - $cid = Contact::getIdForURL($dest); + // bdest is preferred as it is hex-encoded and can survive url rewrite and argument parsing + if (!empty($bdest)) { + $dest = hex2bin($bdest); + $this->logger->debug('bdest detected', ['dest' => $dest]); } - if (!$cid) { - $this->logger->info('No contact record found', $_REQUEST); - // @TODO Finding a more elegant possibility to redirect to either internal or external URL - $this->app->redirect($dest); + $target = $dest ?: $addr; + + if ($addr ?: $dest) { + $contact = Contact::getByURL($addr ?: $dest); } - $contact = $this->dba->selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]); - // Redirect if the contact is already authenticated on this site. - if ($this->app->getContactId() && strpos($contact['nurl'], Strings::normaliseLink($this->baseUrl)) !== false) { - $this->logger->info('Contact is already authenticated'); - System::externalRedirect($dest); + if (empty($contact)) { + if (!$owa) { + $this->logger->info('No contact record found, no oWA, redirecting to destination.', ['request' => $request, 'server' => $_SERVER, 'dest' => $dest]); + $this->app->redirect($dest); + } + } else { + // Redirect if the contact is already authenticated on this site. + if ($this->app->getContactId() && strpos($contact['nurl'], Strings::normaliseLink($this->baseUrl)) !== false) { + $this->logger->info('Contact is already authenticated, redirecting to destination.', ['dest' => $dest]); + System::externalRedirect($dest); + } + + $this->logger->debug('Contact found', ['url' => $contact['url']]); + } + + if (!$this->userSession->getLocalUserId() || !$owa) { + $this->logger->notice('Not logged in or not OWA, redirecting to destination.', ['uid' => $this->userSession->getLocalUserId(), 'owa' => $owa, 'dest' => $dest]); + $this->app->redirect($dest); } // OpenWebAuth - if ($this->userSession->getLocalUserId() && $owa) { - $user = User::getById($this->userSession->getLocalUserId()); - - // Extract the basepath - // NOTE: we need another solution because this does only work - // for friendica contacts :-/ . We should have the basepath - // of a contact also in the contact table. - $exp = explode('/profile/', $contact['url']); - $basepath = $exp[0]; - - $header = [ - 'Accept' => ['application/x-dfrn+json', 'application/x-zot+json'], - 'X-Open-Web-Auth' => [Strings::getRandomHex()], - ]; - - // Create a header that is signed with the local users private key. - $header = HTTPSignature::createSig( - $header, - $user['prvkey'], - 'acct:' . $user['nickname'] . '@' . $this->baseUrl->getHost() . ($this->baseUrl->getPath() ? '/' . $this->baseUrl->getPath() : '') - ); - - // Try to get an authentication token from the other instance. - $curlResult = $this->httpClient->get($basepath . '/owa', HttpClientAccept::DEFAULT, [HttpClientOptions::HEADERS => $header]); - - if ($curlResult->isSuccess()) { - $j = json_decode($curlResult->getBody(), true); - - if ($j['success']) { - $token = ''; - if ($j['encrypted_token']) { - // The token is encrypted. If the local user is really the one the other instance - // thinks he/she is, the token can be decrypted with the local users public key. - openssl_private_decrypt(Strings::base64UrlDecode($j['encrypted_token']), $token, $user['prvkey']); - } else { - $token = $j['token']; - } - $args = (strpbrk($dest, '?&') ? '&' : '?') . 'owt=' . $token; - - $this->logger->info('Redirecting', ['path' => $dest . $args]); - System::externalRedirect($dest . $args); - } + $owner = User::getOwnerDataById($this->userSession->getLocalUserId()); + + if (!empty($contact['gsid'])) { + $gserver = $this->dba->selectFirst('gserver', ['url'], ['id' => $contact['gsid']]); + if (empty($gserver)) { + $this->logger->notice('Server not found, redirecting to destination.', ['gsid' => $contact['gsid'], 'dest' => $dest]); + System::externalRedirect($dest); } + + $basepath = $gserver['url']; + } elseif (GServer::check($target)) { + $basepath = (string)GServer::cleanUri(new Uri($target)); + } else { + $this->logger->notice('The target is not a server path, redirecting to destination.', ['target' => $target]); + System::externalRedirect($dest); + } + + $header = [ + 'Accept' => 'application/x-dfrn+json, application/x-zot+json', + 'X-Open-Web-Auth' => Strings::getRandomHex() + ]; + + // Create a header that is signed with the local users private key. + $header = HTTPSignature::createSig( + $header, + $owner['prvkey'], + 'acct:' . $owner['addr'] + ); + + $this->logger->info('Fetch from remote system', ['basepath' => $basepath, 'headers' => $header]); + + // Try to get an authentication token from the other instance. + try { + $curlResult = $this->httpClient->request('get', $basepath . '/owa', [HttpClientOptions::HEADERS => $header]); + } catch (Exception $exception) { + $this->logger->notice('URL is invalid, redirecting to destination.', ['url' => $basepath, 'error' => $exception, 'dest' => $dest]); + System::externalRedirect($dest); + } + if (!$curlResult->isSuccess()) { + $this->logger->notice('OWA request failed, redirecting to destination.', ['returncode' => $curlResult->getReturnCode(), 'dest' => $dest]); System::externalRedirect($dest); } - // @TODO Finding a more elegant possibility to redirect to either internal or external URL - $this->app->redirect($dest); + $j = json_decode($curlResult->getBodyString(), true); + if (empty($j) || !$j['success']) { + $this->logger->notice('Invalid JSON, redirecting to destination.', ['json' => $j, 'dest' => $dest]); + $this->app->redirect($dest); + } + + if ($j['encrypted_token']) { + // The token is encrypted. If the local user is really the one the other instance + // thinks they is, the token can be decrypted with the local users public key. + $token = ''; + openssl_private_decrypt(Strings::base64UrlDecode($j['encrypted_token']), $token, $owner['prvkey']); + } else { + $token = $j['token']; + } + $args = (strpbrk($dest, '?&') ? '&' : '?') . 'owt=' . $token; + + $this->logger->debug('Redirecting', ['path' => $dest . $args]); + System::externalRedirect($dest . $args); } }