]> git.mxchange.org Git - friendica.git/blob - src/Module/Magic.php
f0be114e3dbb056aa1c46897360d3e4ef1158d3d
[friendica.git] / src / Module / Magic.php
1 <?php
2 /**
3  * @file src/Module/Magic.php
4  */
5 namespace Friendica\Module;
6
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;
15
16 /**
17  * Magic Auth (remote authentication) module.
18  *
19  * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Magic.php
20  */
21 class Magic extends BaseModule
22 {
23         public static function init()
24         {
25                 $a = self::getApp();
26                 $ret = ['success' => false, 'url' => '', 'message' => ''];
27                 Logger::log('magic mdule: invoked', Logger::DEBUG);
28
29                 Logger::log('args: ' . print_r($_REQUEST, true), Logger::DATA);
30
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);
35
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);
40
41                 if (!$cid && !empty($addr)) {
42                         $cid = Contact::getIdForURL($addr);
43                 }
44
45                 if (!$cid) {
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
48                         $a->redirect($dest);
49                 }
50                 $contact = DBA::selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]);
51
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) {
54                         if ($test) {
55                                 $ret['success'] = true;
56                                 $ret['message'] .= 'Local site - you are already authenticated.' . EOL;
57                                 return $ret;
58                         }
59
60                         Logger::log('Contact is already authenticated', Logger::DEBUG);
61                         System::externalRedirect($dest);
62                 }
63
64                 if (local_user()) {
65                         $user = $a->user;
66
67                         // OpenWebAuth
68                         if ($owa) {
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']);
74                                 $basepath = $exp[0];
75
76                                 $headers = [];
77                                 $headers['Accept'] = 'application/x-dfrn+json';
78                                 $headers['X-Open-Web-Auth'] = Strings::getRandomHex();
79
80                                 // Create a header that is signed with the local users private key.
81                                 $headers = HTTPSignature::createSig(
82                                         $headers,
83                                         $user['prvkey'],
84                                         'acct:' . $user['nickname'] . '@' . $a->getHostName() . ($a->getURLPath() ? '/' . $a->getURLPath() : '')
85                                 );
86
87                                 // Try to get an authentication token from the other instance.
88                                 $curlResult = Network::curl($basepath . '/owa', false, $redirects, ['headers' => $headers]);
89
90                                 if ($curlResult->isSuccess()) {
91                                         $j = json_decode($curlResult->getBody(), true);
92
93                                         if ($j['success']) {
94                                                 $token = '';
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']);
99                                                 } else {
100                                                         $token = $j['token'];
101                                                 }
102                                                 $x = strpbrk($dest, '?&');
103                                                 $args = (($x) ? '&owt=' . $token : '?f=&owt=' . $token);
104
105                                                 System::externalRedirect($dest . $args);
106                                         }
107                                 }
108                                 System::externalRedirect($dest);
109                         }
110                 }
111
112                 if ($test) {
113                         $ret['message'] = 'Not authenticated or invalid arguments' . EOL;
114                         return $ret;
115                 }
116
117                 // @TODO Finding a more elegant possibility to redirect to either internal or external URL
118                 $a->redirect($dest);
119         }
120 }