]> git.mxchange.org Git - friendica.git/blob - src/Module/Magic.php
Remove duplicated TOC loop in Module\Help
[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\DI;
12 use Friendica\Model\Contact;
13 use Friendica\Util\HTTPSignature;
14 use Friendica\Util\Network;
15 use Friendica\Util\Strings;
16
17 /**
18  * Magic Auth (remote authentication) module.
19  *
20  * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Magic.php
21  */
22 class Magic extends BaseModule
23 {
24         public static function init(array $parameters = [])
25         {
26                 $a = DI::app();
27                 $ret = ['success' => false, 'url' => '', 'message' => ''];
28                 Logger::log('magic mdule: invoked', Logger::DEBUG);
29
30                 Logger::log('args: ' . print_r($_REQUEST, true), Logger::DATA);
31
32                 $addr = $_REQUEST['addr'] ?? '';
33                 $dest = $_REQUEST['dest'] ?? '';
34                 $test = (!empty($_REQUEST['test']) ? intval($_REQUEST['test']) : 0);
35                 $owa  = (!empty($_REQUEST['owa'])  ? intval($_REQUEST['owa'])  : 0);
36                 $cid  = 0;
37
38                 if (!empty($addr)) {
39                         $cid = Contact::getIdForURL($addr);
40                 } elseif (!empty($dest)) {
41                         $cid = Contact::getIdForURL($dest);
42                 }
43
44                 if (!$cid) {
45                         Logger::info('No contact record found', $_REQUEST);
46                         // @TODO Finding a more elegant possibility to redirect to either internal or external URL
47                         $a->redirect($dest);
48                 }
49                 $contact = DBA::selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]);
50
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'], Strings::normaliseLink(DI::baseUrl()->get())) !== false) {
53                         if ($test) {
54                                 $ret['success'] = true;
55                                 $ret['message'] .= 'Local site - you are already authenticated.' . EOL;
56                                 return $ret;
57                         }
58
59                         Logger::log('Contact is already authenticated', Logger::DEBUG);
60                         System::externalRedirect($dest);
61                 }
62
63                 if (local_user()) {
64                         $user = $a->user;
65
66                         // OpenWebAuth
67                         if ($owa) {
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']);
73                                 $basepath = $exp[0];
74
75                                 $headers = [];
76                                 $headers['Accept'] = 'application/x-dfrn+json, application/x-zot+json';
77                                 $headers['X-Open-Web-Auth'] = Strings::getRandomHex();
78
79                                 // Create a header that is signed with the local users private key.
80                                 $headers = HTTPSignature::createSig(
81                                         $headers,
82                                         $user['prvkey'],
83                                         'acct:' . $user['nickname'] . '@' . DI::baseUrl()->getHostname() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : '')
84                                 );
85
86                                 // Try to get an authentication token from the other instance.
87                                 $curlResult = Network::curl($basepath . '/owa', false, ['headers' => $headers]);
88
89                                 if ($curlResult->isSuccess()) {
90                                         $j = json_decode($curlResult->getBody(), true);
91
92                                         if ($j['success']) {
93                                                 $token = '';
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(Strings::base64UrlDecode($j['encrypted_token']), $token, $user['prvkey']);
98                                                 } else {
99                                                         $token = $j['token'];
100                                                 }
101                                                 $args = (strpbrk($dest, '?&') ? '&' : '?') . 'owt=' . $token;
102
103                                                 Logger::info('Redirecting', ['path' => $dest . $args]);
104                                                 System::externalRedirect($dest . $args);
105                                         }
106                                 }
107                                 System::externalRedirect($dest);
108                         }
109                 }
110
111                 if ($test) {
112                         $ret['message'] = 'Not authenticated or invalid arguments' . EOL;
113                         return $ret;
114                 }
115
116                 // @TODO Finding a more elegant possibility to redirect to either internal or external URL
117                 $a->redirect($dest);
118         }
119 }