]> git.mxchange.org Git - friendica.git/blob - src/Module/Magic.php
Logger Levels
[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
15 /**
16  * Magic Auth (remote authentication) module.
17  *
18  * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Magic.php
19  */
20 class Magic extends BaseModule
21 {
22         public static function init()
23         {
24                 $a = self::getApp();
25                 $ret = ['success' => false, 'url' => '', 'message' => ''];
26                 Logger::log('magic mdule: invoked', Logger::DEBUG);
27
28                 Logger::log('args: ' . print_r($_REQUEST, true), Logger::DATA);
29
30                 $addr = ((x($_REQUEST, 'addr')) ? $_REQUEST['addr'] : '');
31                 $dest = ((x($_REQUEST, 'dest')) ? $_REQUEST['dest'] : '');
32                 $test = ((x($_REQUEST, 'test')) ? intval($_REQUEST['test']) : 0);
33                 $owa  = ((x($_REQUEST, 'owa'))  ? intval($_REQUEST['owa'])  : 0);
34
35                 // NOTE: I guess $dest isn't just the profile url (could be also
36                 // other profile pages e.g. photo). We need to find a solution
37                 // to be able to redirct to other pages than the contact profile.
38                 $cid = Contact::getIdForURL($dest);
39
40                 if (!$cid && !empty($addr)) {
41                         $cid = Contact::getIdForURL($addr);
42                 }
43
44                 if (!$cid) {
45                         Logger::log('No contact record found: ' . print_r($_REQUEST, true), Logger::DEBUG);
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'], normalise_link(self::getApp()->getBaseURL())) !== 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';
77                                 $headers['X-Open-Web-Auth'] = random_string();
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'] . '@' . $a->getHostName() . ($a->getURLPath() ? '/' . $a->getURLPath() : '')
84                                 );
85
86                                 // Try to get an authentication token from the other instance.
87                                 $curlResult = Network::curl($basepath . '/owa', false, $redirects, ['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(base64url_decode($j['encrypted_token']), $token, $user['prvkey']);
98                                                 } else {
99                                                         $token = $j['token'];
100                                                 }
101                                                 $x = strpbrk($dest, '?&');
102                                                 $args = (($x) ? '&owt=' . $token : '?f=&owt=' . $token);
103
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 }