3 * @file src/Module/Magic.php
5 namespace Friendica\Module;
7 use Friendica\BaseModule;
8 use Friendica\Database\DBA;
9 use Friendica\Model\Contact;
10 use Friendica\Core\System;
11 use Friendica\Util\HTTPSignature;
12 use Friendica\Util\Network;
15 * Magic Auth (remote authentication) module.
17 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Magic.php
19 class Magic extends BaseModule
21 public static function init()
24 $ret = ['success' => false, 'url' => '', 'message' => ''];
25 logger('magic mdule: invoked', LOGGER_DEBUG);
27 logger('args: ' . print_r($_REQUEST, true), LOGGER_DATA);
29 $addr = ((x($_REQUEST, 'addr')) ? $_REQUEST['addr'] : '');
30 $dest = ((x($_REQUEST, 'dest')) ? $_REQUEST['dest'] : '');
31 $test = ((x($_REQUEST, 'test')) ? intval($_REQUEST['test']) : 0);
32 $owa = ((x($_REQUEST, 'owa')) ? intval($_REQUEST['owa']) : 0);
34 // NOTE: I guess $dest isn't just the profile url (could be also
35 // other profile pages e.g. photo). We need to find a solution
36 // to be able to redirct to other pages than the contact profile.
37 $cid = Contact::getIdForURL($dest);
39 if (!$cid && !empty($addr)) {
40 $cid = Contact::getIdForURL($addr);
44 logger('No contact record found: ' . print_r($_REQUEST, true), LOGGER_DEBUG);
45 // @TODO Finding a more elegant possibility to redirect to either internal or external URL
46 if (filter_var($dest, FILTER_VALIDATE_URL)) {
47 System::externalRedirect($dest);
49 $a->internalRedirect($dest);
52 $contact = DBA::selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]);
54 // Redirect if the contact is already authenticated on this site.
55 if (!empty($a->contact) && array_key_exists('id', $a->contact) && strpos($contact['nurl'], normalise_link(self::getApp()->getBaseURL())) !== false) {
57 $ret['success'] = true;
58 $ret['message'] .= 'Local site - you are already authenticated.' . EOL;
62 logger('Contact is already authenticated', LOGGER_DEBUG);
63 System::externalRedirect($dest);
71 // Extract the basepath
72 // NOTE: we need another solution because this does only work
73 // for friendica contacts :-/ . We should have the basepath
74 // of a contact also in the contact table.
75 $exp = explode('/profile/', $contact['url']);
79 $headers['Accept'] = 'application/x-dfrn+json';
80 $headers['X-Open-Web-Auth'] = random_string();
82 // Create a header that is signed with the local users private key.
83 $headers = HTTPSignature::createSig(
86 'acct:' . $user['nickname'] . '@' . $a->getHostName() . ($a->getURLPath() ? '/' . $a->getURLPath() : '')
89 // Try to get an authentication token from the other instance.
90 $curlResult = Network::curl($basepath . '/owa', false, $redirects, ['headers' => $headers]);
92 if ($curlResult->isSuccess()) {
93 $j = json_decode($curlResult->getBody(), true);
97 if ($j['encrypted_token']) {
98 // The token is encrypted. If the local user is really the one the other instance
99 // thinks he/she is, the token can be decrypted with the local users public key.
100 openssl_private_decrypt(base64url_decode($j['encrypted_token']), $token, $user['prvkey']);
102 $token = $j['token'];
104 $x = strpbrk($dest, '?&');
105 $args = (($x) ? '&owt=' . $token : '?f=&owt=' . $token);
107 System::externalRedirect($dest . $args);
110 System::externalRedirect($dest);
115 $ret['message'] = 'Not authenticated or invalid arguments' . EOL;
119 // @TODO Finding a more elegant possibility to redirect to either internal or external URL
120 if (filter_var($dest, FILTER_VALIDATE_URL)) {
121 System::externalRedirect($dest);
123 $a->internalRedirect($dest);