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

Function complete

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

Source from the content-addressed store, hash-verified

1170 // complete — redeem the session, exchange the code, mint the connection.
1171 // -----------------------------------------------------------------------
1172 const complete = (
1173 input: OAuthCompleteInput,
1174 ): Effect.Effect<Connection, OAuthCompleteError | OAuthSessionNotFoundError | StorageFailure> =>
1175 Effect.gen(function* () {
1176 const sessionRow = yield* deps.fuma.use("oauth_session.findFirst", (db) =>
1177 looseDb(db).findFirst("oauth_session", {
1178 where: (b: any) => b("state", "=", String(input.state)),
1179 }),
1180 );
1181 if (!sessionRow) {
1182 return yield* new OAuthSessionNotFoundError({ state: input.state });
1183 }
1184 const session = {
1185 owner: String(sessionRow.owner) as Owner,
1186 clientSlug: OAuthClientSlug.make(String(sessionRow.client_slug)),
1187 integration: IntegrationSlug.make(String(sessionRow.integration)),
1188 name: ConnectionName.make(String(sessionRow.name)),
1189 template: AuthTemplateSlug.make(String(sessionRow.template)),
1190 redirectUrl: String(sessionRow.redirect_url),
1191 pkceVerifier: sessionRow.pkce_verifier == null ? null : String(sessionRow.pkce_verifier),
1192 identityLabel: sessionRow.identity_label == null ? null : String(sessionRow.identity_label),
1193 expiresAt: Number(sessionRow.expires_at),
1194 // The scope set `start` requested (the integration's declared or
1195 // discovered scopes), persisted on the session payload. Drives the
1196 // recorded-scope fallback when the AS omits `scope`. Missing/legacy
1197 // payloads fall back to the client's scopes below.
1198 requestedScopes: requestedScopesFromPayload(sessionRow.payload),
1199 // The app's owner, recorded by `start` — reload the SAME app at
1200 // completion by explicit owner (no derivation). Defaults to the session
1201 // owner for same-owner connects.
1202 clientOwner:
1203 clientOwnerFromPayload(sessionRow.payload) ?? (String(sessionRow.owner) as Owner),
1204 };
1205
1206 // Expired sessions are not redeemable — drop + treat as not found.
1207 if (Number.isFinite(session.expiresAt) && session.expiresAt <= Date.now()) {
1208 yield* deleteSession(input.state);
1209 return yield* new OAuthSessionNotFoundError({ state: input.state });
1210 }
1211
1212 // Reload the SAME app `start` resolved, by its explicit recorded owner.
1213 const client = yield* loadClient(session.clientOwner, session.clientSlug);
1214 if (!client) {
1215 return yield* new OAuthCompleteError({
1216 message: `OAuth client not found: ${session.clientSlug}`,
1217 restartRequired: true,
1218 });
1219 }
1220
1221 // The PKCE verifier is minted by `start` for every authorization_code
1222 // session. A null/missing one means a corrupt session row — exchanging
1223 // with an empty verifier would violate RFC 7636 and the AS would reject
1224 // it with an opaque error. Fail loudly + require a restart instead.
1225 if (session.pkceVerifier == null) {
1226 return yield* new OAuthCompleteError({
1227 message: `OAuth session ${input.state} is missing its PKCE code verifier; restart the flow.`,
1228 restartRequired: true,
1229 });

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