3 * @copyright Copyright (C) 2020, Friendica
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 public static function init(array $parameters = [])
50 $ret = [ 'success' => false ];
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];
59 $sigblock = HTTPSignature::parseSigheader($_SERVER[$head]);
61 $keyId = $sigblock['keyId'];
64 // Try to find the public contact entry of the handle.
65 $handle = str_replace('acct:', '', $keyId);
67 $cid = Contact::getIdForURL($handle);
68 $fields = ['id', 'url', 'addr', 'pubkey'];
69 $condition = ['id' => $cid];
71 $contact = DBA::selectFirst('contact', $fields, $condition);
73 if (DBA::isResult($contact)) {
74 // Try to verify the signed header with the public key of the contact record
76 $verified = HTTPSignature::verifyMagic($contact['pubkey']);
78 if ($verified && $verified['header_signed'] && $verified['header_valid']) {
79 Logger::log('OWA header: ' . print_r($verified, true), Logger::DATA);
80 Logger::log('OWA success: ' . $contact['addr'], Logger::DATA);
82 $ret['success'] = true;
83 $token = Strings::getRandomHex(32);
85 // Store the generated token in the databe.
86 OpenWebAuthToken::create('owt', 0, $token, $contact['addr']);
90 // Encrypt the token with the public contacts publik key.
91 // Only the specific public contact will be able to encrypt it.
92 // At a later time, we will compare weather the token we're getting
93 // is really the same token we have stored in the database.
94 openssl_public_encrypt($token, $result, $contact['pubkey']);
95 $ret['encrypted_token'] = Strings::base64UrlEncode($result);
97 Logger::log('OWA fail: ' . $contact['id'] . ' ' . $contact['addr'] . ' ' . $contact['url'], Logger::DEBUG);
100 Logger::log('Contact not found: ' . $handle, Logger::DEBUG);
106 System::jsonExit($ret, 'application/x-zot+json');