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

Function registerDynamicClient

packages/core/sdk/src/oauth-service.ts:1435–1522  ·  view source on GitHub ↗
(
    input: RegisterDynamicClientInput,
  )

Source from the content-addressed store, hash-verified

1433 });
1434
1435 const registerDynamicClient = (
1436 input: RegisterDynamicClientInput,
1437 ): Effect.Effect<
1438 OAuthClientSlug,
1439 OAuthRegisterDynamicError | OrgWriteDeniedError | StorageFailure
1440 > =>
1441 Effect.gen(function* () {
1442 yield* deps.guardOrgWrite(input.owner);
1443 const issuer = canonicalDcrIssuer(input.issuer, input.registrationEndpoint);
1444 // Resolved before the reuse decision: a persisted client registered with
1445 // a DIFFERENT callback must not be reused (strict servers 400 the
1446 // authorize request), so the reuse lookup compares against this value.
1447 const flowRedirectUri = input.redirectUri ?? redirectUri ?? null;
1448 const reuse = yield* decideDcrClientReuse(input, issuer, flowRedirectUri);
1449 if (reuse.existingSlug !== null) return reuse.existingSlug;
1450
1451 const slug = reuse.registrationSlug;
1452 // DCR registers our callback as the client's redirect_uri — fail loudly
1453 // if the executor has none rather than registering a localhost URL.
1454 if (flowRedirectUri == null) {
1455 return yield* new OAuthRegisterDynamicError({
1456 message: REDIRECT_URI_REQUIRED_MESSAGE,
1457 });
1458 }
1459 const authMethod = pickDcrAuthMethod(input.tokenEndpointAuthMethodsSupported);
1460 const registrationScopes = dedupeScopes([
1461 ...input.scopes,
1462 ...additionalAuthorizationLifecycleScopes(input),
1463 ]);
1464 const information = yield* registerDynamicClientDcr(
1465 {
1466 registrationEndpoint: input.registrationEndpoint,
1467 metadata: {
1468 client_name: input.clientName,
1469 redirect_uris: [flowRedirectUri],
1470 grant_types: ["authorization_code", "refresh_token"],
1471 response_types: ["code"],
1472 token_endpoint_auth_method: authMethod,
1473 application_type: isLoopbackHttpUrl(flowRedirectUri) ? "native" : "web",
1474 scope: registrationScopes.length > 0 ? registrationScopes.join(" ") : undefined,
1475 },
1476 },
1477 { httpClientLayer, endpointUrlPolicy: deps.endpointUrlPolicy },
1478 ).pipe(
1479 Effect.mapError((cause) => {
1480 // Some authorization servers (Vercel, and others that follow RFC 8252
1481 // strictly) reject anonymous Dynamic Client Registration unless the
1482 // redirect URI is loopback (http://localhost or http://127.0.0.1).
1483 // Executor registers its browser origin, so any hosted, tailnet, or
1484 // LAN origin trips `invalid_redirect_uri`. Turn that opaque RFC code
1485 // into guidance the user can act on instead of the raw error.
1486 // oxlint-disable-next-line executor/no-unknown-error-message -- boundary: OAuthDiscoveryError carries a typed `message`
1487 const rawMessage = cause.message;
1488 const message =
1489 cause.error === "invalid_redirect_uri" && !isLoopbackHttpUrl(flowRedirectUri)
1490 ? `Automatic OAuth setup failed: this server only approves loopback redirect ` +
1491 `URLs (http://localhost or http://127.0.0.1) for automatic registration, but ` +
1492 `Executor is using ${flowRedirectUri}. Register an OAuth app manually with that ` +

Callers

nothing calls this directly

Calls 7

isLoopbackHttpUrlFunction · 0.90
canonicalDcrIssuerFunction · 0.85
decideDcrClientReuseFunction · 0.85
pickDcrAuthMethodFunction · 0.85
dedupeScopesFunction · 0.85
createClientFunction · 0.70

Tested by

no test coverage detected