2019-03-06 22:50:04 +01:00
|
|
|
import { CipherService } from 'jslib/abstractions/cipher.service';
|
|
|
|
import { LockService } from 'jslib/abstractions/lock.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
2018-01-05 22:30:15 +01:00
|
|
|
|
2017-12-07 03:54:38 +01:00
|
|
|
export default class WebRequestBackground {
|
|
|
|
private pendingAuthRequests: any[] = [];
|
|
|
|
private webRequest: any;
|
|
|
|
private isFirefox: boolean;
|
|
|
|
|
2019-03-06 22:50:04 +01:00
|
|
|
constructor(platformUtilsService: PlatformUtilsService, private cipherService: CipherService,
|
|
|
|
private lockService: LockService) {
|
2017-12-07 03:54:38 +01:00
|
|
|
this.webRequest = (window as any).chrome.webRequest;
|
2018-01-05 22:30:15 +01:00
|
|
|
this.isFirefox = platformUtilsService.isFirefox();
|
2017-12-07 03:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pendingAuthRequests.push(details.requestId);
|
|
|
|
|
|
|
|
if (this.isFirefox) {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
2018-03-02 18:04:21 +01:00
|
|
|
await this.resolveAuthCredentials(details.url, resolve, reject);
|
2017-12-07 03:54:38 +01:00
|
|
|
});
|
|
|
|
} else {
|
2018-03-02 18:04:21 +01:00
|
|
|
await this.resolveAuthCredentials(details.url, callback, callback);
|
2017-12-07 03:54:38 +01:00
|
|
|
}
|
|
|
|
}, { 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) {
|
2019-03-06 22:50:04 +01:00
|
|
|
if (await this.lockService.isLocked()) {
|
|
|
|
error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-07 03:54:38 +01:00
|
|
|
try {
|
2018-03-02 18:04:21 +01:00
|
|
|
const ciphers = await this.cipherService.getAllDecryptedForUrl(domain);
|
2017-12-07 03:54:38 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|