Skip to content

Commit

Permalink
fix: disabling of authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade committed Apr 4, 2024
1 parent 4ec0280 commit 4f81c94
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
12 changes: 10 additions & 2 deletions packages/puppeteer-core/src/api/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type {HTTPResponse} from '../api/HTTPResponse.js';
import type {Accessibility} from '../cdp/Accessibility.js';
import type {Coverage} from '../cdp/Coverage.js';
import type {DeviceRequestPrompt} from '../cdp/DeviceRequestPrompt.js';
import type {Credentials, NetworkConditions} from '../cdp/NetworkManager.js';
import type {NetworkConditions} from '../cdp/NetworkManager.js';
import type {Tracing} from '../cdp/Tracing.js';
import type {ConsoleMessage} from '../common/ConsoleMessage.js';
import type {
Expand Down Expand Up @@ -512,6 +512,14 @@ export interface PageEvents extends Record<EventType, unknown> {
[PageEvent.WorkerDestroyed]: WebWorker;
}

/**
* @public
*/
export interface Credentials {
username: string;
password: string;
}

/**
* @public
*/
Expand Down Expand Up @@ -1417,7 +1425,7 @@ export abstract class Page extends EventEmitter<PageEvents> {
* @remarks
* To disable authentication, pass `null`.
*/
abstract authenticate(credentials: Credentials): Promise<void>;
abstract authenticate(credentials: Credentials | null): Promise<void>;

/**
* The extra HTTP headers will be sent with every request the page initiates.
Expand Down
13 changes: 3 additions & 10 deletions packages/puppeteer-core/src/cdp/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {Protocol} from 'devtools-protocol';

import {CDPSessionEvent, type CDPSession} from '../api/CDPSession.js';
import type {Frame} from '../api/Frame.js';
import type {Credentials} from '../api/Page.js';
import {EventEmitter, EventSubscription} from '../common/EventEmitter.js';
import {
NetworkManagerEvent,
Expand All @@ -24,14 +25,6 @@ import {
type FetchRequestId,
} from './NetworkEventManager.js';

/**
* @public
*/
export interface Credentials {
username: string;
password: string;
}

/**
* @public
*/
Expand Down Expand Up @@ -72,7 +65,7 @@ export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
#frameManager: FrameProvider;
#networkEventManager = new NetworkEventManager();
#extraHTTPHeaders?: Record<string, string>;
#credentials?: Credentials;
#credentials: Credentials | null = null;
#attemptedAuthentications = new Set<string>();
#userRequestInterceptionEnabled = false;
#protocolRequestInterceptionEnabled = false;
Expand Down Expand Up @@ -135,7 +128,7 @@ export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
this.#clients.delete(client);
}

async authenticate(credentials?: Credentials): Promise<void> {
async authenticate(credentials: Credentials | null): Promise<void> {
this.#credentials = credentials;
const enabled = this.#userRequestInterceptionEnabled || !!this.#credentials;
if (enabled === this.#protocolRequestInterceptionEnabled) {
Expand Down
5 changes: 3 additions & 2 deletions packages/puppeteer-core/src/cdp/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {Frame, WaitForOptions} from '../api/Frame.js';
import type {HTTPRequest} from '../api/HTTPRequest.js';
import type {HTTPResponse} from '../api/HTTPResponse.js';
import type {JSHandle} from '../api/JSHandle.js';
import type {Credentials} from '../api/Page.js';
import {
Page,
PageEvent,
Expand Down Expand Up @@ -71,7 +72,7 @@ import {FrameManagerEvent} from './FrameManagerEvents.js';
import {CdpKeyboard, CdpMouse, CdpTouchscreen} from './Input.js';
import {MAIN_WORLD} from './IsolatedWorlds.js';
import {releaseObject} from './JSHandle.js';
import type {Credentials, NetworkConditions} from './NetworkManager.js';
import type {NetworkConditions} from './NetworkManager.js';
import type {CdpTarget} from './Target.js';
import type {TargetManager} from './TargetManager.js';
import {TargetManagerEvent} from './TargetManager.js';
Expand Down Expand Up @@ -735,7 +736,7 @@ export class CdpPage extends Page {
this.#bindings.delete(name);
}

override async authenticate(credentials: Credentials): Promise<void> {
override async authenticate(credentials: Credentials | null): Promise<void> {
return await this.#frameManager.networkManager.authenticate(credentials);
}

Expand Down
5 changes: 1 addition & 4 deletions test/src/network.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,10 +759,7 @@ describe('network', function () {
});
let response = (await page.goto(server.EMPTY_PAGE))!;
expect(response.status()).toBe(200);
await page.authenticate({
username: '',
password: '',
});
await page.authenticate(null);
// Navigate to a different origin to bust Chrome's credential caching.
try {
response = (await page.goto(
Expand Down

0 comments on commit 4f81c94

Please sign in to comment.