Skip to content
Sessions, sign-out and API clients

Sessions, sign-out and API clients

Browser sessions

  • Access tokens live 5 minutes. The browser session behind them lives 14 days, extended by every use.
  • Page loads need nothing from you: an expired access cookie sends the browser through the sign-in path, which refreshes it silently and comes back to the same URL.
  • XHR and fetch calls get a 401 when the access cookie has expired. Call POST /_portal/refresh (same origin, cookies only, no body) and retry once: 204 means refreshed, 401 means the session is gone (reload the page so the user signs in).
async function api(url, opts = {}) {
  let r = await fetch(url, { credentials: "same-origin", ...opts });
  if (r.status === 401) {
    const f = await fetch("/_portal/refresh",
                          { method: "POST", credentials: "same-origin" });
    if (!f.ok) { location.reload(); return r; }
    r = await fetch(url, { credentials: "same-origin", ...opts });
  }
  return r;
}

Signing out

Either link to the sign-out page, which asks for confirmation:

<a href="/_portal/sign-out">Sign out</a>

or sign out in one click with a form on your own page:

<form method="post" action="/_portal/sign-out">
  <input type="hidden" name="return_to" value="/">
  <button>Sign out</button>
</form>

Sign-out ends the session (its tokens stop working within seconds) and sends the browser to the sign-in page; after signing in again it returns to return_to, a path on the same address. The POST needs no token, but it is accepted only from your portal’s own address (the browser’s Origin): a form on another site, or on another portal’s pages, gets a 403.

Requests from other sites

Browsers send the session cookie along with requests that other pages start, including the pages of every other portal (they share a parent domain). The edge therefore accepts the cookie on a POST, PUT, PATCH, DELETE or WebSocket upgrade only from your portal’s own address (the browser’s Origin, or Sec-Fetch-Site: same-origin); from anywhere else it answers 403 and your service never sees the request. Your own pages' fetch, forms and new WebSocket(...) are unaffected. GET requests pass as before, so keep them free of side effects. Bearer tokens pass from any origin.

Same origin as the sign-in pages

Your pages share an origin with the sign-in pages. Script on your pages can do what the user can do there, including fetching an API token. Treat cross-site scripting in your service as a full account takeover: escape output, and set a Content-Security-Policy.

API clients and bearer tokens

A signed-in user gets a token at /_portal/token; it is valid for 5 minutes. Clients send Authorization: Bearer <token>; the edge checks it, removes the header and passes the identity (kind: "session").

MCP and OAuth clients

MCP clients (Claude Code, claude.ai and others) sign in with OAuth. Unauthenticated requests get 401 with WWW-Authenticate: Bearer resource_metadata="https://<host>/.well-known/oauth-protected-resource" (RFC 9728). Clients discover the authorization server from there and register themselves (dynamic client registration, or a client metadata document); then the user signs in and consents. Your service sees kind: "oauth" with scope and client_id. Access tokens last 5 minutes; clients refresh them on their own.

Your MCP server is an ordinary endpoint of your service (for example POST /mcp). Require scope mcp there.

Connecting Claude

Give people your MCP endpoint’s URL, https://<host>/mcp:

claude mcp add --transport http myapp https://quiet-harbor.agent-app.eu/mcp

In Claude Code, /mcp then signs in: a browser opens on the portal’s sign-in page (with its second step if the account has two-factor) and asks to allow the client. In claude.ai and Claude Desktop, add the same URL as a custom connector; in ChatGPT, add it as a connector. Only accounts of the portal get in, so an invite-only portal’s MCP server is invite-only too.

Clients that identify themselves with a client metadata document (ChatGPT does) sign in as public clients with PKCE: the document must list none among its token_endpoint_auth_methods_supported, or name it as its token_endpoint_auth_method. A client that can only authenticate with a secret or a signed assertion is refused (invalid_client, “unsupported token_endpoint_auth_method”).

  • The token’s resource is the portal’s origin, the one in the protected-resource metadata. Clients that follow WWW-Authenticate (Claude does) use it; a client that sends the MCP URL itself as resource is refused (invalid_target). The metadata is at /.well-known/oauth-protected-resource only; .../oauth-protected-resource/mcp is 404, and MCP clients fall back to the root.
  • Give your MCP tools the same rules as your pages: the identity is the same user. Read-only tools for everyone and changes for admins is a good start.
  • Disabling a user, their sign-out everywhere, a password reset or change end their MCP clients’ access too: the refresh tokens are revoked and the edge refuses their access tokens at once.

Ending sessions

Every session and OAuth client of a user ends when they:

  • choose Sign out everywhere or change their password on /_portal/account (a password change keeps the session it was made in);
  • reset their password;
  • are signed out or disabled by your service (space-env web-portal sign-out or disable, Invitations).

The edge refuses their tokens from then on; your service sees no more requests from them until they sign in again.

WebSockets

WebSockets are checked once, at the upgrade. The edge doesn’t close an open connection when the token behind it expires or the user is removed. If that matters, close long-lived connections yourself after exp, and have the client reconnect.

Last updated on