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

Function dcrCandidatesForIssuer

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

Source from the content-addressed store, hash-verified

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