1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-19 07:35:48 +02:00
bitwarden-browser/src/services/autofillService.js

461 lines
16 KiB
JavaScript
Raw Normal View History

2017-10-18 03:10:44 +02:00
function AutofillService(utilsService, totpService, tokenService, cipherService, constantsService) {
this.utilsService = utilsService;
this.totpService = totpService;
this.tokenService = tokenService;
2017-10-17 05:11:32 +02:00
this.cipherService = cipherService;
this.constantsService = constantsService;
initAutofill();
2017-07-14 21:34:05 +02:00
}
function initAutofill() {
// Add other languages to this array
var usernameFieldNames = ['username', 'user name', 'email', 'email address', 'e-mail', 'e-mail address',
'userid', 'user id'];
AutofillService.prototype.getFormsWithPasswordFields = function (pageDetails) {
var passwordFields = [],
formData = [];
passwordFields = loadPasswordFields(pageDetails, true);
if (passwordFields.length) {
for (var formKey in pageDetails.forms) {
for (var i = 0; i < passwordFields.length; i++) {
var pf = passwordFields[i];
if (formKey === pf.form) {
2017-01-21 18:38:52 +01:00
var uf = findUsernameField(pageDetails, pf, false, false);
if (!uf) {
// not able to find any viewable username fields. maybe there are some "hidden" ones?
2017-01-21 18:38:52 +01:00
uf = findUsernameField(pageDetails, pf, true, false);
}
formData.push({
form: pageDetails.forms[formKey],
password: pf,
username: uf
});
break;
}
}
}
}
return formData;
};
2017-10-07 19:10:04 +02:00
AutofillService.prototype.doAutoFill = function (options) {
var deferred = Q.defer();
2017-09-22 21:11:30 +02:00
var self = this,
totpPromise = null;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
var tab = null;
if (tabs.length > 0) {
tab = tabs[0];
}
else {
deferred.reject();
return;
}
if (!tab || !options.cipher || !options.pageDetails || !options.pageDetails.length) {
deferred.reject();
return;
}
var didAutofill = false;
2017-10-07 19:10:04 +02:00
for (var i = 0; i < options.pageDetails.length; i++) {
// make sure we're still on correct tab
2017-10-07 19:10:04 +02:00
if (options.pageDetails[i].tab.id !== tab.id || options.pageDetails[i].tab.url !== tab.url) {
continue;
}
var fillScript = generateFillScript(self, options.pageDetails[i].details, {
skipUsernameOnlyFill: options.skipUsernameOnlyFill || false,
cipher: options.cipher
});
if (!fillScript || !fillScript.script || !fillScript.script.length) {
continue;
}
didAutofill = true;
2017-10-07 19:10:04 +02:00
if (!options.skipLastUsed) {
self.cipherService.updateLastUsedDate(options.cipher.id);
}
chrome.tabs.sendMessage(tab.id, {
command: 'fillForm',
fillScript: fillScript
2017-10-07 19:10:04 +02:00
}, { frameId: options.pageDetails[i].frameId });
2017-10-18 03:10:44 +02:00
if (options.cipher.type !== self.constantsService.cipherType.login || totpPromise ||
(options.fromBackground && self.utilsService.isFirefox()) || options.skipTotp ||
!options.cipher.login.totp || !self.tokenService.getPremium()) {
2017-09-22 21:11:30 +02:00
continue;
}
2017-09-22 21:11:30 +02:00
totpPromise = self.totpService.isAutoCopyEnabled().then(function (enabled) {
if (enabled) {
2017-10-06 16:36:45 +02:00
/* jshint ignore:start */
return self.totpService.getCode(options.cipher.login.totp);
2017-10-06 16:36:45 +02:00
/* jshint ignore:end */
}
return null;
}).then(function (code) {
if (code) {
2017-10-06 16:36:45 +02:00
/* jshint ignore:start */
self.utilsService.copyToClipboard(code);
2017-10-06 16:36:45 +02:00
/* jshint ignore:end */
}
return code;
});
}
2017-09-22 21:11:30 +02:00
if (didAutofill) {
if (totpPromise) {
totpPromise.then(function (totpCode) {
deferred.resolve(totpCode);
2017-09-22 21:11:30 +02:00
});
}
else {
deferred.resolve();
}
}
else {
deferred.reject();
}
});
return deferred.promise;
};
AutofillService.prototype.doAutoFillForLastUsedLogin = function (pageDetails, fromCommand) {
var self = this;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
var tab = null;
if (tabs.length > 0) {
tab = tabs[0];
}
if (!tab || !tab.url) {
return;
}
var tabDomain = self.utilsService.getDomain(tab.url);
if (!tabDomain) {
return;
}
2017-10-17 05:11:32 +02:00
self.cipherService.getLastUsedForDomain(tabDomain).then(function (cipher) {
if (!cipher) {
return;
}
2017-10-07 19:10:04 +02:00
self.doAutoFill({
cipher: cipher,
2017-10-07 19:10:04 +02:00
pageDetails: pageDetails,
fromBackground: true,
skipTotp: !fromCommand,
2017-10-07 19:10:04 +02:00
skipLastUsed: true,
skipUsernameOnlyFill: !fromCommand
2017-10-07 19:10:04 +02:00
});
});
});
};
function generateFillScript(self, pageDetails, options) {
if (!pageDetails) {
return null;
}
var fillScript = {
documentUUID: pageDetails.documentUUID,
script: [],
autosubmit: null,
properties: {},
options: {},
metadata: {}
};
switch (options.cipher.type) {
case self.constantsService.cipherType.login:
fillScript = generateLoginFillScript(fillScript, pageDetails, options);
break;
case self.constantsService.cipherType.card:
fillScript = generateLoginFillScript(fillScript, pageDetails, options);
break;
case self.constantsService.cipherType.identity:
fillScript = generateLoginFillScript(fillScript, pageDetails, options);
break;
default:
return null;
}
return fillScript;
}
function generateLoginFillScript(fillScript, pageDetails, options) {
if (!options.cipher.login) {
return null;
}
var passwordFields = [],
passwords = [],
usernames = [],
filledFields = {},
pf = null,
username = null,
i = 0,
fields = options.cipher.fields,
login = options.cipher.login;
if (fields && fields.length) {
var fieldNames = [];
for (i = 0; i < fields.length; i++) {
if (fields[i].name && fields[i].name !== '') {
fieldNames.push(fields[i].name.toLowerCase());
}
else {
fieldNames.push(null);
}
}
for (i = 0; i < pageDetails.fields.length; i++) {
var field = pageDetails.fields[i];
if (filledFields.hasOwnProperty(field.opid) || !field.viewable) {
continue;
}
var matchingIndex = findMatchingFieldIndex(field, fieldNames);
if (matchingIndex > -1) {
filledFields[field.opid] = field;
fillScript.script.push(['click_on_opid', field.opid]);
fillScript.script.push(['fill_by_opid', field.opid, fields[matchingIndex].value]);
}
}
}
if (!login.password || login.password === '') {
// No password for this login. Maybe they just wanted to auto-fill some custom fields?
fillScript = setFillScriptForFocus(filledFields, fillScript);
return fillScript;
}
passwordFields = loadPasswordFields(pageDetails, false);
if (!passwordFields.length) {
// not able to find any viewable password fields. maybe there are some "hidden" ones?
passwordFields = loadPasswordFields(pageDetails, true);
}
for (var formKey in pageDetails.forms) {
var passwordFieldsForForm = [];
for (i = 0; i < passwordFields.length; i++) {
if (formKey === passwordFields[i].form) {
passwordFieldsForForm.push(passwordFields[i]);
}
}
for (i = 0; i < passwordFieldsForForm.length; i++) {
pf = passwordFieldsForForm[i];
passwords.push(pf);
if (login.username) {
username = findUsernameField(pageDetails, pf, false, false);
if (!username) {
// not able to find any viewable username fields. maybe there are some "hidden" ones?
username = findUsernameField(pageDetails, pf, true, false);
}
if (username) {
usernames.push(username);
}
}
}
}
if (passwordFields.length && !passwords.length) {
// The page does not have any forms with password fields. Use the first password field on the page and the
// input field just before it as the username.
pf = passwordFields[0];
passwords.push(pf);
if (login.username && pf.elementNumber > 0) {
username = findUsernameField(pageDetails, pf, false, true);
if (!username) {
// not able to find any viewable username fields. maybe there are some "hidden" ones?
username = findUsernameField(pageDetails, pf, true, true);
}
if (username) {
usernames.push(username);
}
}
}
if (!passwordFields.length && !options.skipUsernameOnlyFill) {
// No password fields on this page. Let's try to just fuzzy fill the username.
for (i = 0; i < pageDetails.fields.length; i++) {
var f = pageDetails.fields[i];
if (f.viewable && (f.type === 'text' || f.type === 'email' || f.type === 'tel') &&
fieldIsFuzzyMatch(f, usernameFieldNames)) {
usernames.push(f);
}
}
}
for (i = 0; i < usernames.length; i++) {
if (filledFields.hasOwnProperty(usernames[i].opid)) {
continue;
}
filledFields[usernames[i].opid] = usernames[i];
fillScript.script.push(['click_on_opid', usernames[i].opid]);
fillScript.script.push(['fill_by_opid', usernames[i].opid, login.username]);
}
for (i = 0; i < passwords.length; i++) {
if (filledFields.hasOwnProperty(passwords[i].opid)) {
continue;
}
filledFields[passwords[i].opid] = passwords[i];
fillScript.script.push(['click_on_opid', passwords[i].opid]);
fillScript.script.push(['fill_by_opid', passwords[i].opid, login.password]);
}
fillScript = setFillScriptForFocus(filledFields, fillScript);
return fillScript;
}
function generateCardFillScript(fillScript, pageDetails, options) {
if (!options.cipher.card) {
return null;
}
var card = options.cipher.card;
}
function generateIdentityFillScript(fillScript, pageDetails, options) {
if (!options.cipher.identity) {
return null;
}
var id = options.cipher.identity;
}
function loadPasswordFields(pageDetails, canBeHidden) {
var arr = [];
for (var i = 0; i < pageDetails.fields.length; i++) {
if (pageDetails.fields[i].type === 'password' && (canBeHidden || pageDetails.fields[i].viewable)) {
arr.push(pageDetails.fields[i]);
}
}
return arr;
}
2017-01-21 18:38:52 +01:00
function findUsernameField(pageDetails, passwordField, canBeHidden, withoutForm) {
var usernameField = null;
for (var i = 0; i < pageDetails.fields.length; i++) {
var f = pageDetails.fields[i];
2017-01-21 18:38:52 +01:00
if (f.elementNumber >= passwordField.elementNumber) {
break;
}
2017-01-21 18:38:52 +01:00
if ((withoutForm || f.form === passwordField.form) && (canBeHidden || f.viewable) &&
(f.type === 'text' || f.type === 'email' || f.type === 'tel')) {
usernameField = f;
if (findMatchingFieldIndex(f, usernameFieldNames) > -1) {
// We found an exact match. No need to keep looking.
break;
}
}
}
return usernameField;
}
function findMatchingFieldIndex(field, names) {
var matchingIndex = -1;
if (field.htmlID && field.htmlID !== '') {
matchingIndex = names.indexOf(field.htmlID.toLowerCase());
}
if (matchingIndex < 0 && field.htmlName && field.htmlName !== '') {
matchingIndex = names.indexOf(field.htmlName.toLowerCase());
}
if (matchingIndex < 0 && field['label-tag'] && field['label-tag'] !== '') {
matchingIndex = names.indexOf(field['label-tag'].replace(/(?:\r\n|\r|\n)/g, '').trim().toLowerCase());
}
if (matchingIndex < 0 && field.placeholder && field.placeholder !== '') {
matchingIndex = names.indexOf(field.placeholder.toLowerCase());
}
return matchingIndex;
}
function fieldIsFuzzyMatch(field, names) {
if (field.htmlID && field.htmlID !== '' && fuzzyMatch(names, field.htmlID.toLowerCase())) {
return true;
}
if (field.htmlName && field.htmlName !== '' && fuzzyMatch(names, field.htmlName.toLowerCase())) {
return true;
}
if (field['label-tag'] && field['label-tag'] !== '' &&
fuzzyMatch(names, field['label-tag'].replace(/(?:\r\n|\r|\n)/g, '').trim().toLowerCase())) {
return true;
}
if (field.placeholder && field.placeholder !== '' && fuzzyMatch(names, field.placeholder.toLowerCase())) {
return true;
}
return false;
}
function fuzzyMatch(options, value) {
if (!options || !options.length || !value || value === '') {
return false;
}
for (var i = 0; i < options.length; i++) {
if (value.indexOf(options[i]) > -1) {
return true;
}
}
return false;
}
2017-09-29 21:12:23 +02:00
function setFillScriptForFocus(filledFields, fillScript) {
var lastField = null,
lastPasswordField = null;
for (var opid in filledFields) {
if (filledFields.hasOwnProperty(opid) && filledFields[opid].viewable) {
lastField = filledFields[opid];
if (filledFields[opid].type === 'password') {
lastPasswordField = filledFields[opid];
}
}
}
// Prioritize password field over others.
if (lastPasswordField) {
fillScript.script.push(['focus_by_opid', lastPasswordField.opid]);
}
else if (lastField) {
fillScript.script.push(['focus_by_opid', lastField.opid]);
}
return fillScript;
}
2017-07-14 21:34:05 +02:00
}