1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 08:13:42 +00:00

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

View File

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

View File

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