Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions browser_patches/firefox/juggler/NetworkObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,14 @@ class NetworkRequest {
}
if (!credentials)
return false;
const origin = aChannel.URI.scheme + '://' + aChannel.URI.hostPort;
if (credentials.origin && origin.toLowerCase() !== credentials.origin.toLowerCase())
const list = Array.isArray(credentials) ? credentials : [credentials];
const origin = (aChannel.URI.scheme + '://' + aChannel.URI.hostPort).toLowerCase();
const exact = list.find(c => c.origin && origin === c.origin.toLowerCase());
const chosen = exact || (list.length === 1 && !list[0].origin ? list[0] : null);
if (!chosen)
return false;
authInfo.username = credentials.username;
authInfo.password = credentials.password;
authInfo.username = chosen.username;
authInfo.password = chosen.password;
// This will produce a new request with respective auth header set.
// It will have the same id as ours. We expect it to arrive as new request and
// will treat it as our own redirect.
Expand Down
12 changes: 12 additions & 0 deletions browser_patches/firefox/juggler/protocol/PrimitiveTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ t.Array = function(scheme) {
}
}

t.Either = function(...schemes) {
return function(x, details = {}, path = ['<root>']) {
for (const scheme of schemes) {
const nestedDetails = {};
if (checkScheme(scheme, x, nestedDetails, path.slice()))
return true;
}
details.error = `Expected "${path.join('.')}" to match one of the alternative schemes; found \`${JSON.stringify(x)}\` (${typeof x}) instead.`;
return false;
}
}

t.Recursive = function(types, schemeName) {
return function(x, details = {}, path = ['<root>']) {
const scheme = types[schemeName];
Expand Down
5 changes: 4 additions & 1 deletion browser_patches/firefox/juggler/protocol/Protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ networkTypes.HTTPCredentials = {
origin: t.Optional(t.String),
};

networkTypes.HTTPCredentialsList = t.Array(networkTypes.HTTPCredentials);

networkTypes.SecurityDetails = {
protocol: t.String,
subjectName: t.String,
Expand Down Expand Up @@ -271,7 +273,8 @@ const Browser = {
'setHTTPCredentials': {
params: {
browserContextId: t.Optional(t.String),
credentials: t.Nullable(networkTypes.HTTPCredentials),
// Accept a single credentials object (legacy) or an array for multiple origins.
credentials: t.Nullable(t.Either(networkTypes.HTTPCredentials, networkTypes.HTTPCredentialsList)),
},
},
'setRequestInterception': {
Expand Down
3 changes: 3 additions & 0 deletions docs/src/api/class-apirequest.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ for all status codes.
### option: APIRequest.newContext.httpCredentials = %%-context-option-httpcredentials-%%
* since: v1.16

### option: APIRequest.newContext.httpCredentials = %%-context-option-httpcredentials-csharp-java-python-%%
* since: v1.16

### option: APIRequest.newContext.proxy = %%-browser-option-proxy-%%
* since: v1.16

Expand Down
4 changes: 3 additions & 1 deletion docs/src/api/class-browsercontext.md
Original file line number Diff line number Diff line change
Expand Up @@ -1523,9 +1523,11 @@ its geolocation.

### param: BrowserContext.setHTTPCredentials.httpCredentials
* since: v1.8
- `httpCredentials` <[null]|[Object]>
* langs: js
- `httpCredentials` <[null]|[Object]|[Array]<[Object]>>
- `username` <[string]>
- `password` <[string]>
- `origin` ?<[string]> Restrain sending http credentials on specific origin (scheme://host:port). Required when providing more than one credentials entry.

## async method: BrowserContext.setOffline
* since: v1.8
Expand Down
20 changes: 20 additions & 0 deletions docs/src/api/params.md
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,25 @@ An object containing additional HTTP headers to be sent with every request. Defa
Whether to emulate network being offline. Defaults to `false`. Learn more about [network emulation](../emulation.md#offline).

## context-option-httpcredentials
* langs: js
- `httpCredentials` <[Object]|[Array]<[Object]>>
* alias: HttpCredentials
- `username` <[string]>
- `password` <[string]>
- `origin` ?<[string]> Restrain sending http credentials on specific origin (scheme://host:port). Required when providing more than one credentials entry.
- `send` ?<[HttpCredentialsSend]<"unauthorized"|"always">> This option only applies to the requests sent from corresponding [APIRequestContext] and does not affect requests sent from the browser. `'always'` - `Authorization` header with basic authentication credentials will be sent with the each API request. `'unauthorized` - the credentials are only sent when 401 (Unauthorized) response with `WWW-Authenticate` header is received. Defaults to `'unauthorized'`.

Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).
If no origin is specified, the username and password are sent to any servers upon unauthorized responses.

Pass an array to supply different credentials for different origins. When the array has more than one entry, every entry must include a unique `origin`.

:::note
WebKit does not support multiple `httpCredentials` entries. Pass a single credentials object (or a one-element array) when using WebKit.
:::

## context-option-httpcredentials-csharp-java-python
* langs: csharp, java, python
- `httpCredentials` <[Object]>
* alias: HttpCredentials
- `username` <[string]>
Expand Down Expand Up @@ -1091,6 +1110,7 @@ between the same pixel in compared images, between zero (strict) and one (lax),
- %%-context-option-extrahttpheaders-%%
- %%-context-option-offline-%%
- %%-context-option-httpcredentials-%%
- %%-context-option-httpcredentials-csharp-java-python-%%
- %%-context-option-colorscheme-%%
- %%-context-option-colorscheme-csharp-python-%%
- %%-context-option-reducedMotion-%%
Expand Down
3 changes: 3 additions & 0 deletions docs/src/electron-api/class-electron.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ Maximum time in milliseconds to wait for the application to start. Defaults to `
### option: Electron.launch.httpcredentials = %%-context-option-httpcredentials-%%
* since: v1.12

### option: Electron.launch.httpcredentials = %%-context-option-httpcredentials-csharp-java-python-%%
* since: v1.12

### option: Electron.launch.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%%
* since: v1.12

Expand Down
8 changes: 8 additions & 0 deletions docs/src/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ const context = await browser.newContext({
});
const page = await context.newPage();
await page.goto('https://example.com');

// Different credentials for different origins:
const context2 = await browser.newContext({
httpCredentials: [
{ username: 'user1', password: 'pass1', origin: 'https://server1.com' },
{ username: 'user2', password: 'pass2', origin: 'https://server2.com' },
],
});
```

```java
Expand Down
Loading