7d4d30994d02fd3897a1e916e7ca8fe8b011d4ea
[Packages/TYPO3.CMS.git] / typo3 / sysext / rsaauth / Resources / Public / JavaScript / RsaEncryptionModule.js
1 /*
2 * This file is part of the TYPO3 CMS project.
3 *
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
7 *
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 /**
15 * Module: TYPO3/CMS/Rsaauth/RsaEncryptionModule
16 * Object that handles RSA encryption and submission of the form
17 */
18 define(['jquery', './RsaLibrary'], function($) {
19 'use strict';
20
21 /**
22 *
23 * @type {{$currentForm: null, fetchedRsaKey: boolean, initialize: Function, handleFormSubmitRequest: Function, handlePublicKeyResponse: Function}}
24 * @exports TYPO3/CMS/Rsaauth/RsaEncryptionModule
25 */
26 var RsaEncryption = {
27
28 /**
29 * Remember the form which was submitted
30 */
31 $currentForm: null,
32
33 /**
34 * Remember if we fetched the RSA key already
35 */
36 fetchedRsaKey: false,
37
38 /**
39 * Replace event handler of submit button
40 */
41 initialize: function() {
42 $(':input[data-rsa-encryption]').closest('form').each(function() {
43 var $this = $(this);
44
45 // Store the original submit handler that is executed later
46 $this.data('original-onsubmit', $this.attr('onsubmit'));
47
48 // Remove the original submit handler and register RsaEncryption.handleFormSubmitRequest instead
49 $this.removeAttr('onsubmit').on('submit', RsaEncryption.handleFormSubmitRequest);
50
51 // Bind submit event first (this is a dirty hack with jquery internals, but there is no way around that)
52 var handlers = $._data(this, 'events').submit;
53 var handler = handlers.pop();
54 handlers.unshift(handler);
55 });
56 rng_seed_time();
57 },
58
59 /**
60 * Fetches a new public key by Ajax and encrypts the password for transmission
61 *
62 * @param {Event} event
63 */
64 handleFormSubmitRequest: function(event) {
65 if (!RsaEncryption.fetchedRsaKey) {
66 event.stopImmediatePropagation();
67
68 RsaEncryption.fetchedRsaKey = true;
69 RsaEncryption.$currentForm = $(this);
70
71 $.ajax({
72 url: TYPO3.settings.ajaxUrls['rsa_publickey'],
73 data: {'skipSessionUpdate': 1},
74 success: RsaEncryption.handlePublicKeyResponse
75 });
76
77 return false;
78 } else {
79 // we come here again when the submit is triggered below
80 // reset the variable to fetch a new key for next attempt
81 RsaEncryption.fetchedRsaKey = false;
82 }
83 },
84
85 /**
86 * Parses the Json response and triggers submission of the form
87 *
88 * @param {Object} response Ajax response object
89 */
90 handlePublicKeyResponse: function(response) {
91 var publicKey = response.split(':');
92 if (!publicKey[0] || !publicKey[1]) {
93 alert('No public key could be generated. Please inform your TYPO3 administrator to check the OpenSSL settings.');
94 return;
95 }
96
97 var rsa = new RSAKey();
98 rsa.setPublic(publicKey[0], publicKey[1]);
99 RsaEncryption.$currentForm.find(':input[data-rsa-encryption]').each(function() {
100 var $this = $(this);
101 var encryptedValue = rsa.encrypt($this.val());
102 var dataAttribute = $this.data('rsa-encryption');
103 var rsaValue = 'rsa:' + hex2b64(encryptedValue);
104
105 if (!dataAttribute) {
106 $this.val(rsaValue);
107 } else {
108 var $typo3Field = $('#' + dataAttribute);
109 $typo3Field.val(rsaValue);
110 // Reset user password field to prevent it from being submitted
111 $this.val('');
112 }
113 });
114
115 // Try to fetch the field which submitted the form
116 var $currentField = RsaEncryption.$currentForm.find('input[type=submit]:focus,input[type=image]:focus');
117 if ($currentField.length === 1) {
118 $currentField.trigger('click');
119 } else {
120 // Create a hidden input field to fake pressing the submit button
121 RsaEncryption.$currentForm.append('<input type="hidden" name="commandLI" value="Submit">');
122
123 // Restore the original submit handler
124 var originalOnSubmit = RsaEncryption.$currentForm.data('original-onsubmit');
125 if (typeof originalOnSubmit === 'string' && originalOnSubmit.length > 0) {
126 RsaEncryption.$currentForm.attr('onsubmit', originalOnSubmit);
127 RsaEncryption.$currentForm.removeData('original-onsubmit');
128 }
129
130 // Submit the form
131 RsaEncryption.$currentForm.trigger('submit');
132 }
133 }
134 };
135
136 $(RsaEncryption.initialize);
137
138 return RsaEncryption;
139 });