]> git.mxchange.org Git - friendica.git/blob - src/Module/Magic.php
2b46d461836b4ade130421c83b0ef2a18d342552
[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\System;
9 use Friendica\Database\DBA;
10 use Friendica\Model\Contact;
11 use Friendica\Util\HTTPSignature;
12 use Friendica\Util\Network;
13
14 /**
15  * Magic Auth (remote authentication) module.
16  *
17  * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Magic.php
18  */
19 class Magic extends BaseModule
20 {
21         public static function init()
22         {
23                 $a = self::getApp();
24                 $ret = ['success' => false, 'url' => '', 'message' => ''];
25                 logger('magic mdule: invoked', LOGGER_DEBUG);
26
27                 logger('args: ' . print_r($_REQUEST, true), LOGGER_DATA);
28
29                 $addr = ((x($_REQUEST, 'addr')) ? $_REQUEST['addr'] : '');
30                 $dest = ((x($_REQUEST, 'dest')) ? $_REQUEST['dest'] : '');
31                 $test = ((x($_REQUEST, 'test')) ? intval($_REQUEST['test']) : 0);
32                 $owa  = ((x($_REQUEST, 'owa'))  ? intval($_REQUEST['owa'])  : 0);
33
34                 // NOTE: I guess $dest isn't just the profile url (could be also
35                 // other profile pages e.g. photo). We need to find a solution
36                 // to be able to redirct to other pages than the contact profile.
37                 $cid = Contact::getIdForURL($dest);
38
39                 if (!$cid && !empty($addr)) {
40                         $cid = Contact::getIdForURL($addr);
41                 }
42
43                 if (!$cid) {
44                         logger('No contact record found: ' . print_r($_REQUEST, true), LOGGER_DEBUG);
45                         $a->internalRedirect($dest);
46                 }
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'], normalise_link(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('Contact is already authenticated', LOGGER_DEBUG);
59                         $a->internalRedirect($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';
76                                 $headers['X-Open-Web-Auth'] = random_string();
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, $redirects, ['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(base64url_decode($j['encrypted_token']), $token, $user['prvkey']);
97                                                 } else {
98                                                         $token = $j['token'];
99                                                 }
100                                                 $x = strpbrk($dest, '?&');
101                                                 $args = (($x) ? '&owt=' . $token : '?f=&owt=' . $token);
102
103                                                 $a->internalRedirect($dest . $args);
104                                         }
105                                 }
106                                 $a->internalRedirect($dest);
107                         }
108                 }
109
110                 if ($test) {
111                         $ret['message'] = 'Not authenticated or invalid arguments' . EOL;
112                         return $ret;
113                 }
114
115                 $a->internalRedirect($dest);
116         }
117 }