Skip to content

Signed-in sites

Read and work with sites that have only a sign-in form, through managed sign-in, browser actions, fetch, and browser sessions.

Updated

Some services offer no API key and no OAuth, only a page where a person signs in with a username and password. An extension can still read them. You declare how the site signs in, a person connects with their username and password, and Forge signs in to the site as a password manager would. Your tools then use the session that the sign-in leaves, in three ways:

  • Read one page with a ctx.browser action, such as markdown or screenshot.
  • Call the site’s own JSON API with fetch, with no browser.
  • Work through several pages, filling forms and clicking through results, in a browser session.

Extension code never holds the password or the session’s cookies, only sealed handles that Forge replaces on the way out to the site.

How it works

  1. You declare the sign-in. A provider’s secrets method declares signIn: the sign-in page, a page that only a signed-in person can open, and which of its fields are the username and password.
  2. A person connects. When they submit the connect form, Forge signs in to the site first. If the site refuses the username and password, the form says so and nothing is saved. Otherwise Forge saves the session’s cookies with the connection, encrypted like its credentials.
  3. Your tools use the session. Each browser page, fetch request, or browser session with the connection gets the saved session, so most calls don’t sign in at all.
  4. Forge keeps the session working. When the site has ended the session, and a page answers 401 or lands on the sign-in page, Forge signs in again once and saves the new session. When the site stops accepting the password, the connection stops signing in, so the site never locks the account. It reads as unhealthy until someone connects again.

A complete example

This extension declares a portal that people sign in to, and three tools that read it in each of the three ways:

import { defineExtension, defineProvider, object, plain, secrets, string, tool } from "@buildr/sdk";

const portal = defineProvider({
  name: "Portal",
  hosts: ["portal.example.com"],
  auth: {
    login: secrets({
      label: "Email and password",
      fields: object({ email: plain(string()), password: string() }),
      signIn: ({ email, password }) => ({
        url: "https://portal.example.com/login",
        check: "https://portal.example.com/account",
        username: email,
        password,
      }),
    }),
  },
});

// One page, read as Markdown.
const account = tool(
  { description: "Reads the account page", input: object({}) },
  async (ctx) => {
    const page = await ctx.browser.markdown({
      url: "https://portal.example.com/account",
      connection: ctx.user.connections.portal,
    });
    return page.markdown;
  },
);

// The site's own JSON API, with no browser.
const orders = tool(
  { description: "Lists the account's orders", input: object({}) },
  async (ctx) => {
    const response = await fetch("https://portal.example.com/api/orders", {
      headers: ctx.user.connections.portal.headers(),
    });
    return await response.json();
  },
);

// Several pages: search the orders, then follow "Next page" until the last page.
const searchOrders = tool(
  { description: "Searches the account's orders", input: object({ q: string() }) },
  async (ctx, { q }) => {
    const session = await ctx.browser.session({ connection: ctx.user.connections.portal });
    await session.goto("https://portal.example.com/orders");
    await session.fill("input[name=q]", q);
    await session.click("button[type=submit]", { navigation: true });
    const pages = [];
    for (;;) {
      pages.push((await session.markdown()).markdown);
      const { outline } = await session.snapshot();
      const next = /button "Next page" \[ref=(e\d+)\]/.exec(outline)?.[1];
      if (next === undefined) break;
      await session.click(next, { navigation: true });
    }
    await session.close();
    return pages;
  },
);

export default defineExtension(
  { user: { connections: { portal } } },
  { tools: { account, orders, searchOrders } },
);

Declaring the sign-in

signIn answers four things:

Key What it is
url The sign-in page. Forge finds the form’s fields by their autocomplete attributes: username or email, and current-password.
check A page that answers 200 itself only to someone signed in, such as an account page or an account API. When it answers anything else, or redirects, Forge takes it that the site refused the username and password.
username The field that holds the username. It may be plain(), so that extension code can read it in connection.fields.
password The field that holds the password. It must be secret.

Both pages must be on the provider’s hosts. List every host of the site that its signed-in pages load, such as static.portal.example.com, or use a wildcard, *.example.com; see Signed-in pages for why. Sign-ins with a username and password has the full rules, including multi-page sign-ins and forms that a script draws.

Choosing a way to read the site

