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

Function complete

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

Source from the content-addressed store, hash-verified

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

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