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 {Accessibility} from '../cdp/Accessibility.js';
|
||||||
import type {Coverage} from '../cdp/Coverage.js';
|
import type {Coverage} from '../cdp/Coverage.js';
|
||||||
import type {DeviceRequestPrompt} from '../cdp/DeviceRequestPrompt.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 {Tracing} from '../cdp/Tracing.js';
|
||||||
import type {ConsoleMessage} from '../common/ConsoleMessage.js';
|
import type {ConsoleMessage} from '../common/ConsoleMessage.js';
|
||||||
import type {
|
import type {
|
||||||
@ -131,6 +131,14 @@ export interface Metrics {
|
|||||||
JSHeapTotalSize?: number;
|
JSHeapTotalSize?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
export interface Credentials {
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
|
@ -115,7 +115,6 @@ export class BidiFrame extends Frame {
|
|||||||
this.browsingContext.on('request', ({request}) => {
|
this.browsingContext.on('request', ({request}) => {
|
||||||
const httpRequest = BidiHTTPRequest.from(request, this);
|
const httpRequest = BidiHTTPRequest.from(request, this);
|
||||||
request.once('success', () => {
|
request.once('success', () => {
|
||||||
// SAFETY: BidiHTTPRequest will create this before here.
|
|
||||||
this.page().trustedEmitter.emit(PageEvent.RequestFinished, httpRequest);
|
this.page().trustedEmitter.emit(PageEvent.RequestFinished, httpRequest);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -72,6 +72,7 @@ export class BidiHTTPRequest extends HTTPRequest {
|
|||||||
this.#request.once('success', data => {
|
this.#request.once('success', data => {
|
||||||
this.#response = BidiHTTPResponse.from(data, this);
|
this.#response = BidiHTTPResponse.from(data, this);
|
||||||
});
|
});
|
||||||
|
this.#request.on('authenticate', this.#handleAuthentication);
|
||||||
|
|
||||||
this.#frame?.page().trustedEmitter.emit(PageEvent.Request, this);
|
this.#frame?.page().trustedEmitter.emit(PageEvent.Request, this);
|
||||||
}
|
}
|
||||||
@ -232,6 +233,29 @@ export class BidiHTTPRequest extends HTTPRequest {
|
|||||||
throw error;
|
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>) {
|
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 {WaitForOptions} from '../api/Frame.js';
|
||||||
import type {HTTPResponse} from '../api/HTTPResponse.js';
|
import type {HTTPResponse} from '../api/HTTPResponse.js';
|
||||||
import type {
|
import type {
|
||||||
|
Credentials,
|
||||||
GeolocationOptions,
|
GeolocationOptions,
|
||||||
MediaFeature,
|
MediaFeature,
|
||||||
PageEvents,
|
PageEvents,
|
||||||
@ -514,10 +515,7 @@ export class BidiPage extends Page {
|
|||||||
override async setRequestInterception(enable: boolean): Promise<void> {
|
override async setRequestInterception(enable: boolean): Promise<void> {
|
||||||
if (enable && !this.#interception) {
|
if (enable && !this.#interception) {
|
||||||
this.#interception = await this.#frame.browsingContext.addIntercept({
|
this.#interception = await this.#frame.browsingContext.addIntercept({
|
||||||
phases: [
|
phases: [Bidi.Network.InterceptPhase.BeforeRequestSent],
|
||||||
Bidi.Network.InterceptPhase.BeforeRequestSent,
|
|
||||||
Bidi.Network.InterceptPhase.AuthRequired,
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
} else if (!enable && this.#interception) {
|
} else if (!enable && this.#interception) {
|
||||||
await this.#frame.browsingContext.userContext.browser.removeIntercept(
|
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 {
|
override setDragInterception(): never {
|
||||||
throw new UnsupportedOperation();
|
throw new UnsupportedOperation();
|
||||||
}
|
}
|
||||||
@ -637,10 +654,6 @@ export class BidiPage extends Page {
|
|||||||
await this.#frame.removeExposedFunction(name);
|
await this.#frame.removeExposedFunction(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
override authenticate(): never {
|
|
||||||
throw new UnsupportedOperation();
|
|
||||||
}
|
|
||||||
|
|
||||||
override setExtraHTTPHeaders(): never {
|
override setExtraHTTPHeaders(): never {
|
||||||
throw new UnsupportedOperation();
|
throw new UnsupportedOperation();
|
||||||
}
|
}
|
||||||
|
@ -162,6 +162,10 @@ export interface Commands {
|
|||||||
params: Bidi.Network.ContinueRequestParameters;
|
params: Bidi.Network.ContinueRequestParameters;
|
||||||
returnType: Bidi.EmptyResult;
|
returnType: Bidi.EmptyResult;
|
||||||
};
|
};
|
||||||
|
'network.continueWithAuth': {
|
||||||
|
params: Bidi.Network.ContinueWithAuthParameters;
|
||||||
|
returnType: Bidi.EmptyResult;
|
||||||
|
};
|
||||||
'network.failRequest': {
|
'network.failRequest': {
|
||||||
params: Bidi.Network.FailRequestParameters;
|
params: Bidi.Network.FailRequestParameters;
|
||||||
returnType: Bidi.EmptyResult;
|
returnType: Bidi.EmptyResult;
|
||||||
|
@ -19,6 +19,8 @@ export class Request extends EventEmitter<{
|
|||||||
/** Emitted when the request is redirected. */
|
/** Emitted when the request is redirected. */
|
||||||
redirect: Request;
|
redirect: Request;
|
||||||
/** Emitted when the request succeeds. */
|
/** Emitted when the request succeeds. */
|
||||||
|
authenticate: void;
|
||||||
|
/** Emitted when the request succeeds. */
|
||||||
success: Bidi.Network.ResponseData;
|
success: Bidi.Network.ResponseData;
|
||||||
/** Emitted when the request fails. */
|
/** Emitted when the request fails. */
|
||||||
error: string;
|
error: string;
|
||||||
@ -74,6 +76,17 @@ export class Request extends EventEmitter<{
|
|||||||
this.emit('redirect', this.#redirect);
|
this.emit('redirect', this.#redirect);
|
||||||
this.dispose();
|
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 => {
|
sessionEmitter.on('network.fetchError', event => {
|
||||||
if (
|
if (
|
||||||
event.context !== this.#browsingContext.id ||
|
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
|
@inertIfDisposed
|
||||||
private dispose(): void {
|
private dispose(): void {
|
||||||
this[disposeSymbol]();
|
this[disposeSymbol]();
|
||||||
|
@ -8,6 +8,7 @@ import type {Protocol} from 'devtools-protocol';
|
|||||||
|
|
||||||
import {CDPSessionEvent, type CDPSession} from '../api/CDPSession.js';
|
import {CDPSessionEvent, type CDPSession} from '../api/CDPSession.js';
|
||||||
import type {Frame} from '../api/Frame.js';
|
import type {Frame} from '../api/Frame.js';
|
||||||
|
import type {Credentials} from '../api/Page.js';
|
||||||
import {EventEmitter, EventSubscription} from '../common/EventEmitter.js';
|
import {EventEmitter, EventSubscription} from '../common/EventEmitter.js';
|
||||||
import {
|
import {
|
||||||
NetworkManagerEvent,
|
NetworkManagerEvent,
|
||||||
@ -24,14 +25,6 @@ import {
|
|||||||
type FetchRequestId,
|
type FetchRequestId,
|
||||||
} from './NetworkEventManager.js';
|
} from './NetworkEventManager.js';
|
||||||
|
|
||||||
/**
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
export interface Credentials {
|
|
||||||
username: string;
|
|
||||||
password: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
|
@ -15,6 +15,7 @@ import type {Frame, WaitForOptions} from '../api/Frame.js';
|
|||||||
import type {HTTPRequest} from '../api/HTTPRequest.js';
|
import type {HTTPRequest} from '../api/HTTPRequest.js';
|
||||||
import type {HTTPResponse} from '../api/HTTPResponse.js';
|
import type {HTTPResponse} from '../api/HTTPResponse.js';
|
||||||
import type {JSHandle} from '../api/JSHandle.js';
|
import type {JSHandle} from '../api/JSHandle.js';
|
||||||
|
import type {Credentials} from '../api/Page.js';
|
||||||
import {
|
import {
|
||||||
Page,
|
Page,
|
||||||
PageEvent,
|
PageEvent,
|
||||||
@ -71,7 +72,7 @@ import {FrameManagerEvent} from './FrameManagerEvents.js';
|
|||||||
import {CdpKeyboard, CdpMouse, CdpTouchscreen} from './Input.js';
|
import {CdpKeyboard, CdpMouse, CdpTouchscreen} from './Input.js';
|
||||||
import {MAIN_WORLD} from './IsolatedWorlds.js';
|
import {MAIN_WORLD} from './IsolatedWorlds.js';
|
||||||
import {releaseObject} from './JSHandle.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 {CdpTarget} from './Target.js';
|
||||||
import type {TargetManager} from './TargetManager.js';
|
import type {TargetManager} from './TargetManager.js';
|
||||||
import {TargetManagerEvent} from './TargetManager.js';
|
import {TargetManagerEvent} from './TargetManager.js';
|
||||||
|
@ -83,20 +83,6 @@
|
|||||||
"expectations": ["FAIL"],
|
"expectations": ["FAIL"],
|
||||||
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
|
"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 *",
|
"testIdPattern": "[network.spec] network Page.setBypassServiceWorker *",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
@ -2719,6 +2705,13 @@
|
|||||||
"expectations": ["FAIL"],
|
"expectations": ["FAIL"],
|
||||||
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
|
"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",
|
"testIdPattern": "[network.spec] network Page.authenticate should fail if wrong credentials",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
@ -2733,6 +2726,13 @@
|
|||||||
"expectations": ["FAIL"],
|
"expectations": ["FAIL"],
|
||||||
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
|
"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",
|
"testIdPattern": "[network.spec] network Page.authenticate should work",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
@ -2740,6 +2740,13 @@
|
|||||||
"expectations": ["SKIP"],
|
"expectations": ["SKIP"],
|
||||||
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
|
"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",
|
"testIdPattern": "[network.spec] network Page.setExtraHTTPHeaders should work",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
|
@ -722,7 +722,7 @@ describe('network', function () {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
// In headful, an error is thrown instead of 401.
|
// In headful, an error is thrown instead of 401.
|
||||||
if (
|
if (
|
||||||
!(error as Error).message.startsWith(
|
!(error as Error).message?.includes(
|
||||||
'net::ERR_INVALID_AUTH_CREDENTIALS'
|
'net::ERR_INVALID_AUTH_CREDENTIALS'
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
@ -772,7 +772,7 @@ describe('network', function () {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
// In headful, an error is thrown instead of 401.
|
// In headful, an error is thrown instead of 401.
|
||||||
if (
|
if (
|
||||||
!(error as Error).message.startsWith(
|
!(error as Error).message?.includes(
|
||||||
'net::ERR_INVALID_AUTH_CREDENTIALS'
|
'net::ERR_INVALID_AUTH_CREDENTIALS'
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
|
Loading…
Reference in New Issue
Block a user