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

Function registerDynamicClient

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

Source from the content-addressed store, hash-verified

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