2021-06-07 19:25:37 +02:00
|
|
|
import { CipherService } from 'jslib-common/abstractions/cipher.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
|
|
|
|
import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.service';
|
2018-01-05 22:30:15 +01:00
|
|
|
|
2021-09-17 15:44:27 +02:00
|
|
|
import { UriMatchType } from 'jslib-common/enums/uriMatchType';
|
2020-09-21 16:21:09 +02: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,
|
2020-04-06 17:40:16 +02:00
|
|
|
private vaultTimeoutService: VaultTimeoutService) {
|
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) {
|
2020-04-06 17:40:16 +02:00
|
|
|
if (await this.vaultTimeoutService.isLocked()) {
|
2019-03-06 22:50:04 +01:00
|
|
|
error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-07 03:54:38 +01:00
|
|
|
try {
|
2020-09-21 16:21:09 +02:00
|
|
|
const ciphers = await this.cipherService.getAllDecryptedForUrl(domain, null, UriMatchType.Host);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|