1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 08:13:42 +00:00

web request background. fixes to setIcon.

This commit is contained in:
Kyle Spearrin
2017-12-06 21:54:38 -05:00
parent 985c02c5e1
commit c2e5945be5
2 changed files with 105 additions and 81 deletions

View File

@@ -0,0 +1,77 @@
import CipherService from '../services/cipher.service';
import UtilsService from '../services/utils.service';
export default class WebRequestBackground {
private pendingAuthRequests: any[] = [];
private webRequest: any;
private isFirefox: boolean;
constructor(utilsService: UtilsService, private cipherService: CipherService) {
this.webRequest = (window as any).chrome.webRequest;
this.isFirefox = utilsService.isFirefox();
}
async init() {
if (!this.webRequest || !this.webRequest.onAuthRequired) {
return;
}
this.webRequest.onAuthRequired.addListener(async (details: any, callback: any) => {
if (!details.url || this.pendingAuthRequests.indexOf(details.requestId) !== -1) {
if (callback) {
callback();
}
return;
}
const domain = UtilsService.getDomain(details.url);
if (domain == null) {
if (callback) {
callback();
}
return;
}
this.pendingAuthRequests.push(details.requestId);
if (this.isFirefox) {
return new Promise(async (resolve, reject) => {
await this.resolveAuthCredentials(domain, resolve, reject);
});
} else {
await this.resolveAuthCredentials(domain, callback, callback);
}
}, { urls: ['http://*/*', 'https://*/*'] }, [this.isFirefox ? 'blocking' : 'asyncBlocking']);
this.webRequest.onCompleted.addListener(
(details: any) => this.completeAuthRequest(details), { urls: ['http://*/*'] });
this.webRequest.onErrorOccurred.addListener(
(details: any) => this.completeAuthRequest(details), { urls: ['http://*/*'] });
}
private async resolveAuthCredentials(domain: string, success: Function, error: Function) {
try {
const ciphers = await this.cipherService.getAllDecryptedForDomain(domain);
if (ciphers == null || ciphers.length !== 1) {
error();
return;
}
success({
authCredentials: {
username: ciphers[0].login.username,
password: ciphers[0].login.password,
},
});
} catch {
error();
}
}
private completeAuthRequest(details: any) {
const i = this.pendingAuthRequests.indexOf(details.requestId);
if (i > -1) {
this.pendingAuthRequests.splice(i, 1);
}
}
}