]> git.mxchange.org Git - friendica.git/blob - src/Module/Owa.php
7243c111344b4d159e5fa6d5a86b701936ecd241
[friendica.git] / src / Module / Owa.php
1 <?php
2 /**
3  * @file src/Module/Owa.php
4  */
5 namespace Friendica\Module;
6
7 use Friendica\BaseModule;
8 use Friendica\Core\Logger;
9 use Friendica\Core\System;
10 use Friendica\Database\DBA;
11 use Friendica\Model\Contact;
12 use Friendica\Model\OpenWebAuthToken;
13 use Friendica\Util\HTTPSignature;
14 use Friendica\Util\Strings;
15
16 /**
17  * @brief OpenWebAuth verifier and token generator
18  *
19  * See https://macgirvin.com/wiki/mike/OpenWebAuth/Home
20  * Requests to this endpoint should be signed using HTTP Signatures
21  * using the 'Authorization: Signature' authentication method
22  * If the signature verifies a token is returned.
23  *
24  * This token may be exchanged for an authenticated cookie.
25  *
26  * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Module/Owa.php
27  */
28 class Owa extends BaseModule
29 {
30         public static function init()
31         {
32
33                 $ret = [ 'success' => false ];
34
35                 foreach (['REDIRECT_REMOTE_USER', 'HTTP_AUTHORIZATION'] as $head) {
36                         if (array_key_exists($head, $_SERVER) && substr(trim($_SERVER[$head]), 0, 9) === 'Signature') {
37                                 if ($head !== 'HTTP_AUTHORIZATION') {
38                                         $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER[$head];
39                                         continue;
40                                 }
41
42                                 $sigblock = HTTPSignature::parseSigheader($_SERVER[$head]);
43                                 if ($sigblock) {
44                                         $keyId = $sigblock['keyId'];
45
46                                         if ($keyId) {
47                                                 // Try to find the public contact entry of the handle.
48                                                 $handle = str_replace('acct:', '', $keyId);
49
50                                                 $cid       = Contact::getIdForURL($handle);
51                                                 $fields    = ['id', 'url', 'addr', 'pubkey'];
52                                                 $condition = ['id' => $cid];
53
54                                                 $contact = DBA::selectFirst('contact', $fields, $condition);
55
56                                                 if (DBA::isResult($contact)) {
57                                                         // Try to verify the signed header with the public key of the contact record
58                                                         // we have found.
59                                                         $verified = HTTPSignature::verifyMagic($contact['pubkey']);
60
61                                                         if ($verified && $verified['header_signed'] && $verified['header_valid']) {
62                                                                 Logger::log('OWA header: ' . print_r($verified, true), Logger::DATA);
63                                                                 Logger::log('OWA success: ' . $contact['addr'], Logger::DATA);
64
65                                                                 $ret['success'] = true;
66                                                                 $token = Strings::getRandomHex(32);
67
68                                                                 // Store the generated token in the databe.
69                                                                 OpenWebAuthToken::create('owt', 0, $token, $contact['addr']);
70
71                                                                 $result = '';
72
73                                                                 // Encrypt the token with the public contacts publik key.
74                                                                 // Only the specific public contact will be able to encrypt it.
75                                                                 // At a later time, we will compare weather the token we're getting
76                                                                 // is really the same token we have stored in the database.
77                                                                 openssl_public_encrypt($token, $result, $contact['pubkey']);
78                                                                 $ret['encrypted_token'] = Strings::base64UrlEncode($result);
79                                                         } else {
80                                                                 Logger::log('OWA fail: ' . $contact['id'] . ' ' . $contact['addr'] . ' ' . $contact['url'], Logger::DEBUG);
81                                                         }
82                                                 } else {
83                                                         Logger::log('Contact not found: ' . $handle, Logger::DEBUG);
84                                                 }
85                                         }
86                                 }
87                         }
88                 }
89                 System::jsonExit($ret, 'application/x-zot+json');
90         }
91 }