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