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";
|
[AC-1266] Enums filename conventions (#5140)
* refactor: update clientType enum
* refactor: update deviceType filename
* refactor: update encryptedExportType filename
* refactor: update encryptionType filename
* refactor: update eventType filename
* refactor: update fieldType filename
* refactor: update fileUploadType filename
* refactor: update hashPurpose filename
* refactor: update htmlStorageLocation filename
* refactor: update kdfType filename
* refactor: update keySuffixOptions filename
* refactor: update linkedIdType filename
* refactor: update logLevelType filename
* refactor: update nativeMessagingVersion filename
* refactor: update notificationType filename
* refactor: update productType filename
* refactor: update secureNoteType filename
* refactor: update stateVersion filename
* refactor: update storageLocation filename
* refactor: update themeType filename
* refactor: update uriMatchType filename
* fix: update kdfType classes missed in initial pass, refs AC-1266
* fix: missing import update for device-type
* refactor: add barrel file for enums and update pathed import statements, refs AC-1266
* fix: incorrect import statements for web, refs AC-1266
* fix: missed import statement updates (browser), refs AC-1266
* fix: missed import statement changes (cli), refs AC-1266
* fix: missed import statement changes (desktop), refs AC-1266
* fix: prettier, refs AC-1266
* refactor: (libs) update relative paths to use barrel file, refs AC-1266
* fix: missed find/replace import statements for SecureNoteType, refs AC-1266
* refactor: apply .enum suffix to enums folder and modify leftover relative paths, refs AC-1266
* fix: find/replace errors for native-messaging-version, refs AC-1266
2023-04-05 05:42:21 +02:00
|
|
|
import { UriMatchType } from "@bitwarden/common/enums";
|
2023-06-06 22:34:53 +02:00
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
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
|
|
|
|
2023-06-06 22:34:53 +02:00
|
|
|
import { BrowserApi } from "../platform/browser/browser-api";
|
2022-10-19 15:55:57 +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,
|
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
|
|
|
}
|