2022-06-14 17:10:53 +02:00
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
2023-02-06 22:53:37 +01:00
|
|
|
import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
|
|
|
|
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { UriMatchType } from "@bitwarden/common/enums/uriMatchType";
|
2023-01-31 22:08:37 +01:00
|
|
|
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
2020-09-21 16:21:09 +02:00
|
|
|
|
2022-10-19 15:55:57 +02:00
|
|
|
import { BrowserApi } from "../browser/browserApi";
|
|
|
|
|
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,
|
2022-05-01 23:57:40 +02:00
|
|
|
private authService: AuthService
|
2020-04-06 17:40:16 +02:00
|
|
|
) {
|
2022-10-19 15:55:57 +02:00
|
|
|
if (BrowserApi.manifestVersion === 2) {
|
2022-08-10 03:30:26 +02:00
|
|
|
this.webRequest = (window as any).chrome.webRequest;
|
|
|
|
}
|
2018-01-05 22:30:15 +01:00
|
|
|
this.isFirefox = platformUtilsService.isFirefox();
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 03:54:38 +01:00
|
|
|
async init() {
|
|
|
|
if (!this.webRequest || !this.webRequest.onAuthRequired) {
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
2017-12-07 03:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.webRequest.onAuthRequired.addListener(
|
|
|
|
async (details: any, callback: any) => {
|
|
|
|
if (!details.url || this.pendingAuthRequests.indexOf(details.requestId) !== -1) {
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2017-12-07 03:54:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pendingAuthRequests.push(details.requestId);
|
|
|
|
|
|
|
|
if (this.isFirefox) {
|
2022-02-24 18:14:04 +01:00
|
|
|
// eslint-disable-next-line
|
2017-12-07 03:54:38 +01:00
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
await this.resolveAuthCredentials(details.url, resolve, reject);
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
} else {
|
2018-03-02 18:04:21 +01:00
|
|
|
await this.resolveAuthCredentials(details.url, callback, callback);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
},
|
2017-12-07 03:54:38 +01:00
|
|
|
{ urls: ["http://*/*", "https://*/*"] },
|
|
|
|
[this.isFirefox ? "blocking" : "asyncBlocking"]
|
2021-12-21 15:43:35 +01:00
|
|
|
);
|
2017-12-07 03:54:38 +01:00
|
|
|
|
2018-03-02 18:04:21 +01:00
|
|
|
this.webRequest.onCompleted.addListener((details: any) => this.completeAuthRequest(details), {
|
|
|
|
urls: ["http://*/*"],
|
2017-12-07 03:54:38 +01:00
|
|
|
});
|
2018-03-02 18:04:21 +01:00
|
|
|
this.webRequest.onErrorOccurred.addListener(
|
|
|
|
(details: any) => this.completeAuthRequest(details),
|
2017-12-07 03:54:38 +01:00
|
|
|
{
|
|
|
|
urls: ["http://*/*"],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-24 18:14:04 +01:00
|
|
|
// eslint-disable-next-line
|
2017-12-07 03:54:38 +01:00
|
|
|
private async resolveAuthCredentials(domain: string, success: Function, error: Function) {
|
2022-05-01 23:57:40 +02:00
|
|
|
if ((await this.authService.getAuthStatus()) < AuthenticationStatus.Unlocked) {
|
2017-12-07 03:54:38 +01:00
|
|
|
error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2017-12-07 03:54:38 +01:00
|
|
|
|
|
|
|
private completeAuthRequest(details: any) {
|
|
|
|
const i = this.pendingAuthRequests.indexOf(details.requestId);
|
|
|
|
if (i > -1) {
|
|
|
|
this.pendingAuthRequests.splice(i, 1);
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2017-12-07 03:54:38 +01:00
|
|
|
}
|