Skip to content

Commit

Permalink
Use Bidi for Basic Auth for Firefox (124 or 125?).
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore committed Mar 11, 2024
1 parent 6b40b7c commit 4b3536b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
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.cookie ||
options.clearCacheKeepCookies)
? true
Expand Down
38 changes: 37 additions & 1 deletion lib/firefox/firefoxBidi.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Bidi } from '../core/engine/command/bidi.js';
import intel from 'intel';
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 @@ -22,4 +24,38 @@ export class FirefoxBidi {
log.error('Could not inject JavaScript:' + error);
}
}

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);
}
});
}
}
5 changes: 5 additions & 0 deletions lib/firefox/webdriver/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class Firefox {
this.bidi = new FirefoxBidi(
await runner.getDriver().getBidi(),
this.windowId,
runner.getDriver(),
this.options
);
}
Expand All @@ -90,6 +91,10 @@ export class Firefox {
await this.bidi.injectJavaScript(this.options.injectJs);
}

if (this.options.basicAuth) {
await this.bidi.setBasicAuth(this.options.basicAuth);
}

if (
this.firefoxConfig.appendToUserAgent ||
this.options.appendToUserAgent
Expand Down

0 comments on commit 4b3536b

Please sign in to comment.