From fe8b5030f1d8f9d84694667070102b9a3e610ec2 Mon Sep 17 00:00:00 2001 From: Peter Hedenskog Date: Tue, 19 Mar 2024 13:17:44 +0100 Subject: [PATCH] Use Bidi for Basic Auth for Firefox 124 (#2093) --- lib/core/engine/command/bidi.js | 8 +++---- lib/extensionserver/index.js | 1 - lib/firefox/firefoxBidi.js | 38 +++++++++++++++++++++++++++++++- lib/firefox/webdriver/firefox.js | 4 ++++ 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/lib/core/engine/command/bidi.js b/lib/core/engine/command/bidi.js index 2298073ad2..97e776edc2 100644 --- a/lib/core/engine/command/bidi.js +++ b/lib/core/engine/command/bidi.js @@ -32,7 +32,7 @@ export class Bidi { */ async onMessage(f) { if (this.browserName === 'firefox') { - const client = this.engineDelegate.getBidi(); + const client = await this.engineDelegate.getBidi(); const ws = await client.socket; ws.on('message', f); } else { @@ -65,7 +65,7 @@ export class Bidi { */ async subscribe(messageType) { if (this.browserName === 'firefox') { - const client = this.engineDelegate.getBidi(); + const client = await this.engineDelegate.getBidi(); return client.subscribe(messageType, [ await this.engineDelegate.getWindowHandle() ]); @@ -85,7 +85,7 @@ export class Bidi { */ async unsubscribe(messageType) { if (this.browserName === 'firefox') { - const client = this.engineDelegate.getBidi(); + const client = await this.engineDelegate.getBidi(); return client.unsubscribe(messageType, [ this.engineDelegate.getWindowHandle() ]); @@ -113,7 +113,7 @@ export class Bidi { async send(parameters) { if (this.browserName === 'firefox') { try { - const client = this.engineDelegate.getBidi(); + const client = await this.engineDelegate.getBidi(); return client.send(parameters); } catch (error) { log.error( diff --git a/lib/extensionserver/index.js b/lib/extensionserver/index.js index df4d8f6d37..d1fb13c464 100644 --- a/lib/extensionserver/index.js +++ b/lib/extensionserver/index.js @@ -18,7 +18,6 @@ export class ExtensionServer { (options.cacheClearRaw || options.requestheader || options.block || - options.basicAuth || options.clearCacheKeepCookies) ? true : false; diff --git a/lib/firefox/firefoxBidi.js b/lib/firefox/firefoxBidi.js index 8bba9df659..a24f13814b 100644 --- a/lib/firefox/firefoxBidi.js +++ b/lib/firefox/firefoxBidi.js @@ -1,11 +1,13 @@ +import { Bidi } from '../core/engine/command/bidi.js'; import intel from 'intel'; import { toArray } from '../support/util.js'; const log = intel.getLogger('browsertime.firefox.bidi'); export class FirefoxBidi { - constructor(bidi, browsingContextId, options) { + constructor(bidi, browsingContextId, driver, options) { this.options = options; this.bidi = bidi; + this.driver = driver; this.browsingContextId = browsingContextId; } @@ -24,6 +26,40 @@ export class FirefoxBidi { } } + async setBasicAuth(basicAuth) { + const parts = basicAuth.split('@'); + const bidi = new Bidi(this.driver, this.options.browser); + + const command = { + method: 'network.addIntercept', + params: { + phases: ['authRequired'] + } + }; + await bidi.send(command); + + await bidi.subscribe('network.authRequired'); + + await bidi.onMessage(async function (event) { + const parsedEvent = JSON.parse(Buffer.from(event.toString())); + if (parsedEvent.method === 'network.authRequired') { + const continueWithAuth = { + method: 'network.continueWithAuth', + params: { + request: parsedEvent.params.request.request, + action: 'provideCredentials', + credentials: { + type: 'password', + username: parts[0], + password: parts[1] + } + } + }; + await bidi.send(continueWithAuth); + } + }); + } + async setCookie(url, cookie) { const cookies = toArray(cookie); for (let cookieParts of cookies) { diff --git a/lib/firefox/webdriver/firefox.js b/lib/firefox/webdriver/firefox.js index e3228bee20..a5f54ca176 100644 --- a/lib/firefox/webdriver/firefox.js +++ b/lib/firefox/webdriver/firefox.js @@ -93,6 +93,10 @@ export class Firefox { await this.browsertimeBidi.injectJavaScript(this.options.injectJs); } + if (this.options.basicAuth) { + await this.browsertimeBidi.setBasicAuth(this.options.basicAuth); + } + if ( this.firefoxConfig.appendToUserAgent || this.options.appendToUserAgent