2018-06-18 21:05:44 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file src/Module/Magic.php
|
|
|
|
*/
|
|
|
|
namespace Friendica\Module;
|
|
|
|
|
|
|
|
use Friendica\BaseModule;
|
2018-07-20 12:19:26 +00:00
|
|
|
use Friendica\Database\DBA;
|
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-06-18 21:05:44 +00:00
|
|
|
use Friendica\Util\Network;
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
public static function init()
|
|
|
|
{
|
|
|
|
$a = self::getApp();
|
|
|
|
$ret = ['success' => false, 'url' => '', 'message' => ''];
|
|
|
|
logger('magic mdule: invoked', LOGGER_DEBUG);
|
|
|
|
|
|
|
|
logger('args: ' . print_r($_REQUEST, true), LOGGER_DATA);
|
|
|
|
|
|
|
|
$addr = ((x($_REQUEST, 'addr')) ? $_REQUEST['addr'] : '');
|
|
|
|
$dest = ((x($_REQUEST, 'dest')) ? $_REQUEST['dest'] : '');
|
|
|
|
$test = ((x($_REQUEST, 'test')) ? intval($_REQUEST['test']) : 0);
|
|
|
|
$owa = ((x($_REQUEST, 'owa')) ? intval($_REQUEST['owa']) : 0);
|
|
|
|
|
2018-07-10 12:27:56 +00:00
|
|
|
// NOTE: I guess $dest isn't just the profile url (could be also
|
2018-06-18 21:05:44 +00:00
|
|
|
// other profile pages e.g. photo). We need to find a solution
|
|
|
|
// to be able to redirct to other pages than the contact profile.
|
2018-06-19 14:15:28 +00:00
|
|
|
$cid = Contact::getIdForURL($dest);
|
2018-06-18 21:05:44 +00:00
|
|
|
|
2018-06-19 14:15:28 +00:00
|
|
|
if (!$cid && !empty($addr)) {
|
|
|
|
$cid = Contact::getIdForURL($addr);
|
2018-06-18 21:05:44 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 14:15:28 +00:00
|
|
|
if (!$cid) {
|
|
|
|
logger('No contact record found: ' . print_r($_REQUEST, true), LOGGER_DEBUG);
|
2018-06-18 21:05:44 +00:00
|
|
|
goaway($dest);
|
|
|
|
}
|
|
|
|
|
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.
|
2018-07-10 12:27:56 +00:00
|
|
|
if (!empty($a->contact) && array_key_exists('id', $a->contact) && strpos($contact['nurl'], normalise_link(self::getApp()->get_baseurl())) !== false) {
|
|
|
|
if ($test) {
|
2018-06-18 21:05:44 +00:00
|
|
|
$ret['success'] = true;
|
|
|
|
$ret['message'] .= 'Local site - you are already authenticated.' . EOL;
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2018-06-19 14:15:28 +00:00
|
|
|
logger('Contact is already authenticated', LOGGER_DEBUG);
|
2018-06-18 21:05:44 +00:00
|
|
|
goaway($dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
$headers = [];
|
|
|
|
$headers['Accept'] = 'application/x-dfrn+json';
|
|
|
|
$headers['X-Open-Web-Auth'] = random_string();
|
|
|
|
|
|
|
|
// Create a header that is signed with the local users private key.
|
2018-06-20 16:38:23 +00:00
|
|
|
$headers = HTTPSignature::createSig(
|
2018-06-20 17:32:26 +00:00
|
|
|
$headers,
|
|
|
|
$user['prvkey'],
|
2018-09-20 18:16:14 +00:00
|
|
|
'acct:' . $user['nickname'] . '@' . $a->get_hostname() . ($a->urlpath ? '/' . $a->urlpath : '')
|
2018-06-18 21:05:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Try to get an authentication token from the other instance.
|
|
|
|
$x = Network::curl($basepath . '/owa', false, $redirects, ['headers' => $headers]);
|
|
|
|
|
|
|
|
if ($x['success']) {
|
|
|
|
$j = json_decode($x['body'], 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(base64url_decode($j['encrypted_token']), $token, $user['prvkey']);
|
|
|
|
} else {
|
|
|
|
$token = $j['token'];
|
|
|
|
}
|
|
|
|
$x = strpbrk($dest, '?&');
|
|
|
|
$args = (($x) ? '&owt=' . $token : '?f=&owt=' . $token);
|
|
|
|
|
|
|
|
goaway($dest . $args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goaway($dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
goaway($dest);
|
|
|
|
}
|
|
|
|
}
|