(
input: RegisterDynamicClientInput,
)
| 855 | }); |
| 856 | |
| 857 | const registerDynamicClient = ( |
| 858 | input: RegisterDynamicClientInput, |
| 859 | ): Effect.Effect<OAuthClientSlug, OAuthRegisterDynamicError | StorageFailure> => |
| 860 | Effect.gen(function* () { |
| 861 | const issuer = canonicalDcrIssuer(input.issuer, input.registrationEndpoint); |
| 862 | // Resolved before the reuse decision: a persisted client registered with |
| 863 | // a DIFFERENT callback must not be reused (strict servers 400 the |
| 864 | // authorize request), so the reuse lookup compares against this value. |
| 865 | const flowRedirectUri = input.redirectUri ?? redirectUri ?? null; |
| 866 | const reuse = yield* decideDcrClientReuse(input, issuer, flowRedirectUri); |
| 867 | if (reuse.existingSlug !== null) return reuse.existingSlug; |
| 868 | |
| 869 | const slug = reuse.registrationSlug; |
| 870 | // DCR registers our callback as the client's redirect_uri — fail loudly |
| 871 | // if the executor has none rather than registering a localhost URL. |
| 872 | if (flowRedirectUri == null) { |
| 873 | return yield* new OAuthRegisterDynamicError({ |
| 874 | message: REDIRECT_URI_REQUIRED_MESSAGE, |
| 875 | }); |
| 876 | } |
| 877 | const authMethod = pickDcrAuthMethod(input.tokenEndpointAuthMethodsSupported); |
| 878 | const information = yield* registerDynamicClientDcr( |
| 879 | { |
| 880 | registrationEndpoint: input.registrationEndpoint, |
| 881 | metadata: { |
| 882 | client_name: input.clientName, |
| 883 | redirect_uris: [flowRedirectUri], |
| 884 | grant_types: ["authorization_code", "refresh_token"], |
| 885 | response_types: ["code"], |
| 886 | token_endpoint_auth_method: authMethod, |
| 887 | scope: input.scopes.length > 0 ? input.scopes.join(" ") : undefined, |
| 888 | }, |
| 889 | }, |
| 890 | { httpClientLayer, endpointUrlPolicy: deps.endpointUrlPolicy }, |
| 891 | ).pipe( |
| 892 | Effect.mapError((cause) => { |
| 893 | // Some authorization servers (Vercel, and others that follow RFC 8252 |
| 894 | // strictly) reject anonymous Dynamic Client Registration unless the |
| 895 | // redirect URI is loopback (http://localhost or http://127.0.0.1). |
| 896 | // Executor registers its browser origin, so any hosted, tailnet, or |
| 897 | // LAN origin trips `invalid_redirect_uri`. Turn that opaque RFC code |
| 898 | // into guidance the user can act on instead of the raw error. |
| 899 | // oxlint-disable-next-line executor/no-unknown-error-message -- boundary: OAuthDiscoveryError carries a typed `message` |
| 900 | const rawMessage = cause.message; |
| 901 | const message = |
| 902 | cause.error === "invalid_redirect_uri" && !isLoopbackHttpUrl(flowRedirectUri) |
| 903 | ? `Automatic OAuth setup failed: this server only approves loopback redirect ` + |
| 904 | `URLs (http://localhost or http://127.0.0.1) for automatic registration, but ` + |
| 905 | `Executor is using ${flowRedirectUri}. Register an OAuth app manually with that ` + |
| 906 | `redirect URL approved by the server, or run Executor on http://localhost.` |
| 907 | : `Dynamic Client Registration failed: ${rawMessage}`; |
| 908 | return new OAuthRegisterDynamicError({ message }); |
| 909 | }), |
| 910 | ); |
| 911 | |
| 912 | // Persist the minted client. DCR-minted public clients have no secret; we |
| 913 | // store "" so the PKCE-only token exchange omits `client_secret`. |
| 914 | // Confidential DCR clients keep the returned secret in the credential |
nothing calls this directly
no test coverage detected