3 * @file src/Module/Magic.php
5 namespace Friendica\Module;
7 use Friendica\BaseModule;
8 use Friendica\Core\Logger;
9 use Friendica\Core\System;
10 use Friendica\Database\DBA;
12 use Friendica\Model\Contact;
13 use Friendica\Util\HTTPSignature;
14 use Friendica\Util\Network;
15 use Friendica\Util\Strings;
18 * Magic Auth (remote authentication) module.
20 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Magic.php
22 class Magic extends BaseModule
24 public static function init(array $parameters = [])
27 $ret = ['success' => false, 'url' => '', 'message' => ''];
28 Logger::log('magic mdule: invoked', Logger::DEBUG);
30 Logger::log('args: ' . print_r($_REQUEST, true), Logger::DATA);
32 $addr = $_REQUEST['addr'] ?? '';
33 $dest = $_REQUEST['dest'] ?? '';
34 $test = (!empty($_REQUEST['test']) ? intval($_REQUEST['test']) : 0);
35 $owa = (!empty($_REQUEST['owa']) ? intval($_REQUEST['owa']) : 0);
39 $cid = Contact::getIdForURL($addr);
40 } elseif (!empty($dest)) {
41 $cid = Contact::getIdForURL($dest);
45 Logger::info('No contact record found', $_REQUEST);
46 // @TODO Finding a more elegant possibility to redirect to either internal or external URL
49 $contact = DBA::selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]);
51 // Redirect if the contact is already authenticated on this site.
52 if (!empty($a->contact) && array_key_exists('id', $a->contact) && strpos($contact['nurl'], Strings::normaliseLink(DI::baseUrl()->get())) !== false) {
54 $ret['success'] = true;
55 $ret['message'] .= 'Local site - you are already authenticated.' . EOL;
59 Logger::log('Contact is already authenticated', Logger::DEBUG);
60 System::externalRedirect($dest);
68 // Extract the basepath
69 // NOTE: we need another solution because this does only work
70 // for friendica contacts :-/ . We should have the basepath
71 // of a contact also in the contact table.
72 $exp = explode('/profile/', $contact['url']);
76 $headers['Accept'] = 'application/x-dfrn+json, application/x-zot+json';
77 $headers['X-Open-Web-Auth'] = Strings::getRandomHex();
79 // Create a header that is signed with the local users private key.
80 $headers = HTTPSignature::createSig(
83 'acct:' . $user['nickname'] . '@' . DI::baseUrl()->getHostname() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : '')
86 // Try to get an authentication token from the other instance.
87 $curlResult = Network::curl($basepath . '/owa', false, ['headers' => $headers]);
89 if ($curlResult->isSuccess()) {
90 $j = json_decode($curlResult->getBody(), true);
94 if ($j['encrypted_token']) {
95 // The token is encrypted. If the local user is really the one the other instance
96 // thinks he/she is, the token can be decrypted with the local users public key.
97 openssl_private_decrypt(Strings::base64UrlDecode($j['encrypted_token']), $token, $user['prvkey']);
101 $args = (strpbrk($dest, '?&') ? '&' : '?') . 'owt=' . $token;
103 Logger::info('Redirecting', ['path' => $dest . $args]);
104 System::externalRedirect($dest . $args);
107 System::externalRedirect($dest);
112 $ret['message'] = 'Not authenticated or invalid arguments' . EOL;
116 // @TODO Finding a more elegant possibility to redirect to either internal or external URL