]> git.mxchange.org Git - friendica.git/blob - src/Module/Magic.php
Merge remote-tracking branch 'upstream/develop' into item-view
[friendica.git] / src / Module / Magic.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Logger;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Contact;
30 use Friendica\Util\HTTPSignature;
31 use Friendica\Util\Strings;
32
33 /**
34  * Magic Auth (remote authentication) module.
35  *
36  * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Magic.php
37  */
38 class Magic extends BaseModule
39 {
40         public static function init(array $parameters = [])
41         {
42                 $a = DI::app();
43                 $ret = ['success' => false, 'url' => '', 'message' => ''];
44                 Logger::info('magic mdule: invoked');
45
46                 Logger::debug('args', ['request' => $_REQUEST]);
47
48                 $addr = $_REQUEST['addr'] ?? '';
49                 $dest = $_REQUEST['dest'] ?? '';
50                 $test = (!empty($_REQUEST['test']) ? intval($_REQUEST['test']) : 0);
51                 $owa  = (!empty($_REQUEST['owa'])  ? intval($_REQUEST['owa'])  : 0);
52                 $cid  = 0;
53
54                 if (!empty($addr)) {
55                         $cid = Contact::getIdForURL($addr);
56                 } elseif (!empty($dest)) {
57                         $cid = Contact::getIdForURL($dest);
58                 }
59
60                 if (!$cid) {
61                         Logger::info('No contact record found', $_REQUEST);
62                         // @TODO Finding a more elegant possibility to redirect to either internal or external URL
63                         $a->redirect($dest);
64                 }
65                 $contact = DBA::selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]);
66
67                 // Redirect if the contact is already authenticated on this site.
68                 if (!empty($a->contact) && array_key_exists('id', $a->contact) && strpos($contact['nurl'], Strings::normaliseLink(DI::baseUrl()->get())) !== false) {
69                         if ($test) {
70                                 $ret['success'] = true;
71                                 $ret['message'] .= 'Local site - you are already authenticated.' . EOL;
72                                 return $ret;
73                         }
74
75                         Logger::info('Contact is already authenticated');
76                         System::externalRedirect($dest);
77                 }
78
79                 if (local_user()) {
80                         $user = $a->user;
81
82                         // OpenWebAuth
83                         if ($owa) {
84                                 // Extract the basepath
85                                 // NOTE: we need another solution because this does only work
86                                 // for friendica contacts :-/ . We should have the basepath
87                                 // of a contact also in the contact table.
88                                 $exp = explode('/profile/', $contact['url']);
89                                 $basepath = $exp[0];
90
91                                 $header = [];
92                                 $header['Accept'] = 'application/x-dfrn+json, application/x-zot+json';
93                                 $header['X-Open-Web-Auth'] = Strings::getRandomHex();
94
95                                 // Create a header that is signed with the local users private key.
96                                 $header = HTTPSignature::createSig(
97                                         $header,
98                                         $user['prvkey'],
99                                         'acct:' . $user['nickname'] . '@' . DI::baseUrl()->getHostname() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : '')
100                                 );
101
102                                 // Try to get an authentication token from the other instance.
103                                 $curlResult = DI::httpRequest()->get($basepath . '/owa', ['header' => $header]);
104
105                                 if ($curlResult->isSuccess()) {
106                                         $j = json_decode($curlResult->getBody(), true);
107
108                                         if ($j['success']) {
109                                                 $token = '';
110                                                 if ($j['encrypted_token']) {
111                                                         // The token is encrypted. If the local user is really the one the other instance
112                                                         // thinks he/she is, the token can be decrypted with the local users public key.
113                                                         openssl_private_decrypt(Strings::base64UrlDecode($j['encrypted_token']), $token, $user['prvkey']);
114                                                 } else {
115                                                         $token = $j['token'];
116                                                 }
117                                                 $args = (strpbrk($dest, '?&') ? '&' : '?') . 'owt=' . $token;
118
119                                                 Logger::info('Redirecting', ['path' => $dest . $args]);
120                                                 System::externalRedirect($dest . $args);
121                                         }
122                                 }
123                                 System::externalRedirect($dest);
124                         }
125                 }
126
127                 if ($test) {
128                         $ret['message'] = 'Not authenticated or invalid arguments' . EOL;
129                         return $ret;
130                 }
131
132                 // @TODO Finding a more elegant possibility to redirect to either internal or external URL
133                 $a->redirect($dest);
134         }
135 }