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

Function decideDcrClientReuse

packages/core/sdk/src/oauth-service.ts:776–817  ·  view source on GitHub ↗
(
    input: RegisterDynamicClientInput,
    issuer: string | null,
  )

Source from the content-addressed store, hash-verified

774 );
775
776 const decideDcrClientReuse = (
777 input: RegisterDynamicClientInput,
778 issuer: string | null,
779 ): Effect.Effect<
780 {
781 readonly existingSlug: OAuthClientSlug | null;
782 readonly registrationSlug: OAuthClientSlug;
783 },
784 StorageFailure
785 > =>
786 Effect.gen(function* () {
787 const candidates = yield* dcrCandidatesForIssuer(input.owner, issuer);
788 const resource = input.resource ?? null;
789 if (resource !== null) {
790 const matchingResource = candidates.find((client) => client.resource === resource);
791 if (matchingResource) {
792 return { existingSlug: matchingResource.slug, registrationSlug: matchingResource.slug };
793 }
794 const slug = dcrClientSlug(issuer, candidates.length > 0 ? resource : null, input.slug);
795 return {
796 existingSlug: null,
797 registrationSlug: slug,
798 };
799 }
800
801 // Resource-less request: only reuse a resource-LESS candidate. A client
802 // minted for a specific RFC 8707 resource must NOT be reused for a
803 // resource-less flow (its tokens are bound to that resource), so when only
804 // resource-scoped candidates exist we register a fresh resource-less client
805 // rather than silently borrowing one (the old `?? candidates[0]` bug).
806 const reusable = candidates.find((client) => client.resource === null);
807 if (reusable) return { existingSlug: reusable.slug, registrationSlug: reusable.slug };
808 // Fresh resource-less client. Its slug is the bare `dcr-<host>` base, but
809 // the FIRST resource-scoped registration for an issuer also takes that base
810 // (dcrClientSlug only suffixes once candidates exist). `createClient`
811 // deletes any row with a colliding (owner, slug) first, so reusing the base
812 // here would silently clobber that resource-scoped client. Dedupe against
813 // the existing candidate slugs so the resource-less client keeps its own row.
814 const takenSlugs = new Set(candidates.map((client) => String(client.slug)));
815 const slug = uniqueDcrSlug(dcrClientSlug(issuer, null, input.slug), takenSlugs);
816 return { existingSlug: null, registrationSlug: slug };
817 });
818
819 const registerDynamicClient = (
820 input: RegisterDynamicClientInput,

Callers 1

registerDynamicClientFunction · 0.85

Calls 3

dcrCandidatesForIssuerFunction · 0.85
dcrClientSlugFunction · 0.85
uniqueDcrSlugFunction · 0.85

Tested by

no test coverage detected