(
input: RegisterDynamicClientInput,
)
| 1091 | }); |
| 1092 | |
| 1093 | const registerDynamicClient = ( |
| 1094 | input: RegisterDynamicClientInput, |
| 1095 | ): Effect.Effect<OAuthClientSlug, OAuthRegisterDynamicError | StorageFailure> => |
| 1096 | Effect.gen(function* () { |
| 1097 | const issuer = canonicalDcrIssuer(input.issuer, input.registrationEndpoint); |
| 1098 | // Resolved before the reuse decision: a persisted client registered with |
| 1099 | // a DIFFERENT callback must not be reused (strict servers 400 the |
| 1100 | // authorize request), so the reuse lookup compares against this value. |
| 1101 | const flowRedirectUri = input.redirectUri ?? redirectUri ?? null; |
| 1102 | const reuse = yield* decideDcrClientReuse(input, issuer, flowRedirectUri); |
| 1103 | if (reuse.existingSlug !== null) return reuse.existingSlug; |
| 1104 | |
| 1105 | const slug = reuse.registrationSlug; |
| 1106 | // DCR registers our callback as the client's redirect_uri — fail loudly |
| 1107 | // if the executor has none rather than registering a localhost URL. |
| 1108 | if (flowRedirectUri == null) { |
| 1109 | return yield* new OAuthRegisterDynamicError({ |
| 1110 | message: REDIRECT_URI_REQUIRED_MESSAGE, |
| 1111 | }); |
| 1112 | } |
| 1113 | const authMethod = pickDcrAuthMethod(input.tokenEndpointAuthMethodsSupported); |
| 1114 | const information = yield* registerDynamicClientDcr( |
| 1115 | { |
| 1116 | registrationEndpoint: input.registrationEndpoint, |
| 1117 | metadata: { |
| 1118 | client_name: input.clientName, |
| 1119 | redirect_uris: [flowRedirectUri], |
| 1120 | grant_types: ["authorization_code", "refresh_token"], |
| 1121 | response_types: ["code"], |
| 1122 | token_endpoint_auth_method: authMethod, |
| 1123 | application_type: isLoopbackHttpUrl(flowRedirectUri) ? "native" : "web", |
| 1124 | scope: input.scopes.length > 0 ? input.scopes.join(" ") : undefined, |
| 1125 | }, |
| 1126 | }, |
| 1127 | { httpClientLayer, endpointUrlPolicy: deps.endpointUrlPolicy }, |
| 1128 | ).pipe( |
| 1129 | Effect.mapError((cause) => { |
| 1130 | // Some authorization servers (Vercel, and others that follow RFC 8252 |
| 1131 | // strictly) reject anonymous Dynamic Client Registration unless the |
| 1132 | // redirect URI is loopback (http://localhost or http://127.0.0.1). |
| 1133 | // Executor registers its browser origin, so any hosted, tailnet, or |
| 1134 | // LAN origin trips `invalid_redirect_uri`. Turn that opaque RFC code |
| 1135 | // into guidance the user can act on instead of the raw error. |
| 1136 | // oxlint-disable-next-line executor/no-unknown-error-message -- boundary: OAuthDiscoveryError carries a typed `message` |
| 1137 | const rawMessage = cause.message; |
| 1138 | const message = |
| 1139 | cause.error === "invalid_redirect_uri" && !isLoopbackHttpUrl(flowRedirectUri) |
| 1140 | ? `Automatic OAuth setup failed: this server only approves loopback redirect ` + |
| 1141 | `URLs (http://localhost or http://127.0.0.1) for automatic registration, but ` + |
| 1142 | `Executor is using ${flowRedirectUri}. Register an OAuth app manually with that ` + |
| 1143 | `redirect URL approved by the server, or run Executor on http://localhost.` |
| 1144 | : `Dynamic Client Registration failed: ${rawMessage}`; |
| 1145 | return new OAuthRegisterDynamicError({ message }); |
| 1146 | }), |
| 1147 | ); |
| 1148 | |
| 1149 | // Persist the minted client. DCR-minted public clients have no secret; we |
| 1150 | // store "" so the PKCE-only token exchange omits `client_secret`. |
nothing calls this directly
no test coverage detected