| @@ -25,6 +25,18 @@ const TEMPLATE = AuthTemplateSlug.make("oauth"); · 24 unmodified lines |
| 25 | 25 | const CLIENT = OAuthClientSlug.make("acme-dcr"); |
| 26 | 26 | const FLOW_REDIRECT_URI = "https://localhost:5394/api/oauth/callback"; |
| 27 | 27 | |
| 28 | +const dcrSlugFor = (issuerUrl: string): OAuthClientSlug => |
| 29 | + OAuthClientSlug.make( |
| 30 | + `dcr-${new URL(issuerUrl).host |
| 31 | + .toLowerCase() |
| 32 | + .replace(/[^a-z0-9]+/g, "-") |
| 33 | + .replace(/^-+|-+$/g, "")}`, |
| 34 | + ); |
| 35 | + |
| 36 | +const registerRequestCount = ( |
| 37 | + requests: readonly { readonly path: string; readonly method: string }[], |
| 38 | +): number => requests.filter((r) => r.path === "/register" && r.method === "POST").length; |
| 39 | + |
| 28 | 40 | const oauthPlugin = definePlugin(() => ({ |
| 29 | 41 | id: "acme" as const, |
| 30 | 42 | storage: () => ({}), |
| @@ -56,6 +68,7 @@ describe("oauth.registerDynamicClient", () => { · 25 unmodified lines |
| 56 | 68 | // Probe surfaces the registration endpoint + advertised auth methods so |
| 57 | 69 | // the caller knows a public client is allowed. |
| 58 | 70 | const probe = yield* executor.oauth.probe({ url: server.mcpResourceUrl }); |
| 71 | + expect(probe.issuer).toBe(server.issuerUrl); |
| 59 | 72 | expect(probe.registrationEndpoint).toBe(server.registrationEndpoint); |
| 60 | 73 | expect(probe.tokenEndpointAuthMethodsSupported).toContain("none"); |
| 61 | 74 | expect(probe.resource).toBe(server.mcpResourceUrl); |
| @@ -64,6 +77,7 @@ describe("oauth.registerDynamicClient", () => { · 2 unmodified lines |
| 64 | 77 | const slug = yield* executor.oauth.registerDynamicClient({ |
| 65 | 78 | owner: "org", |
| 66 | 79 | slug: CLIENT, |
| 80 | + issuer: probe.issuer, |
| 67 | 81 | registrationEndpoint: probe.registrationEndpoint!, |
| 68 | 82 | authorizationUrl: probe.authorizationUrl, |
| 69 | 83 | tokenUrl: probe.tokenUrl, |
| @@ -74,12 +88,13 @@ describe("oauth.registerDynamicClient", () => { · 4 unmodified lines |
| 74 | 88 | redirectUri: FLOW_REDIRECT_URI, |
| 75 | 89 | originIntegration: INTEG, |
| 76 | 90 | }); |
| 77 | | − expect(String(slug)).toBe(String(CLIENT)); |
| 91 | + const expectedSlug = dcrSlugFor(server.issuerUrl); |
| 92 | + expect(String(slug)).toBe(String(expectedSlug)); |
| 78 | 93 | |
| 79 | 94 | // The minted client appears in listClients with a server-issued |
| 80 | 95 | // client_id and NO secret ever projected. |
| 81 | 96 | const clients = yield* executor.oauth.listClients(); |
| 82 | | − const minted = clients.find((c) => String(c.slug) === String(CLIENT)); |
| 97 | + const minted = clients.find((c) => String(c.slug) === String(expectedSlug)); |
| 83 | 98 | expect(minted).toBeDefined(); |
| 84 | 99 | expect(minted!.owner).toBe("org"); |
| 85 | 100 | expect(minted!.grant).toBe("authorization_code"); |
| @@ -98,7 +113,7 @@ describe("oauth.registerDynamicClient", () => { · 12 unmodified lines |
| 98 | 113 | // PKCE flow with NO client_secret on the token exchange. |
| 99 | 114 | const started = yield* executor.oauth.start({ |
| 100 | 115 | owner: "org", |
| 101 | | − client: CLIENT, |
| 116 | + client: expectedSlug, |
| 102 | 117 | clientOwner: "org", |
| 103 | 118 | name: ConnectionName.make("main"), |
| 104 | 119 | integration: INTEG, |
| @@ -145,6 +160,213 @@ describe("oauth.registerDynamicClient", () => { · 40 unmodified lines |
| 145 | 160 | ), |
| 146 | 161 | ); |
| 147 | 162 | |
| 163 | + it.effect("reuses an existing DCR client for the same owner and authorization server", () => |
| 164 | + Effect.scoped( |
| 165 | + Effect.gen(function* () { |
| 166 | + const server = yield* serveOAuthTestServer({ scopes: ["read"] }); |
| 167 | + const { executor } = yield* makeTestWorkspaceHarness({ plugins }); |
| 168 | + yield* executor.acme.seed(); |
| 169 | + const probe = yield* executor.oauth.probe({ url: server.mcpResourceUrl }); |
| 170 | + |
| 171 | + const first = yield* executor.oauth.registerDynamicClient({ |
| 172 | + owner: "org", |
| 173 | + slug: OAuthClientSlug.make("first-attempt"), |
| 174 | + issuer: probe.issuer, |
| 175 | + registrationEndpoint: probe.registrationEndpoint!, |
| 176 | + authorizationUrl: probe.authorizationUrl, |
| 177 | + tokenUrl: probe.tokenUrl, |
| 178 | + resource: probe.resource, |
| 179 | + scopes: ["read"], |
| 180 | + tokenEndpointAuthMethodsSupported: probe.tokenEndpointAuthMethodsSupported, |
| 181 | + clientName: "Acme DCR", |
| 182 | + redirectUri: FLOW_REDIRECT_URI, |
| 183 | + originIntegration: INTEG, |
| 184 | + }); |
| 185 | + yield* server.clearRequests; |
| 186 | + |
| 187 | + const second = yield* executor.oauth.registerDynamicClient({ |
| 188 | + owner: "org", |
| 189 | + slug: OAuthClientSlug.make("second-attempt"), |
| 190 | + issuer: probe.issuer, |
| 191 | + registrationEndpoint: probe.registrationEndpoint!, |
| 192 | + authorizationUrl: probe.authorizationUrl, |
| 193 | + tokenUrl: probe.tokenUrl, |
| 194 | + resource: probe.resource, |
| 195 | + scopes: ["read"], |
| 196 | + tokenEndpointAuthMethodsSupported: probe.tokenEndpointAuthMethodsSupported, |
| 197 | + clientName: "Other integration DCR", |
| 198 | + redirectUri: FLOW_REDIRECT_URI, |
| 199 | + originIntegration: IntegrationSlug.make("other"), |
| 200 | + }); |
| 201 | + |
| 202 | + expect(second).toBe(first); |
| 203 | + const requests = yield* server.requests; |
| 204 | + expect(registerRequestCount(requests)).toBe(0); |
| 205 | + }), |
| 206 | + ), |
| 207 | + ); |
| 208 | + |
| 209 | + it.effect("does not reuse a DCR client across owners", () => |
| 210 | + Effect.scoped( |
| 211 | + Effect.gen(function* () { |
| 212 | + const server = yield* serveOAuthTestServer({ scopes: ["read"] }); |
| 213 | + const { executor } = yield* makeTestWorkspaceHarness({ plugins }); |
| 214 | + yield* executor.acme.seed(); |
| 215 | + const probe = yield* executor.oauth.probe({ url: server.mcpResourceUrl }); |
| 216 | + |
| 217 | + const orgSlug = yield* executor.oauth.registerDynamicClient({ |
| 218 | + owner: "org", |
| 219 | + slug: OAuthClientSlug.make("org-attempt"), |
| 220 | + issuer: probe.issuer, |
| 221 | + registrationEndpoint: probe.registrationEndpoint!, |
| 222 | + authorizationUrl: probe.authorizationUrl, |
| 223 | + tokenUrl: probe.tokenUrl, |
| 224 | + resource: probe.resource, |
| 225 | + scopes: ["read"], |
| 226 | + tokenEndpointAuthMethodsSupported: probe.tokenEndpointAuthMethodsSupported, |
| 227 | + clientName: "Org DCR", |
| 228 | + redirectUri: FLOW_REDIRECT_URI, |
| 229 | + originIntegration: INTEG, |
| 230 | + }); |
| 231 | + yield* server.clearRequests; |
| 232 | + |
| 233 | + const userSlug = yield* executor.oauth.registerDynamicClient({ |
| 234 | + owner: "user", |
| 235 | + slug: OAuthClientSlug.make("user-attempt"), |
| 236 | + issuer: probe.issuer, |
| 237 | + registrationEndpoint: probe.registrationEndpoint!, |
| 238 | + authorizationUrl: probe.authorizationUrl, |
| 239 | + tokenUrl: probe.tokenUrl, |
| 240 | + resource: probe.resource, |
| 241 | + scopes: ["read"], |
| 242 | + tokenEndpointAuthMethodsSupported: probe.tokenEndpointAuthMethodsSupported, |
| 243 | + clientName: "User DCR", |
| 244 | + redirectUri: FLOW_REDIRECT_URI, |
| 245 | + originIntegration: INTEG, |
| 246 | + }); |
| 247 | + |
| 248 | + expect(String(userSlug)).toBe(String(orgSlug)); |
| 249 | + const requests = yield* server.requests; |
| 250 | + expect(registerRequestCount(requests)).toBe(1); |
| 251 | + }), |
| 252 | + ), |
| 253 | + ); |
| 254 | + |
| 255 | + it.effect("reuses legacy DCR-looking rows with no stored issuer by token host", () => |
| 256 | + Effect.scoped( |
| 257 | + Effect.gen(function* () { |
| 258 | + const server = yield* serveOAuthTestServer({ scopes: ["read"] }); |
| 259 | + const { config, executor } = yield* makeTestWorkspaceHarness({ plugins }); |
| 260 | + yield* executor.acme.seed(); |
| 261 | + const probe = yield* executor.oauth.probe({ url: server.mcpResourceUrl }); |
| 262 | + const legacySlug = OAuthClientSlug.make("cloudflare-mcp"); |
| 263 | + |
| 264 | + yield* executor.oauth.createClient({ |
| 265 | + owner: "org", |
| 266 | + slug: legacySlug, |
| 267 | + authorizationUrl: probe.authorizationUrl, |
| 268 | + tokenUrl: probe.tokenUrl, |
| 269 | + resource: server.mcpResourceUrl, |
| 270 | + grant: "authorization_code", |
| 271 | + clientId: "legacy-dcr-client", |
| 272 | + clientSecret: "", |
| 273 | + }); |
| 274 | + yield* Effect.promise(() => |
| 275 | + config.db.updateMany("oauth_client", { |
| 276 | + where: (b) => b("slug", "=", String(legacySlug)), |
| 277 | + set: { origin_kind: null, origin_integration: null, origin_issuer: null }, |
| 278 | + }), |
| 279 | + ); |
| 280 | + yield* server.clearRequests; |
| 281 | + |
| 282 | + const reused = yield* executor.oauth.registerDynamicClient({ |
| 283 | + owner: "org", |
| 284 | + slug: OAuthClientSlug.make("new-attempt"), |
| 285 | + issuer: probe.issuer, |
| 286 | + registrationEndpoint: probe.registrationEndpoint!, |
| 287 | + authorizationUrl: probe.authorizationUrl, |
| 288 | + tokenUrl: probe.tokenUrl, |
| 289 | + resource: server.mcpResourceUrl, |
| 290 | + scopes: ["read"], |
| 291 | + tokenEndpointAuthMethodsSupported: probe.tokenEndpointAuthMethodsSupported, |
| 292 | + clientName: "Acme DCR", |
| 293 | + redirectUri: FLOW_REDIRECT_URI, |
| 294 | + originIntegration: INTEG, |
| 295 | + }); |
| 296 | + |
| 297 | + expect(reused).toBe(legacySlug); |
| 298 | + const requests = yield* server.requests; |
| 299 | + expect(registerRequestCount(requests)).toBe(0); |
| 300 | + }), |
| 301 | + ), |
| 302 | + ); |
| 303 | + |
| 304 | + it.effect("uses resource to distinguish DCR clients only after an issuer already differs", () => |
| 305 | + Effect.scoped( |
| 306 | + Effect.gen(function* () { |
| 307 | + const server = yield* serveOAuthTestServer({ scopes: ["read"] }); |
| 308 | + const { executor } = yield* makeTestWorkspaceHarness({ plugins }); |
| 309 | + yield* executor.acme.seed(); |
| 310 | + const probe = yield* executor.oauth.probe({ url: server.mcpResourceUrl }); |
| 311 | + const resourceA = `${server.issuerUrl}/mcp/a`; |
| 312 | + const resourceB = `${server.issuerUrl}/mcp/b`; |
| 313 | + |
| 314 | + const first = yield* executor.oauth.registerDynamicClient({ |
| 315 | + owner: "org", |
| 316 | + slug: OAuthClientSlug.make("first-resource"), |
| 317 | + issuer: probe.issuer, |
| 318 | + registrationEndpoint: probe.registrationEndpoint!, |
| 319 | + authorizationUrl: probe.authorizationUrl, |
| 320 | + tokenUrl: probe.tokenUrl, |
| 321 | + resource: resourceA, |
| 322 | + scopes: ["read"], |
| 323 | + tokenEndpointAuthMethodsSupported: probe.tokenEndpointAuthMethodsSupported, |
| 324 | + clientName: "Resource A", |
| 325 | + redirectUri: FLOW_REDIRECT_URI, |
| 326 | + originIntegration: INTEG, |
| 327 | + }); |
| 328 | + expect(String(first)).toBe(String(dcrSlugFor(server.issuerUrl))); |
| 329 | + |
| 330 | + const second = yield* executor.oauth.registerDynamicClient({ |
| 331 | + owner: "org", |
| 332 | + slug: OAuthClientSlug.make("second-resource"), |
| 333 | + issuer: probe.issuer, |
| 334 | + registrationEndpoint: probe.registrationEndpoint!, |
| 335 | + authorizationUrl: probe.authorizationUrl, |
| 336 | + tokenUrl: probe.tokenUrl, |
| 337 | + resource: resourceB, |
| 338 | + scopes: ["read"], |
| 339 | + tokenEndpointAuthMethodsSupported: probe.tokenEndpointAuthMethodsSupported, |
| 340 | + clientName: "Resource B", |
| 341 | + redirectUri: FLOW_REDIRECT_URI, |
| 342 | + originIntegration: INTEG, |
| 343 | + }); |
| 344 | + expect(String(second)).not.toBe(String(first)); |
| 345 | + expect(String(second)).toContain(String(first)); |
| 346 | + yield* server.clearRequests; |
| 347 | + |
| 348 | + const third = yield* executor.oauth.registerDynamicClient({ |
| 349 | + owner: "org", |
| 350 | + slug: OAuthClientSlug.make("third-resource"), |
| 351 | + issuer: probe.issuer, |
| 352 | + registrationEndpoint: probe.registrationEndpoint!, |
| 353 | + authorizationUrl: probe.authorizationUrl, |
| 354 | + tokenUrl: probe.tokenUrl, |
| 355 | + resource: resourceB, |
| 356 | + scopes: ["read"], |
| 357 | + tokenEndpointAuthMethodsSupported: probe.tokenEndpointAuthMethodsSupported, |
| 358 | + clientName: "Resource B again", |
| 359 | + redirectUri: FLOW_REDIRECT_URI, |
| 360 | + originIntegration: IntegrationSlug.make("other"), |
| 361 | + }); |
| 362 | + |
| 363 | + expect(third).toBe(second); |
| 364 | + const requests = yield* server.requests; |
| 365 | + expect(registerRequestCount(requests)).toBe(0); |
| 366 | + }), |
| 367 | + ), |
| 368 | + ); |
| 369 | + |
| 148 | 370 | // Regression: issue #770. Vercel (and other RFC 8252-strict servers) only |
| 149 | 371 | // approve loopback redirect URIs for anonymous DCR, so a hosted/tailnet/LAN |
| 150 | 372 | // origin trips `invalid_redirect_uri`. The failure must explain the loopback |