]> git.mxchange.org Git - friendica.git/blob - src/Module/Magic.php
Update use statement lists with new Friendica\Database\dba class
[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\Database\dba;
9 use Friendica\Model\Contact;
10 use Friendica\Util\HTTPSignature;
11 use Friendica\Util\Network;
12
13 /**
14  * Magic Auth (remote authentication) module.
15  *
16  * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Magic.php
17  */
18 class Magic extends BaseModule
19 {
20         public static function init()
21         {
22                 $a = self::getApp();
23                 $ret = ['success' => false, 'url' => '', 'message' => ''];
24                 logger('magic mdule: invoked', LOGGER_DEBUG);
25
26                 logger('args: ' . print_r($_REQUEST, true), LOGGER_DATA);
27
28                 $addr = ((x($_REQUEST, 'addr')) ? $_REQUEST['addr'] : '');
29                 $dest = ((x($_REQUEST, 'dest')) ? $_REQUEST['dest'] : '');
30                 $test = ((x($_REQUEST, 'test')) ? intval($_REQUEST['test']) : 0);
31                 $owa  = ((x($_REQUEST, 'owa'))  ? intval($_REQUEST['owa'])  : 0);
32
33                 // NOTE: I guess $dest isn't just the profile url (could be also
34                 // other profile pages e.g. photo). We need to find a solution
35                 // to be able to redirct to other pages than the contact profile.
36                 $cid = Contact::getIdForURL($dest);
37
38                 if (!$cid && !empty($addr)) {
39                         $cid = Contact::getIdForURL($addr);
40                 }
41
42                 if (!$cid) {
43                         logger('No contact record found: ' . print_r($_REQUEST, true), LOGGER_DEBUG);
44                         goaway($dest);
45                 }
46
47                 $contact = dba::selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]);
48
49                 // Redirect if the contact is already authenticated on this site.
50                 if (!empty($a->contact) && array_key_exists('id', $a->contact) && strpos($contact['nurl'], normalise_link(self::getApp()->get_baseurl())) !== false) {
51                         if ($test) {
52                                 $ret['success'] = true;
53                                 $ret['message'] .= 'Local site - you are already authenticated.' . EOL;
54                                 return $ret;
55                         }
56
57                         logger('Contact is already authenticated', LOGGER_DEBUG);
58                         goaway($dest);
59                 }
60
61                 if (local_user()) {
62                         $user = $a->user;
63
64                         // OpenWebAuth
65                         if ($owa) {
66                                 // Extract the basepath
67                                 // NOTE: we need another solution because this does only work
68                                 // for friendica contacts :-/ . We should have the basepath
69                                 // of a contact also in the contact table.
70                                 $exp = explode('/profile/', $contact['url']);
71                                 $basepath = $exp[0];
72
73                                 $headers = [];
74                                 $headers['Accept'] = 'application/x-dfrn+json';
75                                 $headers['X-Open-Web-Auth'] = random_string();
76
77                                 // Create a header that is signed with the local users private key.
78                                 $headers = HTTPSignature::createSig(
79                                         '',
80                                         $headers,
81                                         $user['prvkey'],
82                                         'acct:' . $user['nickname'] . '@' . $a->get_hostname() . ($a->urlpath ? '/' . $a->urlpath : ''),
83                                         false,
84                                         true,
85                                         'sha512'
86                                 );
87
88                                 // Try to get an authentication token from the other instance.
89                                 $x = Network::curl($basepath . '/owa', false, $redirects, ['headers' => $headers]);
90
91                                 if ($x['success']) {
92                                         $j = json_decode($x['body'], true);
93
94                                         if ($j['success']) {
95                                                 $token = '';
96                                                 if ($j['encrypted_token']) {
97                                                         // The token is encrypted. If the local user is really the one the other instance
98                                                         // thinks he/she is, the token can be decrypted with the local users public key.
99                                                         openssl_private_decrypt(base64url_decode($j['encrypted_token']), $token, $user['prvkey']);
100                                                 } else {
101                                                         $token = $j['token'];
102                                                 }
103                                                 $x = strpbrk($dest, '?&');
104                                                 $args = (($x) ? '&owt=' . $token : '?f=&owt=' . $token);
105
106                                                 goaway($dest . $args);
107                                         }
108                                 }
109                                 goaway($dest);
110                         }
111                 }
112
113                 if ($test) {
114                         $ret['message'] = 'Not authenticated or invalid arguments' . EOL;
115                         return $ret;
116                 }
117
118                 goaway($dest);
119         }
120 }