mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 10:44:52 +01:00
fix: preserve Slack recovery choices and registration secrets
Persist existing-app selection, validate setup-method changes before cleanup, and verify the OAuth continuation in a real browser. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
1c2dad5343
commit
49041e0d8b
10 files changed
+45
-20
No files matched your search
@@ -34,7 +34,8 @@ the draft and all saved non-secret progress.
|
||||
|
||||
**Create manually** uses the same generated manifest. **Use an existing app**
|
||||
keeps the existing credential-entry workflow. Both options remain available for
|
||||
recovery on the same draft. The setup prompt describes browser assistance and
|
||||
recovery on the same draft. The selected automatic, manual, or existing-app method
|
||||
is saved, so an existing-app draft reopens at credential entry. The setup prompt describes browser assistance and
|
||||
requires the user to enter the temporary token and handle login/admin approval.
|
||||
|
||||
## Deployment
|
||||
|
||||
@@ -188,7 +188,7 @@ export interface SlackAppConfiguration {
|
||||
}
|
||||
|
||||
export interface ChatEndpointSetupState {
|
||||
slackSetupMethod?: "automatic" | "manual";
|
||||
slackSetupMethod?: "automatic" | "manual" | "existing";
|
||||
slackRegistration?: import("../slack-app-manifest.js").SlackRegistrationState;
|
||||
slackOAuthCallbackUri?: string | null;
|
||||
github?: {
|
||||
@@ -485,7 +485,7 @@ export interface CreateChatEndpointInput {
|
||||
}
|
||||
|
||||
export interface UpdateChatEndpointInput {
|
||||
slackSetupMethod?: "automatic" | "manual";
|
||||
slackSetupMethod?: "automatic" | "manual" | "existing";
|
||||
slackApp?: SlackAppConfiguration;
|
||||
communicationInstructions?: string;
|
||||
allowDirectMessages?: boolean;
|
||||
|
||||
@@ -86,7 +86,7 @@ export const updateChatEndpointSchema = z
|
||||
.object({
|
||||
communicationInstructions: multilineTextSchema.pipe(z.string().trim().max(4000)).optional(),
|
||||
slackApp: slackAppConfigurationSchema.optional(),
|
||||
slackSetupMethod: z.enum(["automatic", "manual"]).optional(),
|
||||
slackSetupMethod: z.enum(["automatic", "manual", "existing"]).optional(),
|
||||
allowDirectMessages: z.boolean().optional(),
|
||||
allowGroupChats: z.boolean().optional(),
|
||||
allowUnlinkedPeople: z.boolean().optional(),
|
||||
|
||||
@@ -4062,6 +4062,8 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => {
|
||||
expect(await f.service.listPrincipals(f.endpoint.id)).toEqual([]);
|
||||
const [row] = await db.select().from(chatSlackRegistrations).where(eq(chatSlackRegistrations.endpointId, f.endpoint.id));
|
||||
expect(Object.keys(row.secretIds)).toEqual(["clientSecret"]);
|
||||
await request(f.app).patch(`/api/chat-endpoints/${f.endpoint.id}`).send({ slackSetupMethod: "manual" }).expect(409);
|
||||
expect((await f.service.slackRegistration.registration(f.endpoint.id))?.secretIds).toEqual(row.secretIds);
|
||||
const body = JSON.stringify({ type: "url_verification", challenge: "auto-challenge" });
|
||||
await f.service.handleWebhook(saved.publicId, "slack", signedSlackWebhookRequest({ url: saved.setup.webhookUrl!, body, contentType: "application/json", signingSecret }));
|
||||
expect((await f.service.get(f.endpoint.id)).setup.webhookVerifiedAt).toBeTruthy();
|
||||
|
||||
@@ -789,7 +789,7 @@ const chatAdapterCapabilitiesResponseSchema = z
|
||||
const chatEndpointSetupResponseSchema = z
|
||||
.object({
|
||||
step: z.enum(["choose_agent", "provider_setup", "test", "complete"]),
|
||||
slackSetupMethod: z.enum(["automatic", "manual"]).optional(),
|
||||
slackSetupMethod: z.enum(["automatic", "manual", "existing"]).optional(),
|
||||
slackRegistration: slackRegistrationStateSchema.optional(),
|
||||
slackOAuthCallbackUri: z.string().nullable().optional(),
|
||||
slackApp: slackAppConfigurationSchema.optional(),
|
||||
|
||||
@@ -6076,6 +6076,9 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) {
|
||||
}
|
||||
values.communicationInstructions = input.communicationInstructions;
|
||||
}
|
||||
if (input.slackSetupMethod && (existing.endpoint.provider !== "slack" || existing.endpoint.botExternalId || existing.endpoint.status !== "draft")) {
|
||||
throw conflict("Setup method can only change before connecting the app");
|
||||
}
|
||||
if (input.slackApp || input.slackSetupMethod) {
|
||||
const [registration] = await db.select().from(chatSlackRegistrations).where(eq(chatSlackRegistrations.endpointId, endpointId));
|
||||
if (registration && registration.status !== "failed") {
|
||||
@@ -6085,8 +6088,6 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) {
|
||||
}
|
||||
}
|
||||
if (input.slackSetupMethod) {
|
||||
if (existing.endpoint.provider !== "slack" || existing.endpoint.botExternalId || existing.endpoint.status !== "draft")
|
||||
throw conflict("Setup method can only change before connecting the app");
|
||||
values.setup = { ...existing.endpoint.setup, slackSetupMethod: input.slackSetupMethod };
|
||||
}
|
||||
if (input.slackApp) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { oauthCallbackInterstitialHtml } from "../../server/src/lib/oauth-browser-return";
|
||||
import { createServer } from "node:http";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { expect, test, type Page } from "@playwright/test";
|
||||
|
||||
@@ -114,6 +115,28 @@ async function exerciseGitHubReviewSetup(page: Page, mock: ChatMock, seed: Seed,
|
||||
* chat-control-plane mock live in ./chat-adapters-ui.shared.ts; the
|
||||
* messaging-flow describes run in chat-adapters-ui-messaging.spec.ts.
|
||||
*/
|
||||
test("Slack OAuth continuation reloads once from the callback origin without echoing the code", async ({ page }) => {
|
||||
const sites: Array<string | undefined> = [];
|
||||
const server = createServer((req, res) => {
|
||||
sites.push(req.headers["sec-fetch-site"] as string | undefined);
|
||||
res.setHeader("Content-Type", "text/html");
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
if (sites.length === 1) res.end(oauthCallbackInterstitialHtml());
|
||||
else res.end("<h1>Callback completed</h1>");
|
||||
});
|
||||
await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve));
|
||||
try {
|
||||
const address = server.address();
|
||||
if (!address || typeof address === "string") throw new Error("Missing callback fixture listener");
|
||||
const callback = `http://127.0.0.1:${address.port}/callback?code=fixture-code`;
|
||||
await page.route("https://slack.test/consent", route => route.fulfill({ contentType: "text/html", body: `<a href="${callback}">Approve installation</a>` }));
|
||||
await page.goto("https://slack.test/consent");
|
||||
await page.getByRole("link", { name: "Approve installation" }).click();
|
||||
await expect(page.getByRole("heading", { name: "Callback completed" })).toBeVisible();
|
||||
expect(sites.slice(0, 2)).toEqual(["cross-site", "same-origin"]);
|
||||
} finally { await new Promise<void>((resolve, reject) => server.close(error => error ? reject(error) : resolve())); }
|
||||
});
|
||||
|
||||
test.describe.serial("native chat adapter UI", () => {
|
||||
test.setTimeout(180_000);
|
||||
|
||||
@@ -128,11 +151,10 @@ test.describe.serial("native chat adapter UI", () => {
|
||||
const mock = await installChatControlPlaneMock(page, slack, seed, { enableChatConnectors: true, automaticSlack: true });
|
||||
const rootUrl = new URL(`/${seed.prefix}/apps/chat/connect?provider=slack&purpose=chat&resume=endpoint-slack`, test.info().project.use.baseURL).href;
|
||||
const callbackUrl = new URL("/api/chat-slack/oauth/callback?state=fixture-state&code=fixture-code", rootUrl).href;
|
||||
const callbackSites: string[] = [];
|
||||
let callbackRequests = 0;
|
||||
await page.route("**/api/chat-slack/oauth/callback?**", async route => {
|
||||
const site = route.request().headers()["sec-fetch-site"];
|
||||
callbackSites.push(site);
|
||||
if (site === "cross-site") {
|
||||
callbackRequests += 1;
|
||||
if (callbackRequests === 1) {
|
||||
const body = oauthCallbackInterstitialHtml();
|
||||
expect(body).not.toContain("fixture-code");
|
||||
await route.fulfill({ contentType: "text/html", body });
|
||||
@@ -142,9 +164,7 @@ test.describe.serial("native chat adapter UI", () => {
|
||||
}
|
||||
});
|
||||
await page.route("https://slack.test/**", async route => {
|
||||
if (route.request().url().endsWith("/approve")) {
|
||||
await route.fulfill({ status: 302, headers: { location: callbackUrl } });
|
||||
} else await route.fulfill({ contentType: "text/html", body: '<h1>Fixture Slack consent</h1><a href="https://slack.test/approve">Approve installation</a>' });
|
||||
await route.fulfill({ contentType: "text/html", body: `<h1>Fixture Slack consent</h1><a href="${callbackUrl}">Approve installation</a>` });
|
||||
});
|
||||
await page.goto(`/${seed.prefix}/apps/chat/connect?provider=slack&purpose=chat&agentId=${seed.agentId}`);
|
||||
await page.getByRole("button", { name: "Continue", exact: true }).click();
|
||||
@@ -172,7 +192,7 @@ test.describe.serial("native chat adapter UI", () => {
|
||||
await page.getByRole("button", { name: "Continue to message test", exact: true }).click();
|
||||
await expect(page.getByRole("heading", { name: "Try Maya in Slack", exact: true })).toBeVisible();
|
||||
expect(mock.slackCreations).toBe(1); expect(mock.slackInstallations).toBe(1);
|
||||
expect(callbackSites).toEqual(["cross-site", "same-origin"]);
|
||||
expect(callbackRequests).toBe(2);
|
||||
await page.screenshot({ path: test.info().outputPath("automatic-slack-identity-linked.png"), fullPage: true });
|
||||
await page.getByRole("button", { name: "Skip test and finish", exact: true }).click();
|
||||
await expect(page).toHaveURL(/\/apps\/chat\/endpoint-slack\/settings$/);
|
||||
@@ -196,6 +216,7 @@ test.describe.serial("native chat adapter UI", () => {
|
||||
await expect(page.getByRole("heading", { name: "Add Slack credentials", exact: true })).toBeVisible();
|
||||
await page.reload();
|
||||
await expect(page.getByRole("heading", { name: "Add Slack credentials", exact: true })).toBeVisible();
|
||||
await expect(page.getByRole("heading", { name: "Add Slack credentials", exact: true })).toBeVisible();
|
||||
expect(mock.slackCreations).toBe(0);
|
||||
});
|
||||
|
||||
|
||||
@@ -281,7 +281,7 @@ export function endpointFixture(provider: ProviderCase, seed: Seed) {
|
||||
proactiveDirectMessages: false,
|
||||
},
|
||||
setup: {
|
||||
slackSetupMethod: undefined as "automatic" | "manual" | undefined,
|
||||
slackSetupMethod: undefined as "automatic" | "manual" | "existing" | undefined,
|
||||
slackRegistration: undefined as SlackRegistrationState | undefined,
|
||||
slackApp: undefined as SlackAppConfiguration | undefined,
|
||||
slackOAuthCallbackUri: "https://paperclip.example.test/api/chat-slack/oauth/callback",
|
||||
@@ -509,7 +509,7 @@ export async function installChatControlPlaneMock(
|
||||
if (method === "PATCH") {
|
||||
const body = bodyOf(route);
|
||||
if (body.slackApp) endpoint.setup.slackApp = body.slackApp as SlackAppConfiguration;
|
||||
if (body.slackSetupMethod) endpoint.setup.slackSetupMethod = body.slackSetupMethod as "automatic" | "manual";
|
||||
if (body.slackSetupMethod) endpoint.setup.slackSetupMethod = body.slackSetupMethod as "automatic" | "manual" | "existing";
|
||||
if (typeof body.allowDirectMessages === "boolean")
|
||||
state.allowDirectMessages = body.allowDirectMessages;
|
||||
if (typeof body.allowGroupChats === "boolean")
|
||||
|
||||
@@ -136,7 +136,7 @@ export interface ChatEndpoint {
|
||||
messagingEndpoint?: string | null;
|
||||
command?: string | null;
|
||||
slackApp?: SlackAppConfiguration;
|
||||
slackSetupMethod?: "automatic" | "manual";
|
||||
slackSetupMethod?: "automatic" | "manual" | "existing";
|
||||
slackRegistration?: SlackRegistrationState;
|
||||
slackOAuthCallbackUri?: string | null;
|
||||
webhookVerifiedAt?: string | null;
|
||||
|
||||
@@ -428,7 +428,7 @@ function ChatSdkEndpointSetup() {
|
||||
: tryStep
|
||||
: automaticSlack && endpoint.setup?.slackRegistration?.status === "credentials_saved" ? 2
|
||||
: isSlack && endpoint.providerAccountId && !repairing ? 3
|
||||
: isSlack && (slackCredentialsReady || repairing || automaticSlack && endpoint.setup?.slackRegistration?.appId) ? 2 : 1
|
||||
: isSlack && (slackCredentialsReady || repairing || endpoint.setup?.slackSetupMethod === "existing" || automaticSlack && endpoint.setup?.slackRegistration?.appId) ? 2 : 1
|
||||
: 0;
|
||||
const step = Math.min(viewedStep ?? availableStep, availableStep);
|
||||
const avatarAgent = useQuery({
|
||||
@@ -1670,7 +1670,7 @@ function ProviderConnectStep({
|
||||
onBusy={setAutomaticBusy} onSaved={onEndpointSaved} onContinue={onSlackAppCreated}
|
||||
onManual={async existing => {
|
||||
if (!registrationLocked && slackValidation.success) await saveSlackApp.mutateAsync(slackValidation.data);
|
||||
onEndpointSaved(await chatEndpointsApi.update(endpoint.id, { slackSetupMethod: "manual" }));
|
||||
onEndpointSaved(await chatEndpointsApi.update(endpoint.id, { slackSetupMethod: existing ? "existing" : "manual" }));
|
||||
if (existing) onSlackAppCreated();
|
||||
}}
|
||||
onSaveExit={() => void saveSlackAndExit()}
|
||||
|
||||
Reference in new issue
Block a user