[TASK] Re-work/simplify copyright header in PHP files - Part 9
[Packages/TYPO3.CMS.git] / typo3 / sysext / rsaauth / Classes / Hook / UserSetupHook.php
1 <?php
2 namespace TYPO3\CMS\Rsaauth\Hook;
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 * This class provides a hook to the login form to add extra javascript code
18 * and supply a proper form tag.
19 *
20 * @author Helmut Hummel <helmut.hummel@typo3.org>
21 */
22 class UserSetupHook {
23
24 /**
25 * Decrypt the password fields if they are filled.
26 *
27 * @param array $parameters Parameters to the script
28 * @return void
29 */
30 public function decryptPassword(array $parameters) {
31 if ($this->isRsaAvailable()) {
32 $be_user_data = &$parameters['be_user_data'];
33 if (substr($be_user_data['password'], 0, 4) === 'rsa:' && substr($be_user_data['password2'], 0, 4) === 'rsa:') {
34 $backend = \TYPO3\CMS\Rsaauth\Backend\BackendFactory::getBackend();
35 /** @var $storage \TYPO3\CMS\Rsaauth\Storage\AbstractStorage */
36 $storage = \TYPO3\CMS\Rsaauth\Storage\StorageFactory::getStorage();
37 $key = $storage->get();
38 $password = $backend->decrypt($key, substr($be_user_data['password'], 4));
39 $password2 = $backend->decrypt($key, substr($be_user_data['password2'], 4));
40 $be_user_data['password'] = $password ?: $be_user_data['password'];
41 $be_user_data['password2'] = $password2 ?: $be_user_data['password2'];
42 }
43 }
44 }
45
46 /**
47 * Provides form code and javascript for the user setup.
48 *
49 * @param array $parameters Parameters to the script
50 * @param \TYPO3\CMS\Setup\Controller\SetupModuleController $userSetupObject Calling object: user setup module
51 * @return string The code for the user setup
52 */
53 public function getLoginScripts(array $parameters, \TYPO3\CMS\Setup\Controller\SetupModuleController $userSetupObject) {
54 $content = '';
55 if ($this->isRsaAvailable()) {
56 // If we can get the backend, we can proceed
57 $backend = \TYPO3\CMS\Rsaauth\Backend\BackendFactory::getBackend();
58 $javascriptPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::siteRelPath('rsaauth') . 'resources/';
59 $files = array(
60 'jsbn/jsbn.js',
61 'jsbn/prng4.js',
62 'jsbn/rng.js',
63 'jsbn/rsa.js',
64 'jsbn/base64.js',
65 'rsaauth_min.js'
66 );
67 $content = '';
68 foreach ($files as $file) {
69 $content .= '<script type="text/javascript" src="' . \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . $javascriptPath . $file . '"></script>';
70 }
71 // Generate a new key pair
72 $keyPair = $backend->createNewKeyPair();
73 // Save private key
74 $storage = \TYPO3\CMS\Rsaauth\Storage\StorageFactory::getStorage();
75 /** @var $storage \TYPO3\CMS\Rsaauth\Storage\AbstractStorage */
76 $storage->put($keyPair->getPrivateKey());
77 // Add form tag
78 $form = '<form action="' . \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleUrl('user_setup') . '" method="post" name="usersetup" enctype="application/x-www-form-urlencoded" onsubmit="tx_rsaauth_encryptUserSetup();">';
79 // Add RSA hidden fields
80 $form .= '<input type="hidden" id="rsa_n" name="n" value="' . htmlspecialchars($keyPair->getPublicKeyModulus()) . '" />';
81 $form .= '<input type="hidden" id="rsa_e" name="e" value="' . sprintf('%x', $keyPair->getExponent()) . '" />';
82 $userSetupObject->doc->form = $form;
83 }
84 return $content;
85 }
86
87 /**
88 * Rsa is available if loginSecurityLevel is set and rsa backend is working.
89 *
90 * @return boolean
91 */
92 protected function isRsaAvailable() {
93 return trim($GLOBALS['TYPO3_CONF_VARS']['BE']['loginSecurityLevel']) === 'rsa' && \TYPO3\CMS\Rsaauth\Backend\BackendFactory::getBackend() !== NULL;
94 }
95
96 }