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

Function persistRefreshedToken

packages/core/sdk/src/executor.ts:2314–2353  ·  view source on GitHub ↗
(
      row: ConnectionRow,
      provider: CredentialProvider,
      token: OAuth2TokenResponse,
      storedRefreshToken?: string | undefined,
    )

Source from the content-addressed store, hash-verified

2312 * has not changed; a rotated token never matches, so the write that
2313 * actually matters is never skipped. */
2314 const persistRefreshedToken = (
2315 row: ConnectionRow,
2316 provider: CredentialProvider,
2317 token: OAuth2TokenResponse,
2318 storedRefreshToken?: string | undefined,
2319 ): Effect.Effect<void, StorageFailure> =>
2320 Effect.gen(function* () {
2321 if (provider.set) {
2322 // OAuth is always single-input: the access token lives in the `token`
2323 // item. Fall back to a deterministic id if the map is somehow empty.
2324 const tokenItemId =
2325 connectionItemIds(row)[PRIMARY_INPUT_VARIABLE] ??
2326 `connection:${row.owner}:${row.integration}:${row.name}:${PRIMARY_INPUT_VARIABLE}`;
2327 if (
2328 token.refresh_token &&
2329 row.refresh_item_id &&
2330 token.refresh_token !== storedRefreshToken
2331 ) {
2332 yield* provider.set(ProviderItemId.make(row.refresh_item_id), token.refresh_token);
2333 }
2334 yield* provider.set(ProviderItemId.make(tokenItemId), token.access_token);
2335 }
2336
2337 const nextExpiresAt =
2338 typeof token.expires_in === "number" ? Date.now() + token.expires_in * 1000 : null;
2339 const set: Record<string, unknown> = {
2340 expires_at: nextExpiresAt,
2341 updated_at: new Date(),
2342 };
2343 if (token.scope !== undefined) set.oauth_scope = token.scope;
2344 yield* core.updateMany("connection", {
2345 where: (b: AnyCb) =>
2346 b.and(
2347 byOwner(row.owner as Owner)(b),
2348 b("integration", "=", String(row.integration)),
2349 b("name", "=", String(row.name)),
2350 ),
2351 set,
2352 });
2353 });
2354
2355 /** The rendered message of a typed enterprise-managed failure. */
2356 const enterpriseManagedMessage = (cause: EnterpriseManagedMintError): string =>

Callers 1

performTokenRefreshFunction · 0.85

Calls 3

connectionItemIdsFunction · 0.85
byOwnerFunction · 0.85
setMethod · 0.65

Tested by

no test coverage detected