]> git.mxchange.org Git - friendica.git/blob - src/Module/Owa.php
Inverted condition
[friendica.git] / src / Module / Owa.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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         public static function init(array $parameters = [])
48         {
49
50                 $ret = [ 'success' => false ];
51
52                 foreach (['REDIRECT_REMOTE_USER', 'HTTP_AUTHORIZATION'] as $head) {
53                         if (array_key_exists($head, $_SERVER) && substr(trim($_SERVER[$head]), 0, 9) === 'Signature') {
54                                 if ($head !== 'HTTP_AUTHORIZATION') {
55                                         $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER[$head];
56                                         continue;
57                                 }
58
59                                 $sigblock = HTTPSignature::parseSigheader($_SERVER[$head]);
60                                 if ($sigblock) {
61                                         $keyId = $sigblock['keyId'];
62
63                                         if ($keyId) {
64                                                 // Try to find the public contact entry of the handle.
65                                                 $handle = str_replace('acct:', '', $keyId);
66
67                                                 $cid       = Contact::getIdForURL($handle);
68                                                 $fields    = ['id', 'url', 'addr', 'pubkey'];
69                                                 $condition = ['id' => $cid];
70
71                                                 $contact = DBA::selectFirst('contact', $fields, $condition);
72
73                                                 if (DBA::isResult($contact)) {
74                                                         // Try to verify the signed header with the public key of the contact record
75                                                         // we have found.
76                                                         $verified = HTTPSignature::verifyMagic($contact['pubkey']);
77
78                                                         if ($verified && $verified['header_signed'] && $verified['header_valid']) {
79                                                                 Logger::debug('OWA header', ['addr' => $contact['addr'], 'data' => $verified]);
80
81                                                                 $ret['success'] = true;
82                                                                 $token = Strings::getRandomHex(32);
83
84                                                                 // Store the generated token in the databe.
85                                                                 OpenWebAuthToken::create('owt', 0, $token, $contact['addr']);
86
87                                                                 $result = '';
88
89                                                                 // Encrypt the token with the public contacts publik key.
90                                                                 // Only the specific public contact will be able to encrypt it.
91                                                                 // At a later time, we will compare weather the token we're getting
92                                                                 // is really the same token we have stored in the database.
93                                                                 openssl_public_encrypt($token, $result, $contact['pubkey']);
94                                                                 $ret['encrypted_token'] = Strings::base64UrlEncode($result);
95                                                         } else {
96                                                                 Logger::info('OWA fail', ['id' => $contact['id'], 'addr' => $contact['addr'], 'url' => $contact['url']]);
97                                                         }
98                                                 } else {
99                                                         Logger::info('Contact not found', ['handle' => $handle]);
100                                                 }
101                                         }
102                                 }
103                         }
104                 }
105                 System::jsonExit($ret, 'application/x-zot+json');
106         }
107 }