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;
16 * Magic Auth (remote authentication) module.
18 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Magic.php
20 class Magic extends BaseModule
22 public static function init()
25 $ret = ['success' => false, 'url' => '', 'message' => ''];
26 Logger::log('magic mdule: invoked', Logger::DEBUG);
28 Logger::log('args: ' . print_r($_REQUEST, true), Logger::DATA);
30 $addr = ((x($_REQUEST, 'addr')) ? $_REQUEST['addr'] : '');
31 $dest = ((x($_REQUEST, 'dest')) ? $_REQUEST['dest'] : '');
32 $test = ((x($_REQUEST, 'test')) ? intval($_REQUEST['test']) : 0);
33 $owa = ((x($_REQUEST, 'owa')) ? intval($_REQUEST['owa']) : 0);
35 // NOTE: I guess $dest isn't just the profile url (could be also
36 // other profile pages e.g. photo). We need to find a solution
37 // to be able to redirct to other pages than the contact profile.
38 $cid = Contact::getIdForURL($dest);
40 if (!$cid && !empty($addr)) {
41 $cid = Contact::getIdForURL($addr);
45 Logger::log('No contact record found: ' . print_r($_REQUEST, true), Logger::DEBUG);
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'], normalise_link(self::getApp()->getBaseURL())) !== 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';
77 $headers['X-Open-Web-Auth'] = random_string();
79 // Create a header that is signed with the local users private key.
80 $headers = HTTPSignature::createSig(
83 'acct:' . $user['nickname'] . '@' . $a->getHostName() . ($a->getURLPath() ? '/' . $a->getURLPath() : '')
86 // Try to get an authentication token from the other instance.
87 $curlResult = Network::curl($basepath . '/owa', false, $redirects, ['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(base64url_decode($j['encrypted_token']), $token, $user['prvkey']);
101 $x = strpbrk($dest, '?&');
102 $args = (($x) ? '&owt=' . $token : '?f=&owt=' . $token);
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