3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module;
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;
34 * OpenWebAuth verifier and token generator
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.
41 * This token may be exchanged for an authenticated cookie.
43 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Owa.php
45 class Owa extends BaseModule
47 protected function rawContent(array $request = [])
49 $ret = [ 'success' => false ];
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];
58 $sigblock = HTTPSignature::parseSigheader($_SERVER[$head]);
60 $keyId = $sigblock['keyId'];
63 // Try to find the public contact entry of the handle.
64 $handle = str_replace('acct:', '', $keyId);
66 $cid = Contact::getIdForURL($handle);
67 $fields = ['id', 'url', 'addr', 'pubkey'];
68 $condition = ['id' => $cid];
70 $contact = DBA::selectFirst('contact', $fields, $condition);
72 if (DBA::isResult($contact)) {
73 // Try to verify the signed header with the public key of the contact record
75 $verified = HTTPSignature::verifyMagic($contact['pubkey']);
77 if ($verified && $verified['header_signed'] && $verified['header_valid']) {
78 Logger::debug('OWA header', ['addr' => $contact['addr'], 'data' => $verified]);
80 $ret['success'] = true;
81 $token = Strings::getRandomHex(32);
83 // Store the generated token in the database.
84 OpenWebAuthToken::create('owt', 0, $token, $contact['addr']);
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);
95 Logger::info('OWA fail', ['id' => $contact['id'], 'addr' => $contact['addr'], 'url' => $contact['url']]);
98 Logger::info('Contact not found', ['handle' => $handle]);
104 System::jsonExit($ret, 'application/x-zot+json');