You want to Use Notes
Read one page as Markdown, links, or HTML, or keep it as a screenshot or PDF A ctx.browser action with connection Runs the page’s scripts. Opens and closes its page in one step.
Read a JSON API that the signed-in site calls fetch with connection.headers() Fastest: no browser. The session goes in the cookie header. See Signed-in sessions on fetch.
Fill a search form, page through results, or follow links ctx.browser.session() with connection Keeps one page open across steps. Targets elements by CSS selector or by a ref from snapshot().

To find a site’s JSON API, open the signed-in site in your own browser, and watch its developer tools’ network panel while the page loads its data.

Connecting and binding

Release the extension, or call its tools on a branch, then connect and bind the provider as you would any other. See Connection requests and repo bindings:

# 1. As the person whose account it is, create a connection request
forge connections create-connection-request --org "$ORG" --data '{
  "provider_id": "prv_...",
  "audience": "user",
  "extension_name": "integrations.portal"
}'

# 2. Open the returned link in the dashboard, signed in as the same person,
#    and submit the email and password. The link opens for no one else.
#    Forge signs in to the site before it saves anything.

# 3. As the same person, bind the saved connection to the repo's requirement
forge connections upsert-connection-binding --repo "$OPS_REPO" --extension integrations.portal --requirement portal \
  --data '{"connection_id":"connection_..."}'

A site account is usually one person’s, so declare the requirement under user and read it from ctx.user.connections. Declare it under org for an account that the whole organization shares.

Each person who connects their own account needs createConnections on the org and bindConnections on the installation, which the default Editor policy doesn’t allow. See Connection requests and repo bindings.

What Forge keeps safe

  • The password goes only to the sign-in. Forge types it into the sign-in form of a page on the provider’s hosts, and nowhere else. It never fills a sign-up or change-password form; a sign-in page whose only password field is marked as new counts as a sign-in form. While it signs in, the page can’t post or navigate off the provider’s hosts.
  • The session stays on the site. A page with the session can’t send requests to hosts of the site’s domain that the provider doesn’t declare. fetch sends the session only to the provider’s hosts, over HTTPS, and only in the cookie header.
  • Extension code sees neither. A session cookie’s value of eight or more characters that a page shows comes back as [signed-in session] from a browser read, and as its sealed handle from fetch. fill refuses a sealed handle, so a tool can’t type a password or session into a page.
  • A screenshot or PDF shows the page as it is. It holds whatever the signed-in page displays.

Forge doesn’t solve CAPTCHAs or other bot challenges. A site that shows one when signing in can’t be read this way.

When something goes wrong

What you see Why What to do
The connect form says the site didn’t accept the username and password The site refused them Check them by signing in to the site yourself.
ConnectionSignInFailed with reason: "credentials_refused", and the connection reads as unhealthy After signing in, check didn’t answer 200. Usually the site stopped accepting the password, as after someone changed it on the site. A check that answers 200 to no one, such as a page that redirects even when signed in, looks the same. Connect again with the right password, and bind the new connection in place of the old one. If the password is right, point check at a page that answers 200 when signed in.
ConnectionSignInFailed with reason: "sign_in_unusable" The sign-in page has no form that Forge fills, the form asks for a new password (twice, or on a page after a submit), no password field appeared within three pages, the sign-in went off the provider’s hosts, or the sign-in left no cookie. message says which. Fix signIn: point url at the page with the sign-in form, and add the hosts that the sign-in passes through to hosts.
NetworkRefused with refusal.reason: "credential_host" The page or request isn’t on the provider’s hosts Add the host to hosts, or read that page without the connection.
A signed-in page is missing its styles or data It loads them from a host of the site’s domain that hosts doesn’t list, and the browser refused it Add that host, or a wildcard such as *.example.com, to hosts.
fetch answers 401 The connection holds no session: the site ended it and signing in again failed, or the site refused the password before Check the connection’s health with forge connections list-connections --org "$ORG", and connect again if the site refused the password.
BrowserElementNotFound in a session No element matched the selector in time, or the ref came from before the page changed Take a new snapshot() after each navigation, or waitFor the element first.
BrowserRateLimited The org opened pages too fast, or every browser is in use Try again later.

See Refusals for the errors that a browser action or session step rejects with.

Type to search…

↑↓ navigate↵ selectEsc close