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;
11 use Friendica\Model\Contact;
12 use Friendica\Util\HTTPSignature;
13 use Friendica\Util\Network;
14 use Friendica\Util\Strings;
17 * Magic Auth (remote authentication) module.
19 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Magic.php
21 class Magic extends BaseModule
23 public static function init()
26 $ret = ['success' => false, 'url' => '', 'message' => ''];
27 Logger::log('magic mdule: invoked', Logger::DEBUG);
29 Logger::log('args: ' . print_r($_REQUEST, true), Logger::DATA);
31 $addr = defaults($_REQUEST, 'addr', '');
32 $dest = defaults($_REQUEST, 'dest', '');
33 $test = (!empty($_REQUEST['test']) ? intval($_REQUEST['test']) : 0);
34 $owa = (!empty($_REQUEST['owa']) ? intval($_REQUEST['owa']) : 0);
37 $cid = Contact::getIdForURL($addr);
39 $cid = Contact::getIdForURL($dest);
43 Logger::log('No contact record found: ' . json_encode($_REQUEST), Logger::DEBUG);
44 // @TODO Finding a more elegant possibility to redirect to either internal or external URL
47 $contact = DBA::selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]);
49 // Redirect if the contact is already authenticated on this site.
50 if (!empty($a->contact) && array_key_exists('id', $a->contact) && strpos($contact['nurl'], Strings::normaliseLink(self::getApp()->getBaseURL())) !== false) {
52 $ret['success'] = true;
53 $ret['message'] .= 'Local site - you are already authenticated.' . EOL;
57 Logger::log('Contact is already authenticated', Logger::DEBUG);
58 System::externalRedirect($dest);
66 // Extract the basepath
67 // NOTE: we need another solution because this does only work
68 // for friendica contacts :-/ . We should have the basepath
69 // of a contact also in the contact table.
70 $exp = explode('/profile/', $contact['url']);
74 $headers['Accept'] = 'application/x-dfrn+json, application/x-zot+json';
75 $headers['X-Open-Web-Auth'] = Strings::getRandomHex();
77 // Create a header that is signed with the local users private key.
78 $headers = HTTPSignature::createSig(
81 'acct:' . $user['nickname'] . '@' . $a->getHostName() . ($a->getURLPath() ? '/' . $a->getURLPath() : '')
84 // Try to get an authentication token from the other instance.
85 $curlResult = Network::curl($basepath . '/owa', false, ['headers' => $headers]);
87 if ($curlResult->isSuccess()) {
88 $j = json_decode($curlResult->getBody(), true);
92 if ($j['encrypted_token']) {
93 // The token is encrypted. If the local user is really the one the other instance
94 // thinks he/she is, the token can be decrypted with the local users public key.
95 openssl_private_decrypt(Strings::base64UrlDecode($j['encrypted_token']), $token, $user['prvkey']);
99 $x = strpbrk($dest, '?&');
100 $args = (($x) ? '&owt=' . $token : '?f=&owt=' . $token);
102 System::externalRedirect($dest . $args);
105 System::externalRedirect($dest);
110 $ret['message'] = 'Not authenticated or invalid arguments' . EOL;
114 // @TODO Finding a more elegant possibility to redirect to either internal or external URL