MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / start

Function start

packages/core/sdk/src/oauth-service.ts:1696–2151  ·  view source on GitHub ↗
(
    input: OAuthStartInput,
  )

Source from the content-addressed store, hash-verified

1694 // start — begin a flow through a client to mint a connection.
1695 // -----------------------------------------------------------------------
1696 const start = (
1697 input: OAuthStartInput,
1698 ): Effect.Effect<ConnectResult, OAuthStartError | OrgWriteDeniedError | StorageFailure> =>
1699 Effect.gen(function* () {
1700 // Gate before any session row or upstream exchange: minting a Workspace
1701 // connection (including a reconnect that would replace its credential)
1702 // is a workspace-level change. Personal connections remain member-owned.
1703 yield* deps.guardOrgWrite(input.owner);
1704 const keys = yield* Effect.try({
1705 try: () => deps.ownedKeys(input.owner),
1706 catch: (cause) =>
1707 new StorageError({
1708 message: "Cannot start OAuth flow for owner without a subject",
1709 cause,
1710 }),
1711 });
1712 // The integration must exist BEFORE any session or provider round trip.
1713 // A stale reference (a connection whose integration was removed, or an
1714 // agent replaying an old slug) would otherwise complete authorization at
1715 // the provider and fail only at the mint.
1716 if (!(yield* deps.integrationExists(input.integration))) {
1717 return yield* new OAuthStartError({
1718 message: `Integration not found: ${String(input.integration)}`,
1719 });
1720 }
1721 // Sharing is one-directional (org → members): a Workspace (org) connection
1722 // cannot be backed by a member's private (user) app. The connection owner
1723 // and the app owner are otherwise independent — a Personal connection
1724 // through a shared Workspace app is the supported cross-owner case.
1725 // First-party apps are deployment-owned, outside the owner lattice
1726 // entirely, so the rule does not apply to them.
1727 const firstPartyFlow = isFirstPartyOAuthClientSlug(String(input.client));
1728 yield* Effect.annotateCurrentSpan({
1729 "executor.oauth.client_first_party": firstPartyFlow,
1730 });
1731 if (!firstPartyFlow && input.owner === "org" && input.clientOwner === "user") {
1732 return yield* new OAuthStartError({
1733 message: "A Workspace connection must use a Workspace app.",
1734 });
1735 }
1736 // Load the app by its EXPLICIT owner (the caller knows it — no derivation).
1737 // The connection is still minted under `input.owner`. Storage visibility
1738 // policy hides apps the actor cannot see, so a wrong owner yields null.
1739 const client = yield* loadClient(input.clientOwner, input.client);
1740 if (!client) {
1741 return yield* new OAuthStartError({
1742 message: `OAuth client not found: ${input.client}`,
1743 });
1744 }
1745
1746 // Normalize the name the same way the mint stores it, so the free-name
1747 // guard below compares against the exact stored form.
1748 const requestedName = connectionIdentifier(String(input.name));
1749 // newConnection: resolve the requested name to a FREE one against the
1750 // stored rows (not a client-side, policy-filtered view), so a second
1751 // untyped connect mints `personalGmail2` instead of silently re-minting
1752 // the first account's row. Reconnects omit the flag and keep targeting
1753 // their existing row. Bounded: a pathological owner with 1000 same-named

Callers

nothing calls this directly

Calls 15

connectionIdentifierFunction · 0.90
providerAuthorizeExtrasFunction · 0.90
createPkceCodeVerifierFunction · 0.90
createPkceCodeChallengeFunction · 0.90
createOAuthStateFunction · 0.90
encodeOAuthCallbackStateFunction · 0.90
buildAuthorizationUrlFunction · 0.90
loadClientFunction · 0.85

Tested by

no test coverage detected