-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathInFormManager.js
284 lines (261 loc) · 11.3 KB
/
InFormManager.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* Passbolt ~ Open source password manager for teams
* Copyright (c) 2021 Passbolt SA (https://www.passbolt.com)
*
* Licensed under GNU Affero General Public License version 3 of the or any later version.
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) 2021 Passbolt SA (https://www.passbolt.com)
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
* @link https://www.passbolt.com Passbolt(tm)
* @since 3.3.0
*/
import InFormCallToActionField from "./InFormCallToActionField";
import InFormFieldSelector from "./InFormFieldSelector";
import InFormMenuField from "./InformMenuField";
import {fireEvent} from "@testing-library/dom/dist/events";
import InFormCredentialsFormField from "./InFormCredentialsFormField";
/**
* Manages the in-form web integration including call-to-action and menu
*/
class InFormManager {
/**
* Default constructor
*/
constructor() {
/** In-form username and password callToActionFields in the target page*/
this.callToActionFields = [];
/** In-form menu menuField in the target page*/
this.menuField = null;
/** In-form form fields in the target page*/
this.credentialsFormFields = [];
/** Mutation observer to detect any change on the DOM */
this.mutationObserver = null;
this.bindCallbacks();
}
/**
* Initializes the in-form manager
*/
initialize() {
this.findAndSetAuthenticationFields();
this.handleDomChange();
this.handleInformCallToActionRepositionEvent();
this.handlePortDisconnectEvent();
this.handleInFormMenuInsertionEvent();
this.handleInFormMenuRemoveEvent();
this.handleInformCallToActionClickEvent();
this.handleGetLastCallToActionClickedInput();
this.handleGetCurrentCredentials();
this.handleFillCredentials();
this.handleFillPassword();
}
/**
* Binds the callbacks
*/
bindCallbacks() {
this.findAndSetAuthenticationFields = this.findAndSetAuthenticationFields.bind(this);
this.handleInformCallToActionClickEvent = this.handleInformCallToActionClickEvent.bind(this);
this.clean = this.clean.bind(this);
this.destroy = this.destroy.bind(this);
}
/**
* Find authentication fields in the document and set them as object properties
*/
findAndSetAuthenticationFields() {
this.findAndSetUsernameAndPasswordFields();
this.findAndSetCredentialsFormFields();
}
/**
* Find authentication callToActionFields in the document and set them as object properties
*/
findAndSetUsernameAndPasswordFields() {
/* We find the username / passwords DOM callToActionFields.
* If it was previously found, we reuse the same InformUsernameField, otherwise we create one
* Else we clean and reset callToActionFields
*/
const newUsernameFields = InFormCallToActionField.findAll(InFormFieldSelector.USERNAME_FIELD_SELECTOR);
const newPasswordFields = InFormCallToActionField.findAll(InFormFieldSelector.PASSWORD_FIELD_SELECTOR);
const newFields = newUsernameFields.concat(newPasswordFields);
if (newFields.length > 0) {
this.callToActionFields = newFields.map(newField => {
const matchField = fieldToMatch => callToActionField => callToActionField.field === fieldToMatch;
const existingField = this.callToActionFields.find(matchField(newField));
const fieldType = newField.matches(InFormFieldSelector.USERNAME_FIELD_SELECTOR) ? 'username' : 'password';
return existingField || new InFormCallToActionField(newField, fieldType);
});
} else {
this.clean();
this.callToActionFields = [];
}
}
/**
* Find authentication formFields in the document and set them as object properties
*/
findAndSetCredentialsFormFields() {
/* We find the form DOM formFields.
* If it was previously found, we reuse the same InformFormField, otherwise we create one
*/
const newCredentialsFormFields = InFormCredentialsFormField.findAll();
this.credentialsFormFields = newCredentialsFormFields.map(newField => {
const matchField = fieldToMatch => credentialsFormField => credentialsFormField.field === fieldToMatch;
const existingField = this.credentialsFormFields.find(matchField(newField));
const usernameField = this.callToActionFields.find(callToActionField => callToActionField.fieldType === 'username' && newField.contains(callToActionField.field));
const passwordField = this.callToActionFields.find(callToActionField => callToActionField.fieldType === 'password' && newField.contains(callToActionField.field));
return existingField || new InFormCredentialsFormField(newField, usernameField?.field, passwordField?.field);
});
}
/**
* Clean the DOM of in-form entities
*/
clean() {
this.callToActionFields.forEach(field => field.removeCallToActionIframe());
this.menuField?.removeMenuIframe();
}
/**
* Whenever the DOM changes
*/
handleDomChange() {
const updateAuthenticationFields = (mutationsList) => {
// Check if a child node has been added or removed with type
const mutationChildNodes = mutation => mutation.type === 'childList';
// Check if the mutation is an iframe added or removed by us
const isMutationInformIframe = mutation => mutationChildNodes(mutation) && this.isInformIframe(mutation);
// Check if our iframe is in the mutation list
const hasNotMutationFromInformIframe = !mutationsList.some(isMutationInformIframe);
if (hasNotMutationFromInformIframe) {
this.findAndSetAuthenticationFields();
this.handleInformCallToActionClickEvent();
}
}
// Search again for authentication callToActionFields to attach when the DOM changes
this.mutationObserver = new MutationObserver(updateAuthenticationFields);
this.mutationObserver.observe(document.body, { attributes: true, childList: true, subtree: true });
}
/**
* Check if the mutation is an iframe added or removed by us
* @param mutation
* @returns {boolean}
*/
isInformIframe(mutation) {
const nodeList = mutation.addedNodes.length > 0 ? mutation.addedNodes : mutation.removedNodes;
let isInformIframe = false;
if (this.callToActionFields.length > 0) {
const isIdPresent = iframe => Array.prototype.some.call(nodeList, node => iframe.iframeId === node.id);
isInformIframe = this.callToActionFields.some(isIdPresent)
}
if (!isInformIframe && this.menuField) {
isInformIframe = Array.prototype.some.call(nodeList, node => this.menuField.iframeId === node.id);
}
return isInformIframe;
}
/**
* Whenever the username / password callToActionFields change its position, reposition the call-to-action
*/
handleInformCallToActionRepositionEvent() {
window.addEventListener('resize', this.clean);
}
/**
* Whenever the user clicks on the in-form call-to-action, it inserts the in-form menu iframe
*/
handleInFormMenuInsertionEvent() {
port.on('passbolt.in-form-menu.open', () => {
this.menuField = new InFormMenuField(this.lastCallToActionFieldClicked.field);
});
}
/**
* Whenever the user clicks on the in-form menu, it removes the in-form menu iframe
*/
handleInFormMenuRemoveEvent() {
port.on('passbolt.in-form-menu.close', () => {
this.menuField.removeMenuIframe();
});
}
/**
* Handle the click on the in-form call-to-action (iframe)
*/
handleInformCallToActionClickEvent() {
const setLastCallToActionFieldClicked = callToActionField => callToActionField.onClick(() => {this.lastCallToActionFieldClicked = callToActionField});
this.callToActionFields.forEach(setLastCallToActionFieldClicked);
}
/** Whenever one requires to get the type and value of the input attached to the last call-to-action performed */
handleGetLastCallToActionClickedInput() {
port.on('passbolt.web-integration.last-performed-call-to-action-input', (requestId) => {
port.emit(requestId, 'SUCCESS', {type: this.lastCallToActionFieldClicked.fieldType, value: this.lastCallToActionFieldClicked.field.value})
});
}
/** Whenever one requires to get the current credentials */
handleGetCurrentCredentials() {
port.on('passbolt.web-integration.get-credentials', (requestId) => {
const currentFieldType = this.lastCallToActionFieldClicked?.fieldType;
const isUsernameType = currentFieldType === 'username';
const isPasswordType = currentFieldType === 'password';
let username = null;
let password = null;
if (!isUsernameType) {
username = this.callToActionFields.find(field => field.fieldType === 'username')?.field.value || "";
password = this.lastCallToActionFieldClicked?.field.value;
}
if (!isPasswordType) {
username = this.lastCallToActionFieldClicked?.field.value;
password = this.callToActionFields.find(field => field.fieldType === 'password')?.field.value || "";
}
port.emit(requestId, 'SUCCESS', {username, password});
});
}
/**
* Whenever one requests to fill the current page form with given credentials
*/
handleFillCredentials() {
port.on('passbolt.web-integration.fill-credentials', ({username, password}) => {
const currentFieldType = this.lastCallToActionFieldClicked?.fieldType;
const isUsernameType = currentFieldType === 'username';
const isPasswordType = currentFieldType === 'password';
if (!isUsernameType) {
const usernameField = this.callToActionFields.find(field => field.fieldType === 'username')?.field;
fireEvent.input( this.lastCallToActionFieldClicked.field, { target: { value: password } });
if (usernameField) {
fireEvent.input(usernameField, { target: { value: username } });
}
}
if (!isPasswordType) {
const passwordField = this.callToActionFields.find(field => field.fieldType === 'password')?.field;
fireEvent.input( this.lastCallToActionFieldClicked.field, { target: { value: username } });
if (passwordField) {
fireEvent.input(passwordField, { target: { value: password } });
}
}
});
}
/**
* Whenever one requests to fill the current page form with a password
*/
handleFillPassword() {
port.on('passbolt.web-integration.fill-password', password => {
this.callToActionFields
.filter(callToActionField => callToActionField.fieldType === 'password')
.forEach(callToActionField => fireEvent.input(callToActionField.field, { target: { value: password }}));
this.menuField.removeMenuIframe();
// Listen the auto-save on the appropriate form field
const formField = this.credentialsFormFields.find(formField => formField.field.contains(this.lastCallToActionFieldClicked.field));
formField?.handleAutoSaveEvent();
});
}
/**
* Remove all event, observer and iframe
*/
destroy() {
this.mutationObserver.disconnect();
this.callToActionFields.forEach(field => field.destroy());
this.menuField?.destroy();
this.credentialsFormFields.forEach(field => field.destroy());
window.removeEventListener('resize', this.clean);
}
/**
* Whenever the port is disconnected due to an update of the extension
*/
handlePortDisconnectEvent() {
port._port.onDisconnect.addListener(this.destroy)
}
}
export default new InFormManager();