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