[!!!][FEATURE] Introduce PSR-7-based Routing for Backend AJAX Requests
[Packages/TYPO3.CMS.git] / typo3 / sysext / rsaauth / Classes / RsaEncryptionEncoder.php
1 <?php
2 namespace TYPO3\CMS\Rsaauth;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Page\PageRenderer;
20 use TYPO3\CMS\Core\SingletonInterface;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
23
24 /**
25 * This class adds necessary Javascript code to encrypt fields in a form
26 */
27 class RsaEncryptionEncoder implements SingletonInterface {
28
29 /**
30 * @var bool
31 */
32 protected $moduleLoaded = FALSE;
33
34 /**
35 * @var PageRenderer
36 */
37 protected $pageRenderer = NULL;
38
39 /**
40 * Load all necessary Javascript files
41 *
42 * @param bool $useRequireJsModule
43 */
44 public function enableRsaEncryption($useRequireJsModule = FALSE) {
45 if ($this->moduleLoaded || !$this->isAvailable()) {
46 return;
47 }
48 $this->moduleLoaded = TRUE;
49 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
50 // Include necessary javascript files
51 if ($useRequireJsModule) {
52 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Rsaauth/RsaEncryptionModule');
53 } else {
54 // Register ajax handler url
55 $code = 'var TYPO3RsaEncryptionPublicKeyUrl = ' . GeneralUtility::quoteJSvalue(GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . 'index.php?eID=RsaPublicKeyGenerationController') . ';';
56 $pageRenderer->addJsInlineCode('TYPO3RsaEncryptionPublicKeyUrl', $code);
57 $javascriptPath = ExtensionManagementUtility::siteRelPath('rsaauth') . 'Resources/Public/JavaScript/';
58 if (!$GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['debug']) {
59 $files = array('RsaEncryptionWithLib.min.js');
60 } else {
61 $files = array(
62 'RsaLibrary.js',
63 'RsaEncryption.js',
64 );
65 }
66 foreach ($files as $file) {
67 $pageRenderer->addJsFile($javascriptPath . $file);
68 }
69 }
70 }
71
72 /**
73 * @return bool
74 */
75 public function isAvailable() {
76 return trim($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['loginSecurityLevel']) === 'rsa';
77 }
78
79 /**
80 * Gets RSA Public Key.
81 *
82 * @return Keypair|NULL
83 */
84 public function getRsaPublicKey() {
85 $keyPair = NULL;
86 $backend = Backend\BackendFactory::getBackend();
87 if ($backend !== NULL) {
88 $keyPair = $backend->createNewKeyPair();
89 $storage = Storage\StorageFactory::getStorage();
90 $storage->put($keyPair->getPrivateKey());
91 session_commit();
92 }
93
94 return $keyPair;
95 }
96
97 /**
98 * Ajax handler to return a RSA public key.
99 *
100 * @param ServerRequestInterface $request
101 * @param ResponseInterface $response
102 * @return ResponseInterface
103 */
104 public function getRsaPublicKeyAjaxHandler(ServerRequestInterface $request, ResponseInterface $response) {
105 $keyPair = $this->getRsaPublicKey();
106 if ($keyPair !== NULL) {
107 $response->getBody()->write(implode('', [
108 'publicKeyModulus' => $keyPair->getPublicKeyModulus(),
109 'spacer' => ':',
110 'exponent' => sprintf('%x', $keyPair->getExponent())
111 ]));
112 $response = $response->withHeader('Content-Type', 'text/html; charset=utf-8');
113 } else {
114 $response->getBody()->write('No OpenSSL backend could be obtained for rsaauth.');
115 $response = $response->withStatus(500);
116 }
117 return $response;
118 }
119
120 }