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);
38 $cid = Contact::getIdForURL($addr);
39 } elseif (!empty($dest)) {
40 $cid = Contact::getIdForURL($dest);
44 Logger::info('No contact record found', $_REQUEST);
45 // @TODO Finding a more elegant possibility to redirect to either internal or external URL
48 $contact = DBA::selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]);
50 // Redirect if the contact is already authenticated on this site.
51 if (!empty($a->contact) && array_key_exists('id', $a->contact) && strpos($contact['nurl'], Strings::normaliseLink(self::getApp()->getBaseURL())) !== false) {
53 $ret['success'] = true;
54 $ret['message'] .= 'Local site - you are already authenticated.' . EOL;
58 Logger::log('Contact is already authenticated', Logger::DEBUG);
59 System::externalRedirect($dest);
67 // Extract the basepath
68 // NOTE: we need another solution because this does only work
69 // for friendica contacts :-/ . We should have the basepath
70 // of a contact also in the contact table.
71 $exp = explode('/profile/', $contact['url']);
75 $headers['Accept'] = 'application/x-dfrn+json, application/x-zot+json';
76 $headers['X-Open-Web-Auth'] = Strings::getRandomHex();
78 // Create a header that is signed with the local users private key.
79 $headers = HTTPSignature::createSig(
82 'acct:' . $user['nickname'] . '@' . $a->getHostName() . ($a->getURLPath() ? '/' . $a->getURLPath() : '')
85 // Try to get an authentication token from the other instance.
86 $curlResult = Network::curl($basepath . '/owa', false, ['headers' => $headers]);
88 if ($curlResult->isSuccess()) {
89 $j = json_decode($curlResult->getBody(), true);
93 if ($j['encrypted_token']) {
94 // The token is encrypted. If the local user is really the one the other instance
95 // thinks he/she is, the token can be decrypted with the local users public key.
96 openssl_private_decrypt(Strings::base64UrlDecode($j['encrypted_token']), $token, $user['prvkey']);
100 $x = strpbrk($dest, '?&');
101 $args = (($x) ? '&owt=' . $token : '?f=&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