-
Notifications
You must be signed in to change notification settings - Fork 638
/
RsaEncryptionEncoder.php
137 lines (124 loc) · 4.2 KB
/
RsaEncryptionEncoder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace TYPO3\CMS\Rsaauth;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* This class adds necessary Javascript code to encrypt fields in a form
*/
class RsaEncryptionEncoder implements SingletonInterface
{
/**
* @var bool
*/
protected $moduleLoaded = false;
/**
* @var PageRenderer
*/
protected $pageRenderer = null;
/**
* This method is called by the hook constructPostProcess
*/
public function enableEncryptionFromBackendControllerPostConstructor()
{
$this->enableRsaEncryption(true);
}
/**
* Load all necessary Javascript files
*
* @param bool $useRequireJsModule
*/
public function enableRsaEncryption($useRequireJsModule = false)
{
if ($this->moduleLoaded || !$this->isAvailable()) {
return;
}
$this->moduleLoaded = true;
$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
// Include necessary javascript files
if ($useRequireJsModule) {
$pageRenderer->loadRequireJsModule('TYPO3/CMS/Rsaauth/RsaEncryptionModule');
} else {
// Register ajax handler url
$code = 'var TYPO3RsaEncryptionPublicKeyUrl = ' . GeneralUtility::quoteJSvalue(GeneralUtility::getIndpEnv('TYPO3_SITE_PATH') . 'index.php?eID=RsaPublicKeyGenerationController') . ';';
$pageRenderer->addJsInlineCode('TYPO3RsaEncryptionPublicKeyUrl', $code);
if (!$GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['debug']) {
$files = ['RsaEncryptionWithLib.min.js'];
} else {
$files = [
'RsaLibrary.js',
'RsaEncryption.js',
];
}
foreach ($files as $file) {
$pageRenderer->addJsFile('EXT:rsaauth/Resources/Public/JavaScript/' . $file);
}
}
}
/**
* @return bool
*/
public function isAvailable()
{
return trim($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['loginSecurityLevel']) === 'rsa';
}
/**
* Gets RSA Public Key.
*
* @return Keypair|null
*
* @deprecated since TYPO3 v9. Will be removed in v10.
*/
public function getRsaPublicKey()
{
trigger_error('Method getRsaPublicKey() will be removed in v10.', E_USER_DEPRECATED);
$keyPair = null;
$backend = Backend\BackendFactory::getBackend();
if ($backend !== null) {
$keyPair = $backend->createNewKeyPair();
$storage = Storage\StorageFactory::getStorage();
$storage->put($keyPair->getPrivateKey());
session_commit();
}
return $keyPair;
}
/**
* Ajax handler to return a RSA public key.
*
* @return ResponseInterface
*
* @deprecated since TYPO3 v9. Will be removed in v10.
*/
public function getRsaPublicKeyAjaxHandler(): ResponseInterface
{
trigger_error('Method getRsaPublicKeyAjaxHandler() will be removed in v10.', E_USER_DEPRECATED);
$keyPair = $this->getRsaPublicKey();
if ($keyPair !== null) {
return new HtmlResponse(
implode('', [
'publicKeyModulus' => $keyPair->getPublicKeyModulus(),
'spacer' => ':',
'exponent' => sprintf('%x', $keyPair->getExponent())
])
);
}
$response = new Response('php://temp', 500, ['Content-Type' => 'application/json; charset=utf-8']);
$response->getBody()->write('No OpenSSL backend could be obtained for rsaauth.');
return $response;
}
}