]> git.mxchange.org Git - friendica.git/blob - src/Module/Owa.php
Fix reference to Entity\LocalRelationship class after simplifying `use` statement...
[friendica.git] / src / Module / Owa.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
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\Model\Contact;
29 use Friendica\Model\OpenWebAuthToken;
30 use Friendica\Util\HTTPSignature;
31 use Friendica\Util\Strings;
32
33 /**
34  * OpenWebAuth verifier and token generator
35  *
36  * See https://macgirvin.com/wiki/mike/OpenWebAuth/Home
37  * Requests to this endpoint should be signed using HTTP Signatures
38  * using the 'Authorization: Signature' authentication method
39  * If the signature verifies a token is returned.
40  *
41  * This token may be exchanged for an authenticated cookie.
42  *
43  * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Owa.php
44  */
45 class Owa extends BaseModule
46 {
47         protected function rawContent(array $request = [])
48         {
49                 $ret = [ 'success' => false ];
50
51                 foreach (['REDIRECT_REMOTE_USER', 'HTTP_AUTHORIZATION'] as $head) {
52                         if (array_key_exists($head, $_SERVER) && substr(trim($_SERVER[$head]), 0, 9) === 'Signature') {
53                                 if ($head !== 'HTTP_AUTHORIZATION') {
54                                         $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER[$head];
55                                         continue;
56                                 }
57
58                                 $sigblock = HTTPSignature::parseSigheader($_SERVER[$head]);
59                                 if ($sigblock) {
60                                         $keyId = $sigblock['keyId'];
61
62                                         if ($keyId) {
63                                                 // Try to find the public contact entry of the handle.
64                                                 $handle = str_replace('acct:', '', $keyId);
65
66                                                 $cid       = Contact::getIdForURL($handle);
67                                                 $fields    = ['id', 'url', 'addr', 'pubkey'];
68                                                 $condition = ['id' => $cid];
69
70                                                 $contact = DBA::selectFirst('contact', $fields, $condition);
71
72                                                 if (DBA::isResult($contact)) {
73                                                         // Try to verify the signed header with the public key of the contact record
74                                                         // we have found.
75                                                         $verified = HTTPSignature::verifyMagic($contact['pubkey']);
76
77                                                         if ($verified && $verified['header_signed'] && $verified['header_valid']) {
78                                                                 Logger::debug('OWA header', ['addr' => $contact['addr'], 'data' => $verified]);
79
80                                                                 $ret['success'] = true;
81                                                                 $token = Strings::getRandomHex(32);
82
83                                                                 // Store the generated token in the database.
84                                                                 OpenWebAuthToken::create('owt', 0, $token, $contact['addr']);
85
86                                                                 $result = '';
87
88                                                                 // Encrypt the token with the public contacts public key.
89                                                                 // Only the specific public contact will be able to encrypt it.
90                                                                 // At a later time, we will compare weather the token we're getting
91                                                                 // is really the same token we have stored in the database.
92                                                                 openssl_public_encrypt($token, $result, $contact['pubkey']);
93                                                                 $ret['encrypted_token'] = Strings::base64UrlEncode($result);
94                                                         } else {
95                                                                 Logger::info('OWA fail', ['id' => $contact['id'], 'addr' => $contact['addr'], 'url' => $contact['url']]);
96                                                         }
97                                                 } else {
98                                                         Logger::info('Contact not found', ['handle' => $handle]);
99                                                 }
100                                         }
101                                 }
102                         }
103                 }
104                 System::jsonExit($ret, 'application/x-zot+json');
105         }
106 }