[PM-6144] Basic auth autofill in Manifest v3 (#8975)

* Add Support for autofilling Basic Auth to MV3

* Remove `any`
This commit is contained in:
Justin Baur 2024-04-30 12:35:36 -04:00 committed by GitHub
parent be50a174de
commit 3acbffa072
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 29 deletions

View File

@ -4,40 +4,29 @@ 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 pendingAuthRequests: Set<string> = new Set<string>([]);
private isFirefox: boolean;
constructor(
platformUtilsService: PlatformUtilsService,
private cipherService: CipherService,
private authService: AuthService,
private readonly webRequest: typeof chrome.webRequest,
) {
if (BrowserApi.isManifestVersion(2)) {
this.webRequest = chrome.webRequest;
}
this.isFirefox = platformUtilsService.isFirefox();
}
async init() {
if (!this.webRequest || !this.webRequest.onAuthRequired) {
return;
}
startListening() {
this.webRequest.onAuthRequired.addListener(
async (details: any, callback: any) => {
if (!details.url || this.pendingAuthRequests.indexOf(details.requestId) !== -1) {
async (details, callback) => {
if (!details.url || this.pendingAuthRequests.has(details.requestId)) {
if (callback) {
callback();
callback(null);
}
return;
}
this.pendingAuthRequests.push(details.requestId);
this.pendingAuthRequests.add(details.requestId);
if (this.isFirefox) {
// eslint-disable-next-line
return new Promise(async (resolve, reject) => {
@ -51,7 +40,7 @@ export default class WebRequestBackground {
[this.isFirefox ? "blocking" : "asyncBlocking"],
);
this.webRequest.onCompleted.addListener((details: any) => this.completeAuthRequest(details), {
this.webRequest.onCompleted.addListener((details) => this.completeAuthRequest(details), {
urls: ["http://*/*"],
});
this.webRequest.onErrorOccurred.addListener(
@ -91,10 +80,7 @@ export default class WebRequestBackground {
}
}
private completeAuthRequest(details: any) {
const i = this.pendingAuthRequests.indexOf(details.requestId);
if (i > -1) {
this.pendingAuthRequests.splice(i, 1);
}
private completeAuthRequest(details: chrome.webRequest.WebResponseCacheDetails) {
this.pendingAuthRequests.delete(details.requestId);
}
}

View File

@ -1056,11 +1056,12 @@ export default class MainBackground {
this.cipherService,
);
if (BrowserApi.isManifestVersion(2)) {
if (chrome.webRequest != null && chrome.webRequest.onAuthRequired != null) {
this.webRequestBackground = new WebRequestBackground(
this.platformUtilsService,
this.cipherService,
this.authService,
chrome.webRequest,
);
}
}
@ -1106,9 +1107,7 @@ export default class MainBackground {
await this.tabsBackground.init();
this.contextMenusBackground?.init();
await this.idleBackground.init();
if (BrowserApi.isManifestVersion(2)) {
await this.webRequestBackground.init();
}
this.webRequestBackground?.startListening();
if (this.platformUtilsService.isFirefox() && !this.isPrivateMode) {
// Set Private Mode windows to the default icon - they do not share state with the background page

View File

@ -60,7 +60,9 @@
"clipboardWrite",
"idle",
"scripting",
"offscreen"
"offscreen",
"webRequest",
"webRequestAuthProvider"
],
"optional_permissions": ["nativeMessaging", "privacy"],
"host_permissions": ["<all_urls>"],