mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-26 10:07:24 +01:00
<!-- Write all pull request text in Simplified Technical English
(ASD-STE100): short sentences, one instruction per sentence, simple
approved vocabulary, and the active voice. -->
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - Agents use Google Workspace through a connection. A connection can
use Paperclip's own Google sign-in ("Connect with Paperclip") or the
customer's own Google OAuth client
> - Paperclip's own Google sign-in goes through the Paperclip Cloud
broker with Paperclip's verified Google client
> - Google's restricted-scope verification and security assessment cover
only the servers that Paperclip operates
> - The owner decided on 2026-11-23: Paperclip's own Google sign-in is
for Paperclip Cloud workspaces only. Self-hosted instances use their own
Google OAuth client
> - This pull request hides and refuses Paperclip's own Google sign-in
on self-hosted instances, keeps saved connections working, and gives
clear guidance
> - The benefit is that restricted-scope Google tokens stay on servers
that Paperclip operates, and self-hosted users always see a path that
works
## Linked Issues or Issue Description
Refs #22635
**What happened?**
A self-hosted Paperclip instance offered "Connect with Paperclip" for
Google Workspace and other Google apps. That sign-in uses Paperclip's
verified Google client. It sends restricted-scope Google tokens to a
server that Paperclip does not operate. A self-hosted instance also
offered Paperclip Cloud enrollment only to use that sign-in.
**What did you expect to happen?**
Paperclip's own Google sign-in is available only on Paperclip Cloud
workspaces. A self-hosted instance shows only "Use your own Google OAuth
app", with a short reason and a link to a setup guide. Saved Google
connections on a self-hosted instance keep working until a sunset date
that the owner sets on the Paperclip Cloud broker. After that date, the
connection tells the user to reconnect with their own Google client.
Paperclip Cloud workspaces do not change.
**Steps to reproduce**
1. Run Paperclip without Paperclip Cloud markers (self-hosted).
2. Turn on the Google Workspace connector in **Settings →
Experimental**.
3. Open **Apps** and choose **Google Workspace** or **Gmail**.
4. Before this change: setup offers "Connect with Paperclip" or asks to
connect the instance to Paperclip Cloud first.
## What Changed
- `packages/shared/src/google-sign-in-deployment.ts`: new
`appDefinitionForDeployment` and `isCloudOnlyConnectionMethod`. On a
self-hosted instance they remove every Paperclip-managed Google method.
Other providers do not change. New shared message
`GOOGLE_SIGN_IN_CLOUD_ONLY_MESSAGE`.
- `server/src/services/google-sign-in-availability.ts`:
`paperclipGoogleSignInAvailable()` returns `isCloudManagedInstance()`.
This is the existing single Cloud check. No new environment variable.
- `appWithPaperclipCloudConnectorAvailability` (gallery and setup
validation) uses that check. A self-hosted gallery and setup do not show
managed Google methods.
- `authorization-start.ts`: a self-hosted instance refuses to start a
managed Google authorization before it calls Paperclip Cloud (`422`,
code `google_sign_in_cloud_only`). A broker refusal with
`PROVIDER_CLOUD_ONLY` maps to the same message, also on a Cloud
instance.
- `paperclip-cloud-connector.ts`: `PROVIDER_CLOUD_ONLY` is a known
broker reason.
- `grant-refresh.ts`: when the broker refuses a refresh with
`PROVIDER_CLOUD_ONLY` (after the sunset), the grant is marked for
reconnection. The error keeps code `oauth_reauthorization_required`,
adds reason `google_sign_in_cloud_only`, and tells the user to reconnect
with their own Google OAuth client.
- `ConnectionSetupFlow.tsx`: on a self-hosted instance (no `cloud` block
in health), Google setup uses the filtered definition. It never shows
Cloud enrollment for Google. The customer-owned Google client step shows
this note and a link to Google's client guide.
- Docs: `doc/connections/GOOGLE-WORKSPACE.md` ("Who can use Paperclip's
Google sign-in", "Self-hosted instances: use your own Google client")
and a dated decision row in `doc/memory/google-connector-memory.md`.
- `tests/runner-e2e/source-sdk-public-inputs.json`: pins the new shared
files (generated).
## Verification
- Red first, then green:
- `packages/shared/src/google-sign-in-deployment.test.ts` fails without
the new module and passes with it (4 tests).
- With the source files from `origin/master`, these new tests fail: 2 in
`server/src/__tests__/tool-access-service.test.ts` (self-hosted refusal,
refresh guidance and saved-connection refresh; broker refusal mapping),
3 in `server/src/services/paperclip-cloud-connector.test.ts`, and the
self-hosted test in `ui/src/pages/apps/AppsConnect.test.tsx`. With the
change they pass.
- Cloud path proof: "never shows the self-hosted Google notice on
Paperclip Cloud" (UI), "leaves every app unchanged on Paperclip Cloud"
(shared, identity check on every definition), "decides Cloud or
self-hosted from the instance's Cloud markers" (server). All existing
managed Google service tests (515) pass in a Cloud context.
- `npx vitest run src/__tests__/tool-access-service.test.ts` (server):
517 passed.
- `npx vitest run src/services/paperclip-cloud-connector.test.ts`
(server): 71 passed.
- `npx vitest run src/pages/apps/AppsConnect.test.tsx
src/features/connections` (ui): all passed.
- `npx vitest run` for `agent-connection-catalog.integration`,
`tool-gateway-service`, `google-access-removal`,
`connection-management.integration`, `generic-mcp-connection`,
`remote-mcp-connectors` and `plugin-connectors-circleback-parity`
(server): 446 passed, 1 skipped.
- `npx vitest run` in `packages/shared`: 1383 passed.
- `pnpm verify` exits 2 because `doc/connections/GOOGLE-WORKSPACE.md` is
a global CI input. Required CI supplies the broad coverage.
- Not run: no requests to staging, production, or Google. The reconnect
of a saved managed Google connection on a self-hosted instance after the
sunset was not run end to end in a browser.
## Risks
- Behavior change for self-hosted instances: new Paperclip-managed
Google connections stop. Saved ones keep refreshing until the broker
sunset (not set yet).
- The decision uses `isCloudManagedInstance()`. If a Cloud workspace
does not have its Cloud markers, it shows the self-hosted path. Cloud
stacks always have the tenant token, so the risk is low.
- Works with old and new brokers. An old broker does not refuse. The app
check still hides and refuses the method. A new broker refuses
self-hosted instances, and the app maps the refusal to the same
guidance.
- Deploy order: the broker change (paperclip-cloud) and this change can
deploy in any order.
> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.
## Model Used
- Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use
(shell, file edits, test runs).
## Checklist
- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
446 lines
26 KiB
TypeScript
446 lines
26 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import express from "express";
|
|
import request from "supertest";
|
|
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
|
import {
|
|
activityLog,
|
|
companies,
|
|
companySecrets,
|
|
connectionGrants,
|
|
createDb,
|
|
googlePendingRevocations,
|
|
toolAccessAuditEvents,
|
|
toolConnections,
|
|
} from "@paperclipai/db";
|
|
import { and, eq } from "drizzle-orm";
|
|
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
|
|
import {
|
|
connectManagedGmail as connectManagedGmailFixture,
|
|
deleteManagedGoogleTestRows,
|
|
fakeConnector,
|
|
liveSecretCount as countLiveSecrets,
|
|
REFRESH_TOKEN,
|
|
} from "./helpers/managed-google-connection.js";
|
|
import { toolAccessRoutes } from "../routes/tool-access.js";
|
|
import { instanceSettingsService } from "../services/instance-settings.js";
|
|
import { errorHandler } from "../middleware/index.js";
|
|
import { PaperclipCloudConnectorError, type PaperclipCloudConnector } from "../services/paperclip-cloud-connector.js";
|
|
import {
|
|
createGooglePendingRevocation,
|
|
GOOGLE_REVOCATION_LIFETIME_MS,
|
|
googleRevocationRetryDelayMs,
|
|
queueGoogleRevocationFromSecret,
|
|
revokeGoogleAccessForRemovedInstance,
|
|
} from "../services/tool-access/connections/google-pending-revocation.js";
|
|
import { secretService } from "../services/secrets.js";
|
|
import { deleteCompany } from "../services/company-deletion.js";
|
|
import { listConnectionLifecycleEvents } from "../services/tool-connection-activity.js";
|
|
|
|
vi.mock("../services/google-sign-in-availability.js", () => ({ paperclipGoogleSignInAvailable: () => true }));
|
|
|
|
vi.mock("../services/paperclip-cloud-connector-status.js", () => ({
|
|
reconcilePaperclipCloudConnectorEnrollmentStatus: async (_env: unknown, _request: unknown, onRemoved?: () => Promise<unknown>) => {
|
|
await onRemoved?.();
|
|
return { configured: false, status: "not_configured", brokerBaseUrl: "https://broker.example", instanceId: null, environment: "development", origins: [] };
|
|
},
|
|
}));
|
|
|
|
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
|
|
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
|
|
|
|
describeEmbeddedPostgres("retrying an unconfirmed Google access removal", () => {
|
|
let db!: ReturnType<typeof createDb>;
|
|
let tempDb: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | null = null;
|
|
|
|
beforeAll(async () => {
|
|
tempDb = await startEmbeddedPostgresTestDatabase("paperclip-google-retry-");
|
|
db = createDb(tempDb.connectionString, { maxConnections: 4 });
|
|
await instanceSettingsService(db).updateExperimental({ enableGoogleWorkspaceConnector: true });
|
|
}, 20_000);
|
|
|
|
afterEach(async () => {
|
|
vi.restoreAllMocks();
|
|
vi.unstubAllEnvs();
|
|
await deleteManagedGoogleTestRows(db);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await db?.$client.end({ timeout: 1 });
|
|
await tempDb?.cleanup();
|
|
});
|
|
|
|
const connectManagedGmail = (grantKind: "user" | "organization" = "user") => connectManagedGmailFixture(db, grantKind);
|
|
const liveSecretCount = (companyId: string) => countLiveSecrets(db, companyId);
|
|
|
|
function worker(connector: ReturnType<typeof fakeConnector>, at: () => number) {
|
|
return createGooglePendingRevocation(db, { currentCloudConnector: () => connector as unknown as PaperclipCloudConnector, now: at });
|
|
}
|
|
|
|
async function drain(connector: ReturnType<typeof fakeConnector>, at: number) {
|
|
await worker(connector, () => at).drain(new AbortController().signal);
|
|
}
|
|
|
|
async function retryAudit(companyId: string) {
|
|
const rows = await db.select().from(toolAccessAuditEvents).where(and(
|
|
eq(toolAccessAuditEvents.companyId, companyId), eq(toolAccessAuditEvents.action, "connection.google_access_retry")));
|
|
return rows.map((row) => ({ outcome: row.outcome, reasonCode: row.reasonCode }));
|
|
}
|
|
|
|
async function retryActivity(companyId: string) {
|
|
const rows = await db.select().from(activityLog).where(eq(activityLog.companyId, companyId));
|
|
return rows.filter((row) => row.action.startsWith("tool_connection.google_access_")).map((row) => row.action);
|
|
}
|
|
|
|
it("keeps an unconfirmed token only in the pending record, encrypted, and retries until Google confirms", async () => {
|
|
const fixture = await connectManagedGmail();
|
|
fixture.connector.revoke.mockResolvedValueOnce({ outcome: "provider_revocation_failed" });
|
|
const startedAt = Date.now();
|
|
|
|
const response = await request(fixture.app).delete(`/api/tool-connections/${fixture.connectionId}`);
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body.removal.providerAccess).toEqual({ provider: "google", result: "retrying", outcomes: ["provider_revocation_failed"] });
|
|
expect(await liveSecretCount(fixture.company.id)).toBe(0);
|
|
const [pending] = await db.select().from(googlePendingRevocations);
|
|
expect(pending).toMatchObject({ companyId: fixture.company.id, connectionId: fixture.connectionId, grantId: fixture.grant.id,
|
|
subject: fixture.userId, profile: "gmail.draft", reason: "connection_removed", attempts: 1, lastOutcome: "provider_revocation_failed" });
|
|
expect(JSON.stringify(pending)).not.toContain(REFRESH_TOKEN);
|
|
expect(pending!.nextAttemptAt.getTime() - startedAt).toBeGreaterThanOrEqual(50_000);
|
|
expect(pending!.giveUpAt.getTime() - pending!.createdAt.getTime()).toBe(GOOGLE_REVOCATION_LIFETIME_MS);
|
|
const listed = await secretService(db).list(fixture.company.id);
|
|
expect(JSON.stringify(listed)).not.toContain(REFRESH_TOKEN);
|
|
|
|
await drain(fixture.connector, startedAt + 10_000);
|
|
expect(fixture.connector.revoke).toHaveBeenCalledTimes(1);
|
|
|
|
fixture.connector.revoke.mockResolvedValueOnce({ outcome: "revocation_in_progress" });
|
|
await drain(fixture.connector, pending!.nextAttemptAt.getTime());
|
|
expect(fixture.connector.revoke).toHaveBeenCalledTimes(2);
|
|
const [second] = await db.select().from(googlePendingRevocations);
|
|
expect(second).toMatchObject({ attempts: 2, lastOutcome: "revocation_in_progress" });
|
|
|
|
fixture.connector.revoke.mockResolvedValueOnce({ outcome: "revoked" });
|
|
await drain(fixture.connector, second!.nextAttemptAt.getTime());
|
|
expect(fixture.connector.revoke).toHaveBeenLastCalledWith(expect.objectContaining({
|
|
subject: fixture.userId, companyId: fixture.company.id, profile: "gmail.draft", token: REFRESH_TOKEN }));
|
|
expect(await db.select().from(googlePendingRevocations)).toEqual([]);
|
|
expect(await retryAudit(fixture.company.id)).toEqual([{ outcome: "success", reasonCode: "ended" }]);
|
|
expect(await retryActivity(fixture.company.id)).toEqual(["tool_connection.google_access_ended"]);
|
|
const lifecycle = await listConnectionLifecycleEvents(db, { companyId: fixture.company.id, connectionIds: [fixture.connectionId], limit: 50 });
|
|
expect(lifecycle.map((event) => event.type)).toContain("google_access_ended");
|
|
});
|
|
|
|
it("keeps the token before calling Cloud, so a restart during the call still revokes it", async () => {
|
|
const fixture = await connectManagedGmail();
|
|
let queuedDuringCall = 0;
|
|
fixture.connector.revoke.mockImplementationOnce(async () => {
|
|
queuedDuringCall = (await db.select().from(googlePendingRevocations)).length;
|
|
throw new PaperclipCloudConnectorError("Paperclip Cloud connector is unavailable", "CONNECTOR_UNAVAILABLE");
|
|
});
|
|
|
|
await request(fixture.app).delete(`/api/tool-connections/${fixture.connectionId}/grants/${fixture.grant.id}`);
|
|
|
|
expect(queuedDuringCall).toBe(1);
|
|
await db.update(googlePendingRevocations).set({ attempts: 0, nextAttemptAt: new Date(Date.now() + 120_000) });
|
|
const fresh = fakeConnector(fixture.company.id, fixture.userId);
|
|
await drain(fresh, Date.now() + 130_000);
|
|
expect(fresh.revoke).toHaveBeenCalledWith(expect.objectContaining({ token: REFRESH_TOKEN, subject: fixture.userId }));
|
|
expect(await db.select().from(googlePendingRevocations)).toEqual([]);
|
|
});
|
|
|
|
it.each(["binding_not_known", "other_connections_remain", "provider_revocation_disabled"] as const)(
|
|
"stops retrying and deletes the token when Cloud answers %s", async (outcome) => {
|
|
const fixture = await connectManagedGmail();
|
|
fixture.connector.revoke.mockResolvedValueOnce({ outcome: "provider_revocation_failed" });
|
|
await request(fixture.app).delete(`/api/tool-connections/${fixture.connectionId}`);
|
|
const [pending] = await db.select().from(googlePendingRevocations);
|
|
|
|
fixture.connector.revoke.mockResolvedValueOnce({ outcome });
|
|
await drain(fixture.connector, pending!.nextAttemptAt.getTime());
|
|
|
|
expect(await db.select().from(googlePendingRevocations)).toEqual([]);
|
|
const result = outcome === "other_connections_remain" ? "other_connections_remain" : "not_ended";
|
|
expect(await retryAudit(fixture.company.id)).toEqual([{ outcome: result === "not_ended" ? "failure" : "success", reasonCode: result }]);
|
|
expect(await retryActivity(fixture.company.id)).toEqual(["tool_connection.google_access_not_ended"]);
|
|
});
|
|
|
|
it("backs off from one minute to six hours and gives up after 30 days with a final entry", async () => {
|
|
expect([1, 2, 3, 9, 10, 50].map(googleRevocationRetryDelayMs)).toEqual([60_000, 120_000, 240_000, 15_360_000, 21_600_000, 21_600_000]);
|
|
const fixture = await connectManagedGmail();
|
|
fixture.connector.revoke.mockResolvedValue({ outcome: "provider_revocation_failed" });
|
|
await request(fixture.app).delete(`/api/tool-connections/${fixture.connectionId}`);
|
|
const [pending] = await db.select().from(googlePendingRevocations);
|
|
|
|
await drain(fixture.connector, pending!.giveUpAt.getTime() - 1_000);
|
|
const [last] = await db.select().from(googlePendingRevocations);
|
|
expect(last!.nextAttemptAt.getTime()).toBe(pending!.giveUpAt.getTime());
|
|
const calls = fixture.connector.revoke.mock.calls.length;
|
|
|
|
await drain(fixture.connector, pending!.giveUpAt.getTime());
|
|
expect(fixture.connector.revoke).toHaveBeenCalledTimes(calls);
|
|
expect(await db.select().from(googlePendingRevocations)).toEqual([]);
|
|
expect(await retryAudit(fixture.company.id)).toEqual([{ outcome: "failure", reasonCode: "gave_up" }]);
|
|
expect(await retryActivity(fixture.company.id)).toEqual(["tool_connection.google_access_not_ended"]);
|
|
});
|
|
|
|
it("keeps retrying past an unused copy of the token, and stops without revoking once a live connection uses it", async () => {
|
|
const fixture = await connectManagedGmail();
|
|
fixture.connector.revoke.mockResolvedValueOnce({ outcome: "broker_error" });
|
|
await request(fixture.app).delete(`/api/tool-connections/${fixture.connectionId}`);
|
|
const [pending] = await db.select().from(googlePendingRevocations);
|
|
const copy = await secretService(db).create(fixture.company.id, { name: "Copied token", key: "copied_token", provider: "local_encrypted", value: REFRESH_TOKEN });
|
|
|
|
fixture.connector.revoke.mockResolvedValueOnce({ outcome: "broker_error" });
|
|
await drain(fixture.connector, pending!.nextAttemptAt.getTime());
|
|
expect(fixture.connector.revoke).toHaveBeenCalledTimes(2);
|
|
const [unreferenced] = await db.select().from(googlePendingRevocations);
|
|
expect(unreferenced).toMatchObject({ attempts: 2 });
|
|
|
|
await db.update(toolConnections).set({ status: "active", credentialSecretRefs: [{ secretId: copy.id, versionSelector: "latest", configPath: "oauth.refresh_token", required: false, label: "Refresh" }] as never })
|
|
.where(eq(toolConnections.id, fixture.connectionId));
|
|
await drain(fixture.connector, unreferenced!.nextAttemptAt.getTime());
|
|
|
|
expect(fixture.connector.revoke).toHaveBeenCalledTimes(2);
|
|
expect(await db.select().from(googlePendingRevocations)).toEqual([]);
|
|
expect(await retryAudit(fixture.company.id)).toEqual([{ outcome: "success", reasonCode: "other_connections_remain" }]);
|
|
});
|
|
|
|
it("still revokes when the process stopped before the removed grant's own secret was deleted", async () => {
|
|
const fixture = await connectManagedGmail();
|
|
const refreshSecretId = fixture.grant.credentialSecretRefs.find((ref) => ref.configPath === "oauth.refresh_token")!.secretId;
|
|
const [secret] = await db.select({ latestVersion: companySecrets.latestVersion }).from(companySecrets).where(eq(companySecrets.id, refreshSecretId));
|
|
await db.update(connectionGrants).set({ status: "revoked", revokedAt: new Date() }).where(eq(connectionGrants.id, fixture.grant.id));
|
|
await queueGoogleRevocationFromSecret(db, {
|
|
companyId: fixture.company.id, connectionId: fixture.connectionId, grantId: fixture.grant.id, subject: fixture.userId,
|
|
profile: "gmail.draft", secretId: refreshSecretId, version: secret!.latestVersion, reason: "grant_revoked",
|
|
});
|
|
const [pending] = await db.select().from(googlePendingRevocations);
|
|
expect(pending!.sourceSecretId).toBe(refreshSecretId);
|
|
|
|
await drain(fixture.connector, pending!.nextAttemptAt.getTime());
|
|
|
|
expect(fixture.connector.revoke).toHaveBeenCalledWith(expect.objectContaining({ token: REFRESH_TOKEN }));
|
|
expect(await db.select().from(googlePendingRevocations)).toEqual([]);
|
|
expect(await retryAudit(fixture.company.id)).toEqual([{ outcome: "success", reasonCode: "ended" }]);
|
|
});
|
|
|
|
it("stops without revoking when the grant was signed in again and its own secret holds the same token", async () => {
|
|
const fixture = await connectManagedGmail();
|
|
const refreshSecretId = fixture.grant.credentialSecretRefs.find((ref) => ref.configPath === "oauth.refresh_token")!.secretId;
|
|
const [secret] = await db.select({ latestVersion: companySecrets.latestVersion }).from(companySecrets).where(eq(companySecrets.id, refreshSecretId));
|
|
await queueGoogleRevocationFromSecret(db, {
|
|
companyId: fixture.company.id, connectionId: fixture.connectionId, grantId: fixture.grant.id, subject: fixture.userId,
|
|
profile: "gmail.draft", secretId: refreshSecretId, version: secret!.latestVersion, reason: "grant_revoked",
|
|
});
|
|
const [pending] = await db.select().from(googlePendingRevocations);
|
|
|
|
await drain(fixture.connector, pending!.nextAttemptAt.getTime());
|
|
|
|
expect(fixture.connector.revoke).not.toHaveBeenCalled();
|
|
expect(await db.select().from(googlePendingRevocations)).toEqual([]);
|
|
expect(await retryAudit(fixture.company.id)).toEqual([{ outcome: "success", reasonCode: "other_connections_remain" }]);
|
|
});
|
|
|
|
it("keeps the token version that was sent when the grant pins an older version", async () => {
|
|
const fixture = await connectManagedGmail();
|
|
const refreshRef = fixture.grant.credentialSecretRefs.find((ref) => ref.configPath === "oauth.refresh_token")!;
|
|
const [secret] = await db.select({ latestVersion: companySecrets.latestVersion }).from(companySecrets).where(eq(companySecrets.id, refreshRef.secretId));
|
|
await secretService(db).rotate(refreshRef.secretId, { value: "newer-refresh-token-do-not-log" });
|
|
await db.update(connectionGrants).set({
|
|
credentialSecretRefs: fixture.grant.credentialSecretRefs.map((ref) => ref.secretId === refreshRef.secretId ? { ...ref, versionSelector: secret!.latestVersion } : ref),
|
|
}).where(eq(connectionGrants.id, fixture.grant.id));
|
|
fixture.connector.revoke.mockResolvedValueOnce({ outcome: "broker_error" });
|
|
|
|
await request(fixture.app).delete(`/api/tool-connections/${fixture.connectionId}/grants/${fixture.grant.id}`);
|
|
|
|
expect(fixture.connector.revoke).toHaveBeenLastCalledWith(expect.objectContaining({ token: REFRESH_TOKEN }));
|
|
const [pending] = await db.select().from(googlePendingRevocations);
|
|
fixture.connector.revoke.mockResolvedValueOnce({ outcome: "revoked" });
|
|
await drain(fixture.connector, pending!.nextAttemptAt.getTime());
|
|
expect(fixture.connector.revoke).toHaveBeenLastCalledWith(expect.objectContaining({ token: REFRESH_TOKEN }));
|
|
});
|
|
|
|
it("saves the removed instance's revocations before dropping any token, and overlapping sweeps queue each token once", async () => {
|
|
const fixture = await connectManagedGmail();
|
|
const failing = vi.fn(async () => { throw new Error("stopped before the token was dropped"); });
|
|
|
|
await Promise.all([
|
|
revokeGoogleAccessForRemovedInstance(db, failing),
|
|
revokeGoogleAccessForRemovedInstance(db, failing),
|
|
]);
|
|
|
|
expect(await db.select().from(googlePendingRevocations)).toHaveLength(1);
|
|
await drain(fixture.connector, Date.now());
|
|
expect(fixture.connector.revoke).toHaveBeenCalledTimes(1);
|
|
expect(fixture.connector.revoke).toHaveBeenCalledWith(expect.objectContaining({ token: REFRESH_TOKEN }));
|
|
});
|
|
|
|
it.each([
|
|
["a board member", false, 0],
|
|
["an instance admin", true, 1],
|
|
] as const)("starts the removed-instance cleanup from the status check only for %s", async (_label, isInstanceAdmin, rows) => {
|
|
const fixture = await connectManagedGmail();
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use((req, _res, next) => {
|
|
req.actor = {
|
|
type: "board", userId: fixture.userId, sessionId: `session-${randomUUID()}`, userName: "Owner", userEmail: null,
|
|
isInstanceAdmin, source: "session", companyIds: [fixture.company.id],
|
|
memberships: [{ companyId: fixture.company.id, membershipRole: "owner", status: "active" }],
|
|
};
|
|
next();
|
|
});
|
|
app.use("/api", toolAccessRoutes(db, { paperclipCloudConnector: fixture.connector as unknown as PaperclipCloudConnector }));
|
|
app.use(errorHandler);
|
|
|
|
const response = await request(app).get("/api/tools/oauth/cloud-connector/enrollment");
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(await db.select().from(googlePendingRevocations)).toHaveLength(rows);
|
|
expect(await liveSecretCount(fixture.company.id)).toBe(rows === 1 ? 1 : 2);
|
|
});
|
|
|
|
async function pinOlderRefreshVersion(fixture: Awaited<ReturnType<typeof connectManagedGmail>>) {
|
|
const refreshRef = fixture.grant.credentialSecretRefs.find((ref) => ref.configPath === "oauth.refresh_token")!;
|
|
const [secret] = await db.select({ latestVersion: companySecrets.latestVersion }).from(companySecrets).where(eq(companySecrets.id, refreshRef.secretId));
|
|
await secretService(db).rotate(refreshRef.secretId, { value: "newer-refresh-token-do-not-log" });
|
|
await db.update(connectionGrants).set({
|
|
credentialSecretRefs: fixture.grant.credentialSecretRefs.map((ref) => ref.secretId === refreshRef.secretId ? { ...ref, versionSelector: secret!.latestVersion } : ref),
|
|
}).where(eq(connectionGrants.id, fixture.grant.id));
|
|
return { secretId: refreshRef.secretId, pinned: secret!.latestVersion };
|
|
}
|
|
|
|
it("queues the grant's pinned token version when the company is deleted", async () => {
|
|
const fixture = await connectManagedGmail();
|
|
await pinOlderRefreshVersion(fixture);
|
|
|
|
await deleteCompany(db, fixture.company.id);
|
|
await drain(fixture.connector, Date.now());
|
|
|
|
expect(fixture.connector.revoke).toHaveBeenCalledTimes(1);
|
|
expect(fixture.connector.revoke).toHaveBeenCalledWith(expect.objectContaining({ token: REFRESH_TOKEN }));
|
|
});
|
|
|
|
it("judges live use by the version a live grant selects, not the latest version", async () => {
|
|
const fixture = await connectManagedGmail();
|
|
const { secretId, pinned } = await pinOlderRefreshVersion(fixture);
|
|
await queueGoogleRevocationFromSecret(db, {
|
|
companyId: fixture.company.id, connectionId: fixture.connectionId, grantId: fixture.grant.id, subject: fixture.userId,
|
|
profile: "gmail.draft", secretId, version: pinned, reason: "grant_revoked",
|
|
});
|
|
const [held] = await db.select().from(googlePendingRevocations);
|
|
|
|
await drain(fixture.connector, held!.nextAttemptAt.getTime());
|
|
expect(fixture.connector.revoke).not.toHaveBeenCalled();
|
|
expect(await db.select().from(googlePendingRevocations)).toEqual([]);
|
|
|
|
const [latest] = await db.select({ latestVersion: companySecrets.latestVersion }).from(companySecrets).where(eq(companySecrets.id, secretId));
|
|
await queueGoogleRevocationFromSecret(db, {
|
|
companyId: fixture.company.id, connectionId: fixture.connectionId, grantId: fixture.grant.id, subject: fixture.userId,
|
|
profile: "gmail.draft", secretId, version: latest!.latestVersion, reason: "grant_revoked",
|
|
});
|
|
const [unused] = await db.select().from(googlePendingRevocations);
|
|
await drain(fixture.connector, unused!.nextAttemptAt.getTime());
|
|
expect(fixture.connector.revoke).toHaveBeenCalledWith(expect.objectContaining({ token: "newer-refresh-token-do-not-log" }));
|
|
});
|
|
|
|
it("keeps retrying the old token when the user signs in again with a new token during the first call", async () => {
|
|
const fixture = await connectManagedGmail();
|
|
const refreshSecretId = fixture.grant.credentialSecretRefs.find((ref) => ref.configPath === "oauth.refresh_token")!.secretId;
|
|
fixture.connector.revoke.mockImplementationOnce(async () => {
|
|
await secretService(db).rotate(refreshSecretId, { value: "signed-in-again-token-do-not-log" });
|
|
await db.update(connectionGrants).set({ status: "active", revokedAt: null }).where(eq(connectionGrants.id, fixture.grant.id));
|
|
return { outcome: "broker_error" };
|
|
});
|
|
|
|
const response = await request(fixture.app).delete(`/api/tool-connections/${fixture.connectionId}/grants/${fixture.grant.id}`);
|
|
|
|
expect(response.body.providerAccess).toEqual({ provider: "google", result: "retrying", outcomes: ["broker_error"] });
|
|
const [pending] = await db.select().from(googlePendingRevocations);
|
|
fixture.connector.revoke.mockResolvedValueOnce({ outcome: "other_connections_remain" });
|
|
await drain(fixture.connector, pending!.nextAttemptAt.getTime());
|
|
expect(fixture.connector.revoke).toHaveBeenLastCalledWith(expect.objectContaining({ token: REFRESH_TOKEN }));
|
|
expect(await db.select().from(googlePendingRevocations)).toEqual([]);
|
|
});
|
|
|
|
it("moves a pending removal to the removed-instance rule when Cloud removes the instance", async () => {
|
|
const fixture = await connectManagedGmail();
|
|
const refreshSecretId = fixture.grant.credentialSecretRefs.find((ref) => ref.configPath === "oauth.refresh_token")!.secretId;
|
|
const [secret] = await db.select({ latestVersion: companySecrets.latestVersion }).from(companySecrets).where(eq(companySecrets.id, refreshSecretId));
|
|
await queueGoogleRevocationFromSecret(db, {
|
|
companyId: fixture.company.id, connectionId: fixture.connectionId, grantId: fixture.grant.id, subject: fixture.userId,
|
|
profile: "gmail.draft", secretId: refreshSecretId, version: secret!.latestVersion, reason: "grant_revoked",
|
|
});
|
|
|
|
await revokeGoogleAccessForRemovedInstance(db, async () => { throw new Error("stopped before the token was dropped"); });
|
|
|
|
const [pending] = await db.select().from(googlePendingRevocations);
|
|
expect(pending!.reason).toBe("instance_removed");
|
|
await drain(fixture.connector, pending!.nextAttemptAt.getTime());
|
|
expect(fixture.connector.revoke).toHaveBeenCalledWith(expect.objectContaining({ token: REFRESH_TOKEN }));
|
|
});
|
|
|
|
it("keeps one company's pending revocation apart from another company's with the same token", async () => {
|
|
const first = await connectManagedGmail();
|
|
const second = await connectManagedGmail();
|
|
first.connector.revoke.mockResolvedValueOnce({ outcome: "broker_error" });
|
|
second.connector.revoke.mockResolvedValueOnce({ outcome: "revoked" });
|
|
|
|
await request(first.app).delete(`/api/tool-connections/${first.connectionId}`);
|
|
await request(second.app).delete(`/api/tool-connections/${second.connectionId}`);
|
|
|
|
const rows = await db.select().from(googlePendingRevocations);
|
|
expect(rows.map((row) => row.companyId)).toEqual([first.company.id]);
|
|
expect(rows[0]).toMatchObject({ attempts: 1, lastOutcome: "broker_error" });
|
|
});
|
|
|
|
it("treats an older broker refusing a removed instance as retryable", async () => {
|
|
const fixture = await connectManagedGmail();
|
|
fixture.connector.revoke.mockRejectedValue(new PaperclipCloudConnectorError("rejected", "CONNECTOR_REQUEST_FAILED", 403, undefined, "INSTANCE_SUSPENDED"));
|
|
|
|
const response = await request(fixture.app).delete(`/api/tool-connections/${fixture.connectionId}`);
|
|
|
|
expect(response.body.removal.providerAccess).toEqual({ provider: "google", result: "retrying", outcomes: ["broker_error"] });
|
|
const [pending] = await db.select().from(googlePendingRevocations);
|
|
await drain(fixture.connector, pending!.nextAttemptAt.getTime());
|
|
const [after] = await db.select().from(googlePendingRevocations);
|
|
expect(after).toMatchObject({ attempts: 2, lastOutcome: "broker_error" });
|
|
});
|
|
|
|
it("queues every managed Google token when the company is deleted, and revokes it after the company is gone", async () => {
|
|
const fixture = await connectManagedGmail();
|
|
|
|
await deleteCompany(db, fixture.company.id);
|
|
|
|
expect(await db.select().from(companies).where(eq(companies.id, fixture.company.id))).toEqual([]);
|
|
const [pending] = await db.select().from(googlePendingRevocations);
|
|
expect(pending).toMatchObject({ companyId: fixture.company.id, subject: fixture.userId, profile: "gmail.draft", reason: "company_deleted", attempts: 0 });
|
|
expect(JSON.stringify(pending)).not.toContain(REFRESH_TOKEN);
|
|
expect(fixture.connector.revoke).not.toHaveBeenCalled();
|
|
|
|
await drain(fixture.connector, Date.now());
|
|
expect(fixture.connector.revoke).toHaveBeenCalledWith(expect.objectContaining({
|
|
subject: fixture.userId, companyId: fixture.company.id, profile: "gmail.draft", token: REFRESH_TOKEN }));
|
|
expect(await db.select().from(googlePendingRevocations)).toEqual([]);
|
|
});
|
|
|
|
it("revokes every managed Google grant through the broker when Cloud removed the instance, then drops the token", async () => {
|
|
const fixture = await connectManagedGmail();
|
|
const secrets = secretService(db);
|
|
|
|
expect(await revokeGoogleAccessForRemovedInstance(db, (secretId, expectedVersion) => secrets.remove(secretId, { expectedVersion }))).toBe(1);
|
|
|
|
const refreshSecretId = fixture.grant.credentialSecretRefs.find((ref) => ref.configPath === "oauth.refresh_token")!.secretId;
|
|
const [refresh] = await db.select({ id: companySecrets.id }).from(companySecrets).where(eq(companySecrets.id, refreshSecretId));
|
|
expect(refresh).toBeUndefined();
|
|
const [pending] = await db.select().from(googlePendingRevocations);
|
|
expect(pending).toMatchObject({ reason: "instance_removed", connectionId: fixture.connectionId });
|
|
expect(await revokeGoogleAccessForRemovedInstance(db, (secretId, expectedVersion) => secrets.remove(secretId, { expectedVersion }))).toBe(0);
|
|
|
|
await drain(fixture.connector, Date.now());
|
|
expect(fixture.connector.revoke).toHaveBeenCalledWith(expect.objectContaining({ token: REFRESH_TOKEN, subject: fixture.userId }));
|
|
expect(await db.select().from(googlePendingRevocations)).toEqual([]);
|
|
expect(await retryAudit(fixture.company.id)).toEqual([{ outcome: "success", reasonCode: "ended" }]);
|
|
});
|
|
});
|