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