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