Skip to content

Commit

Permalink
feat(authenticate): Provide credentials for HTTP authentication
Browse files Browse the repository at this point in the history
fix(ci): disable AppArmor restrictions on Ubuntu

refer puppeteer/puppeteer#13196
  • Loading branch information
ahuigo committed Jan 7, 2025
1 parent 0a4ed05 commit 5ce1c0f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ jobs:
with:
deno-version: v2.x

- name: Disable AppArmor
if: ${{ matrix.os == 'ubuntu-latest' }}
run: echo 0 | sudo tee /proc/sys/kernel/apparmor_restrict_unprivileged_userns

- name: check format
if: matrix.os == 'ubuntu-latest'
run: deno fmt --check
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ const browser = await launch();
const anotherBrowser = await launch({ wsEndpoint: browser.wsEndpoint() });
```

### Page authenticate
[authenticate example code](https://github.com/lino-levan/astral/blob/main/examples/evaluate.ts):

// Open a new page
const page = await browser.newPage("https://httpbin.org/basic-auth/user/passwd");

// Provide credentials for HTTP authentication.
await page.authenticate({ username: "user", password: "passwd" });

## BYOB - Bring Your Own Browser

Essentially the process is as simple as running a chromium-like binary with the
Expand Down
18 changes: 18 additions & 0 deletions examples/authenticate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference lib="dom" />

// Import Astral
import { launch } from "../mod.ts";

// Launch the browser
const browser = await launch();

// Open a new page
const page = await browser.newPage(
"https://httpbin.org/basic-auth/user/passwd",
);

// Provide credentials for HTTP authentication.
await page.authenticate({ username: "user", password: "passwd" });

// Close the browser
await browser.close();
25 changes: 25 additions & 0 deletions src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,31 @@ export class Page extends EventTarget {
return this.#celestial;
}

/**
* Provide credentials for HTTP authentication.
*
* @example
* ```ts
* await page.authenticate({ 'username': username, 'password': password });
* ```
*/
authenticate(
{ username, password }: { username: string; password: string },
): Promise<void> {
function base64encoded(s: string) {
const codeUnits = new Uint16Array(s.length);
for (let i = 0; i < codeUnits.length; i++) {
codeUnits[i] = s.charCodeAt(i);
}
return btoa(String.fromCharCode(...new Uint8Array(codeUnits.buffer)));
}

const auth = base64encoded(`${username}:${password}`);
return this.#celestial.Network.setExtraHTTPHeaders({
headers: { "Authorization": `Basic ${auth}` },
});
}

/**
* Runs `document.querySelector` within the page. If no element matches the selector, the return value resolves to `null`.
*
Expand Down

0 comments on commit 5ce1c0f

Please sign in to comment.