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);
36 // NOTE: I guess $dest isn't just the profile url (could be also
37 // other profile pages e.g. photo). We need to find a solution
38 // to be able to redirct to other pages than the contact profile.
39 $cid = Contact::getIdForURL($dest);
41 if (!$cid && !empty($addr)) {
42 $cid = Contact::getIdForURL($addr);
46 Logger::log('No contact record found: ' . json_encode($_REQUEST), Logger::DEBUG);
47 // @TODO Finding a more elegant possibility to redirect to either internal or external URL
50 $contact = DBA::selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]);
52 // Redirect if the contact is already authenticated on this site.
53 if (!empty($a->contact) && array_key_exists('id', $a->contact) && strpos($contact['nurl'], Strings::normaliseLink(self::getApp()->getBaseURL())) !== false) {
55 $ret['success'] = true;
56 $ret['message'] .= 'Local site - you are already authenticated.' . EOL;
60 Logger::log('Contact is already authenticated', Logger::DEBUG);
61 System::externalRedirect($dest);
69 // Extract the basepath
70 // NOTE: we need another solution because this does only work
71 // for friendica contacts :-/ . We should have the basepath
72 // of a contact also in the contact table.
73 $exp = explode('/profile/', $contact['url']);
77 $headers['Accept'] = 'application/x-dfrn+json';
78 $headers['X-Open-Web-Auth'] = Strings::getRandomHex();
80 // Create a header that is signed with the local users private key.
81 $headers = HTTPSignature::createSig(
84 'acct:' . $user['nickname'] . '@' . $a->getHostName() . ($a->getURLPath() ? '/' . $a->getURLPath() : '')
87 // Try to get an authentication token from the other instance.
88 $curlResult = Network::curl($basepath . '/owa', false, $redirects, ['headers' => $headers]);
90 if ($curlResult->isSuccess()) {
91 $j = json_decode($curlResult->getBody(), true);
95 if ($j['encrypted_token']) {
96 // The token is encrypted. If the local user is really the one the other instance
97 // thinks he/she is, the token can be decrypted with the local users public key.
98 openssl_private_decrypt(Strings::base64UrlDecode($j['encrypted_token']), $token, $user['prvkey']);
100 $token = $j['token'];
102 $x = strpbrk($dest, '?&');
103 $args = (($x) ? '&owt=' . $token : '?f=&owt=' . $token);
105 System::externalRedirect($dest . $args);
108 System::externalRedirect($dest);
113 $ret['message'] = 'Not authenticated or invalid arguments' . EOL;
117 // @TODO Finding a more elegant possibility to redirect to either internal or external URL