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

Function registerDynamicClient

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

Source from the content-addressed store, hash-verified

1176 });
1177
1178 const registerDynamicClient = (
1179 input: RegisterDynamicClientInput,
1180 ): Effect.Effect<OAuthClientSlug, OAuthRegisterDynamicError | StorageFailure> =>
1181 Effect.gen(function* () {
1182 const issuer = canonicalDcrIssuer(input.issuer, input.registrationEndpoint);
1183 // Resolved before the reuse decision: a persisted client registered with
1184 // a DIFFERENT callback must not be reused (strict servers 400 the
1185 // authorize request), so the reuse lookup compares against this value.
1186 const flowRedirectUri = input.redirectUri ?? redirectUri ?? null;
1187 const reuse = yield* decideDcrClientReuse(input, issuer, flowRedirectUri);
1188 if (reuse.existingSlug !== null) return reuse.existingSlug;
1189
1190 const slug = reuse.registrationSlug;
1191 // DCR registers our callback as the client's redirect_uri — fail loudly
1192 // if the executor has none rather than registering a localhost URL.
1193 if (flowRedirectUri == null) {
1194 return yield* new OAuthRegisterDynamicError({
1195 message: REDIRECT_URI_REQUIRED_MESSAGE,
1196 });
1197 }
1198 const authMethod = pickDcrAuthMethod(input.tokenEndpointAuthMethodsSupported);
1199 const information = yield* registerDynamicClientDcr(
1200 {
1201 registrationEndpoint: input.registrationEndpoint,
1202 metadata: {
1203 client_name: input.clientName,
1204 redirect_uris: [flowRedirectUri],
1205 grant_types: ["authorization_code", "refresh_token"],
1206 response_types: ["code"],
1207 token_endpoint_auth_method: authMethod,
1208 application_type: isLoopbackHttpUrl(flowRedirectUri) ? "native" : "web",
1209 scope: input.scopes.length > 0 ? input.scopes.join(" ") : undefined,
1210 },
1211 },
1212 { httpClientLayer, endpointUrlPolicy: deps.endpointUrlPolicy },
1213 ).pipe(
1214 Effect.mapError((cause) => {
1215 // Some authorization servers (Vercel, and others that follow RFC 8252
1216 // strictly) reject anonymous Dynamic Client Registration unless the
1217 // redirect URI is loopback (http://localhost or http://127.0.0.1).
1218 // Executor registers its browser origin, so any hosted, tailnet, or
1219 // LAN origin trips `invalid_redirect_uri`. Turn that opaque RFC code
1220 // into guidance the user can act on instead of the raw error.
1221 // oxlint-disable-next-line executor/no-unknown-error-message -- boundary: OAuthDiscoveryError carries a typed `message`
1222 const rawMessage = cause.message;
1223 const message =
1224 cause.error === "invalid_redirect_uri" && !isLoopbackHttpUrl(flowRedirectUri)
1225 ? `Automatic OAuth setup failed: this server only approves loopback redirect ` +
1226 `URLs (http://localhost or http://127.0.0.1) for automatic registration, but ` +
1227 `Executor is using ${flowRedirectUri}. Register an OAuth app manually with that ` +
1228 `redirect URL approved by the server, or run Executor on http://localhost.`
1229 : `Dynamic Client Registration failed: ${rawMessage}`;
1230 return new OAuthRegisterDynamicError({ message });
1231 }),
1232 );
1233
1234 // Persist the minted client. DCR-minted public clients have no secret; we
1235 // store "" so the PKCE-only token exchange omits `client_secret`.

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected