]> git.mxchange.org Git - friendica.git/blob - src/Module/Magic.php
b04ea80c04bdd73f84ae90a4b515fa7f02c05dd5
[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 = $_REQUEST['addr'] ?? '';
32                 $dest = $_REQUEST['dest'] ?? '';
33                 $test = (!empty($_REQUEST['test']) ? intval($_REQUEST['test']) : 0);
34                 $owa  = (!empty($_REQUEST['owa'])  ? intval($_REQUEST['owa'])  : 0);
35                 $cid  = 0;
36
37                 if (!empty($addr)) {
38                         $cid = Contact::getIdForURL($addr);
39                 } elseif (!empty($dest)) {
40                         $cid = Contact::getIdForURL($dest);
41                 }
42
43                 if (!$cid) {
44                         Logger::info('No contact record found', $_REQUEST);
45                         // @TODO Finding a more elegant possibility to redirect to either internal or external URL
46                         $a->redirect($dest);
47                 }
48                 $contact = DBA::selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]);
49
50                 // Redirect if the contact is already authenticated on this site.
51                 if (!empty($a->contact) && array_key_exists('id', $a->contact) && strpos($contact['nurl'], Strings::normaliseLink(self::getApp()->getBaseURL())) !== false) {
52                         if ($test) {
53                                 $ret['success'] = true;
54                                 $ret['message'] .= 'Local site - you are already authenticated.' . EOL;
55                                 return $ret;
56                         }
57
58                         Logger::log('Contact is already authenticated', Logger::DEBUG);
59                         System::externalRedirect($dest);
60                 }
61
62                 if (local_user()) {
63                         $user = $a->user;
64
65                         // OpenWebAuth
66                         if ($owa) {
67                                 // Extract the basepath
68                                 // NOTE: we need another solution because this does only work
69                                 // for friendica contacts :-/ . We should have the basepath
70                                 // of a contact also in the contact table.
71                                 $exp = explode('/profile/', $contact['url']);
72                                 $basepath = $exp[0];
73
74                                 $headers = [];
75                                 $headers['Accept'] = 'application/x-dfrn+json, application/x-zot+json';
76                                 $headers['X-Open-Web-Auth'] = Strings::getRandomHex();
77
78                                 // Create a header that is signed with the local users private key.
79                                 $headers = HTTPSignature::createSig(
80                                         $headers,
81                                         $user['prvkey'],
82                                         'acct:' . $user['nickname'] . '@' . $a->getHostName() . ($a->getURLPath() ? '/' . $a->getURLPath() : '')
83                                 );
84
85                                 // Try to get an authentication token from the other instance.
86                                 $curlResult = Network::curl($basepath . '/owa', false, ['headers' => $headers]);
87
88                                 if ($curlResult->isSuccess()) {
89                                         $j = json_decode($curlResult->getBody(), true);
90
91                                         if ($j['success']) {
92                                                 $token = '';
93                                                 if ($j['encrypted_token']) {
94                                                         // The token is encrypted. If the local user is really the one the other instance
95                                                         // thinks he/she is, the token can be decrypted with the local users public key.
96                                                         openssl_private_decrypt(Strings::base64UrlDecode($j['encrypted_token']), $token, $user['prvkey']);
97                                                 } else {
98                                                         $token = $j['token'];
99                                                 }
100                                                 $args = (strpbrk($dest, '?&') ? '&' : '?') . 'owt=' . $token;
101
102                                                 Logger::info('Redirecting', ['path' => $dest . $args]);
103                                                 System::externalRedirect($dest . $args);
104                                         }
105                                 }
106                                 System::externalRedirect($dest);
107                         }
108                 }
109
110                 if ($test) {
111                         $ret['message'] = 'Not authenticated or invalid arguments' . EOL;
112                         return $ret;
113                 }
114
115                 // @TODO Finding a more elegant possibility to redirect to either internal or external URL
116                 $a->redirect($dest);
117         }
118 }