Skip to content

Commit

Permalink
Use Bidi for Basic Auth for Firefox 124 (#2093)
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore authored Mar 19, 2024
1 parent ea682d4 commit fe8b503
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/core/engine/command/bidi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
]);
Expand All @@ -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()
]);
Expand Down Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion lib/extensionserver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export class ExtensionServer {
(options.cacheClearRaw ||
options.requestheader ||
options.block ||
options.basicAuth ||
options.clearCacheKeepCookies)
? true
: false;
Expand Down
38 changes: 37 additions & 1 deletion lib/firefox/firefoxBidi.js
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions lib/firefox/webdriver/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fe8b503

Please sign in to comment.