mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-30 05:16:50 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work.
> - Connections provide governed tools, chat channels, model accounts,
and gateways.
> - Several management routes require a human board actor even when an
agent has the required grant.
> - Default agents must configure these resources within the responsible
user's authority.
> - This pull request adds a shared authorization path to the existing
REST and native API surface.
> - Human authentication, execution approvals, and local host boundaries
remain enforced.
> - The benefit is that a standard agent can complete authorized setup
with a bounded human handoff.
## Linked Issues or Issue Description
**Subsystem affected**
Server authorization, connection services, shared contracts, native API
discovery, database schema.
**Problem or motivation**
Connection administration is available in the web app but often
unavailable to agents. Agent grants, responsible-user authority,
credential ownership, and actor attribution need separate checks.
Opening gateway-token creation also requires preserving the issuing
agent's delegated authority during token use.
**Proposed solution**
Use the existing authorization evaluator for every connection family.
Enforce current responsible-user authority, effective trust, grant
scope, resource visibility, and target-agent access. Keep existing agent
grants unchanged. Expose authorized configuration through default
`search_api` and `call_api`. Save human authentication steps and let
agents continue after completion.
**Roadmap alignment**
This change completes the API access path for the existing Connected
Apps, Personal & Shared AI Accounts, and governed MCP Gateway
capabilities in `ROADMAP.md`. It follows the maintainer's requested
design. Related work: #11899 enforces connection execution access and
creator/manager rules; this PR extends agent management across
connection families. It does not replace that execution-policy work.
## What Changed
- Add shared connection management context and mandatory
responsible-user, trust, scope, and target checks.
- Allow authorized agents to configure tool, chat/email, GitHub/Slack,
AI, profile, and gateway resources through existing routes.
- Preserve human credential ownership and record the actual agent,
responsible user, and run or key in audit data.
- Preserve saved browser authentication handoffs and callback session
checks.
- Bind agent-issued durable gateway tokens to their responsible user and
recheck delegated authority at use.
- Add explicit REST transport and management capability metadata,
built-in guidance, and sensitive-response handling.
- Intersect caller and endpoint-agent Slack test/invitation policies,
including independent and shared rates. Require the addressed human’s
current endpoint identity link before completing identity handoffs.
- Add production-authenticated integration scenarios. Companion #17739
contains the saved setup UI (including the required human
import/rotation step), product and permission documentation,
catalog/Vercel credential and callback ownership, and operation maps to
keep each review below 100 files. Both PRs must ship together; merge
this implementation first, then its companion.
## Verification
- Current implementation head:
`2ecd6451d0d0c8d82121a4e3fa4ce94c5befbbd6`, including master
`ea727dc3c2`, 99 files. The latest follow-up fixes the two valid
findings from the preceding f9 review. Exact-head Greptile has completed
at5/5 with no findings and Superagent passed. Full exact-head Linux CI
workflow40446606056 completed SUCCESS; only the queued external
Paperclip Review remains pending; earlier Linux passes do not qualify
this successor.
- Latest preserved-stack verification passed **2,165 tests** in fresh
PostgreSQL: **70 actual-auth scenarios**, 27 OpenAPI contracts, 2,022
native API tests and 46 saved GitHub wizard cases. **141 existing
gateway/policy acceptance tests** also passed. Workspace
typecheck/build, maps and token gates passed. The first workspace
typecheck was blocked by sandbox tsx IPC permissions; its authorized
rerun passed. This is focused local evidence; the full Linux CI matrix
is still required.
- Issuer/target gateway policy regressions were red before the fix. Both
decisions now intersect, including Block/Ask first/rates, same
credential identity and current token/user/agent authority after
approval. Invocations retain their token and actual requesting agent;
deleted tokens fail closed. Thirteen actual-auth cases cover revocation,
narrowed scope, approvals, shared rates and board compatibility.
Intermediate missing-token attribution, review-context and
implicit-session actor failures remain in the qualification record; all
final cases pass.
- Activity pagination now retains a bounded cursor across private-only
windows. The actual-JWT regression reaches three older readable events
after 27 private ones without exposing private details. A native
device-login regression was red for plaintext receipts; local check and
test-call results now use existing protected-response handling. The
passing actual-auth case proves encrypted persistence, exact replay,
redacted history and no response asset.
- Previous-head `5b55195593` Linux CI initially failed e2e shard 4/8 in
the existing connection-intent continuation test: periodic recovery
queued a repair at 01:21:47, the scripted journey completed the task at
01:21:48, and the extra queued run was cancelled at 01:21:49 because the
task was terminal. The assertion selected that latest cancelled run.
GitHub refused the first attempt while the workflow was nonterminal.
After completion, the single evidence-supported failed-job rerun was
accepted as **attempt 2** on the same head (workflow 40431202466). The
failed shard and final E2E aggregate passed on attempt 2, and the
complete Linux workflow for that head is successful. The original
failure remains part of qualification. No additional unchanged-code
rerun will be used for this failure.
- Companion CI also has an unchanged login-cleanup idleness timeout,
retained in its qualification record. That same 16-case suite passed in
this parent Linux run and a fresh local database. Both preceding
workflows passed after their single failed-job reruns as attempt 2; the
original failures remain recorded. No additional unchanged-code rerun
was used.
- Migration `0380_broken_aqueduct.sql` was regenerated through the
repository workflow after master took 0379 for project Git
authorization. It adds the responsible-user column with `IF NOT EXISTS`,
preserves upstream migrations, and satisfies snapshot retention and
migration safety checks.
- Upstream published Copilot v62 and v63 identities remain byte-for-byte
unchanged. The combined guidance is now v64, digest
`sha256:22e31f95ef972bcead98a4a707c9f33433558fd111e390e027a2bbb4346e112a`.
The normal shared TypeScript/Rust identity check and **61 Copilot
installation tests passed**. The old source-identity assertion first
failed against unchanged published v63; regenerating a new v64 preserves
the published release.
- Workspace `pnpm -r typecheck` and `pnpm build`, map validation, and UI
token gates passed on the final combined stack after the upstream merge.
Node 24.22.1 / pnpm 9.15.4 were used.
- **Earlier 82 API/OpenAPI tests passed** in fresh PostgreSQL 18: 55
assembled-server scenarios and 27 contracts. They use real persistent
agent keys, run JWTs, human sessions, production defaults, and
deterministic provider fixtures. No actor injection establishes access.
- The new email regression was red before per-account scope filtering.
The Arcade regression was red before human ownership and inventory-key
fixes; it also verifies actual-agent audit identity and a human-session
inventory read. The activity regression measured **200 authorization
checks before the fix and one afterward** for the same one-item page.
- **82 GitHub UI tests passed**, including all three new
failed-replacement regressions that were red before their fix. The UI
preserves an earlier copy only after confirming its committed
generation. Changed or unreadable results block copying and reconnecting
until reconciled. Human-only secret responses now include their
nonsecret committed generation.
- Private test status now rechecks the originating principal and linked
task, including mandatory agent/responsible-user privacy. Cross-agent
tests require the full caller tool access decision and the same
credential identity, regardless of auth label; caller and target
policies/approvals and rate limits apply before execution and after
human approval. A profile-only precondition was found to reject valid
allow-policy and scoped-grant access and has been removed. Both
permitted-access cases and a no-auth-label identity regression were red
before the correction; caller-specific Block still prevents provider
execution. The private-status regression was also red before its fix.
- The tool-activity regression measured 100 capability decisions and 102
run captures on a 100-event page before the fix. It now uses two task
decisions and at most two captures while retaining the same 50 visible
events, private-event filtering, and 403 after audit-grant revocation.
The regression was red before request-context reuse and task-decision
sharing; final server typecheck/build also pass.
- Generic chat configure/reconnect now hands agent-supplied verification
credentials to the addressed human before provider work or vault
changes. Actual key/JWT regressions were red for Slack signing-key
replacement and generic channel secret-reference rebinding. They now
prove unchanged refs and vaulted signing value, human setup, permitted
agent bot-token repair, and governed test messages. All 82 API/OpenAPI
cases pass in fresh PostgreSQL. Eight native receipt protections and
final server typecheck/build also pass.
- The preceding review found that old healthy state could retire a
credential-entry card before a human changed anything. The actual-JWT
regression was red. Saved field generations now change only with
protected values and commit with refs plus a verification fence; retries
retain the baseline. Old health, bot-token repair and unchanged entry
stay pending. New signing verification and current authority permit
continuation. A separate red race proved that user revocation during
provider authentication could return 403 after refs changed; authority
is now checked before persistence and inside the credential transaction.
The final 82 API/OpenAPI cases, four neighboring real-database
Slack/GitHub lifecycle cases, and final server typecheck/build pass.
- A preceding full run crossed a rate-window boundary. Database rows
prove one consumed slot at 23:35 and another at 23:36 for the same
policy/bucket. Only Date is frozen in the rate test cases; transport and
asynchronous timers remain real. Production rate behavior is unchanged,
and the final full run passes. An intermediate fixture failure that
overwrote hidden generation metadata with an API projection is retained
in the qualification record.
- Linux CI on an earlier head found two rate-admission assertions that
omitted the newly persisted bucket key. The failures were reproduced
locally; both assertions now verify the exact actor/tool bucket and all
49 policy-service tests pass. The companion qualification record retains
the initial and intermediate failures. Catalog creator attribution is
preserved in the companion to keep this implementation review at 99
files.
- The latest review reproduced three regressions before repair: complete
human replacements failed on unavailable old signing secrets, fresh
GitHub links incorrectly selected reconnect, and different
credential-entry requests shared an idempotency key. Each old protected
ref now has a narrow known-unavailable fallback; unexpected resolver
failures and company/scope violations still abort. Fresh imports keep
their credential form, configured endpoints keep reconnect, and
canonical field sets/completion modes distinguish handoffs while
matching retries retain their baseline. All 55 actual-auth scenarios and
27 separately run OpenAPI contracts pass, as do server typecheck/build.
Companion #17739 also passes all 41 wizard tests including actual
fresh-form submission, UI typecheck, maps and token gates. Earlier red
results and a mistyped OpenAPI filename are retained in qualification.
- The configured GitHub handoff regression was red in the
browser-component tests and actual-auth link/persistence tests. Saved
links now name requested fields and all/any completion mode; the
companion form submits those supplied fields through reconnect and
retains other saved credentials. Webhook/client-secret reconnect remains
human-only; first-setup secret restrictions remain. All 46 wizard tests
pass, including combined modes and legacy links. A duplicate review
referenced the older companion bf760912; the current form is already
fixed, linked in the reply, and its thread is resolved.
- Linux CI on bf760912 failed automatic Slack provisioning because vault
persistence cleared an already verified manifest-update challenge. The
same regression was reproduced locally. The provider-owned helper now
preserves proof only for the current signing fingerprint and callback
URL. Eight existing compatibility cases pass, including this case and
initial GitHub secret rejection. The first uncommitted GitHub-field
variant also failed that initial boundary and was narrowed to reconnect;
both failures remain in qualification.
- The upstream merge conflicted only in generated migration metadata.
Upstream 0379 is preserved, normal generation/pruning produced 0380, and
two repeated applications of the idempotent additive SQL passed. The
final combined stack passed **128 tests** (55 actual-auth, 27 OpenAPI
and 46 wizard cases) in fresh PostgreSQL. Eight Google UI cases also
pass; the two protocol cases could not initialize embedded PostgreSQL in
this fresh local attempt. Their earlier passing evidence remains
historical; fresh Linux CI must execute them. Vercel personal credential
attribution is preserved in companion #17739 with its other
ownership/consent follow-ups, keeping this PR at 99 files.
- The suite covers fresh defaults, restricted users/agents, scopes,
personal isolation, token revocation, human handoffs, callback replay,
execution approvals, authorized self-targeted policy changes,
self-approval denial, and current authority before commit. Reproduce API
checks with `pnpm exec vitest run
server/src/__tests__/connection-management.integration.test.ts
server/src/__tests__/openapi-routes.test.ts`, using a fresh database
through `PAPERCLIP_CONNECTION_MANAGEMENT_TEST_DATABASE_URL` or the
default embedded test database.
- Companion #17739 also records consent separately from token refresh so
saved Google choices survive refresh. Its eight service-picker and two
local-HTTP OAuth tests pass; both new UI cases were red before the fix.
Normal migration generation confirms no SQL change is needed for the
additive JSON metadata.
- The integration module shares one assembled server, authentication
factory, and provider lifecycle to preserve the
HTTP-to-vault-to-task-continuation proof. A reused external database
caused a Slack fixture identity conflict; the fresh final run passed.
Earlier fixture/transport and contract-update failures remain in the
companion qualification record.
- The broad local `pnpm test:run` attempt remains blocked by macOS
embedded PostgreSQL's host semaphore limit. It is not a clean pass. The
fresh Linux CI matrix must complete. Provider fixtures do not prove live
setup or an empirical LLM success rate.
- A current-head completed Paperclip assessment is not yet recorded. Its
earlier external recovery boundary has not been bypassed, and no
incomplete assessment is reported as passing. The user authorized both
merges when checks pass; merge this implementation first, then replay,
retarget, and requalify companion #17739 against the squash-merged
master.
- After the master refresh, local workspace typecheck/build, **97
actual-auth/OpenAPI cases** and **141 gateway/policy cases** pass. The
original fixed database port was occupied; Docker assigned a free
localhost port. The first neighbor setup command could not resolve root
`tsx`, leaving its schema uninitialized; the built database package
migrated the separate fixture and all unchanged cases passed. Both setup
failures are retained in qualification. Fresh Linux CI remains required;
no full local test-sweep pass is claimed.
- The f9 Greptile review scored **2/5** for two reproduced findings:
Slack test sends skipped the caller’s tool policy, and healthy App
credentials completed an unconfirmed identity handoff. Both are fixed in
this head and have resolved evidence threads
([Slack](https://github.com/paperclipai/paperclip/pull/17737#discussion_r4384927765),
[identity](https://github.com/paperclipai/paperclip/pull/17737#discussion_r4384927895)).
Actual key/JWT cases cover twelve Slack caller/target/recipient
combinations; the human-session GitHub OAuth callback alone remains
pending until explicit confirmation. All **99 API/OpenAPI tests** (72
actual-auth + 27 contracts), workspace typecheck and build pass. On the
combined companion, all four focused cases (including the twelve Slack
combinations), typecheck/build and maps also pass. The continuation
suite’s 50 embedded-PG cases were skipped because its database could not
start locally; fresh Linux CI must run them. Initial fixture
credential-collision and audit-projection assertion failures are
retained with the red reproductions in the companion qualification
record. No full local sweep or live consent is claimed.
## Risks
- The maintainer explicitly confirmed authorized policy administration,
including self-targeted changes that loosen controls. These writes
require management grants and the responsible user’s current authority.
Agents cannot approve their own governed action requests or edit their
own principal permission grants. The actual-auth regression exercises
both permitted administration and those denials.
- This is a broad authorization change. Incorrect scope or owner
resolution could deny permitted setup or expose another user's
resources. Integration cases cover both permitted and denied requests
with actual authentication.
- The additive nullable gateway-token column does not infer identities
for legacy clients or backfill agent grants. Legacy agent tokens without
a responsible user fail closed.
- Provider fixtures test supported setup and callback contracts. They do
not prove live provider availability.
## Model Used
- OpenAI Codex, GPT-6 family, with reasoning, repository tools, and code
execution. The runtime does not expose a more specific deployment ID or
context-window size.
## 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 — focused suites above;
full local sweep remains blocked as documented
- [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 — exact `2ecd6451d0` Linux CI
completed SUCCESS; external Paperclip Review is still queued
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups —
exact `2ecd6451d0` review completed; both f9 findings have resolved
evidence replies
- [x] I will address all Greptile and reviewer comments before
requesting merge
---------
Co-authored-by: Paperclip <noreply@paperclip.ing>
1568 lines
58 KiB
TypeScript
1568 lines
58 KiB
TypeScript
import type { AuthorizationActor } from "./authorization.js";
|
||
import { connectionManagementService, type ConnectionManagementContext } from "./connection-management.js";
|
||
import { createHash, randomBytes } from "node:crypto";
|
||
import { and, desc, eq, sql } from "drizzle-orm";
|
||
import {
|
||
chatEndpoints,
|
||
chatGitHubRegistrations,
|
||
chatExternalPrincipals,
|
||
chatIdentityLinks,
|
||
companies,
|
||
companyMemberships,
|
||
toolConnections,
|
||
type Db,
|
||
} from "@paperclipai/db";
|
||
import type {
|
||
GitHubAppRegistrationInput,
|
||
GitHubAppWizardState,
|
||
GitHubAppCloudState,
|
||
} from "@paperclipai/shared";
|
||
import { badRequest, conflict, forbidden, notFound } from "../errors.js";
|
||
import { logActivity } from "./activity-log.js";
|
||
import {
|
||
githubBotCredentials,
|
||
githubBotRequest,
|
||
} from "./chat-github-client.js";
|
||
import {
|
||
resyncGitHubAppWebhook,
|
||
listGitHubAppWebhookDeliveries,
|
||
requestGitHubAppWebhookRedelivery,
|
||
} from "./chat-github-webhook-config.js";
|
||
import { githubChatManagementService } from "./chat-github-management.js";
|
||
import {
|
||
createPaperclipCloudConnector,
|
||
paperclipCloudConnectorConfigFromEnv,
|
||
type PaperclipCloudConnector,
|
||
} from "./paperclip-cloud-connector.js";
|
||
import { paperclipCloudConnectorEnrollmentStatus } from "./paperclip-cloud-connector-enrollment.js";
|
||
import { pollGitHubBotCloudSetupEvents } from "./chat-github-cloud-ingress.js";
|
||
|
||
/** Loopback aliases are equivalent locally, but Cloud requires the approved exact origin. */
|
||
export function githubWizardBrowserOrigin(
|
||
value: string | null,
|
||
enrolledOrigins: string[],
|
||
): string | null {
|
||
if (!value) return value;
|
||
const loopback = (url: URL) =>
|
||
url.protocol === "http:" &&
|
||
["localhost", "127.0.0.1", "[::1]"].includes(url.hostname);
|
||
const exactOrigin = (url: URL) =>
|
||
!url.username && !url.password && !url.search && !url.hash && url.pathname === "/";
|
||
const current = new URL(value);
|
||
if (!exactOrigin(current)) return value;
|
||
if (!loopback(current) || enrolledOrigins.includes(current.origin)) return value;
|
||
for (const candidate of enrolledOrigins) {
|
||
const approved = new URL(candidate);
|
||
if (loopback(approved) && approved.port === current.port && exactOrigin(approved))
|
||
return approved.origin;
|
||
}
|
||
return value;
|
||
}
|
||
|
||
const hash = (value: string) =>
|
||
createHash("sha256").update(value).digest("hex");
|
||
const nonce = () => randomBytes(32).toString("base64url");
|
||
type Endpoint = typeof chatEndpoints.$inferSelect;
|
||
type Resource = { id: string; enabled: boolean; availability: string };
|
||
/** Manager-only orchestration. Provider callbacks resume this same durable draft. */
|
||
export function githubChatWizardService(
|
||
db: Db,
|
||
options: {
|
||
origin: () => string | null;
|
||
fetch?: typeof fetch;
|
||
connector?: () => PaperclipCloudConnector | null;
|
||
startDirect: (
|
||
endpointId: string,
|
||
userId: string,
|
||
input: GitHubAppRegistrationInput,
|
||
managementContext?: ConnectionManagementContext,
|
||
) => Promise<{
|
||
registrationUrl: string;
|
||
manifest: Record<string, unknown>;
|
||
expiresAt: string;
|
||
}>;
|
||
resumeDirect?: (
|
||
endpointId: string,
|
||
userId: string,
|
||
managementContext?: ConnectionManagementContext,
|
||
) => Promise<{
|
||
registrationUrl: string;
|
||
manifest: Record<string, unknown>;
|
||
expiresAt: string;
|
||
}>;
|
||
completeDirect?: (state: string, code: string, managementContext?: ConnectionManagementContext) => Promise<string>;
|
||
storeApp: (
|
||
endpointId: string,
|
||
userId: string,
|
||
credentials: Record<string, string>,
|
||
managementContext?: ConnectionManagementContext,
|
||
) => Promise<unknown>;
|
||
storeCredentials: (
|
||
endpointId: string,
|
||
userId: string,
|
||
credentials: Record<string, string>,
|
||
) => Promise<void>;
|
||
refreshRepositories: (
|
||
endpointId: string,
|
||
userId: string,
|
||
managementContext?: ConnectionManagementContext,
|
||
) => Promise<Resource[]>;
|
||
resources: (endpointId: string) => Promise<Resource[]>;
|
||
replaceResources: (
|
||
endpointId: string,
|
||
updates: Array<{ id: string; enabled: boolean }>,
|
||
userId: string,
|
||
options?: { initialGitHubImport?: boolean; managementContext?: ConnectionManagementContext },
|
||
) => Promise<unknown>;
|
||
configure: (endpointId: string, userId: string, managementContext?: ConnectionManagementContext) => Promise<unknown>;
|
||
finish: (endpointId: string, userId: string, managementContext?: ConnectionManagementContext) => Promise<unknown>;
|
||
},
|
||
) {
|
||
const fetchImpl = options.fetch ?? fetch;
|
||
const management = githubChatManagementService(db, fetchImpl);
|
||
const connector =
|
||
options.connector ??
|
||
(() => {
|
||
const config = paperclipCloudConnectorConfigFromEnv();
|
||
return config
|
||
? createPaperclipCloudConnector({ config, request: fetchImpl })
|
||
: null;
|
||
});
|
||
const pending = new Map<string, Promise<GitHubAppWizardState>>();
|
||
async function endpoint(id: string, userId: string): Promise<Endpoint> {
|
||
const [bot] = await db
|
||
.select()
|
||
.from(chatEndpoints)
|
||
.where(
|
||
and(eq(chatEndpoints.id, id), eq(chatEndpoints.provider, "github")),
|
||
);
|
||
if (!bot || bot.status === "archived")
|
||
throw notFound("GitHub bot not found");
|
||
const [member] = await db
|
||
.select()
|
||
.from(companyMemberships)
|
||
.where(
|
||
and(
|
||
eq(companyMemberships.companyId, bot.companyId),
|
||
eq(companyMemberships.principalType, "user"),
|
||
eq(companyMemberships.principalId, userId),
|
||
eq(companyMemberships.status, "active"),
|
||
),
|
||
);
|
||
if (!member || member.membershipRole === "viewer")
|
||
throw forbidden("An active company manager is required");
|
||
return bot;
|
||
}
|
||
async function registration(bot: Endpoint) {
|
||
const [session] = await db
|
||
.select()
|
||
.from(chatGitHubRegistrations)
|
||
.where(
|
||
and(
|
||
eq(chatGitHubRegistrations.companyId, bot.companyId),
|
||
eq(chatGitHubRegistrations.endpointId, bot.id),
|
||
),
|
||
)
|
||
.orderBy(desc(chatGitHubRegistrations.createdAt))
|
||
.limit(1);
|
||
return session;
|
||
}
|
||
async function setup(
|
||
bot: Endpoint,
|
||
values: Partial<NonNullable<Endpoint["setup"]["github"]>>,
|
||
) {
|
||
await db
|
||
.update(chatEndpoints)
|
||
.set({
|
||
setup: sql`jsonb_set(${chatEndpoints.setup}, '{github}', coalesce(${chatEndpoints.setup}->'github','{}'::jsonb) || ${JSON.stringify(values)}::jsonb)`,
|
||
updatedAt: new Date(),
|
||
})
|
||
.where(
|
||
and(
|
||
eq(chatEndpoints.id, bot.id),
|
||
eq(chatEndpoints.companyId, bot.companyId),
|
||
),
|
||
);
|
||
}
|
||
async function resume(bot: Endpoint) {
|
||
const [company] = await db
|
||
.select({ prefix: companies.issuePrefix })
|
||
.from(companies)
|
||
.where(eq(companies.id, bot.companyId));
|
||
return `${options.origin()}/${company!.prefix}/apps/chat/connect?provider=github&purpose=chat&resume=${bot.id}`;
|
||
}
|
||
function origin() {
|
||
const enrollment = paperclipCloudConnectorEnrollmentStatus();
|
||
const value = githubWizardBrowserOrigin(
|
||
options.origin(),
|
||
enrollment.status === "active" ? enrollment.origins : [],
|
||
);
|
||
if (!value)
|
||
throw badRequest(
|
||
"Configure this instance's browser address before connecting GitHub",
|
||
);
|
||
const url = new URL(value);
|
||
if (
|
||
url.username ||
|
||
url.password ||
|
||
url.search ||
|
||
url.hash ||
|
||
url.pathname !== "/" ||
|
||
(url.protocol !== "https:" &&
|
||
!(
|
||
url.protocol === "http:" &&
|
||
["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)
|
||
))
|
||
)
|
||
throw badRequest("Use an enrolled HTTPS or local browser address");
|
||
return url.origin;
|
||
}
|
||
async function start(
|
||
id: string,
|
||
userId: string,
|
||
input: GitHubAppRegistrationInput, managementContext?: ConnectionManagementContext,
|
||
) {
|
||
await managementContext?.reauthorize();
|
||
const bot = await endpoint(id, userId);
|
||
if (bot.botExternalId)
|
||
throw conflict(
|
||
"This draft already has an App. Continue its installation or recover its credentials.",
|
||
);
|
||
const prior = await registration(bot);
|
||
if (prior && ["pending", "exchanging", "failed"].includes(prior.status)) {
|
||
if (prior.userId !== userId)
|
||
throw conflict("The configuring member must resume this registration");
|
||
if (prior.status !== "pending" || prior.expiresAt <= new Date())
|
||
throw conflict(
|
||
"Recover the existing App from GitHub settings; its registration cannot safely be repeated.",
|
||
);
|
||
if (prior.handoff) return advance(id, userId, managementContext);
|
||
// The direct flow's hashed state cannot be reconstructed. Do not create a duplicate App.
|
||
throw conflict(
|
||
"An App registration is already pending. Complete its open GitHub page, or use existing-App recovery.",
|
||
);
|
||
}
|
||
await saveDraft(id, userId, input, managementContext);
|
||
// Only a fresh registration may import GitHub's initial selection. Old
|
||
// drafts without this marker retain even an entirely disabled selection.
|
||
if (
|
||
!bot.setup.github?.repositorySelectionSaved &&
|
||
!(await options.resources(id)).length
|
||
)
|
||
await setup(bot, { initialRepositoryImportPending: true });
|
||
const cloud = connector();
|
||
if (!cloud) {
|
||
if (origin().startsWith("https://"))
|
||
return {
|
||
endpointId: id,
|
||
state: "create",
|
||
registration: await options.startDirect(id, userId, input, managementContext),
|
||
} satisfies GitHubAppWizardState;
|
||
return {
|
||
endpointId: id,
|
||
state: "enrollment",
|
||
message:
|
||
"Use Paperclip Cloud to receive GitHub events for this local instance.",
|
||
} satisfies GitHubAppWizardState;
|
||
}
|
||
if (!(await cloud.githubAppsAvailable()))
|
||
throw conflict(
|
||
"GitHub App setup is not enabled on your Paperclip Cloud connector yet.",
|
||
);
|
||
const trustedOrigin = origin();
|
||
const session = await db.transaction(async (tx) => {
|
||
const [locked] = await tx
|
||
.select()
|
||
.from(chatEndpoints)
|
||
.where(eq(chatEndpoints.id, id))
|
||
.for("update");
|
||
if (!locked || locked.botExternalId || locked.status === "archived")
|
||
throw conflict("This draft changed. Resume its existing App.");
|
||
const [prior] = await tx
|
||
.select()
|
||
.from(chatGitHubRegistrations)
|
||
.where(eq(chatGitHubRegistrations.endpointId, id))
|
||
.orderBy(desc(chatGitHubRegistrations.createdAt))
|
||
.limit(1);
|
||
if (prior) {
|
||
if (
|
||
prior.userId !== userId ||
|
||
!prior.handoff?.cloudId ||
|
||
prior.status !== "pending" ||
|
||
prior.expiresAt <= new Date()
|
||
)
|
||
throw conflict(
|
||
"Resume or recover the App already registered for this draft.",
|
||
);
|
||
return prior;
|
||
}
|
||
const returnState = nonce();
|
||
const [created] = await tx
|
||
.insert(chatGitHubRegistrations)
|
||
.values({
|
||
companyId: bot.companyId,
|
||
endpointId: id,
|
||
userId,
|
||
stateHash: hash(returnState),
|
||
trustedOrigin,
|
||
ownerType: input.ownerType,
|
||
ownerLogin: input.ownerLogin,
|
||
appName: input.name,
|
||
expiresAt: new Date(Date.now() + 30 * 60_000),
|
||
handoff: { cloudId: nonce(), returnState, redemptionId: nonce(), ...(managementContext?.actor.type === "board" && managementContext.actor.sessionId ? { browserSessionId: managementContext.actor.sessionId } : {}) },
|
||
})
|
||
.returning();
|
||
await logActivity(tx as unknown as Db, {
|
||
companyId: bot.companyId,
|
||
...(managementContext?.audit ?? { actorType: "user" as const, actorId: userId }),
|
||
action: "chat_github.registration_started",
|
||
entityType: "tool_connection",
|
||
entityId: bot.connectionId,
|
||
details: {
|
||
endpointId: id,
|
||
registrationId: created!.id,
|
||
ownerType: input.ownerType,
|
||
transport: "paperclip_cloud",
|
||
},
|
||
});
|
||
return created!;
|
||
});
|
||
await setup(bot, {
|
||
stage: "connect",
|
||
ownerType: session.ownerType,
|
||
ownerLogin: session.ownerLogin ?? undefined,
|
||
appName: session.appName ?? undefined,
|
||
cloudRegistrationId: session.handoff!.cloudId,
|
||
registrationStatus: "pending",
|
||
});
|
||
const state = await cloud.githubApp({
|
||
subject: userId,
|
||
companyId: bot.companyId,
|
||
binding: cloudStart(session),
|
||
});
|
||
return {
|
||
endpointId: id,
|
||
state: "create",
|
||
registration: cloudManifest(session, state),
|
||
} satisfies GitHubAppWizardState;
|
||
}
|
||
function cloudStart(
|
||
session: typeof chatGitHubRegistrations.$inferSelect,
|
||
) {
|
||
return {
|
||
action: "start",
|
||
id: session.handoff!.cloudId,
|
||
returnUri: `${origin()}/api/chat-github/cloud/callback`,
|
||
returnState: session.handoff!.returnState,
|
||
};
|
||
}
|
||
function cloudManifest(
|
||
session: typeof chatGitHubRegistrations.$inferSelect,
|
||
route: GitHubAppCloudState,
|
||
) {
|
||
if (
|
||
!route.callbackUrls?.manifest ||
|
||
!route.callbackUrls.install ||
|
||
!route.callbackUrls.oauth ||
|
||
!route.webhookUrl
|
||
)
|
||
throw conflict(
|
||
"This Cloud connector needs the gateway update before setup can continue.",
|
||
);
|
||
const url = new URL(
|
||
session.ownerType === "organization"
|
||
? `https://github.com/organizations/${encodeURIComponent(session.ownerLogin!)}/settings/apps/new`
|
||
: "https://github.com/settings/apps/new",
|
||
);
|
||
url.searchParams.set("state", session.handoff!.returnState);
|
||
return {
|
||
registrationUrl: url.toString(),
|
||
expiresAt: session.expiresAt.toISOString(),
|
||
manifest: {
|
||
name: session.appName,
|
||
url: session.trustedOrigin.startsWith("https:")
|
||
? session.trustedOrigin
|
||
: new URL(route.webhookUrl).origin,
|
||
public: false,
|
||
hook_attributes: { url: route.webhookUrl, active: true },
|
||
redirect_url: route.callbackUrls.manifest,
|
||
setup_url: route.callbackUrls.install,
|
||
setup_on_update: true,
|
||
callback_urls: [route.callbackUrls.oauth],
|
||
default_permissions: {
|
||
contents: "read",
|
||
issues: "write",
|
||
metadata: "read",
|
||
pull_requests: "write",
|
||
checks: "write",
|
||
},
|
||
default_events: [
|
||
"issues",
|
||
"issue_comment",
|
||
"pull_request_review_comment",
|
||
"pull_request_review",
|
||
"pull_request",
|
||
],
|
||
},
|
||
};
|
||
}
|
||
async function exchangeCloudManifest(
|
||
session: typeof chatGitHubRegistrations.$inferSelect,
|
||
claimId: string,
|
||
managementContext?: ConnectionManagementContext,
|
||
) {
|
||
const cloud = connector();
|
||
if (!cloud || !session.handoff)
|
||
throw conflict("Restore Paperclip Cloud enrollment");
|
||
const material = await cloud.claimGitHubApp({
|
||
subject: session.userId,
|
||
companyId: session.companyId,
|
||
claimId,
|
||
redemptionId: session.handoff.redemptionId,
|
||
});
|
||
if (
|
||
material.kind !== "gateway_callback" ||
|
||
material.registrationId !== session.handoff.cloudId ||
|
||
material.callbackKind !== "manifest" ||
|
||
typeof material.state !== "string" ||
|
||
hash(material.state) !== session.stateHash ||
|
||
typeof material.code !== "string" ||
|
||
!/^[A-Za-z0-9_-]{1,256}$/.test(material.code)
|
||
)
|
||
throw forbidden("GitHub manifest return did not match this draft");
|
||
// Claim the single-use operation in the stack database before calling GitHub.
|
||
const [claimed] = await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({ status: "exchanging", consumedAt: new Date() })
|
||
.where(
|
||
and(
|
||
eq(chatGitHubRegistrations.id, session.id),
|
||
eq(chatGitHubRegistrations.status, "pending"),
|
||
sql`${chatGitHubRegistrations.expiresAt} > now()`,
|
||
),
|
||
)
|
||
.returning();
|
||
if (!claimed) return;
|
||
try {
|
||
await endpoint(session.endpointId, session.userId);
|
||
const app = await githubBotRequest<{
|
||
id: number;
|
||
pem: string;
|
||
webhook_secret: string;
|
||
client_id: string;
|
||
client_secret: string;
|
||
slug: string;
|
||
owner: { type: string; login: string };
|
||
}>(
|
||
fetchImpl,
|
||
null,
|
||
`/app-manifests/${encodeURIComponent(material.code)}/conversions`,
|
||
{ method: "POST" },
|
||
);
|
||
if (
|
||
!Number.isSafeInteger(app.id) ||
|
||
app.id <= 0 ||
|
||
!app.pem ||
|
||
!app.webhook_secret ||
|
||
!app.client_id ||
|
||
!app.client_secret ||
|
||
!/^[a-z0-9-]+$/.test(app.slug ?? "")
|
||
)
|
||
throw conflict(
|
||
"GitHub returned incomplete App credentials. Recover the App already created.",
|
||
);
|
||
if (
|
||
app.owner?.type !==
|
||
(session.ownerType === "organization" ? "Organization" : "User") ||
|
||
(session.ownerLogin &&
|
||
app.owner.login?.toLowerCase() !== session.ownerLogin.toLowerCase())
|
||
)
|
||
throw forbidden("GitHub returned an App owned by a different account");
|
||
await managementContext?.reauthorize();
|
||
await options.storeApp(session.endpointId, session.userId, {
|
||
appId: String(app.id),
|
||
privateKey: app.pem,
|
||
webhookSecret: app.webhook_secret,
|
||
clientId: app.client_id,
|
||
clientSecret: app.client_secret,
|
||
slug: app.slug,
|
||
}, managementContext);
|
||
await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({
|
||
status: "completed",
|
||
handoff: sql`${chatGitHubRegistrations.handoff} - 'manifestClaimId'`,
|
||
})
|
||
.where(eq(chatGitHubRegistrations.id, session.id));
|
||
} catch (error) {
|
||
await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({ status: "failed" })
|
||
.where(eq(chatGitHubRegistrations.id, session.id));
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
function restartable(session: typeof chatGitHubRegistrations.$inferSelect) {
|
||
return !!session.handoff?.cloudId && session.status === "pending" &&
|
||
session.expiresAt <= new Date() && !session.consumedAt &&
|
||
!session.handoff.manifestClaimId;
|
||
}
|
||
async function restartRegistration(id: string, userId: string, registrationId: string, managementContext?: ConnectionManagementContext) {
|
||
await managementContext?.reauthorize();
|
||
const bot = await endpoint(id, userId);
|
||
const cloud = connector();
|
||
if (!cloud) throw conflict("Restore Paperclip Cloud enrollment before continuing");
|
||
const trustedOrigin = origin();
|
||
const created = await db.transaction(async (tx) => {
|
||
const [locked] = await tx.select().from(chatEndpoints)
|
||
.where(and(eq(chatEndpoints.id, id), eq(chatEndpoints.companyId, bot.companyId))).for("update");
|
||
const [prior] = await tx.select().from(chatGitHubRegistrations)
|
||
.where(and(eq(chatGitHubRegistrations.endpointId, id), eq(chatGitHubRegistrations.companyId, bot.companyId)))
|
||
.orderBy(desc(chatGitHubRegistrations.createdAt)).limit(1).for("update");
|
||
// A failed Cloud start still owns a durable replacement. Retrying the
|
||
// original request must resume it instead of minting another App route.
|
||
if (locked && !locked.botExternalId && locked.status === "draft" &&
|
||
prior?.handoff?.renewalOf === registrationId && prior.userId === userId &&
|
||
prior.status === "pending" && prior.expiresAt > new Date() &&
|
||
!prior.consumedAt && !prior.handoff.manifestClaimId) return prior;
|
||
if (!locked || locked.botExternalId || locked.status !== "draft" ||
|
||
!prior || prior.id !== registrationId || prior.userId !== userId || !restartable(prior))
|
||
throw conflict("This registration cannot be restarted. Resume or recover its existing App.");
|
||
// Retire the old state before issuing a new one. Never repeat a claimed or uncertain exchange.
|
||
await tx.update(chatGitHubRegistrations).set({ status: "failed", consumedAt: new Date() })
|
||
.where(eq(chatGitHubRegistrations.id, prior.id));
|
||
const returnState = nonce();
|
||
const [next] = await tx.insert(chatGitHubRegistrations).values({
|
||
companyId: bot.companyId, endpointId: id, userId,
|
||
stateHash: hash(returnState), trustedOrigin,
|
||
ownerType: prior.ownerType, ownerLogin: prior.ownerLogin, appName: prior.appName,
|
||
expiresAt: new Date(Date.now() + 30 * 60_000),
|
||
handoff: { cloudId: nonce(), returnState, redemptionId: nonce(), renewalOf: prior.id },
|
||
}).returning();
|
||
await logActivity(tx as unknown as Db, {
|
||
companyId: bot.companyId, ...(managementContext?.audit ?? { actorType: "user" as const, actorId: userId }),
|
||
action: "chat_github.registration_restarted", entityType: "tool_connection", entityId: bot.connectionId,
|
||
details: { endpointId: id, registrationId: next!.id, previousRegistrationId: prior.id, appNotCreated: true },
|
||
});
|
||
return next!;
|
||
});
|
||
await setup(bot, { cloudRegistrationId: created.handoff!.cloudId, registrationStatus: "pending", stage: "connect" });
|
||
return advance(id, userId, managementContext);
|
||
}
|
||
async function saveDraft(
|
||
id: string,
|
||
userId: string,
|
||
input: GitHubAppRegistrationInput, managementContext?: ConnectionManagementContext,
|
||
) {
|
||
await managementContext?.reauthorize();
|
||
const bot = await endpoint(id, userId);
|
||
await db.transaction(async (tx) => {
|
||
await tx
|
||
.select()
|
||
.from(chatEndpoints)
|
||
.where(eq(chatEndpoints.id, id))
|
||
.for("update");
|
||
const [session] = await tx
|
||
.select()
|
||
.from(chatGitHubRegistrations)
|
||
.where(eq(chatGitHubRegistrations.endpointId, id))
|
||
.limit(1);
|
||
if (bot.botExternalId || session)
|
||
throw conflict(
|
||
"Resume the existing App registration before changing its account.",
|
||
);
|
||
await tx
|
||
.update(chatEndpoints)
|
||
.set({
|
||
setup: sql`jsonb_set(${chatEndpoints.setup}, '{github}', coalesce(${chatEndpoints.setup}->'github','{}'::jsonb) || ${JSON.stringify({ ownerType: input.ownerType, ownerLogin: input.ownerType === "organization" ? input.ownerLogin : null, appName: input.name })}::jsonb)`,
|
||
updatedAt: new Date(),
|
||
})
|
||
.where(eq(chatEndpoints.id, id));
|
||
await logActivity(tx as unknown as Db, {
|
||
companyId: bot.companyId,
|
||
...(managementContext?.audit ?? { actorType: "user" as const, actorId: userId }),
|
||
action: "chat_github.draft_saved",
|
||
entityType: "tool_connection",
|
||
entityId: bot.connectionId,
|
||
details: { endpointId: id },
|
||
});
|
||
});
|
||
return { saved: true as const };
|
||
}
|
||
async function repairCloud(id: string, userId: string, managementContext?: ConnectionManagementContext) {
|
||
await managementContext?.reauthorize();
|
||
const bot = await endpoint(id, userId),
|
||
session = await registration(bot),
|
||
cloud = connector();
|
||
if (!session?.handoff?.cloudId) return;
|
||
if (session.userId !== userId)
|
||
throw forbidden("The configuring member must recover this registration");
|
||
if (!cloud)
|
||
throw conflict(
|
||
"Restore Paperclip Cloud enrollment before reconnecting this App",
|
||
);
|
||
const binding = { subject: userId, companyId: bot.companyId };
|
||
const state = await cloud.githubApp({
|
||
...binding,
|
||
binding: cloudStart(session),
|
||
});
|
||
const { credentials, appJwt } = await githubBotCredentials(
|
||
db,
|
||
bot.companyId,
|
||
id,
|
||
);
|
||
if (!credentials.webhookSecret)
|
||
throw conflict("Recover this App's webhook secret before continuing");
|
||
if (!state.webhookUrl)
|
||
throw conflict("Restore this App's Cloud gateway before continuing");
|
||
await logActivity(db, {
|
||
companyId: bot.companyId,
|
||
...(managementContext?.audit ?? { actorType: "user" as const, actorId: userId }),
|
||
action: "chat_github.webhook_repair_started",
|
||
entityType: "tool_connection",
|
||
entityId: bot.connectionId,
|
||
details: { endpointId: id, appId: credentials.appId },
|
||
});
|
||
// The App JWT and webhook secret remain entirely in the instance.
|
||
await resyncGitHubAppWebhook({
|
||
fetch: fetchImpl,
|
||
appToken: appJwt,
|
||
webhookUrl: state.webhookUrl,
|
||
webhookSecret: credentials.webhookSecret,
|
||
});
|
||
await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({
|
||
handoff: sql`jsonb_set(${chatGitHubRegistrations.handoff}, '{webhookSecretHash}', ${JSON.stringify(hash(credentials.webhookSecret))}::jsonb)`,
|
||
status: "completed",
|
||
})
|
||
.where(eq(chatGitHubRegistrations.id, session.id));
|
||
await setup(bot, {
|
||
cloudRegistrationId: session.handoff.cloudId,
|
||
registrationStatus: "completed",
|
||
});
|
||
await db
|
||
.update(chatEndpoints)
|
||
.set({
|
||
setup: sql`jsonb_set(${chatEndpoints.setup}, '{webhookVerifiedAt}', 'null'::jsonb)`,
|
||
updatedAt: new Date(),
|
||
})
|
||
.where(eq(chatEndpoints.id, id));
|
||
try {
|
||
const history = await listGitHubAppWebhookDeliveries({
|
||
fetch: fetchImpl,
|
||
appToken: appJwt,
|
||
});
|
||
const ping = history.deliveries.find(
|
||
(delivery) => delivery.event === "ping",
|
||
);
|
||
if (ping)
|
||
await requestGitHubAppWebhookRedelivery({
|
||
fetch: fetchImpl,
|
||
appToken: appJwt,
|
||
deliveryId: ping.id,
|
||
});
|
||
} catch {
|
||
/* Keep verification pending until a fresh provider-signed delivery arrives. */
|
||
}
|
||
await logActivity(db, {
|
||
companyId: bot.companyId,
|
||
...(managementContext?.audit ?? { actorType: "user" as const, actorId: userId }),
|
||
action: "chat_github.webhook_repaired",
|
||
entityType: "tool_connection",
|
||
entityId: bot.connectionId,
|
||
details: { endpointId: id, appId: credentials.appId },
|
||
});
|
||
}
|
||
async function advance(
|
||
id: string,
|
||
userId: string, managementContext?: ConnectionManagementContext,
|
||
): Promise<GitHubAppWizardState> {
|
||
await managementContext?.reauthorize();
|
||
// Bind an agent-created handoff when its addressed human opens the saved setup.
|
||
if (managementContext?.actor.type === "board" && managementContext.actor.sessionId) {
|
||
const bot = await endpoint(id, userId);
|
||
const session = await registration(bot);
|
||
if (session && session.userId === userId && !session.handoff?.browserSessionId) {
|
||
await db.update(chatGitHubRegistrations).set({
|
||
handoff: sql`coalesce(${chatGitHubRegistrations.handoff}, '{}'::jsonb) || ${JSON.stringify({ browserSessionId: managementContext.actor.sessionId })}::jsonb`,
|
||
}).where(and(eq(chatGitHubRegistrations.id, session.id), sql`${chatGitHubRegistrations.handoff}->>'browserSessionId' is null`));
|
||
}
|
||
}
|
||
// Multiple open tabs for this manager share one operation, not concurrent vault writes.
|
||
const key = `${id}:${userId}`;
|
||
if (pending.has(key)) return pending.get(key)!;
|
||
const work = advanceOnce(id, userId, managementContext);
|
||
pending.set(key, work);
|
||
try {
|
||
return await work;
|
||
} finally {
|
||
if (pending.get(key) === work) pending.delete(key);
|
||
}
|
||
}
|
||
async function advanceOnce(
|
||
id: string,
|
||
userId: string, managementContext?: ConnectionManagementContext,
|
||
): Promise<GitHubAppWizardState> {
|
||
await managementContext?.reauthorize();
|
||
let bot = await endpoint(id, userId);
|
||
if (["paused", "revoked"].includes(bot.status))
|
||
return {
|
||
endpointId: id,
|
||
state: "recovery",
|
||
message: "Reconnect this App from connection settings.",
|
||
};
|
||
const session = await registration(bot),
|
||
cloud = connector();
|
||
if (session?.handoff?.cloudId) {
|
||
if (session.userId !== userId)
|
||
throw forbidden(
|
||
"The configuring member must complete this registration",
|
||
);
|
||
if (!cloud)
|
||
return {
|
||
endpointId: id,
|
||
state: "enrollment",
|
||
message:
|
||
"Restore this instance's Paperclip Cloud enrollment to resume.",
|
||
};
|
||
const [connection] = await db
|
||
.select({ refs: toolConnections.credentialSecretRefs })
|
||
.from(toolConnections)
|
||
.where(
|
||
and(
|
||
eq(toolConnections.id, bot.connectionId),
|
||
eq(toolConnections.companyId, bot.companyId),
|
||
),
|
||
);
|
||
const requiredCredentialKeys = session.handoff.manifestClaimId
|
||
? ["appId", "privateKey", "webhookSecret", "clientId", "clientSecret"]
|
||
: ["appId", "privateKey", "webhookSecret"];
|
||
let hasAppCredentials =
|
||
!!bot.botExternalId &&
|
||
requiredCredentialKeys.every((key) =>
|
||
connection?.refs.some(
|
||
(ref) => ref.configPath === `credentials.${key}`,
|
||
),
|
||
);
|
||
const binding = { subject: userId, companyId: bot.companyId };
|
||
const route = await cloud.githubApp({
|
||
...binding,
|
||
binding: cloudStart(session),
|
||
});
|
||
let exchangedThisTurn = false;
|
||
if (
|
||
session.status === "pending" &&
|
||
session.handoff.manifestClaimId &&
|
||
!hasAppCredentials
|
||
) {
|
||
await exchangeCloudManifest(session, session.handoff.manifestClaimId, managementContext);
|
||
bot = await endpoint(id, userId);
|
||
const [savedConnection] = await db
|
||
.select({ refs: toolConnections.credentialSecretRefs })
|
||
.from(toolConnections)
|
||
.where(
|
||
and(
|
||
eq(toolConnections.id, bot.connectionId),
|
||
eq(toolConnections.companyId, bot.companyId),
|
||
),
|
||
);
|
||
hasAppCredentials = requiredCredentialKeys.every(
|
||
(key) =>
|
||
savedConnection?.refs.some(
|
||
(ref) => ref.configPath === `credentials.${key}`,
|
||
),
|
||
);
|
||
exchangedThisTurn = true;
|
||
}
|
||
if (!bot.botExternalId) {
|
||
const current = await registration(bot);
|
||
if (current?.status !== "pending" || session.expiresAt <= new Date())
|
||
return {
|
||
endpointId: id,
|
||
state: "recovery",
|
||
restartableRegistrationId: current && bot.status === "draft" && restartable(current) ? current.id : undefined,
|
||
message: current && restartable(current)
|
||
? "This GitHub handoff expired. If you haven't created the App on GitHub, you can start a new handoff for this draft. Otherwise, recover the existing App."
|
||
: "Recover the App already created on GitHub. An interrupted exchange cannot safely be repeated.",
|
||
};
|
||
return {
|
||
endpointId: id,
|
||
state: "create",
|
||
registration: cloudManifest(session, route),
|
||
};
|
||
}
|
||
if (!hasAppCredentials)
|
||
return {
|
||
endpointId: id,
|
||
state: "recovery",
|
||
message: "Recover this App's saved credentials before continuing.",
|
||
};
|
||
const { credentials: stored } = await githubBotCredentials(
|
||
db,
|
||
bot.companyId,
|
||
id,
|
||
);
|
||
if (
|
||
(!exchangedThisTurn && session.status !== "completed") ||
|
||
(session.handoff.webhookSecretHash &&
|
||
session.handoff.webhookSecretHash !==
|
||
hash(stored.webhookSecret ?? ""))
|
||
) {
|
||
await repairCloud(id, userId, managementContext);
|
||
bot = await endpoint(id, userId);
|
||
}
|
||
if (session.status !== "completed")
|
||
await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({ status: "completed" })
|
||
.where(eq(chatGitHubRegistrations.id, session.id));
|
||
if (!session.handoff.webhookSecretHash)
|
||
await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({
|
||
handoff: sql`jsonb_set(${chatGitHubRegistrations.handoff}, '{webhookSecretHash}', ${JSON.stringify(hash(stored.webhookSecret ?? ""))}::jsonb)`,
|
||
})
|
||
.where(eq(chatGitHubRegistrations.id, session.id));
|
||
await setup(bot, {
|
||
cloudRegistrationId: session.handoff.cloudId,
|
||
ownerType: session.ownerType,
|
||
ownerLogin: session.ownerLogin ?? undefined,
|
||
appName: session.appName ?? undefined,
|
||
registrationStatus: "completed",
|
||
});
|
||
}
|
||
|
||
bot = await endpoint(id, userId);
|
||
if (!bot.botExternalId) {
|
||
if (session && !session.handoff?.cloudId) {
|
||
if (
|
||
session.status === "pending" &&
|
||
session.expiresAt > new Date() &&
|
||
session.handoff?.returnState &&
|
||
options.resumeDirect
|
||
)
|
||
return {
|
||
endpointId: id,
|
||
state: "create",
|
||
registration: await options.resumeDirect(id, userId, managementContext),
|
||
};
|
||
return {
|
||
endpointId: id,
|
||
state: "recovery",
|
||
message:
|
||
"Resume the open GitHub registration page or recover the existing App from GitHub settings.",
|
||
};
|
||
}
|
||
return { endpointId: id, state: "create" };
|
||
}
|
||
if (bot.setup.github?.initialSetupPending) {
|
||
try {
|
||
const stored = await githubBotCredentials(db, bot.companyId, id);
|
||
if (!stored.credentials.webhookSecret)
|
||
throw conflict("The App webhook secret is missing");
|
||
} catch {
|
||
return {
|
||
endpointId: id,
|
||
state: "recovery",
|
||
message:
|
||
"This App's credentials were not completely saved. Recover its existing credentials before continuing.",
|
||
};
|
||
}
|
||
}
|
||
if (
|
||
!bot.providerAccountId ||
|
||
bot.setup.github?.stage === "install" ||
|
||
bot.setup.github?.initialSetupPending
|
||
) {
|
||
try {
|
||
await options.refreshRepositories(id, userId, managementContext);
|
||
} catch (error) {
|
||
return {
|
||
endpointId: id,
|
||
state: "install",
|
||
installationUrl: bot.setup.github?.installationUrl,
|
||
message:
|
||
error instanceof Error
|
||
? error.message
|
||
: "Waiting for installation or administrator approval on GitHub.",
|
||
};
|
||
}
|
||
}
|
||
bot = await endpoint(id, userId);
|
||
const saved = await management.configuration(id, userId);
|
||
if (!bot.setup.github?.initialRepositoriesImported) {
|
||
const resources = await options.resources(id);
|
||
// Fresh manifest and manual Apps import GitHub's initial choices. Saved
|
||
// selections and configuration still belong to the user on recovery.
|
||
const initializeApp =
|
||
!!session || bot.setup.github?.initialSetupPending === true;
|
||
if (
|
||
initializeApp &&
|
||
bot.status !== "active" &&
|
||
saved.revision === 0 &&
|
||
bot.setup.github?.initialRepositoryImportPending === true &&
|
||
!bot.setup.github?.repositorySelectionSaved &&
|
||
!resources.some((resource) => resource.enabled)
|
||
) {
|
||
await options.replaceResources(
|
||
id,
|
||
resources
|
||
.filter((resource) => resource.availability === "available")
|
||
.map((resource) => ({ id: resource.id, enabled: true })),
|
||
userId,
|
||
{ initialGitHubImport: true, managementContext },
|
||
);
|
||
}
|
||
if (initializeApp && saved.revision === 0 && bot.status !== "active")
|
||
await management.saveConfiguration(
|
||
id,
|
||
{
|
||
expectedRevision: 0,
|
||
configuration: {
|
||
...saved.configuration,
|
||
toolsEnabled: true,
|
||
defaults: {
|
||
...saved.configuration.defaults,
|
||
invocation: "mentions_only",
|
||
events: [],
|
||
},
|
||
},
|
||
},
|
||
userId,
|
||
managementContext,
|
||
);
|
||
await managementContext?.reauthorize();
|
||
await setup(bot, {
|
||
initialRepositoriesImported: true,
|
||
initialSetupPending: false,
|
||
});
|
||
}
|
||
// Installation callbacks can precede the separately delivered signed ping.
|
||
// Keep the same vaulted App in the wizard instead of throwing JSON from
|
||
// the browser callback or relaxing the configure action's signature gate.
|
||
bot = await endpoint(id, userId);
|
||
if (!bot.setup.webhookVerifiedAt && bot.setup.github?.cloudRegistrationId) {
|
||
await pollGitHubBotCloudSetupEvents(db);
|
||
bot = await endpoint(id, userId);
|
||
}
|
||
if (!bot.setup.webhookVerifiedAt)
|
||
return {
|
||
endpointId: id,
|
||
state: "verify",
|
||
message: "Waiting for GitHub to verify webhook delivery…",
|
||
verification: {
|
||
ready: false,
|
||
checks: [
|
||
{
|
||
key: "webhook",
|
||
label: "Signed webhook delivery",
|
||
ok: false,
|
||
detail: "Keep this page open. Setup will continue automatically when GitHub’s signed ping arrives.",
|
||
},
|
||
],
|
||
},
|
||
};
|
||
if (
|
||
bot.status !== "active" &&
|
||
(bot.status !== "verifying" || bot.setup.step === "provider_setup")
|
||
)
|
||
await options.configure(id, userId, ...(managementContext ? [managementContext] : []));
|
||
bot = await endpoint(id, userId);
|
||
let linked = await linkedIdentity(bot, userId);
|
||
if (!linked) {
|
||
const [prior] = await db
|
||
.select({
|
||
githubUserId: chatExternalPrincipals.externalId,
|
||
login: chatExternalPrincipals.handle,
|
||
})
|
||
.from(chatIdentityLinks)
|
||
.innerJoin(
|
||
chatExternalPrincipals,
|
||
eq(chatExternalPrincipals.id, chatIdentityLinks.principalId),
|
||
)
|
||
.where(
|
||
and(
|
||
eq(chatIdentityLinks.companyId, bot.companyId),
|
||
eq(chatIdentityLinks.paperclipUserId, userId),
|
||
eq(chatIdentityLinks.status, "linked"),
|
||
eq(chatExternalPrincipals.provider, "github"),
|
||
),
|
||
)
|
||
.limit(1);
|
||
if (prior?.login) {
|
||
await management.linkObservedIdentity(id, userId, {
|
||
githubUserId: prior.githubUserId,
|
||
login: prior.login,
|
||
avatarUrl: null,
|
||
});
|
||
linked = true;
|
||
}
|
||
}
|
||
if (!linked) {
|
||
const { credentials } = await githubBotCredentials(db, bot.companyId, id);
|
||
return {
|
||
endpointId: id,
|
||
state: "identity",
|
||
identityMethod:
|
||
session && credentials.clientId && credentials.clientSecret
|
||
? "dedicated_app"
|
||
: "existing_connection",
|
||
identity:
|
||
session?.handoff?.identity &&
|
||
Date.parse(session.handoff.identity.expiresAt) > Date.now()
|
||
? session.handoff.identity
|
||
: undefined,
|
||
};
|
||
}
|
||
const verification = await management.verification(id);
|
||
const connectionChecks = verification.checks.filter(
|
||
(check) => !["runtime", "isolation"].includes(check.key),
|
||
);
|
||
const runtimeChecks = verification.checks.filter((check) =>
|
||
["runtime", "isolation"].includes(check.key),
|
||
);
|
||
const connectionReady = connectionChecks.every((check) => check.ok);
|
||
if (connectionReady && bot.status !== "active")
|
||
await options.finish(id, userId, ...(managementContext ? [managementContext] : []));
|
||
return {
|
||
endpointId: id,
|
||
state: connectionReady ? "connected" : "verify",
|
||
identityLinked: true,
|
||
verification: { ready: connectionReady, checks: connectionChecks },
|
||
runtimeChecks,
|
||
};
|
||
}
|
||
async function linkedIdentity(bot: Endpoint, userId: string) {
|
||
const [link] = await db
|
||
.select({ id: chatIdentityLinks.id })
|
||
.from(chatIdentityLinks)
|
||
.where(
|
||
and(
|
||
eq(chatIdentityLinks.companyId, bot.companyId),
|
||
eq(chatIdentityLinks.endpointId, bot.id),
|
||
eq(chatIdentityLinks.paperclipUserId, userId),
|
||
eq(chatIdentityLinks.status, "linked"),
|
||
),
|
||
)
|
||
.limit(1);
|
||
return !!link;
|
||
}
|
||
async function withIdentityLease<T>(
|
||
sessionId: string,
|
||
work: (assertOwned: () => Promise<void>) => Promise<T>,
|
||
): Promise<T> {
|
||
const leaseId = nonce();
|
||
const leaseUntil = () => new Date(Date.now() + 120_000).toISOString();
|
||
const [claimed] = await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({
|
||
handoff: sql`coalesce(${chatGitHubRegistrations.handoff}, '{"cloudId":"","returnState":"","redemptionId":""}'::jsonb) || ${JSON.stringify({ identityLeaseId: leaseId, identityLeaseExpiresAt: leaseUntil() })}::jsonb`,
|
||
})
|
||
.where(
|
||
and(
|
||
eq(chatGitHubRegistrations.id, sessionId),
|
||
sql`(${chatGitHubRegistrations.handoff}->>'identityLeaseExpiresAt' is null or ${chatGitHubRegistrations.handoff}->>'identityLeaseExpiresAt' <= ${new Date().toISOString()})`,
|
||
),
|
||
)
|
||
.returning({ id: chatGitHubRegistrations.id });
|
||
if (!claimed)
|
||
throw conflict(
|
||
"Another account-linking attempt is in progress. Try again shortly.",
|
||
);
|
||
let lost = false;
|
||
const assertOwned = async () => {
|
||
const [owned] = await db
|
||
.select({ id: chatGitHubRegistrations.id })
|
||
.from(chatGitHubRegistrations)
|
||
.where(
|
||
and(
|
||
eq(chatGitHubRegistrations.id, sessionId),
|
||
sql`${chatGitHubRegistrations.handoff}->>'identityLeaseId' = ${leaseId}`,
|
||
sql`${chatGitHubRegistrations.handoff}->>'identityLeaseExpiresAt' > ${new Date().toISOString()}`,
|
||
),
|
||
);
|
||
if (lost || !owned)
|
||
throw conflict("Account-linking changed. Connect your account again.");
|
||
};
|
||
const renewal = setInterval(() => {
|
||
void db
|
||
.update(chatGitHubRegistrations)
|
||
.set({
|
||
handoff: sql`jsonb_set(${chatGitHubRegistrations.handoff}, '{identityLeaseExpiresAt}', ${JSON.stringify(leaseUntil())}::jsonb)`,
|
||
})
|
||
.where(
|
||
and(
|
||
eq(chatGitHubRegistrations.id, sessionId),
|
||
sql`${chatGitHubRegistrations.handoff}->>'identityLeaseId' = ${leaseId}`,
|
||
),
|
||
)
|
||
.returning({ id: chatGitHubRegistrations.id })
|
||
.then((rows) => {
|
||
if (!rows.length) lost = true;
|
||
})
|
||
.catch(() => {
|
||
lost = true;
|
||
});
|
||
}, 30_000);
|
||
renewal.unref();
|
||
try {
|
||
return await work(assertOwned);
|
||
} finally {
|
||
clearInterval(renewal);
|
||
await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({
|
||
handoff: sql`${chatGitHubRegistrations.handoff} - 'identityLeaseId' - 'identityLeaseExpiresAt'`,
|
||
})
|
||
.where(
|
||
and(
|
||
eq(chatGitHubRegistrations.id, sessionId),
|
||
sql`${chatGitHubRegistrations.handoff}->>'identityLeaseId' = ${leaseId}`,
|
||
),
|
||
);
|
||
}
|
||
}
|
||
async function authorizeBrowserReturn(session: typeof chatGitHubRegistrations.$inferSelect, actor?: AuthorizationActor, identity = false) {
|
||
// Internal provider workflows already own their authenticated subject. HTTP
|
||
// callbacks always pass their actual browser actor before consuming state.
|
||
if (!actor) return;
|
||
const sessionId = identity ? session.handoff?.identitySessionId : session.handoff?.browserSessionId;
|
||
if (actor.type !== "board" || actor.userId !== session.userId || (sessionId && actor.sessionId !== sessionId)) throw forbidden("Open this saved setup in the configuring user's browser session");
|
||
const bot = await endpoint(session.endpointId, session.userId);
|
||
return connectionManagementService(db).authorize(actor, "connections:configure", {
|
||
type: "connection_management", companyId: bot.companyId, connectionId: bot.connectionId,
|
||
targetAgentId: bot.assignedAgentId, purpose: "chat", provider: "github", operation: "github.authentication.return",
|
||
});
|
||
}
|
||
async function startIdentity(id: string, userId: string, actor?: AuthorizationActor) {
|
||
const bot = await endpoint(id, userId),
|
||
session = await registration(bot);
|
||
if (!session || session.userId !== userId)
|
||
throw forbidden("The configuring member must link their GitHub account");
|
||
return withIdentityLease(session.id, (assertOwned) =>
|
||
startIdentityLocked(id, userId, assertOwned, actor),
|
||
);
|
||
}
|
||
async function startIdentityLocked(
|
||
id: string,
|
||
userId: string,
|
||
assertOwned: () => Promise<void>,
|
||
actor?: AuthorizationActor,
|
||
) {
|
||
const bot = await endpoint(id, userId),
|
||
session = await registration(bot);
|
||
if (!session || session.userId !== userId)
|
||
throw forbidden("The configuring member must link their GitHub account");
|
||
const credentials = await githubBotCredentials(db, bot.companyId, id);
|
||
if (
|
||
!credentials.credentials.clientId ||
|
||
!credentials.credentials.clientSecret
|
||
)
|
||
throw conflict(
|
||
"This existing App needs a personal GitHub connection to verify your account.",
|
||
);
|
||
const verifier = nonce(),
|
||
state = nonce();
|
||
await assertOwned();
|
||
await options.storeCredentials(id, userId, {
|
||
identityCodeVerifier: verifier,
|
||
});
|
||
await assertOwned();
|
||
await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({
|
||
handoff: sql`(${chatGitHubRegistrations.handoff} - 'identity') || ${JSON.stringify({ ...(actor?.type === "board" && actor.sessionId ? { identitySessionId: actor.sessionId } : {}), identityStateHash: hash(state), identityExpiresAt: new Date(Date.now() + 10 * 60_000).toISOString(), identityRedemptionId: nonce() })}::jsonb`,
|
||
})
|
||
.where(eq(chatGitHubRegistrations.id, session.id));
|
||
const challenge = createHash("sha256").update(verifier).digest("base64url");
|
||
if (session.handoff?.cloudId) {
|
||
const cloud = connector();
|
||
if (!cloud) throw conflict("Restore Paperclip Cloud enrollment");
|
||
const result = await cloud.githubApp({
|
||
subject: userId,
|
||
companyId: bot.companyId,
|
||
binding: {
|
||
action: "callback",
|
||
id: session.handoff.cloudId,
|
||
stateHash: hash(state),
|
||
},
|
||
});
|
||
if (!result.callbackUrls?.oauth)
|
||
throw conflict("GitHub account authorization is unavailable");
|
||
const url = new URL("https://github.com/login/oauth/authorize");
|
||
url.searchParams.set("client_id", credentials.credentials.clientId);
|
||
url.searchParams.set("redirect_uri", result.callbackUrls.oauth);
|
||
url.searchParams.set("state", state);
|
||
url.searchParams.set("code_challenge", challenge);
|
||
url.searchParams.set("code_challenge_method", "S256");
|
||
return { authorizationUrl: url.toString() };
|
||
}
|
||
const url = new URL("https://github.com/login/oauth/authorize");
|
||
url.searchParams.set("client_id", credentials.credentials.clientId);
|
||
url.searchParams.set(
|
||
"redirect_uri",
|
||
`${origin()}/api/chat-github/identity/callback`,
|
||
);
|
||
url.searchParams.set("state", state);
|
||
url.searchParams.set("code_challenge", challenge);
|
||
url.searchParams.set("code_challenge_method", "S256");
|
||
return { authorizationUrl: url.toString() };
|
||
}
|
||
async function completeIdentity(
|
||
session: typeof chatGitHubRegistrations.$inferSelect,
|
||
code: string,
|
||
redirectUri: string,
|
||
actor?: AuthorizationActor,
|
||
) {
|
||
return withIdentityLease(session.id, async (assertOwned) => {
|
||
const [current] = await db
|
||
.select()
|
||
.from(chatGitHubRegistrations)
|
||
.where(eq(chatGitHubRegistrations.id, session.id));
|
||
if (
|
||
!current ||
|
||
current.handoff?.identityStateHash !==
|
||
session.handoff?.identityStateHash
|
||
)
|
||
return resume(await endpoint(session.endpointId, session.userId));
|
||
return completeIdentityLocked(current, code, redirectUri, assertOwned, actor);
|
||
});
|
||
}
|
||
async function completeIdentityLocked(
|
||
session: typeof chatGitHubRegistrations.$inferSelect,
|
||
code: string,
|
||
redirectUri: string,
|
||
assertOwned: () => Promise<void>,
|
||
actor?: AuthorizationActor,
|
||
) {
|
||
const bot = await endpoint(session.endpointId, session.userId);
|
||
if (
|
||
!session.handoff?.identityExpiresAt ||
|
||
Date.parse(session.handoff.identityExpiresAt) <= Date.now()
|
||
)
|
||
throw conflict(
|
||
"GitHub identity authorization expired. Connect your account again.",
|
||
);
|
||
const [consumed] = await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({
|
||
handoff: sql`${chatGitHubRegistrations.handoff} - 'identityStateHash'`,
|
||
})
|
||
.where(
|
||
and(
|
||
eq(chatGitHubRegistrations.id, session.id),
|
||
sql`${chatGitHubRegistrations.handoff}->>'identityStateHash' = ${session.handoff?.identityStateHash ?? ""}`,
|
||
),
|
||
)
|
||
.returning();
|
||
if (!consumed) return resume(bot);
|
||
const material = await githubBotCredentials(db, bot.companyId, bot.id);
|
||
const response = await fetchImpl(
|
||
"https://github.com/login/oauth/access_token",
|
||
{
|
||
method: "POST",
|
||
redirect: "error",
|
||
signal: AbortSignal.timeout(25_000),
|
||
headers: {
|
||
accept: "application/json",
|
||
"content-type": "application/json",
|
||
},
|
||
body: JSON.stringify({
|
||
client_id: material.credentials.clientId,
|
||
client_secret: material.credentials.clientSecret,
|
||
code,
|
||
redirect_uri: redirectUri,
|
||
code_verifier: material.credentials.identityCodeVerifier,
|
||
}),
|
||
},
|
||
);
|
||
if (!response.ok) {
|
||
await response.body?.cancel();
|
||
throw conflict(
|
||
"GitHub authorization failed. Connect your account again.",
|
||
);
|
||
}
|
||
const token = (await response.json()) as { access_token?: string };
|
||
if (!token.access_token)
|
||
throw conflict(
|
||
"GitHub authorization was denied. Connect your account again.",
|
||
);
|
||
const user = await githubBotRequest<{
|
||
id: number;
|
||
login: string;
|
||
avatar_url?: string;
|
||
}>(fetchImpl, token.access_token, "/user");
|
||
let allowed = false;
|
||
for (let page = 1; page <= 100; page++) {
|
||
const result = await githubBotRequest<{
|
||
installations: Array<{ id: number; app_id: number }>;
|
||
}>(
|
||
fetchImpl,
|
||
token.access_token,
|
||
`/user/installations?per_page=100&page=${page}`,
|
||
);
|
||
allowed = result.installations.some(
|
||
(installation) =>
|
||
String(installation.app_id) === bot.botExternalId &&
|
||
String(installation.id) === material.credentials.installationId,
|
||
);
|
||
if (allowed || result.installations.length < 100) break;
|
||
}
|
||
if (!allowed)
|
||
throw forbidden(
|
||
"Your GitHub account cannot access this App installation",
|
||
);
|
||
if (
|
||
!Number.isSafeInteger(user.id) ||
|
||
user.id <= 0 ||
|
||
!/^[A-Za-z0-9-]{1,39}$/.test(user.login)
|
||
)
|
||
throw conflict("GitHub returned an invalid account");
|
||
await authorizeBrowserReturn(session, actor, true);
|
||
// OAuth token remains transient; only an expiring observed identity is saved.
|
||
await assertOwned();
|
||
const identity = {
|
||
githubUserId: String(user.id),
|
||
login: user.login,
|
||
avatarUrl: user.avatar_url ?? null,
|
||
expiresAt: new Date(Date.now() + 10 * 60_000).toISOString(),
|
||
};
|
||
await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({
|
||
handoff: sql`jsonb_set(${chatGitHubRegistrations.handoff}, '{identity}', ${JSON.stringify(identity)}::jsonb)`,
|
||
})
|
||
.where(eq(chatGitHubRegistrations.id, session.id));
|
||
await assertOwned();
|
||
await options.storeCredentials(bot.id, session.userId, {
|
||
identityCodeVerifier: nonce(),
|
||
});
|
||
return resume(bot);
|
||
}
|
||
async function directCallback(state: string, code: string, actor?: AuthorizationActor) {
|
||
const [session] = await db
|
||
.select()
|
||
.from(chatGitHubRegistrations)
|
||
.where(eq(chatGitHubRegistrations.stateHash, hash(state)))
|
||
.limit(1);
|
||
if (
|
||
!session ||
|
||
session.handoff?.cloudId ||
|
||
session.trustedOrigin !== origin()
|
||
)
|
||
throw forbidden("GitHub return did not match this instance");
|
||
const managementContext = await authorizeBrowserReturn(session, actor);
|
||
const bot = await endpoint(session.endpointId, session.userId);
|
||
if (session.status === "failed" || session.status === "exchanging")
|
||
return resume(bot);
|
||
if (session.status === "pending") {
|
||
if (!options.completeDirect)
|
||
throw conflict("GitHub registration is unavailable");
|
||
await options.completeDirect(state, code, managementContext);
|
||
}
|
||
const progress = await advance(bot.id, session.userId, managementContext);
|
||
return progress.state === "install" && progress.installationUrl
|
||
? progress.installationUrl
|
||
: resume(bot);
|
||
}
|
||
async function cloudCallback(
|
||
state: string,
|
||
registrationId: string,
|
||
claimId?: string,
|
||
actor?: AuthorizationActor,
|
||
) {
|
||
const [session] = await db
|
||
.select()
|
||
.from(chatGitHubRegistrations)
|
||
.where(
|
||
sql`${chatGitHubRegistrations.handoff}->>'cloudId' = ${registrationId}`,
|
||
)
|
||
.limit(1);
|
||
if (!session?.handoff || !claimId)
|
||
throw forbidden("GitHub return did not match this instance");
|
||
const managementContext = await authorizeBrowserReturn(session, actor, session.handoff.identityStateHash === hash(state));
|
||
const cloud = connector();
|
||
if (!cloud) throw conflict("Restore Paperclip Cloud enrollment");
|
||
const opened = await cloud.claimGitHubApp({
|
||
subject: session.userId,
|
||
companyId: session.companyId,
|
||
claimId,
|
||
redemptionId:
|
||
session.handoff.identityStateHash &&
|
||
hash(state) === session.handoff.identityStateHash
|
||
? session.handoff.identityRedemptionId!
|
||
: session.handoff.redemptionId,
|
||
});
|
||
if (
|
||
opened.kind !== "gateway_callback" ||
|
||
opened.registrationId !== registrationId ||
|
||
(opened.callbackKind !== "install" && opened.state !== state)
|
||
)
|
||
throw forbidden("GitHub return did not match this draft");
|
||
const manifestReturn =
|
||
opened.callbackKind === "manifest" &&
|
||
session.stateHash === hash(String(opened.state ?? ""));
|
||
const identityReturn =
|
||
opened.callbackKind === "oauth" &&
|
||
session.handoff.identityStateHash === hash(String(opened.state ?? ""));
|
||
if (!manifestReturn && !identityReturn && opened.callbackKind !== "install")
|
||
throw forbidden("GitHub return state did not match");
|
||
const bot = await endpoint(session.endpointId, session.userId);
|
||
if (session.trustedOrigin !== origin()) {
|
||
const cloud = connector();
|
||
if (!cloud) throw forbidden("GitHub return did not match this instance");
|
||
const current = await cloud.githubApp({
|
||
subject: session.userId,
|
||
companyId: session.companyId,
|
||
binding: { action: "status", id: registrationId },
|
||
});
|
||
// A manager-authenticated Cloud lookup, rather than a callback parameter,
|
||
// proves the current enrolled destination after an instance rename.
|
||
if (current.returnOrigin !== origin())
|
||
throw forbidden("GitHub return did not match this instance");
|
||
await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({ trustedOrigin: origin() })
|
||
.where(
|
||
and(
|
||
eq(chatGitHubRegistrations.id, session.id),
|
||
eq(chatGitHubRegistrations.trustedOrigin, session.trustedOrigin),
|
||
),
|
||
);
|
||
await logActivity(db, {
|
||
companyId: session.companyId,
|
||
actorType: "system",
|
||
actorId: "chat-github-setup",
|
||
action: "chat_github.registration_origin_recovered",
|
||
entityType: "tool_connection",
|
||
entityId: bot.connectionId,
|
||
details: { registrationId: session.id },
|
||
});
|
||
}
|
||
if (identityReturn) {
|
||
if (typeof opened.code !== "string" || opened.error) {
|
||
await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({
|
||
handoff: sql`${chatGitHubRegistrations.handoff} - 'identityStateHash'`,
|
||
})
|
||
.where(
|
||
and(
|
||
eq(chatGitHubRegistrations.id, session.id),
|
||
sql`${chatGitHubRegistrations.handoff}->>'identityStateHash' = ${hash(state)}`,
|
||
),
|
||
);
|
||
return resume(bot);
|
||
}
|
||
const route = await cloud.githubApp({
|
||
subject: session.userId,
|
||
companyId: session.companyId,
|
||
binding: { action: "status", id: registrationId },
|
||
});
|
||
if (opened.redirectUri !== route.callbackUrls.oauth)
|
||
throw forbidden("GitHub identity callback did not match");
|
||
return completeIdentity(session, opened.code, route.callbackUrls.oauth, actor);
|
||
}
|
||
if (manifestReturn && session.status === "pending") {
|
||
await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({
|
||
handoff: sql`jsonb_set(${chatGitHubRegistrations.handoff}, '{manifestClaimId}', ${JSON.stringify(claimId)}::jsonb)`,
|
||
})
|
||
.where(
|
||
and(
|
||
eq(chatGitHubRegistrations.id, session.id),
|
||
eq(chatGitHubRegistrations.status, "pending"),
|
||
),
|
||
);
|
||
}
|
||
const progress = await advance(session.endpointId, session.userId, managementContext);
|
||
return progress.state === "install" && progress.installationUrl
|
||
? progress.installationUrl
|
||
: resume(bot);
|
||
}
|
||
async function identityCallback(state: string, code: string, actor?: AuthorizationActor) {
|
||
const [session] = await db
|
||
.select()
|
||
.from(chatGitHubRegistrations)
|
||
.where(
|
||
sql`${chatGitHubRegistrations.handoff}->>'identityStateHash' = ${hash(state)}`,
|
||
)
|
||
.limit(1);
|
||
if (!session || session.trustedOrigin !== origin())
|
||
throw forbidden("GitHub identity return did not match");
|
||
await authorizeBrowserReturn(session, actor, true);
|
||
await endpoint(session.endpointId, session.userId);
|
||
if (!code) {
|
||
await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({
|
||
handoff: sql`${chatGitHubRegistrations.handoff} - 'identityStateHash'`,
|
||
})
|
||
.where(
|
||
and(
|
||
eq(chatGitHubRegistrations.id, session.id),
|
||
sql`${chatGitHubRegistrations.handoff}->>'identityStateHash' = ${hash(state)}`,
|
||
),
|
||
);
|
||
return resume(await endpoint(session.endpointId, session.userId));
|
||
}
|
||
return completeIdentity(
|
||
session,
|
||
code,
|
||
`${origin()}/api/chat-github/identity/callback`,
|
||
actor,
|
||
);
|
||
}
|
||
async function confirmIdentity(
|
||
id: string,
|
||
userId: string,
|
||
githubUserId: string,
|
||
) {
|
||
const bot = await endpoint(id, userId),
|
||
session = await registration(bot),
|
||
candidate = session?.handoff?.identity;
|
||
if (
|
||
session?.userId !== userId ||
|
||
!candidate ||
|
||
candidate.githubUserId !== githubUserId ||
|
||
Date.parse(candidate.expiresAt) <= Date.now()
|
||
)
|
||
throw conflict(
|
||
"Connect your GitHub account again before confirming its identity",
|
||
);
|
||
await withIdentityLease(session.id, async (assertOwned) => {
|
||
const [current] = await db
|
||
.select()
|
||
.from(chatGitHubRegistrations)
|
||
.where(eq(chatGitHubRegistrations.id, session.id));
|
||
if (
|
||
current?.handoff?.identity?.githubUserId !== githubUserId ||
|
||
current.handoff.identity.expiresAt !== candidate.expiresAt
|
||
)
|
||
throw conflict("GitHub identity changed. Connect your account again.");
|
||
await assertOwned();
|
||
await management.linkObservedIdentity(id, userId, candidate);
|
||
await db
|
||
.update(chatGitHubRegistrations)
|
||
.set({ handoff: sql`${chatGitHubRegistrations.handoff} - 'identity'` })
|
||
.where(eq(chatGitHubRegistrations.id, session.id));
|
||
});
|
||
return advance(id, userId);
|
||
}
|
||
return {
|
||
saveDraft,
|
||
repairCloud,
|
||
restartRegistration,
|
||
start,
|
||
advance,
|
||
startIdentity,
|
||
directCallback,
|
||
cloudCallback,
|
||
identityCallback,
|
||
confirmIdentity,
|
||
};
|
||
}
|