mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore(webdriver): support for Page.authenticate (#12217)
This commit is contained in:
parent
50b66591e7
commit
a2f3415df2
@ -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 {
|
||||
@ -131,6 +131,14 @@ export interface Metrics {
|
||||
JSHeapTotalSize?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export interface Credentials {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
|
@ -115,7 +115,6 @@ export class BidiFrame extends Frame {
|
||||
this.browsingContext.on('request', ({request}) => {
|
||||
const httpRequest = BidiHTTPRequest.from(request, this);
|
||||
request.once('success', () => {
|
||||
// SAFETY: BidiHTTPRequest will create this before here.
|
||||
this.page().trustedEmitter.emit(PageEvent.RequestFinished, httpRequest);
|
||||
});
|
||||
|
||||
|
@ -72,6 +72,7 @@ export class BidiHTTPRequest extends HTTPRequest {
|
||||
this.#request.once('success', data => {
|
||||
this.#response = BidiHTTPResponse.from(data, this);
|
||||
});
|
||||
this.#request.on('authenticate', this.#handleAuthentication);
|
||||
|
||||
this.#frame?.page().trustedEmitter.emit(PageEvent.Request, this);
|
||||
}
|
||||
@ -232,6 +233,29 @@ export class BidiHTTPRequest extends HTTPRequest {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
#authenticationHandled = false;
|
||||
#handleAuthentication = async () => {
|
||||
if (!this.#frame) {
|
||||
return;
|
||||
}
|
||||
const credentials = this.#frame.page()._credentials;
|
||||
if (credentials && !this.#authenticationHandled) {
|
||||
this.#authenticationHandled = true;
|
||||
void this.#request.continueWithAuth({
|
||||
action: 'provideCredentials',
|
||||
credentials: {
|
||||
type: 'password',
|
||||
username: credentials.username,
|
||||
password: credentials.password,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
void this.#request.continueWithAuth({
|
||||
action: 'cancel',
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function getBidiHeaders(rawHeaders?: Record<string, unknown>) {
|
||||
|
@ -13,6 +13,7 @@ import type {BoundingBox} from '../api/ElementHandle.js';
|
||||
import type {WaitForOptions} from '../api/Frame.js';
|
||||
import type {HTTPResponse} from '../api/HTTPResponse.js';
|
||||
import type {
|
||||
Credentials,
|
||||
GeolocationOptions,
|
||||
MediaFeature,
|
||||
PageEvents,
|
||||
@ -514,10 +515,7 @@ export class BidiPage extends Page {
|
||||
override async setRequestInterception(enable: boolean): Promise<void> {
|
||||
if (enable && !this.#interception) {
|
||||
this.#interception = await this.#frame.browsingContext.addIntercept({
|
||||
phases: [
|
||||
Bidi.Network.InterceptPhase.BeforeRequestSent,
|
||||
Bidi.Network.InterceptPhase.AuthRequired,
|
||||
],
|
||||
phases: [Bidi.Network.InterceptPhase.BeforeRequestSent],
|
||||
});
|
||||
} else if (!enable && this.#interception) {
|
||||
await this.#frame.browsingContext.userContext.browser.removeIntercept(
|
||||
@ -527,6 +525,25 @@ export class BidiPage extends Page {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
_credentials: Credentials | null = null;
|
||||
#authInterception?: string;
|
||||
override async authenticate(credentials: Credentials | null): Promise<void> {
|
||||
if (credentials && !this.#authInterception) {
|
||||
this.#authInterception = await this.#frame.browsingContext.addIntercept({
|
||||
phases: [Bidi.Network.InterceptPhase.AuthRequired],
|
||||
});
|
||||
} else if (!credentials && this.#authInterception) {
|
||||
await this.#frame.browsingContext.userContext.browser.removeIntercept(
|
||||
this.#authInterception
|
||||
);
|
||||
this.#authInterception = undefined;
|
||||
}
|
||||
this._credentials = credentials;
|
||||
}
|
||||
|
||||
override setDragInterception(): never {
|
||||
throw new UnsupportedOperation();
|
||||
}
|
||||
@ -637,10 +654,6 @@ export class BidiPage extends Page {
|
||||
await this.#frame.removeExposedFunction(name);
|
||||
}
|
||||
|
||||
override authenticate(): never {
|
||||
throw new UnsupportedOperation();
|
||||
}
|
||||
|
||||
override setExtraHTTPHeaders(): never {
|
||||
throw new UnsupportedOperation();
|
||||
}
|
||||
|
@ -162,6 +162,10 @@ export interface Commands {
|
||||
params: Bidi.Network.ContinueRequestParameters;
|
||||
returnType: Bidi.EmptyResult;
|
||||
};
|
||||
'network.continueWithAuth': {
|
||||
params: Bidi.Network.ContinueWithAuthParameters;
|
||||
returnType: Bidi.EmptyResult;
|
||||
};
|
||||
'network.failRequest': {
|
||||
params: Bidi.Network.FailRequestParameters;
|
||||
returnType: Bidi.EmptyResult;
|
||||
|
@ -19,6 +19,8 @@ export class Request extends EventEmitter<{
|
||||
/** Emitted when the request is redirected. */
|
||||
redirect: Request;
|
||||
/** Emitted when the request succeeds. */
|
||||
authenticate: void;
|
||||
/** Emitted when the request succeeds. */
|
||||
success: Bidi.Network.ResponseData;
|
||||
/** Emitted when the request fails. */
|
||||
error: string;
|
||||
@ -74,6 +76,17 @@ export class Request extends EventEmitter<{
|
||||
this.emit('redirect', this.#redirect);
|
||||
this.dispose();
|
||||
});
|
||||
sessionEmitter.on('network.authRequired', event => {
|
||||
if (
|
||||
event.context !== this.#browsingContext.id ||
|
||||
event.request.request !== this.id ||
|
||||
// Don't try to authenticate for events that are not blocked
|
||||
!event.isBlocked
|
||||
) {
|
||||
return;
|
||||
}
|
||||
this.emit('authenticate', undefined);
|
||||
});
|
||||
sessionEmitter.on('network.fetchError', event => {
|
||||
if (
|
||||
event.context !== this.#browsingContext.id ||
|
||||
@ -189,6 +202,25 @@ export class Request extends EventEmitter<{
|
||||
});
|
||||
}
|
||||
|
||||
async continueWithAuth(
|
||||
parameters:
|
||||
| Bidi.Network.ContinueWithAuthCredentials
|
||||
| Bidi.Network.ContinueWithAuthNoCredentials
|
||||
): Promise<void> {
|
||||
if (parameters.action === 'provideCredentials') {
|
||||
await this.#session.send('network.continueWithAuth', {
|
||||
request: this.id,
|
||||
action: parameters.action,
|
||||
credentials: parameters.credentials,
|
||||
});
|
||||
} else {
|
||||
await this.#session.send('network.continueWithAuth', {
|
||||
request: this.id,
|
||||
action: parameters.action,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@inertIfDisposed
|
||||
private dispose(): void {
|
||||
this[disposeSymbol]();
|
||||
|
@ -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,
|
||||
@ -24,14 +25,6 @@ import {
|
||||
type FetchRequestId,
|
||||
} from './NetworkEventManager.js';
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export interface Credentials {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
|
@ -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,
|
||||
@ -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';
|
||||
|
@ -83,20 +83,6 @@
|
||||
"expectations": ["FAIL"],
|
||||
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[network.spec] network Page.authenticate *",
|
||||
"platforms": ["darwin", "linux"],
|
||||
"parameters": ["webDriverBiDi"],
|
||||
"expectations": ["SKIP"],
|
||||
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[network.spec] network Page.authenticate *",
|
||||
"platforms": ["win32"],
|
||||
"parameters": ["webDriverBiDi"],
|
||||
"expectations": ["SKIP"],
|
||||
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[network.spec] network Page.setBypassServiceWorker *",
|
||||
"platforms": ["darwin", "linux", "win32"],
|
||||
@ -2719,6 +2705,13 @@
|
||||
"expectations": ["FAIL"],
|
||||
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[network.spec] network Page.authenticate should allow disable authentication",
|
||||
"platforms": ["darwin", "linux", "win32"],
|
||||
"parameters": ["firefox", "webDriverBiDi"],
|
||||
"expectations": ["FAIL"],
|
||||
"comment": "The Puppeteer implementation does not expect 2 responseCompleted events (that AuthRequired triggered)"
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[network.spec] network Page.authenticate should fail if wrong credentials",
|
||||
"platforms": ["darwin", "linux", "win32"],
|
||||
@ -2733,6 +2726,13 @@
|
||||
"expectations": ["FAIL"],
|
||||
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[network.spec] network Page.authenticate should not disable caching",
|
||||
"platforms": ["darwin", "linux", "win32"],
|
||||
"parameters": ["firefox", "webDriverBiDi"],
|
||||
"expectations": ["FAIL"],
|
||||
"comment": "Firefox returns `fromCache: false`"
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[network.spec] network Page.authenticate should work",
|
||||
"platforms": ["darwin", "linux", "win32"],
|
||||
@ -2740,6 +2740,13 @@
|
||||
"expectations": ["SKIP"],
|
||||
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[network.spec] network Page.authenticate should work",
|
||||
"platforms": ["darwin", "linux", "win32"],
|
||||
"parameters": ["firefox", "webDriverBiDi"],
|
||||
"expectations": ["TIMEOUT"],
|
||||
"comment": "When navigating to page with authentication the command response (error) never comes without interception"
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[network.spec] network Page.setExtraHTTPHeaders should work",
|
||||
"platforms": ["darwin", "linux", "win32"],
|
||||
|
@ -722,7 +722,7 @@ describe('network', function () {
|
||||
} catch (error) {
|
||||
// In headful, an error is thrown instead of 401.
|
||||
if (
|
||||
!(error as Error).message.startsWith(
|
||||
!(error as Error).message?.includes(
|
||||
'net::ERR_INVALID_AUTH_CREDENTIALS'
|
||||
)
|
||||
) {
|
||||
@ -772,7 +772,7 @@ describe('network', function () {
|
||||
} catch (error) {
|
||||
// In headful, an error is thrown instead of 401.
|
||||
if (
|
||||
!(error as Error).message.startsWith(
|
||||
!(error as Error).message?.includes(
|
||||
'net::ERR_INVALID_AUTH_CREDENTIALS'
|
||||
)
|
||||
) {
|
||||
|
Loading…
Reference in New Issue
Block a user