2018-06-18 23:05:44 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2022-01-02 08:27:47 +01:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-02-09 16:18:46 +01: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
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2018-06-18 23:05:44 +02:00
|
|
|
*/
|
2020-02-09 16:18:46 +01:00
|
|
|
|
2018-06-18 23:05:44 +02:00
|
|
|
namespace Friendica\Module;
|
|
|
|
|
2021-11-19 20:18:48 +01:00
|
|
|
use Friendica\App;
|
2018-06-18 23:05:44 +02:00
|
|
|
use Friendica\BaseModule;
|
2021-11-19 20:18:48 +01:00
|
|
|
use Friendica\Core\L10n;
|
2018-10-29 22:20:46 +01:00
|
|
|
use Friendica\Core\System;
|
2021-11-19 20:18:48 +01:00
|
|
|
use Friendica\Database\Database;
|
2018-06-19 16:15:28 +02:00
|
|
|
use Friendica\Model\Contact;
|
2021-08-08 21:30:21 +02:00
|
|
|
use Friendica\Model\User;
|
2021-11-19 20:18:48 +01:00
|
|
|
use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests;
|
2021-10-23 12:50:31 +02:00
|
|
|
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
2018-06-20 18:38:23 +02:00
|
|
|
use Friendica\Util\HTTPSignature;
|
2021-11-20 15:38:03 +01:00
|
|
|
use Friendica\Util\Profiler;
|
2018-11-08 14:45:46 +01:00
|
|
|
use Friendica\Util\Strings;
|
2021-11-19 20:18:48 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
2018-06-18 23:05:44 +02:00
|
|
|
|
2018-06-19 13:30:55 +02:00
|
|
|
/**
|
|
|
|
* Magic Auth (remote authentication) module.
|
2018-07-10 14:27:56 +02:00
|
|
|
*
|
2018-06-19 13:30:55 +02:00
|
|
|
* Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Magic.php
|
|
|
|
*/
|
2018-06-18 23:05:44 +02:00
|
|
|
class Magic extends BaseModule
|
|
|
|
{
|
2021-11-19 20:18:48 +01:00
|
|
|
/** @var App */
|
|
|
|
protected $app;
|
|
|
|
/** @var Database */
|
|
|
|
protected $dba;
|
|
|
|
/** @var ICanSendHttpRequests */
|
|
|
|
protected $httpClient;
|
|
|
|
|
2021-11-21 20:06:36 +01:00
|
|
|
public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, Database $dba, ICanSendHttpRequests $httpClient, array $server, array $parameters = [])
|
2018-06-18 23:05:44 +02:00
|
|
|
{
|
2021-11-21 20:06:36 +01:00
|
|
|
parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
|
2018-06-18 23:05:44 +02:00
|
|
|
|
2021-11-19 20:18:48 +01:00
|
|
|
$this->app = $app;
|
|
|
|
$this->dba = $dba;
|
|
|
|
$this->httpClient = $httpClient;
|
|
|
|
}
|
|
|
|
|
2021-11-20 15:38:03 +01:00
|
|
|
protected function rawContent(array $request = [])
|
2021-11-19 20:18:48 +01:00
|
|
|
{
|
|
|
|
$this->logger->info('magic module: invoked');
|
|
|
|
|
|
|
|
$this->logger->debug('args', ['request' => $_REQUEST]);
|
2018-06-18 23:05:44 +02:00
|
|
|
|
2019-10-15 15:20:32 +02:00
|
|
|
$addr = $_REQUEST['addr'] ?? '';
|
|
|
|
$dest = $_REQUEST['dest'] ?? '';
|
2018-11-30 15:06:22 +01:00
|
|
|
$owa = (!empty($_REQUEST['owa']) ? intval($_REQUEST['owa']) : 0);
|
2019-09-29 12:23:36 +02:00
|
|
|
$cid = 0;
|
2018-06-18 23:05:44 +02:00
|
|
|
|
2019-09-29 02:16:09 +02:00
|
|
|
if (!empty($addr)) {
|
2018-06-19 16:15:28 +02:00
|
|
|
$cid = Contact::getIdForURL($addr);
|
2019-09-29 12:23:36 +02:00
|
|
|
} elseif (!empty($dest)) {
|
2019-09-29 02:16:09 +02:00
|
|
|
$cid = Contact::getIdForURL($dest);
|
2018-06-18 23:05:44 +02:00
|
|
|
}
|
|
|
|
|
2018-06-19 16:15:28 +02:00
|
|
|
if (!$cid) {
|
2021-11-19 20:18:48 +01:00
|
|
|
$this->logger->info('No contact record found', $_REQUEST);
|
2018-10-20 01:01:15 +02:00
|
|
|
// @TODO Finding a more elegant possibility to redirect to either internal or external URL
|
2021-11-19 20:18:48 +01:00
|
|
|
$this->app->redirect($dest);
|
2018-06-18 23:05:44 +02:00
|
|
|
}
|
2021-11-19 20:18:48 +01:00
|
|
|
$contact = $this->dba->selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]);
|
2018-06-19 16:15:28 +02:00
|
|
|
|
2018-06-18 23:05:44 +02:00
|
|
|
// Redirect if the contact is already authenticated on this site.
|
2021-11-19 20:18:48 +01:00
|
|
|
if ($this->app->getContactId() && strpos($contact['nurl'], Strings::normaliseLink($this->baseUrl->get())) !== false) {
|
|
|
|
$this->logger->info('Contact is already authenticated');
|
2018-10-20 01:01:15 +02:00
|
|
|
System::externalRedirect($dest);
|
2018-06-18 23:05:44 +02:00
|
|
|
}
|
|
|
|
|
2021-08-08 21:30:21 +02:00
|
|
|
// OpenWebAuth
|
|
|
|
if (local_user() && $owa) {
|
|
|
|
$user = User::getById(local_user());
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
|
2021-08-25 13:45:00 +02:00
|
|
|
$header = [
|
|
|
|
'Accept' => ['application/x-dfrn+json', 'application/x-zot+json'],
|
|
|
|
'X-Open-Web-Auth' => [Strings::getRandomHex()],
|
|
|
|
];
|
2021-08-08 21:30:21 +02:00
|
|
|
|
|
|
|
// Create a header that is signed with the local users private key.
|
|
|
|
$header = HTTPSignature::createSig(
|
|
|
|
$header,
|
|
|
|
$user['prvkey'],
|
2021-11-19 20:18:48 +01:00
|
|
|
'acct:' . $user['nickname'] . '@' . $this->baseUrl->getHostname() . ($this->baseUrl->getUrlPath() ? '/' . $this->baseUrl->getUrlPath() : '')
|
2021-08-08 21:30:21 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Try to get an authentication token from the other instance.
|
2021-11-19 20:18:48 +01:00
|
|
|
$curlResult = $this->httpClient->get($basepath . '/owa', [HttpClientOptions::HEADERS => $header]);
|
2021-08-08 21:30:21 +02:00
|
|
|
|
|
|
|
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'];
|
2018-06-18 23:05:44 +02:00
|
|
|
}
|
2021-08-08 21:30:21 +02:00
|
|
|
$args = (strpbrk($dest, '?&') ? '&' : '?') . 'owt=' . $token;
|
|
|
|
|
2021-11-19 20:18:48 +01:00
|
|
|
$this->logger->info('Redirecting', ['path' => $dest . $args]);
|
2021-08-08 21:30:21 +02:00
|
|
|
System::externalRedirect($dest . $args);
|
2018-06-18 23:05:44 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-08 21:30:21 +02:00
|
|
|
System::externalRedirect($dest);
|
2018-06-18 23:05:44 +02:00
|
|
|
}
|
|
|
|
|
2018-10-20 01:01:15 +02:00
|
|
|
// @TODO Finding a more elegant possibility to redirect to either internal or external URL
|
2021-11-19 20:18:48 +01:00
|
|
|
$this->app->redirect($dest);
|
2018-06-18 23:05:44 +02:00
|
|
|
}
|
|
|
|
}
|