[TASK] Create own response instance in controller actions
[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\Page\PageRenderer;
20 use TYPO3\CMS\Core\SingletonInterface;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * This class adds necessary Javascript code to encrypt fields in a form
25 */
26 class RsaEncryptionEncoder implements SingletonInterface
27 {
28 /**
29 * @var bool
30 */
31 protected $moduleLoaded = false;
32
33 /**
34 * @var PageRenderer
35 */
36 protected $pageRenderer = null;
37
38 /**
39 * This method is called by the hook constructPostProcess
40 */
41 public function enableEncryptionFromBackendControllerPostConstructor()
42 {
43 $this->enableRsaEncryption(true);
44 }
45
46 /**
47 * Load all necessary Javascript files
48 *
49 * @param bool $useRequireJsModule
50 */
51 public function enableRsaEncryption($useRequireJsModule = false)
52 {
53 if ($this->moduleLoaded || !$this->isAvailable()) {
54 return;
55 }
56 $this->moduleLoaded = true;
57 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
58 // Include necessary javascript files
59 if ($useRequireJsModule) {
60 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Rsaauth/RsaEncryptionModule');
61 } else {
62 // Register ajax handler url
63 $code = 'var TYPO3RsaEncryptionPublicKeyUrl = ' . GeneralUtility::quoteJSvalue(GeneralUtility::getIndpEnv('TYPO3_SITE_PATH') . 'index.php?eID=RsaPublicKeyGenerationController') . ';';
64 $pageRenderer->addJsInlineCode('TYPO3RsaEncryptionPublicKeyUrl', $code);
65 if (!$GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['debug']) {
66 $files = ['RsaEncryptionWithLib.min.js'];
67 } else {
68 $files = [
69 'RsaLibrary.js',
70 'RsaEncryption.js',
71 ];
72 }
73 foreach ($files as $file) {
74 $pageRenderer->addJsFile('EXT:rsaauth/Resources/Public/JavaScript/' . $file);
75 }
76 }
77 }
78
79 /**
80 * @return bool
81 */
82 public function isAvailable()
83 {
84 return trim($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['loginSecurityLevel']) === 'rsa';
85 }
86
87 /**
88 * Gets RSA Public Key.
89 *
90 * @return Keypair|null
91 */
92 public function getRsaPublicKey()
93 {
94 $keyPair = null;
95 $backend = Backend\BackendFactory::getBackend();
96 if ($backend !== null) {
97 $keyPair = $backend->createNewKeyPair();
98 $storage = Storage\StorageFactory::getStorage();
99 $storage->put($keyPair->getPrivateKey());
100 session_commit();
101 }
102
103 return $keyPair;
104 }
105
106 /**
107 * Ajax handler to return a RSA public key.
108 *
109 * @return ResponseInterface
110 */
111 public function getRsaPublicKeyAjaxHandler(): ResponseInterface
112 {
113 $keyPair = $this->getRsaPublicKey();
114 if ($keyPair !== null) {
115 return new HtmlResponse(
116 implode('', [
117 'publicKeyModulus' => $keyPair->getPublicKeyModulus(),
118 'spacer' => ':',
119 'exponent' => sprintf('%x', $keyPair->getExponent())
120 ])
121 );
122 }
123 return new HtmlResponse('No OpenSSL backend could be obtained for rsaauth.', 500);
124 }
125 }