--- title: "Authentication & revocation" description: "Reuse session access. Add custom authorization and revocation when needed." canonical_url: "https://tress-theta.vercel.app/docs/authentication" markdown_url: "https://tress-theta.vercel.app/docs/authentication.md" last_updated: "2026-11-23" x_farming_labs_generated_preamble: true --- # Authentication & revocation URL: /docs/authentication LLM index: /llms.txt Description: Reuse session access. Add custom authorization and revocation when needed. # Authentication & revocation **`createTressAccess` is optional.** You do not need an external authentication service to connect a terminal. Keep the host's existing access flow, whether that is the Tress demo's shared session or your application's login and thread permissions. ## Choose the app's access method The source-preview CLI supports three app-selected policies: | App policy | User connection | | --- | --- | | API key or bearer token | `tress attach https://app.example --api-key-env MY_APP_API_KEY` | | Explicit anonymous access | `tress attach https://app.example` | | Custom request headers | `tress attach https://app.example --headers-file app-headers.json` | The environment variable holds an **app-issued user credential**, not a model key or project-wide Harness key. The app validates it and decides which conversations are visible. Anonymous access exposes exactly the conversations the app intentionally makes available without a credential. With the [discovery endpoint](/docs/existing-app#4-make-the-app-discoverable-and-connect), normal attachment automatically lists all permitted conversations. The same credentials accompany discovery, list, and thread requests. Thread endpoints remain on the app's origin; credentials are not forwarded through redirects. No new login provider or header file is required for the first two policies. API-key environment flags currently apply to app-origin attachment and `tress threads`; direct thread attachment and `tress ask` use header files. ## Session access and shared commands The Tress demo creates a secret session token and includes it in the copied connection command: ```sh tress attach https://your-demo.example -s YOUR_SESSION_TOKEN ``` That token grants access to the shared conversation. The server looks up its hash and checks the session before accepting the thread's WebSocket. It is different from the thread's public ID. Anyone holding the command can join that session, so share it only with its intended participants. No additional `createTressAccess` setup is needed for this demo flow. For an existing Harness app, reuse its session/access implementation. Tress does not automatically add the demo's token issuance and routes to arbitrary apps. A browser cookie stays in the browser; the app must provide a credential or session-sharing mechanism the terminal can present too. The wrapper is optional; the server's access check still matters. ## Use Harness's existing authorization hook The `statewireHost` used by Harness already accepts `authorize`. If your host uses it, keep it. For example, inside your server's host configuration: ```ts import { statewireHost } from "statewire-node"; const host = statewireHost({ ...existingHostOptions, // transport, protocol, key, storage, onError authorize: async ({ headers }) => { // Application functions: resolve a browser session or terminal credential. const session = await app.authenticateHeaders(headers); if (!session) throw new Response("Sign in", { status: 401 }); if (!await app.canAccessThread(session.userId, threadId)) { throw new Response("Forbidden", { status: 403 }); } // Stable principal: avoid putting a rotating access token in this value. return { subject: session.userId, threadId }; }, }); ``` `existingHostOptions`, `app`, and `threadId` are application code. In the pinned version, this hook gates each attachment and binds its client ID to the returned principal. Commands travel on that authorized WebSocket. Clients attach over a WebSocket at the thread URL plus `/ws`. Browsers can't set headers on a WebSocket, so `headers` is the union of the upgrade request's headers (same-origin cookies, `Origin`) and the headers the client sends in the socket's first message (an `Authorization` token, for example). Upgrade headers win when both carry a name. Credentials never go in the URL or the subprotocol, so they stay out of access logs. The hook does **not** re-run on every command or automatically close an open socket when an application session expires. Keep your existing upgrade checks (including `Origin` for cookie sessions) and connection-lifetime policy. If you want the helper to handle those checks and closure, use the optional adapter below. ## Handle an unauthorized browser connection The pinned `useHarness` API has no option named `onUnauthorized`. Its `StatewireWebsocket` transport provides a `headers({ staleAuth })` resolver and an `onChange` callback. These work with either `useHarness` or assistant-ui's `HarnessThread`. For an app that uses bearer tokens in the browser: ```tsx import { useHarness } from "harness-sdk"; import { StatewireWebsocket } from "statewire"; function useAppThread(threadId: string) { return useHarness({ transport: StatewireWebsocket({ url: `/api/threads/${encodeURIComponent(threadId)}`, headers: async ({ staleAuth }) => { const token = await appAuth.getAccessToken({ refresh: staleAuth }); return { Authorization: `Bearer ${token}` }; }, onChange(connection) { if (connection.status !== "stopped") return; if (connection.reason === "unauthorized") appAuth.showSignIn(); if (connection.reason === "forbidden") appAuth.showAccessDenied(); }, }), }); } ``` `appAuth` represents your own login/refresh/UI functions, not Harness exports. The resolver runs on every attach and reconnect, and its headers travel in the socket's first message, never in the URL. Statewire gives a 401 rejection one recovery attempt with `staleAuth: true`; another rejection stops the connection. A 403 stops it as forbidden. For same-origin cookie sessions, omit `headers`: the browser sends the cookie on the WebSocket upgrade. After signing in again, call `harness.transport.connection.reconnect()` on the value returned by `useHarness`. These callbacks recover or display an access failure; server-side checks decide who can connect. They do not add credential refresh to the native Tress CLI. ## Optional: application header credentials An app can issue a separate terminal credential for independent expiry and revocation. Store its hash, owner, allowed thread or workspace, expiration, and revocation status. Keep provider and project-wide keys on the server. The preview CLI reads HTTP headers from a private JSON file: ```json { "Authorization": "Bearer APP_ISSUED_TERMINAL_TOKEN" } ``` Replace the placeholder through your application's credential issuance flow. Custom application headers also work. On Unix, the file must have private permissions. HTTPS is required except on loopback for local development or SSH forwarding. ```sh chmod 600 app-headers.json tress attach https://chat.example.com/api/threads/main \ --headers-file app-headers.json --ui ``` This works with your existing authorized host; `@tress/access` is not required. The client sends the headers with discovery, list, and thread connections, refuses redirects, and does not forward them when switching to a different origin. Obtain a fresh file and reattach after expiry; the CLI does not implement OAuth or automatic token refresh. ## Optional: extra checks and live revocation Use `createTressAccess` if you want a reusable adapter for authorizing each WebSocket, same-origin checks for cookie sessions, and closing expired or revoked connections. It works with your own session store or identity provider; it does not require an external auth service. ```ts import { createTressAccess } from "@tress/access"; const access = createTressAccess({ host: existingThreadHost, origin: appOrigin, authorize: async ({ request, action }) => { // Your app validates the session/credential and this thread's permissions. const grant = await app.authorizeThread(request, threadId, action); if (!grant) throw new Response("Forbidden", { status: 403 }); return { subject: grant.userId, connectionId: grant.id, expiresAt: grant.expiresAt, // milliseconds since Unix epoch }; }, }); // Hand each `/api/threads/:id/ws` upgrade to this same long-lived instance. // `existingThreadHost.upgrade` comes from statewire-node. await listenFetch({ fetch: (request) => access.fetch(request), upgrade: (request) => access.upgrade(request), }, { port: 3000 }); ``` `listenFetch` comes from `@assistant-ui/pinboard/node`; `access.upgrade` returns a refusal Response or a Pinboard `Accept`, which it completes. With another server, accept the `statewire.v1` subprotocol and call `accepted.attach(socket)`. The [reference app](https://github.com/assistant-ui/tress/blob/main/examples/existing-app/server.mjs) shows a full server. `authorize` receives a `request` whose headers combine the upgrade's headers with the socket's first-message headers, and the adapter authorizes the socket before the host sees any frame. `access.fetch` still serves `/stream` and `/frames` for older Tress CLI releases. `@tress/access` is a private source-preview package in `packages/access`, not a published npm dependency. Use a TypeScript-aware build or Node 24. Your app supplies the host, canonical origin, thread, and authorization function. The [reference app](/docs/local) demonstrates this optional setup. To revoke a connection, persist the decision in an authenticated application action, then notify the adapter: ```ts await app.terminalGrants.revoke(connectionId, currentUser.id); access.revoke(connectionId); ``` Your app checks ownership and stores the revocation durably. `access.revoke` closes that grant's open sockets and denies subsequent attachments on this adapter. After restart, `authorize` must still reject revoked credentials. Give the browser and terminal separate grant IDs if they need independent revocation. Disconnecting a client does not undo admitted commands or stop the agent's run. For multiple workers, broadcast revocation to every worker holding an affected socket. A database update alone cannot close a socket on another process. The adapter also closes sockets at expiry, even when the thread is quiet. A managed access service remains future, optional work. Neither it nor this adapter is required to reuse an application's existing authorized host. ## Sitemap See the full [sitemap](/sitemap.md) for all pages. Docs-scoped sitemap: [/docs/sitemap.md](/docs/sitemap.md). Well-known sitemap: [/.well-known/sitemap.md](/.well-known/sitemap.md).