[TASK] Extract request processing from RsaEncryptionEncoder
[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 TYPO3\CMS\Core\Http\HtmlResponse;
19 use TYPO3\CMS\Core\Http\Response;
20 use TYPO3\CMS\Core\Page\PageRenderer;
21 use TYPO3\CMS\Core\SingletonInterface;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
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 * This method is called by the hook constructPostProcess
41 */
42 public function enableEncryptionFromBackendControllerPostConstructor()
43 {
44 $this->enableRsaEncryption(true);
45 }
46
47 /**
48 * Load all necessary Javascript files
49 *
50 * @param bool $useRequireJsModule
51 */
52 public function enableRsaEncryption($useRequireJsModule = false)
53 {
54 if ($this->moduleLoaded || !$this->isAvailable()) {
55 return;
56 }
57 $this->moduleLoaded = true;
58 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
59 // Include necessary javascript files
60 if ($useRequireJsModule) {
61 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Rsaauth/RsaEncryptionModule');
62 } else {
63 // Register ajax handler url
64 $code = 'var TYPO3RsaEncryptionPublicKeyUrl = ' . GeneralUtility::quoteJSvalue(GeneralUtility::getIndpEnv('TYPO3_SITE_PATH') . 'index.php?eID=RsaPublicKeyGenerationController') . ';';
65 $pageRenderer->addJsInlineCode('TYPO3RsaEncryptionPublicKeyUrl', $code);
66 if (!$GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['debug']) {
67 $files = ['RsaEncryptionWithLib.min.js'];
68 } else {
69 $files = [
70 'RsaLibrary.js',
71 'RsaEncryption.js',
72 ];
73 }
74 foreach ($files as $file) {
75 $pageRenderer->addJsFile('EXT:rsaauth/Resources/Public/JavaScript/' . $file);
76 }
77 }
78 }
79
80 /**
81 * @return bool
82 */
83 public function isAvailable()
84 {
85 return trim($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['loginSecurityLevel']) === 'rsa';
86 }
87
88 /**
89 * Gets RSA Public Key.
90 *
91 * @return Keypair|null
92 *
93 * @deprecated since TYPO3 v9. Will be removed in v10.
94 */
95 public function getRsaPublicKey()
96 {
97 trigger_error('Method getRsaPublicKey() will be removed in v10.', E_USER_DEPRECATED);
98
99 $keyPair = null;
100 $backend = Backend\BackendFactory::getBackend();
101 if ($backend !== null) {
102 $keyPair = $backend->createNewKeyPair();
103 $storage = Storage\StorageFactory::getStorage();
104 $storage->put($keyPair->getPrivateKey());
105 session_commit();
106 }
107
108 return $keyPair;
109 }
110
111 /**
112 * Ajax handler to return a RSA public key.
113 *
114 * @return ResponseInterface
115 *
116 * @deprecated since TYPO3 v9. Will be removed in v10.
117 */
118 public function getRsaPublicKeyAjaxHandler(): ResponseInterface
119 {
120 trigger_error('Method getRsaPublicKeyAjaxHandler() will be removed in v10.', E_USER_DEPRECATED);
121
122 $keyPair = $this->getRsaPublicKey();
123 if ($keyPair !== null) {
124 return new HtmlResponse(
125 implode('', [
126 'publicKeyModulus' => $keyPair->getPublicKeyModulus(),
127 'spacer' => ':',
128 'exponent' => sprintf('%x', $keyPair->getExponent())
129 ])
130 );
131 }
132
133 $response = new Response('php://temp', 500, ['Content-Type' => 'application/json; charset=utf-8']);
134 $response->getBody()->write('No OpenSSL backend could be obtained for rsaauth.');
135 return $response;
136 }
137 }