]> git.mxchange.org Git - friendica.git/blob - src/Module/PublicRSAKey.php
Merge pull request #8265 from nupplaphil/task/add_license
[friendica.git] / src / Module / PublicRSAKey.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use ASN_BASE;
25 use Friendica\BaseModule;
26 use Friendica\DI;
27 use Friendica\Model\User;
28 use Friendica\Network\HTTPException\BadRequestException;
29
30 /**
31  * prints the public RSA key of a user
32  */
33 class PublicRSAKey extends BaseModule
34 {
35         public static function rawContent(array $parameters = [])
36         {
37                 $app = DI::app();
38
39                 // @TODO: Replace with parameter from router
40                 if ($app->argc !== 2) {
41                         throw new BadRequestException();
42                 }
43
44                 // @TODO: Replace with parameter from router
45                 $nick = $app->argv[1];
46
47                 $user = User::getByNickname($nick, ['spubkey']);
48                 if (empty($user) || empty($user['spubkey'])) {
49                         throw new BadRequestException();
50                 }
51
52                 $lines = explode("\n", $user['spubkey']);
53                 unset($lines[0]);
54                 unset($lines[count($lines)]);
55
56                 $asnString = base64_decode(implode('', $lines));
57                 $asnBase = ASN_BASE::parseASNString($asnString);
58
59                 $m = $asnBase[0]->asnData[1]->asnData[0]->asnData[0]->asnData;
60                 $e = $asnBase[0]->asnData[1]->asnData[0]->asnData[1]->asnData;
61
62                 header('Content-type: application/magic-public-key');
63                 echo 'RSA' . '.' . $m . '.' . $e;
64
65                 exit();
66         }
67 }