2018-06-18 21:05:44 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2021-03-29 06:40:20 +00:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 15:18:46 +00: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 21:05:44 +00:00
|
|
|
*/
|
2020-02-09 15:18:46 +00:00
|
|
|
|
2018-06-18 21:05:44 +00:00
|
|
|
namespace Friendica\Module;
|
|
|
|
|
|
|
|
use Friendica\BaseModule;
|
2018-10-29 21:20:46 +00:00
|
|
|
use Friendica\Core\Logger;
|
|
|
|
use Friendica\Core\System;
|
2018-07-20 12:19:26 +00:00
|
|
|
use Friendica\Database\DBA;
|
2019-12-15 21:34:11 +00:00
|
|
|
use Friendica\DI;
|
2018-06-19 14:15:28 +00:00
|
|
|
use Friendica\Model\Contact;
|
2018-06-20 16:38:23 +00:00
|
|
|
use Friendica\Util\HTTPSignature;
|
2018-11-08 13:45:46 +00:00
|
|
|
use Friendica\Util\Strings;
|
2018-06-18 21:05:44 +00:00
|
|
|
|
2018-06-19 11:30:55 +00:00
|
|
|
/**
|
|
|
|
* Magic Auth (remote authentication) module.
|
2018-07-10 12:27:56 +00:00
|
|
|
*
|
2018-06-19 11:30:55 +00:00
|
|
|
* Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Magic.php
|
|
|
|
*/
|
2018-06-18 21:05:44 +00:00
|
|
|
class Magic extends BaseModule
|
|
|
|
{
|
2019-11-05 21:48:54 +00:00
|
|
|
public static function init(array $parameters = [])
|
2018-06-18 21:05:44 +00:00
|
|
|
{
|
2019-12-15 21:34:11 +00:00
|
|
|
$a = DI::app();
|
2018-06-18 21:05:44 +00:00
|
|
|
$ret = ['success' => false, 'url' => '', 'message' => ''];
|
2020-06-29 20:22:00 +00:00
|
|
|
Logger::info('magic mdule: invoked');
|
2018-06-18 21:05:44 +00:00
|
|
|
|
2020-06-29 20:22:00 +00:00
|
|
|
Logger::debug('args', ['request' => $_REQUEST]);
|
2018-06-18 21:05:44 +00:00
|
|
|
|
2019-10-15 13:20:32 +00:00
|
|
|
$addr = $_REQUEST['addr'] ?? '';
|
|
|
|
$dest = $_REQUEST['dest'] ?? '';
|
2018-11-30 14:06:22 +00:00
|
|
|
$test = (!empty($_REQUEST['test']) ? intval($_REQUEST['test']) : 0);
|
|
|
|
$owa = (!empty($_REQUEST['owa']) ? intval($_REQUEST['owa']) : 0);
|
2019-09-29 10:23:36 +00:00
|
|
|
$cid = 0;
|
2018-06-18 21:05:44 +00:00
|
|
|
|
2019-09-29 00:16:09 +00:00
|
|
|
if (!empty($addr)) {
|
2018-06-19 14:15:28 +00:00
|
|
|
$cid = Contact::getIdForURL($addr);
|
2019-09-29 10:23:36 +00:00
|
|
|
} elseif (!empty($dest)) {
|
2019-09-29 00:16:09 +00:00
|
|
|
$cid = Contact::getIdForURL($dest);
|
2018-06-18 21:05:44 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 14:15:28 +00:00
|
|
|
if (!$cid) {
|
2019-09-29 10:23:36 +00:00
|
|
|
Logger::info('No contact record found', $_REQUEST);
|
2018-10-19 23:01:15 +00:00
|
|
|
// @TODO Finding a more elegant possibility to redirect to either internal or external URL
|
2018-10-24 18:16:14 +00:00
|
|
|
$a->redirect($dest);
|
2018-06-18 21:05:44 +00:00
|
|
|
}
|
2018-07-20 12:19:26 +00:00
|
|
|
$contact = DBA::selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]);
|
2018-06-19 14:15:28 +00:00
|
|
|
|
2018-06-18 21:05:44 +00:00
|
|
|
// Redirect if the contact is already authenticated on this site.
|
2019-12-16 00:05:15 +00:00
|
|
|
if (!empty($a->contact) && array_key_exists('id', $a->contact) && strpos($contact['nurl'], Strings::normaliseLink(DI::baseUrl()->get())) !== false) {
|
2018-07-10 12:27:56 +00:00
|
|
|
if ($test) {
|
2018-06-18 21:05:44 +00:00
|
|
|
$ret['success'] = true;
|
|
|
|
$ret['message'] .= 'Local site - you are already authenticated.' . EOL;
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2020-06-29 20:22:00 +00:00
|
|
|
Logger::info('Contact is already authenticated');
|
2018-10-19 23:01:15 +00:00
|
|
|
System::externalRedirect($dest);
|
2018-06-18 21:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (local_user()) {
|
|
|
|
$user = $a->user;
|
|
|
|
|
|
|
|
// OpenWebAuth
|
|
|
|
if ($owa) {
|
|
|
|
// 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.
|
2018-06-19 14:15:28 +00:00
|
|
|
$exp = explode('/profile/', $contact['url']);
|
2018-06-18 21:05:44 +00:00
|
|
|
$basepath = $exp[0];
|
|
|
|
|
2020-10-18 20:31:26 +00:00
|
|
|
$header = [];
|
|
|
|
$header['Accept'] = 'application/x-dfrn+json, application/x-zot+json';
|
|
|
|
$header['X-Open-Web-Auth'] = Strings::getRandomHex();
|
2018-06-18 21:05:44 +00:00
|
|
|
|
|
|
|
// Create a header that is signed with the local users private key.
|
2020-10-18 20:31:26 +00:00
|
|
|
$header = HTTPSignature::createSig(
|
|
|
|
$header,
|
2018-06-20 17:32:26 +00:00
|
|
|
$user['prvkey'],
|
2019-12-30 01:17:16 +00:00
|
|
|
'acct:' . $user['nickname'] . '@' . DI::baseUrl()->getHostname() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : '')
|
2018-06-18 21:05:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Try to get an authentication token from the other instance.
|
2020-10-18 20:31:26 +00:00
|
|
|
$curlResult = DI::httpRequest()->get($basepath . '/owa', ['header' => $header]);
|
2018-06-18 21:05:44 +00:00
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
if ($curlResult->isSuccess()) {
|
|
|
|
$j = json_decode($curlResult->getBody(), true);
|
2018-06-18 21:05:44 +00:00
|
|
|
|
|
|
|
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.
|
2018-11-08 15:37:08 +00:00
|
|
|
openssl_private_decrypt(Strings::base64UrlDecode($j['encrypted_token']), $token, $user['prvkey']);
|
2018-06-18 21:05:44 +00:00
|
|
|
} else {
|
|
|
|
$token = $j['token'];
|
|
|
|
}
|
2019-09-29 18:59:03 +00:00
|
|
|
$args = (strpbrk($dest, '?&') ? '&' : '?') . 'owt=' . $token;
|
2018-06-18 21:05:44 +00:00
|
|
|
|
2019-09-29 10:58:07 +00:00
|
|
|
Logger::info('Redirecting', ['path' => $dest . $args]);
|
2018-10-19 23:01:15 +00:00
|
|
|
System::externalRedirect($dest . $args);
|
2018-06-18 21:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-19 23:01:15 +00:00
|
|
|
System::externalRedirect($dest);
|
2018-06-18 21:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:27:56 +00:00
|
|
|
if ($test) {
|
2018-06-18 21:05:44 +00:00
|
|
|
$ret['message'] = 'Not authenticated or invalid arguments' . EOL;
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2018-10-19 23:01:15 +00:00
|
|
|
// @TODO Finding a more elegant possibility to redirect to either internal or external URL
|
2018-10-24 18:16:14 +00:00
|
|
|
$a->redirect($dest);
|
2018-06-18 21:05:44 +00:00
|
|
|
}
|
|
|
|
}
|