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

Function dcrCandidatesForIssuer

packages/core/sdk/src/oauth-service.ts:728–774  ·  view source on GitHub ↗
(
    owner: Owner,
    issuer: string | null,
  )

Source from the content-addressed store, hash-verified

726 };
727
728 const dcrCandidatesForIssuer = (
729 owner: Owner,
730 issuer: string | null,
731 ): Effect.Effect<readonly DcrReuseCandidate[], StorageFailure> =>
732 deps.fuma
733 .use("oauth_client.findMany", (db) =>
734 looseDb(db).findMany("oauth_client", {
735 where: (b: any) => b("owner", "=", owner),
736 }),
737 )
738 .pipe(
739 Effect.map((rows) => {
740 const matches = rows.flatMap(
741 (row): readonly (DcrReuseCandidate & { readonly createdAt: number })[] => {
742 if (parseOAuthClientOrigin(row).kind !== "dynamic_client_registration") return [];
743 // A candidate matches only via a non-null, canonicalized stored
744 // issuer. The GC migration backfills origin_issuer on every
745 // surviving DCR row, so post-migration a null-issuer row is a
746 // transient (unmigrated) row; skipping it just mints one duplicate
747 // the migration then GCs, rather than reusing on a fuzzy token-host
748 // guess.
749 const rowIssuer =
750 row.origin_issuer == null ? null : canonicalIssuerUrl(String(row.origin_issuer));
751 const issuerMatches = rowIssuer !== null && dcrIssuerMatches(rowIssuer, issuer);
752 if (!issuerMatches) return [];
753 return [
754 {
755 slug: OAuthClientSlug.make(String(row.slug)),
756 resource: row.resource == null ? null : String(row.resource),
757 createdAt: candidateCreatedAt(row.created_at),
758 },
759 ];
760 },
761 );
762 // Deterministic reuse order: oldest first, slug as a stable tiebreak
763 // when timestamps collide or are missing. Without this, which of
764 // several live duplicates sharing an (owner, issuer) gets reused is
765 // whatever order the storage backend returned rows in — the reuse
766 // pick must be stable across boots and backends.
767 return [...matches]
768 .sort(
769 (a, b) =>
770 a.createdAt - b.createdAt || (a.slug < b.slug ? -1 : a.slug > b.slug ? 1 : 0),
771 )
772 .map(({ slug, resource }): DcrReuseCandidate => ({ slug, resource }));
773 }),
774 );
775
776 const decideDcrClientReuse = (
777 input: RegisterDynamicClientInput,

Callers 1

decideDcrClientReuseFunction · 0.85

Calls 5

canonicalIssuerUrlFunction · 0.90
looseDbFunction · 0.85
parseOAuthClientOriginFunction · 0.85
dcrIssuerMatchesFunction · 0.85
candidateCreatedAtFunction · 0.85

Tested by

no test coverage detected