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

Function complete

packages/core/sdk/src/oauth-service.ts:1252–1397  ·  view source on GitHub ↗
(
    input: OAuthCompleteInput,
  )

Source from the content-addressed store, hash-verified

1250 // complete — redeem the session, exchange the code, mint the connection.
1251 // -----------------------------------------------------------------------
1252 const complete = (
1253 input: OAuthCompleteInput,
1254 ): Effect.Effect<Connection, OAuthCompleteError | OAuthSessionNotFoundError | StorageFailure> =>
1255 Effect.gen(function* () {
1256 const sessionRow = yield* deps.fuma.use("oauth_session.findFirst", (db) =>
1257 looseDb(db).findFirst("oauth_session", {
1258 where: (b: any) => b("state", "=", String(input.state)),
1259 }),
1260 );
1261 if (!sessionRow) {
1262 return yield* new OAuthSessionNotFoundError({ state: input.state });
1263 }
1264 const session = {
1265 owner: String(sessionRow.owner) as Owner,
1266 clientSlug: OAuthClientSlug.make(String(sessionRow.client_slug)),
1267 integration: IntegrationSlug.make(String(sessionRow.integration)),
1268 name: ConnectionName.make(String(sessionRow.name)),
1269 template: AuthTemplateSlug.make(String(sessionRow.template)),
1270 redirectUrl: String(sessionRow.redirect_url),
1271 pkceVerifier: sessionRow.pkce_verifier == null ? null : String(sessionRow.pkce_verifier),
1272 identityLabel: sessionRow.identity_label == null ? null : String(sessionRow.identity_label),
1273 expiresAt: Number(sessionRow.expires_at),
1274 // The scope set `start` requested (the integration's declared or
1275 // discovered scopes), persisted on the session payload. Drives the
1276 // recorded-scope fallback when the AS omits `scope`. Missing/legacy
1277 // payloads fall back to the client's scopes below.
1278 requestedScopes: requestedScopesFromPayload(sessionRow.payload),
1279 // The app's owner, recorded by `start` — reload the SAME app at
1280 // completion by explicit owner (no derivation). Defaults to the session
1281 // owner for same-owner connects.
1282 clientOwner:
1283 clientOwnerFromPayload(sessionRow.payload) ?? (String(sessionRow.owner) as Owner),
1284 };
1285
1286 // Annotate as soon as the session resolves the flow's identity, so even
1287 // a completion that fails at the exchange still says WHOSE connect died.
1288 yield* Effect.annotateCurrentSpan({
1289 "executor.integration": String(session.integration),
1290 "executor.connection": String(session.name),
1291 "executor.template": String(session.template),
1292 "executor.oauth.client": String(session.clientSlug),
1293 });
1294
1295 // Expired sessions are not redeemable — drop + treat as not found.
1296 if (Number.isFinite(session.expiresAt) && session.expiresAt <= Date.now()) {
1297 yield* deleteSession(input.state);
1298 return yield* new OAuthSessionNotFoundError({ state: input.state });
1299 }
1300
1301 // Reload the SAME app `start` resolved, by its explicit recorded owner.
1302 const client = yield* loadClient(session.clientOwner, session.clientSlug);
1303 if (!client) {
1304 return yield* new OAuthCompleteError({
1305 message: `OAuth client not found: ${session.clientSlug}`,
1306 restartRequired: true,
1307 });
1308 }
1309

Callers

nothing calls this directly

Calls 8

looseDbFunction · 0.85
clientOwnerFromPayloadFunction · 0.85
deleteSessionFunction · 0.85
loadClientFunction · 0.85
mintFromTokenFunction · 0.85

Tested by

no test coverage detected