mirror of
https://github.com/bitwarden/browser.git
synced 2025-03-12 13:39:14 +01:00
* create domain settings state provider * replace callsites for defaultUriMatch and neverDomains with DomainSettingsService equivalents * replace callsites for equivalentDomains with DomainSettingsService equivalents and clean up unused AccountSettingsSettings * add migrations for domain settings state * do not use enum for URI match strategy constants and types * add getUrlEquivalentDomains test * PR suggestions/cleanup * refactor getUrlEquivalentDomains to return an observable Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: ✨ Audrey ✨ <ajensen@bitwarden.com> * update tests * add UriMatchStrategy docs notes * service class renames * use service abstraction at callsites previously using service class directly --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: ✨ Audrey ✨ <ajensen@bitwarden.com>
101 lines
3.0 KiB
TypeScript
101 lines
3.0 KiB
TypeScript
import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
|
|
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
|
|
import { UriMatchStrategy } from "@bitwarden/common/models/domain/domain-service";
|
|
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
|
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
|
|
|
import { BrowserApi } from "../../platform/browser/browser-api";
|
|
|
|
export default class WebRequestBackground {
|
|
private pendingAuthRequests: any[] = [];
|
|
private webRequest: any;
|
|
private isFirefox: boolean;
|
|
|
|
constructor(
|
|
platformUtilsService: PlatformUtilsService,
|
|
private cipherService: CipherService,
|
|
private authService: AuthService,
|
|
) {
|
|
if (BrowserApi.isManifestVersion(2)) {
|
|
this.webRequest = (window as any).chrome.webRequest;
|
|
}
|
|
this.isFirefox = platformUtilsService.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;
|
|
}
|
|
|
|
this.pendingAuthRequests.push(details.requestId);
|
|
|
|
if (this.isFirefox) {
|
|
// eslint-disable-next-line
|
|
return new Promise(async (resolve, reject) => {
|
|
await this.resolveAuthCredentials(details.url, resolve, reject);
|
|
});
|
|
} else {
|
|
await this.resolveAuthCredentials(details.url, 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://*/*"],
|
|
},
|
|
);
|
|
}
|
|
|
|
// eslint-disable-next-line
|
|
private async resolveAuthCredentials(domain: string, success: Function, error: Function) {
|
|
if ((await this.authService.getAuthStatus()) < AuthenticationStatus.Unlocked) {
|
|
error();
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const ciphers = await this.cipherService.getAllDecryptedForUrl(
|
|
domain,
|
|
null,
|
|
UriMatchStrategy.Host,
|
|
);
|
|
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);
|
|
}
|
|
}
|
|
}
|