1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-10 14:43:34 +01:00

auto-fill basic auth prompts

This commit is contained in:
Kyle Spearrin 2017-09-07 23:26:56 -04:00
parent 01e48377e9
commit 41c32f3874
4 changed files with 90 additions and 7 deletions

View File

@ -2,7 +2,8 @@ var isBackground = true,
loginToAutoFill = null, loginToAutoFill = null,
pageDetailsToAutoFill = [], pageDetailsToAutoFill = [],
autofillTimeout = null, autofillTimeout = null,
menuOptionsLoaded = []; menuOptionsLoaded = [],
pendingAuthRequests = [];
var bg_loginsToAdd = []; var bg_loginsToAdd = [];
@ -209,6 +210,72 @@ chrome.contextMenus.onClicked.addListener(function (info, tab) {
} }
}); });
chrome.webRequest.onAuthRequired.addListener(function (details, callback) {
if (!details.url || pendingAuthRequests.indexOf(details.requestId) != -1) {
if (callback) {
callback();
}
return;
}
var domain = bg_utilsService.getDomain(details.url);
if (!domain) {
if (callback) {
callback();
}
return;
}
pendingAuthRequests.push(details.requestId);
if (bg_utilsService.isFirefox()) {
return new Promise(function (resolve, reject) {
bg_loginService.getAllDecryptedForDomain(domain).then(function (logins) {
if (!logins || logins.length !== 1) {
reject();
return;
}
resolve({
authCredentials: {
username: logins[0].username,
password: logins[0].password
}
});
}, function () {
reject();
});
});
}
else {
bg_loginService.getAllDecryptedForDomain(domain).then(function (logins) {
if (!logins || logins.length !== 1) {
callback();
return;
}
callback({
authCredentials: {
username: logins[0].username,
password: logins[0].password
}
});
}, function () {
callback();
});
}
}, { urls: ['http://*/*'] }, [bg_utilsService.isFirefox() ? 'blocking' : 'asyncBlocking']);
chrome.webRequest.onCompleted.addListener(completeAuthRequest, { urls: ['http://*/*'] });
chrome.webRequest.onErrorOccurred.addListener(completeAuthRequest, { urls: ['http://*/*'] });
function completeAuthRequest(details) {
var i = pendingAuthRequests.indexOf(details.requestId);
if (i > -1) {
pendingAuthRequests.splice(i, 1);
}
}
var buildingContextMenu = false; var buildingContextMenu = false;
function buildContextMenu(callback) { function buildContextMenu(callback) {
if (buildingContextMenu) { if (buildingContextMenu) {

View File

@ -72,7 +72,9 @@
"clipboardWrite", "clipboardWrite",
"idle", "idle",
"http://*/*", "http://*/*",
"https://*/*" "https://*/*",
"webRequest",
"webRequestBlocking"
], ],
"commands": { "commands": {
"autofill_login": { "autofill_login": {

View File

@ -228,13 +228,12 @@ function initAutofill() {
return; return;
} }
self.loginService.getAllDecryptedForDomain(tabDomain).then(function (logins) { self.loginService.getLastUsedForDomain(tabDomain).then(function (login) {
if (!logins.length) { if (!login) {
return; return;
} }
var sortedLogins = logins.sort(self.loginService.sortLoginsByLastUsed); self.doAutoFill(login, pageDetails, true, true, true);
self.doAutoFill(sortedLogins[0], pageDetails, true, true, true);
}); });
}); });
}; };

View File

@ -193,6 +193,21 @@ function initLoginService() {
}); });
}; };
LoginService.prototype.getLastUsedForDomain = function (domain) {
var self = this;
var deferred = Q.defer();
self.getAllDecryptedForDomain(domain).then(function (logins) {
if (!logins.length) {
deferred.reject();
return;
}
var sortedLogins = logins.sort(self.sortLoginsByLastUsed);
deferred.resolve(sortedLogins[0]);
});
return deferred.promise;
};
LoginService.prototype.saveWithServer = function (login) { LoginService.prototype.saveWithServer = function (login) {
var deferred = Q.defer(); var deferred = Q.defer();