# Conflicts: # packages/core/sdk/src/oauth-discovery.ts # packages/plugins/openapi/src/sdk/oauth-refresh.test.ts
Unified · Split
| @@ -35,13 +35,9 @@ · 34 unmodified lines | ||
| 35 | 35 | // every strategy because refresh semantics are strategy-independent. |
| 36 | 36 | // --------------------------------------------------------------------------- |
| 37 | 37 | |
| 38 | −import { Effect, Schema } from "effect"; | |
| 38 | +import { Effect, Option, Predicate, Schema } from "effect"; | |
| 39 | 39 | |
| 40 | −import type { | |
| 41 | − DBAdapter, | |
| 42 | − StorageFailure, | |
| 43 | − TypedAdapter, | |
| 44 | −} from "@executor-js/storage-core"; | |
| 40 | +import type { DBAdapter, StorageFailure, TypedAdapter } from "@executor-js/storage-core"; | |
| 45 | 41 | |
| 46 | 42 | import { |
| 47 | 43 | ConnectionRefreshError, |
| @@ -52,9 +48,7 @@ import { · 4 unmodified lines | ||
| 52 | 48 | type ConnectionRefreshResult, |
| 53 | 49 | type ConnectionRef, |
| 54 | 50 | } from "./connections"; |
| 55 | −import type { | |
| 56 | − ConnectionProviderNotRegisteredError, | |
| 57 | −} from "./errors"; | |
| 51 | +import type { ConnectionProviderNotRegisteredError } from "./errors"; | |
| 58 | 52 | import type { CoreSchema } from "./core-schema"; |
| 59 | 53 | import { ConnectionId, ScopeId, SecretId } from "./ids"; |
| 60 | 54 | import { SetSecretInput, type SecretRef } from "./secrets"; |
| @@ -100,6 +94,8 @@ import { · 39 unmodified lines | ||
| 100 | 94 | |
| 101 | 95 | const OAuthAuthorizationServerMetadataJson = Schema.Record(Schema.String, Schema.Unknown); |
| 102 | 96 | const OAuthClientInformationJson = Schema.Record(Schema.String, Schema.Unknown); |
| 97 | +const UnknownRecord = Schema.Record(Schema.String, Schema.Unknown); | |
| 98 | +const JsonValueSchema = Schema.fromJsonString(Schema.Unknown); | |
| 103 | 99 | |
| 104 | 100 | const DynamicDcrSessionPayload = Schema.Struct({ |
| 105 | 101 | kind: Schema.Literal("dynamic-dcr"), |
| @@ -110,9 +106,7 @@ const DynamicDcrSessionPayload = Schema.Struct({ · 4 unmodified lines | ||
| 110 | 106 | authorizationServerMetadata: OAuthAuthorizationServerMetadataJson, |
| 111 | 107 | clientInformation: OAuthClientInformationJson, |
| 112 | 108 | resourceMetadataUrl: Schema.NullOr(Schema.String), |
| 113 | − resourceMetadata: Schema.NullOr( | |
| 114 | − Schema.Record(Schema.String, Schema.Unknown), | |
| 115 | − ), | |
| 109 | + resourceMetadata: Schema.NullOr(Schema.Record(Schema.String, Schema.Unknown)), | |
| 116 | 110 | scopes: Schema.Array(Schema.String), |
| 117 | 111 | }); |
| 118 | 112 | |
| @@ -122,7 +116,9 @@ const AuthorizationCodeSessionPayload = Schema.Struct({ · 3 unmodified lines | ||
| 122 | 116 | codeVerifier: Schema.String, |
| 123 | 117 | authorizationEndpoint: Schema.String, |
| 124 | 118 | tokenEndpoint: Schema.String, |
| 125 | − issuerUrl: Schema.NullOr(Schema.String).pipe(Schema.withDecodingDefaultType(Effect.succeed(null))), | |
| 119 | + issuerUrl: Schema.NullOr(Schema.String).pipe( | |
| 120 | + Schema.withDecodingDefaultType(Effect.succeed(null)), | |
| 121 | + ), | |
| 126 | 122 | clientIdSecretId: Schema.String, |
| 127 | 123 | clientSecretSecretId: Schema.NullOr(Schema.String), |
| 128 | 124 | scopes: Schema.Array(Schema.String), |
| @@ -144,22 +140,22 @@ const encodeSessionPayload = Schema.encodeSync(OAuthSessionPayload); · 15 unmodified lines | ||
| 144 | 140 | |
| 145 | 141 | const coerceJson = (value: unknown): unknown => { |
| 146 | 142 | if (typeof value !== "string") return value; |
| 147 | − try { | |
| 148 | − return JSON.parse(value); | |
| 149 | − } catch { | |
| 150 | − return value; | |
| 151 | − } | |
| 143 | + const parsed = Schema.decodeUnknownOption(JsonValueSchema)(value); | |
| 144 | + return Option.isSome(parsed) ? parsed.value : value; | |
| 152 | 145 | }; |
| 153 | 146 | |
| 154 | 147 | const stringArray = (value: unknown): readonly string[] => |
| 155 | − Array.isArray(value) | |
| 156 | − ? value.filter((scope): scope is string => typeof scope === "string") | |
| 157 | − : []; | |
| 148 | + Array.isArray(value) ? value.filter((scope): scope is string => typeof scope === "string") : []; | |
| 158 | 149 | |
| 159 | 150 | const originOrNull = (value: unknown): string | null => { |
| 160 | 151 | if (typeof value !== "string") return null; |
| 152 | + return parseUrlOption(value)?.origin ?? null; | |
| 153 | +}; | |
| 154 | + | |
| 155 | +const parseUrlOption = (value: string): URL | null => { | |
| 156 | + // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: URL constructor is the platform URL parser | |
| 161 | 157 | try { |
| 162 | − return new URL(value).origin; | |
| 158 | + return new URL(value); | |
| 163 | 159 | } catch { |
| 164 | 160 | return null; |
| 165 | 161 | } |
| @@ -167,8 +163,8 @@ const originOrNull = (value: unknown): string | null => { · 1 unmodified line | ||
| 167 | 163 | |
| 168 | 164 | const decodeProviderState = (value: unknown): OAuthProviderState => { |
| 169 | 165 | const raw = coerceJson(value); |
| 170 | − const record = | |
| 171 | − raw && typeof raw === "object" ? (raw as Record<string, unknown>) : null; | |
| 166 | + const decodedRecord = Schema.decodeUnknownOption(UnknownRecord)(raw); | |
| 167 | + const record = Option.isSome(decodedRecord) ? decodedRecord.value : null; | |
| 172 | 168 | |
| 173 | 169 | if (record && !("kind" in record) && "flow" in record && "tokenUrl" in record) { |
| 174 | 170 | const flow = record.flow; |
| @@ -196,12 +192,7 @@ const decodeProviderState = (value: unknown): OAuthProviderState => { · 21 unmodified lines | ||
| 196 | 192 | } |
| 197 | 193 | } |
| 198 | 194 | |
| 199 | − if ( | |
| 200 | − record && | |
| 201 | − !("kind" in record) && | |
| 202 | − "clientIdSecretId" in record && | |
| 203 | − "scopes" in record | |
| 204 | − ) { | |
| 195 | + if (record && !("kind" in record) && "clientIdSecretId" in record && "scopes" in record) { | |
| 205 | 196 | const scopes = stringArray(record.scopes); |
| 206 | 197 | return Schema.decodeUnknownSync(OAuthProviderStateSchema)({ |
| 207 | 198 | kind: "authorization-code", |
| @@ -214,48 +205,38 @@ const decodeProviderState = (value: unknown): OAuthProviderState => { · 6 unmodified lines | ||
| 214 | 205 | }); |
| 215 | 206 | } |
| 216 | 207 | |
| 217 | − if ( | |
| 218 | − record && | |
| 219 | − !("kind" in record) && | |
| 220 | − "clientInformation" in record && | |
| 221 | − "endpoint" in record | |
| 222 | − ) { | |
| 223 | − const clientInformation = | |
| 224 | − record.clientInformation && typeof record.clientInformation === "object" | |
| 225 | − ? (record.clientInformation as Record<string, unknown>) | |
| 226 | − : null; | |
| 208 | + if (record && !("kind" in record) && "clientInformation" in record && "endpoint" in record) { | |
| 209 | + const decodedClientInformation = Schema.decodeUnknownOption(UnknownRecord)( | |
| 210 | + record.clientInformation, | |
| 211 | + ); | |
| 212 | + const clientInformation = Option.isSome(decodedClientInformation) | |
| 213 | + ? decodedClientInformation.value | |
| 214 | + : null; | |
| 215 | + const decodedAuthorizationServerMetadata = Schema.decodeUnknownOption(UnknownRecord)( | |
| 216 | + record.authorizationServerMetadata, | |
| 217 | + ); | |
| 218 | + const authorizationServerMetadata = Option.isSome(decodedAuthorizationServerMetadata) | |
| 219 | + ? decodedAuthorizationServerMetadata.value | |
| 220 | + : null; | |
| 227 | 221 | return Schema.decodeUnknownSync(OAuthProviderStateSchema)({ |
| 228 | 222 | kind: "dynamic-dcr", |
| 229 | 223 | tokenEndpoint: |
| 230 | 224 | typeof record.tokenEndpoint === "string" |
| 231 | 225 | ? record.tokenEndpoint |
| 232 | − : record.authorizationServerMetadata && | |
| 233 | − typeof record.authorizationServerMetadata === "object" && | |
| 234 | − typeof (record.authorizationServerMetadata as Record<string, unknown>) | |
| 235 | − .token_endpoint === "string" | |
| 236 | − ? ((record.authorizationServerMetadata as Record<string, unknown>) | |
| 237 | − .token_endpoint as string) | |
| 226 | + : typeof authorizationServerMetadata?.token_endpoint === "string" | |
| 227 | + ? authorizationServerMetadata.token_endpoint | |
| 238 | 228 | : "", |
| 239 | 229 | issuerUrl: |
| 240 | − record.authorizationServerMetadata && | |
| 241 | − typeof record.authorizationServerMetadata === "object" && | |
| 242 | − typeof (record.authorizationServerMetadata as Record<string, unknown>).issuer === | |
| 243 | − "string" | |
| 244 | − ? ((record.authorizationServerMetadata as Record<string, unknown>) | |
| 245 | − .issuer as string) | |
| 230 | + typeof authorizationServerMetadata?.issuer === "string" | |
| 231 | + ? authorizationServerMetadata.issuer | |
| 246 | 232 | : null, |
| 247 | 233 | authorizationServerUrl: |
| 248 | − typeof record.authorizationServerUrl === "string" | |
| 249 | − ? record.authorizationServerUrl | |
| 250 | − : null, | |
| 234 | + typeof record.authorizationServerUrl === "string" ? record.authorizationServerUrl : null, | |
| 251 | 235 | authorizationServerMetadataUrl: |
| 252 | 236 | typeof record.authorizationServerMetadataUrl === "string" |
| 253 | 237 | ? record.authorizationServerMetadataUrl |
| 254 | 238 | : null, |
| 255 | − clientId: | |
| 256 | − typeof clientInformation?.client_id === "string" | |
| 257 | − ? clientInformation.client_id | |
| 258 | − : "", | |
| 239 | + clientId: typeof clientInformation?.client_id === "string" ? clientInformation.client_id : "", | |
| 259 | 240 | clientSecretSecretId: null, |
| 260 | 241 | clientAuth: "body", |
| 261 | 242 | scope: null, |
| @@ -288,10 +269,7 @@ export interface OAuthServiceDeps { · 26 unmodified lines | ||
| 288 | 269 | * `complete` (and from `start` for `client-credentials`). */ |
| 289 | 270 | readonly connectionsCreate: ( |
| 290 | 271 | input: CreateConnectionInput, |
| 291 | − ) => Effect.Effect< | |
| 292 | − ConnectionRef, | |
| 293 | − ConnectionProviderNotRegisteredError | StorageFailure | |
| 294 | − >; | |
| 272 | + ) => Effect.Effect<ConnectionRef, ConnectionProviderNotRegisteredError | StorageFailure>; | |
| 295 | 273 | /** Random session id generator. Tests override to make outputs |
| 296 | 274 | * deterministic. */ |
| 297 | 275 | readonly newSessionId?: () => string; |
| @@ -304,9 +282,9 @@ const defaultSessionId = (): string => { · 6 unmodified lines | ||
| 304 | 282 | if (crypto?.randomUUID) return `oauth2_session_${crypto.randomUUID()}`; |
| 305 | 283 | const bytes = new Uint8Array(16); |
| 306 | 284 | crypto.getRandomValues(bytes); |
| 307 | − return `oauth2_session_${Array.from(bytes, (byte) => | |
| 308 | − byte.toString(16).padStart(2, "0"), | |
| 309 | − ).join("")}`; | |
| 285 | + return `oauth2_session_${Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join( | |
| 286 | + "", | |
| 287 | + )}`; | |
| 310 | 288 | }; |
| 311 | 289 | |
| 312 | 290 | const secretIdPart = (value: string): string => |
| @@ -328,11 +306,7 @@ const oauthSecretId = ( · 15 unmodified lines | ||
| 328 | 306 | const scopedSessionId = (scopeId: string, sessionId: string): string => |
| 329 | 307 | `${sessionId}_${secretIdPart(scopeId).slice(0, 24)}`; |
| 330 | 308 | |
| 331 | −const terminalRefreshErrors = new Set([ | |
| 332 | − "invalid_grant", | |
| 333 | − "invalid_client", | |
| 334 | − "unauthorized_client", | |
| 335 | −]); | |
| 309 | +const terminalRefreshErrors = new Set(["invalid_grant", "invalid_client", "unauthorized_client"]); | |
| 336 | 310 | |
| 337 | 311 | // --------------------------------------------------------------------------- |
| 338 | 312 | // Service factory |
| @@ -347,19 +321,16 @@ export const makeOAuth2Service = ( · 8 unmodified lines | ||
| 347 | 321 | // ------------------------------------------------------------------- |
| 348 | 322 | // probe |
| 349 | 323 | // ------------------------------------------------------------------- |
| 350 | − const probe = ( | |
| 351 | − input: OAuthProbeInput, | |
| 352 | − ): Effect.Effect<OAuthProbeResult, OAuthProbeError> => | |
| 324 | + const probe = (input: OAuthProbeInput): Effect.Effect<OAuthProbeResult, OAuthProbeError> => | |
| 353 | 325 | Effect.gen(function* () { |
| 354 | − const resource = yield* discoverProtectedResourceMetadata( | |
| 355 | − input.endpoint, | |
| 356 | − { resourceHeaders: input.headers, resourceQueryParams: input.queryParams }, | |
| 357 | − ).pipe( | |
| 358 | − Effect.catchTag("OAuthDiscoveryError", (err) => | |
| 326 | + const resource = yield* discoverProtectedResourceMetadata(input.endpoint, { | |
| 327 | + resourceHeaders: input.headers, | |
| 328 | + resourceQueryParams: input.queryParams, | |
| 329 | + }).pipe( | |
| 330 | + Effect.catchTag("OAuthDiscoveryError", () => | |
| 359 | 331 | Effect.fail( |
| 360 | 332 | new OAuthProbeError({ |
| 361 | − message: `Protected resource metadata probe failed: ${err.message}`, | |
| 362 | − | |
| 333 | + message: "Protected resource metadata probe failed", | |
| 363 | 334 | }), |
| 364 | 335 | ), |
| 365 | 336 | ), |
| @@ -368,29 +339,19 @@ export const makeOAuth2Service = ( · 2 unmodified lines | ||
| 368 | 339 | const authorizationServerUrl = (() => { |
| 369 | 340 | const fromResource = resource?.metadata.authorization_servers?.[0]; |
| 370 | 341 | if (fromResource) return fromResource; |
| 371 | − try { | |
| 372 | − const u = new URL(input.endpoint); | |
| 373 | − return `${u.protocol}//${u.host}`; | |
| 374 | − } catch { | |
| 375 | − return null; | |
| 376 | − } | |
| 342 | + const u = parseUrlOption(input.endpoint); | |
| 343 | + return u ? `${u.protocol}//${u.host}` : null; | |
| 377 | 344 | })(); |
| 378 | 345 | |
| 379 | 346 | const authServer = authorizationServerUrl |
| 380 | − ? yield* discoverAuthorizationServerMetadata( | |
| 381 | − authorizationServerUrl, | |
| 382 | − ).pipe( | |
| 383 | − Effect.catchTag("OAuthDiscoveryError", () => | |
| 384 | − Effect.succeed(null), | |
| 385 | − ), | |
| 347 | + ? yield* discoverAuthorizationServerMetadata(authorizationServerUrl).pipe( | |
| 348 | + Effect.catchTag("OAuthDiscoveryError", () => Effect.succeed(null)), | |
| 386 | 349 | ) |
| 387 | 350 | : null; |
| 388 | 351 | |
| 389 | 352 | const supportsDynamicRegistration = !!( |
| 390 | 353 | authServer?.metadata.registration_endpoint && |
| 391 | − (authServer.metadata.token_endpoint_auth_methods_supported ?? []).includes( | |
| 392 | − "none", | |
| 393 | − ) | |
| 354 | + (authServer.metadata.token_endpoint_auth_methods_supported ?? []).includes("none") | |
| 394 | 355 | ); |
| 395 | 356 | |
| 396 | 357 | // Bearer challenge probe — POST the endpoint unauth, look for |
| @@ -400,47 +361,40 @@ export const makeOAuth2Service = ( · 3 unmodified lines | ||
| 400 | 361 | // challenge"). |
| 401 | 362 | const isBearerChallengeEndpoint = yield* Effect.tryPromise({ |
| 402 | 363 | try: async (): Promise<boolean> => { |
| 403 | − const controller = new AbortController(); | |
| 404 | − const timer = setTimeout(() => controller.abort(), 6_000); | |
| 405 | − try { | |
| 406 | − const probeUrl = new URL(input.endpoint); | |
| 407 | − for (const [key, value] of Object.entries(input.queryParams ?? {})) { | |
| 408 | − probeUrl.searchParams.set(key, value); | |
| 409 | − } | |
| 410 | − const response = await fetch(probeUrl.toString(), { | |
| 411 | − method: "POST", | |
| 412 | − headers: { | |
| 413 | − ...(input.headers ?? {}), | |
| 414 | − "content-type": "application/json", | |
| 415 | − accept: "application/json, text/event-stream", | |
| 416 | − }, | |
| 417 | − body: JSON.stringify({ | |
| 418 | − jsonrpc: "2.0", | |
| 419 | − id: 1, | |
| 420 | − method: "initialize", | |
| 421 | − params: { | |
| 422 | − protocolVersion: "2025-06-18", | |
| 423 | − capabilities: {}, | |
| 424 | − clientInfo: { name: "executor-probe", version: "0" }, | |
| 425 | − }, | |
| 426 | − }), | |
| 427 | − signal: controller.signal, | |
| 428 | − }); | |
| 429 | − if (response.status !== 401) return false; | |
| 430 | − const wwwAuth = | |
| 431 | − response.headers.get("www-authenticate") ?? | |
| 432 | − response.headers.get("WWW-Authenticate"); | |
| 433 | − return !!wwwAuth && /^\s*bearer\b/i.test(wwwAuth); | |
| 434 | − } finally { | |
| 435 | − clearTimeout(timer); | |
| 364 | + const probeUrl = parseUrlOption(input.endpoint); | |
| 365 | + if (!probeUrl) return false; | |
| 366 | + for (const [key, value] of Object.entries(input.queryParams ?? {})) { | |
| 367 | + probeUrl.searchParams.set(key, value); | |
| 436 | 368 | } |
| 369 | + const response = await fetch(probeUrl.toString(), { | |
| 370 | + method: "POST", | |
| 371 | + headers: { | |
| 372 | + ...(input.headers ?? {}), | |
| 373 | + "content-type": "application/json", | |
| 374 | + accept: "application/json, text/event-stream", | |
| 375 | + }, | |
| 376 | + body: JSON.stringify({ | |
| 377 | + jsonrpc: "2.0", | |
| 378 | + id: 1, | |
| 379 | + method: "initialize", | |
| 380 | + params: { | |
| 381 | + protocolVersion: "2025-06-18", | |
| 382 | + capabilities: {}, | |
| 383 | + clientInfo: { name: "executor-probe", version: "0" }, | |
| 384 | + }, | |
| 385 | + }), | |
| 386 | + signal: AbortSignal.timeout(6_000), | |
| 387 | + }); | |
| 388 | + if (response.status !== 401) return false; | |
| 389 | + const wwwAuth = | |
| 390 | + response.headers.get("www-authenticate") ?? response.headers.get("WWW-Authenticate"); | |
| 391 | + return !!wwwAuth && /^\s*bearer\b/i.test(wwwAuth); | |
| 437 | 392 | }, |
| 438 | 393 | catch: () => null, |
| 439 | 394 | }).pipe(Effect.catch(() => Effect.succeed(false))); |
| 440 | 395 | |
| 441 | 396 | return { |
| 442 | − resourceMetadata: | |
| 443 | − (resource?.metadata as Record<string, unknown> | undefined) ?? null, | |
| 397 | + resourceMetadata: (resource?.metadata as Record<string, unknown> | undefined) ?? null, | |
| 444 | 398 | resourceMetadataUrl: resource?.metadataUrl ?? null, |
| 445 | 399 | authorizationServerMetadata: |
| 446 | 400 | (authServer?.metadata as Record<string, unknown> | undefined) ?? null, |
| @@ -459,20 +413,22 @@ export const makeOAuth2Service = ( · 12 unmodified lines | ||
| 459 | 413 | strategy: OAuthDynamicDcrStrategy, |
| 460 | 414 | ): Effect.Effect<OAuthStartResult, OAuthStartError | StorageFailure> => |
| 461 | 415 | Effect.gen(function* () { |
| 462 | − const started = yield* beginDynamicAuthorization({ | |
| 463 | − endpoint: input.endpoint, | |
| 464 | − redirectUrl: input.redirectUrl, | |
| 465 | − state: "", | |
| 466 | − scopes: strategy.scopes, | |
| 467 | − }, { | |
| 468 | − resourceHeaders: input.headers, | |
| 469 | − resourceQueryParams: input.queryParams, | |
| 470 | − }).pipe( | |
| 471 | − Effect.catchTag("OAuthDiscoveryError", (err) => | |
| 416 | + const started = yield* beginDynamicAuthorization( | |
| 417 | + { | |
| 418 | + endpoint: input.endpoint, | |
| 419 | + redirectUrl: input.redirectUrl, | |
| 420 | + state: "", | |
| 421 | + scopes: strategy.scopes, | |
| 422 | + }, | |
| 423 | + { | |
| 424 | + resourceHeaders: input.headers, | |
| 425 | + resourceQueryParams: input.queryParams, | |
| 426 | + }, | |
| 427 | + ).pipe( | |
| 428 | + Effect.catchTag("OAuthDiscoveryError", () => | |
| 472 | 429 | Effect.fail( |
| 473 | 430 | new OAuthStartError({ |
| 474 | − message: `Dynamic authorization setup failed: ${err.message}`, | |
| 475 | − | |
| 431 | + message: "Dynamic authorization setup failed", | |
| 476 | 432 | }), |
| 477 | 433 | ), |
| 478 | 434 | ), |
| @@ -492,10 +448,7 @@ export const makeOAuth2Service = ( · 13 unmodified lines | ||
| 492 | 448 | authorizationUrl: started.state.authorizationServerMetadata.authorization_endpoint, |
| 493 | 449 | clientId: started.state.clientInformation.client_id, |
| 494 | 450 | redirectUrl: input.redirectUrl, |
| 495 | − scopes: | |
| 496 | − strategy.scopes ?? | |
| 497 | − started.state.authorizationServerMetadata.scopes_supported ?? | |
| 498 | − [], | |
| 451 | + scopes: strategy.scopes ?? started.state.authorizationServerMetadata.scopes_supported ?? [], | |
| 499 | 452 | state: sessionId, |
| 500 | 453 | codeChallenge, |
| 501 | 454 | }); |
| @@ -505,22 +458,20 @@ export const makeOAuth2Service = ( · 3 unmodified lines | ||
| 505 | 458 | identityLabel: input.identityLabel ?? null, |
| 506 | 459 | codeVerifier: started.codeVerifier, |
| 507 | 460 | authorizationServerUrl: started.state.authorizationServerUrl, |
| 508 | − authorizationServerMetadataUrl: | |
| 509 | − started.state.authorizationServerMetadataUrl, | |
| 510 | − authorizationServerMetadata: | |
| 511 | − started.state.authorizationServerMetadata as Record<string, unknown>, | |
| 461 | + authorizationServerMetadataUrl: started.state.authorizationServerMetadataUrl, | |
| 462 | + authorizationServerMetadata: started.state.authorizationServerMetadata as Record< | |
| 463 | + string, | |
| 464 | + unknown | |
| 465 | + >, | |
| 512 | 466 | clientInformation: (() => { |
| 513 | 467 | const value: unknown = started.state.clientInformation; |
| 514 | 468 | return value as Record<string, unknown>; |
| 515 | 469 | })(), |
| 516 | 470 | resourceMetadataUrl: started.state.resourceMetadataUrl, |
| 517 | 471 | resourceMetadata: |
| 518 | − (started.state.resourceMetadata as Record<string, unknown> | null) ?? | |
| 519 | − null, | |
| 472 | + (started.state.resourceMetadata as Record<string, unknown> | null) ?? null, | |
| 520 | 473 | scopes: [ |
| 521 | − ...(strategy.scopes ?? | |
| 522 | − started.state.authorizationServerMetadata.scopes_supported ?? | |
| 523 | − []), | |
| 474 | + ...(strategy.scopes ?? started.state.authorizationServerMetadata.scopes_supported ?? []), | |
| 524 | 475 | ], |
| 525 | 476 | }; |
| 526 | 477 | |
| @@ -544,25 +495,22 @@ export const makeOAuth2Service = ( · 17 unmodified lines | ||
| 544 | 495 | ): Effect.Effect<OAuthStartResult, OAuthStartError | StorageFailure> => |
| 545 | 496 | Effect.gen(function* () { |
| 546 | 497 | const clientId = yield* deps.secretsGet(strategy.clientIdSecretId).pipe( |
| 547 | − Effect.mapError((err) => | |
| 548 | − // Storage failure propagates; null returns aren't errors — the | |
| 549 | − // branch below handles them. | |
| 550 | − err, | |
| 498 | + Effect.mapError( | |
| 499 | + (err) => | |
| 500 | + // Storage failure propagates; null returns aren't errors — the | |
| 501 | + // branch below handles them. | |
| 502 | + err, | |
| 551 | 503 | ), |
| 552 | 504 | ); |
| 553 | 505 | if (clientId === null) { |
| 554 | − return yield* Effect.fail( | |
| 555 | − new OAuthStartError({ | |
| 556 | − message: `client_id secret "${strategy.clientIdSecretId}" not found`, | |
| 557 | − }), | |
| 558 | − ); | |
| 506 | + return yield* new OAuthStartError({ | |
| 507 | + message: `client_id secret "${strategy.clientIdSecretId}" not found`, | |
| 508 | + }); | |
| 559 | 509 | } |
| 560 | 510 | |
| 561 | 511 | const sessionId = scopedSessionId(input.tokenScope, newSessionId()); |
| 562 | 512 | const codeVerifier = createPkceCodeVerifier(); |
| 563 | − const codeChallenge = yield* Effect.promise(() => | |
| 564 | − createPkceCodeChallenge(codeVerifier), | |
| 565 | − ); | |
| 513 | + const codeChallenge = yield* Effect.promise(() => createPkceCodeChallenge(codeVerifier)); | |
| 566 | 514 | |
| 567 | 515 | const authorizationUrl = buildAuthorizationUrl({ |
| 568 | 516 | authorizationUrl: strategy.authorizationEndpoint, |
| @@ -611,11 +559,9 @@ export const makeOAuth2Service = ( · 42 unmodified lines | ||
| 611 | 559 | const clientId = yield* deps.secretsGet(strategy.clientIdSecretId); |
| 612 | 560 | const clientSecret = yield* deps.secretsGet(strategy.clientSecretSecretId); |
| 613 | 561 | if (clientId === null || clientSecret === null) { |
| 614 | − return yield* Effect.fail( | |
| 615 | − new OAuthStartError({ | |
| 616 | − message: "client_id / client_secret secret not found", | |
| 617 | − }), | |
| 618 | − ); | |
| 562 | + return yield* new OAuthStartError({ | |
| 563 | + message: "client_id / client_secret secret not found", | |
| 564 | + }); | |
| 619 | 565 | } |
| 620 | 566 | |
| 621 | 567 | const tokens = yield* exchangeClientCredentials({ |
| @@ -627,18 +573,15 @@ export const makeOAuth2Service = ( · 5 unmodified lines | ||
| 627 | 573 | clientAuth: strategy.clientAuth ?? "body", |
| 628 | 574 | }).pipe( |
| 629 | 575 | Effect.mapError( |
| 630 | − (err) => | |
| 576 | + () => | |
| 631 | 577 | new OAuthStartError({ |
| 632 | − message: `Client credentials exchange failed: ${err.message}`, | |
| 633 | − | |
| 578 | + message: "Client credentials exchange failed", | |
| 634 | 579 | }), |
| 635 | 580 | ), |
| 636 | 581 | ); |
| 637 | 582 | |
| 638 | 583 | const expiresAt = |
| 639 | − typeof tokens.expires_in === "number" | |
| 640 | − ? now() + tokens.expires_in * 1000 | |
| 641 | − : null; | |
| 584 | + typeof tokens.expires_in === "number" ? now() + tokens.expires_in * 1000 : null; | |
| 642 | 585 | |
| 643 | 586 | const providerState: OAuthProviderState = { |
| 644 | 587 | kind: "client-credentials", |
| @@ -666,19 +609,17 @@ export const makeOAuth2Service = ( · 21 unmodified lines | ||
| 666 | 609 | refreshToken: null, |
| 667 | 610 | expiresAt, |
| 668 | 611 | oauthScope: tokens.scope ?? null, |
| 669 | − providerState: Schema.encodeSync(OAuthProviderStateSchema)( | |
| 670 | − providerState, | |
| 671 | − ) as Record<string, unknown>, | |
| 612 | + providerState: Schema.encodeSync(OAuthProviderStateSchema)(providerState) as Record< | |
| 613 | + string, | |
| 614 | + unknown | |
| 615 | + >, | |
| 672 | 616 | }), |
| 673 | 617 | ) |
| 674 | 618 | .pipe( |
| 675 | 619 | Effect.mapError( |
| 676 | − (err) => | |
| 620 | + () => | |
| 677 | 621 | new OAuthStartError({ |
| 678 | − message: `Failed to mint connection: ${ | |
| 679 | − err instanceof Error ? err.message : String(err) | |
| 680 | − }`, | |
| 681 | − | |
| 622 | + message: "Failed to mint connection", | |
| 682 | 623 | }), |
| 683 | 624 | ), |
| 684 | 625 | ); |
| @@ -709,22 +650,24 @@ export const makeOAuth2Service = ( · 24 unmodified lines | ||
| 709 | 650 | payload: OAuthSessionPayload; |
| 710 | 651 | strategyKind: string; |
| 711 | 652 | }): Effect.Effect<void, StorageFailure> => |
| 712 | − deps.adapter.create({ | |
| 713 | − model: "oauth2_session", | |
| 714 | − data: { | |
| 715 | − id: args.sessionId, | |
| 716 | − scope_id: args.input.tokenScope, | |
| 717 | − plugin_id: args.input.pluginId, | |
| 718 | − strategy: args.strategyKind, | |
| 719 | − connection_id: args.input.connectionId, | |
| 720 | − token_scope: args.input.tokenScope, | |
| 721 | − redirect_url: args.input.redirectUrl, | |
| 722 | − payload: encodeSessionPayload(args.payload) as Record<string, unknown>, | |
| 723 | − expires_at: now() + OAUTH2_SESSION_TTL_MS, | |
| 724 | − created_at: new Date(), | |
| 725 | − }, | |
| 726 | − forceAllowId: true, | |
| 727 | − }).pipe(Effect.asVoid); | |
| 653 | + deps.adapter | |
| 654 | + .create({ | |
| 655 | + model: "oauth2_session", | |
| 656 | + data: { | |
| 657 | + id: args.sessionId, | |
| 658 | + scope_id: args.input.tokenScope, | |
| 659 | + plugin_id: args.input.pluginId, | |
| 660 | + strategy: args.strategyKind, | |
| 661 | + connection_id: args.input.connectionId, | |
| 662 | + token_scope: args.input.tokenScope, | |
| 663 | + redirect_url: args.input.redirectUrl, | |
| 664 | + payload: encodeSessionPayload(args.payload) as Record<string, unknown>, | |
| 665 | + expires_at: now() + OAUTH2_SESSION_TTL_MS, | |
| 666 | + created_at: new Date(), | |
| 667 | + }, | |
| 668 | + forceAllowId: true, | |
| 669 | + }) | |
| 670 | + .pipe(Effect.asVoid); | |
| 728 | 671 | |
| 729 | 672 | // ------------------------------------------------------------------- |
| 730 | 673 | // complete — exchange the code, mint the Connection, delete the session |
| @@ -741,53 +684,45 @@ export const makeOAuth2Service = ( · 10 unmodified lines | ||
| 741 | 684 | where: [{ field: "id", value: input.state }], |
| 742 | 685 | }); |
| 743 | 686 | if (!row) { |
| 744 | − return yield* Effect.fail( | |
| 745 | − new OAuthSessionNotFoundError({ sessionId: input.state }), | |
| 746 | − ); | |
| 687 | + return yield* new OAuthSessionNotFoundError({ sessionId: input.state }); | |
| 747 | 688 | } |
| 748 | 689 | |
| 749 | 690 | const deleteSession = deps.adapter.delete({ |
| 750 | 691 | model: "oauth2_session", |
| 751 | 692 | where: [ |
| 752 | 693 | { field: "id", value: input.state }, |
| 753 | − { field: "scope_id", value: row.scope_id as string }, | |
| 694 | + { field: "scope_id", value: row.scope_id }, | |
| 754 | 695 | ], |
| 755 | 696 | }); |
| 756 | 697 | |
| 757 | 698 | if (input.error) { |
| 758 | 699 | yield* deleteSession; |
| 759 | − return yield* Effect.fail( | |
| 760 | − new OAuthCompleteError({ | |
| 761 | − message: `Authorization server returned error: ${input.error}`, | |
| 762 | − code: input.error, | |
| 763 | − }), | |
| 764 | − ); | |
| 700 | + return yield* new OAuthCompleteError({ | |
| 701 | + message: `Authorization server returned error: ${input.error}`, | |
| 702 | + code: input.error, | |
| 703 | + }); | |
| 765 | 704 | } |
| 766 | 705 | if (!input.code) { |
| 767 | 706 | yield* deleteSession; |
| 768 | − return yield* Effect.fail( | |
| 769 | − new OAuthCompleteError({ | |
| 770 | − message: "Missing authorization code", | |
| 771 | − }), | |
| 772 | − ); | |
| 707 | + return yield* new OAuthCompleteError({ | |
| 708 | + message: "Missing authorization code", | |
| 709 | + }); | |
| 773 | 710 | } |
| 774 | 711 | const expiresAt = Number(row.expires_at as number | bigint); |
| 775 | 712 | if (expiresAt <= now()) { |
| 776 | 713 | yield* deleteSession; |
| 777 | − return yield* Effect.fail( | |
| 778 | − new OAuthCompleteError({ | |
| 779 | − message: "OAuth session expired", | |
| 780 | − }), | |
| 781 | − ); | |
| 714 | + return yield* new OAuthCompleteError({ | |
| 715 | + message: "OAuth session expired", | |
| 716 | + }); | |
| 782 | 717 | } |
| 783 | 718 | |
| 784 | 719 | const payload = decodeSessionPayload(coerceJson(row.payload)); |
| 785 | 720 | const endpoint = ""; // not stored on the row — the payload's own |
| 786 | − // endpoint fields drive exchange; we just need | |
| 787 | − // a display string for the identity label. | |
| 788 | − const connectionId = row.connection_id as string; | |
| 789 | − const tokenScope = row.token_scope as string; | |
| 790 | − const redirectUrl = row.redirect_url as string; | |
| 721 | + // endpoint fields drive exchange; we just need | |
| 722 | + // a display string for the identity label. | |
| 723 | + const connectionId = row.connection_id; | |
| 724 | + const tokenScope = row.token_scope; | |
| 725 | + const redirectUrl = row.redirect_url; | |
| 791 | 726 | |
| 792 | 727 | // Dispatch to the strategy-specific exchange. |
| 793 | 728 | const exchangeResult = yield* (() => { |
| @@ -795,11 +730,7 @@ export const makeOAuth2Service = ( · 1 unmodified line | ||
| 795 | 730 | case "dynamic-dcr": |
| 796 | 731 | return exchangeDynamicDcr(payload, input.code, redirectUrl); |
| 797 | 732 | case "authorization-code": |
| 798 | − return exchangeAuthorizationCodeStrategy( | |
| 799 | − payload, | |
| 800 | − input.code, | |
| 801 | − redirectUrl, | |
| 802 | − ); | |
| 733 | + return exchangeAuthorizationCodeStrategy(payload, input.code, redirectUrl); | |
| 803 | 734 | } |
| 804 | 735 | })().pipe(Effect.tapError(() => deleteSession)); |
| 805 | 736 | |
| @@ -828,11 +759,9 @@ export const makeOAuth2Service = ( · 22 unmodified lines | ||
| 828 | 759 | .pipe( |
| 829 | 760 | Effect.as(secretId), |
| 830 | 761 | Effect.mapError( |
| 831 | − (err) => | |
| 762 | + () => | |
| 832 | 763 | new OAuthCompleteError({ |
| 833 | − message: `Failed to persist DCR client_secret: ${ | |
| 834 | − err instanceof Error ? err.message : String(err) | |
| 835 | − }`, | |
| 764 | + message: "Failed to persist DCR client_secret", | |
| 836 | 765 | }), |
| 837 | 766 | ), |
| 838 | 767 | ); |
| @@ -842,21 +771,21 @@ export const makeOAuth2Service = ( · 3 unmodified lines | ||
| 842 | 771 | payload.kind === "dynamic-dcr" |
| 843 | 772 | ? { |
| 844 | 773 | kind: "dynamic-dcr", |
| 845 | − tokenEndpoint: (payload.authorizationServerMetadata as { | |
| 846 | − token_endpoint: string; | |
| 847 | − }).token_endpoint, | |
| 774 | + tokenEndpoint: ( | |
| 775 | + payload.authorizationServerMetadata as { | |
| 776 | + token_endpoint: string; | |
| 777 | + } | |
| 778 | + ).token_endpoint, | |
| 848 | 779 | issuerUrl: |
| 849 | − (payload.authorizationServerMetadata as { issuer?: string }).issuer ?? | |
| 850 | − null, | |
| 780 | + (payload.authorizationServerMetadata as { issuer?: string }).issuer ?? null, | |
| 851 | 781 | authorizationServerUrl: payload.authorizationServerUrl, |
| 852 | − authorizationServerMetadataUrl: | |
| 853 | − payload.authorizationServerMetadataUrl, | |
| 854 | − idTokenSigningAlgValuesSupported: | |
| 855 | − (payload.authorizationServerMetadata as { | |
| 782 | + authorizationServerMetadataUrl: payload.authorizationServerMetadataUrl, | |
| 783 | + idTokenSigningAlgValuesSupported: ( | |
| 784 | + payload.authorizationServerMetadata as { | |
| 856 | 785 | id_token_signing_alg_values_supported?: string[]; |
| 857 | − }).id_token_signing_alg_values_supported, | |
| 858 | − clientId: (payload.clientInformation as { client_id: string }) | |
| 859 | − .client_id, | |
| 786 | + } | |
| 787 | + ).id_token_signing_alg_values_supported, | |
| 788 | + clientId: (payload.clientInformation as { client_id: string }).client_id, | |
| 860 | 789 | clientSecretSecretId: dynamicClientSecretSecretId, |
| 861 | 790 | clientAuth: |
| 862 | 791 | (payload.clientInformation as { token_endpoint_auth_method?: string }) |
| @@ -901,19 +830,17 @@ export const makeOAuth2Service = ( · 38 unmodified lines | ||
| 901 | 830 | : null, |
| 902 | 831 | expiresAt: connectionExpiresAt, |
| 903 | 832 | oauthScope: exchangeResult.tokens.scope ?? null, |
| 904 | − providerState: Schema.encodeSync(OAuthProviderStateSchema)( | |
| 905 | − providerState, | |
| 906 | − ) as Record<string, unknown>, | |
| 833 | + providerState: Schema.encodeSync(OAuthProviderStateSchema)(providerState) as Record< | |
| 834 | + string, | |
| 835 | + unknown | |
| 836 | + >, | |
| 907 | 837 | }), |
| 908 | 838 | ) |
| 909 | 839 | .pipe( |
| 910 | 840 | Effect.mapError( |
| 911 | − (err) => | |
| 841 | + () => | |
| 912 | 842 | new OAuthCompleteError({ |
| 913 | − message: `Failed to mint connection: ${ | |
| 914 | − err instanceof Error ? err.message : String(err) | |
| 915 | − }`, | |
| 916 | − | |
| 843 | + message: "Failed to mint connection", | |
| 917 | 844 | }), |
| 918 | 845 | ), |
| 919 | 846 | ); |
| @@ -962,19 +889,14 @@ export const makeOAuth2Service = ( · 42 unmodified lines | ||
| 962 | 889 | redirectUrl, |
| 963 | 890 | codeVerifier: payload.codeVerifier, |
| 964 | 891 | code, |
| 965 | − idTokenSigningAlgValuesSupported: | |
| 966 | − md.id_token_signing_alg_values_supported, | |
| 967 | − clientAuth: | |
| 968 | − ci.token_endpoint_auth_method === "client_secret_basic" | |
| 969 | − ? "basic" | |
| 970 | − : "body", | |
| 892 | + idTokenSigningAlgValuesSupported: md.id_token_signing_alg_values_supported, | |
| 893 | + clientAuth: ci.token_endpoint_auth_method === "client_secret_basic" ? "basic" : "body", | |
| 971 | 894 | }).pipe( |
| 972 | 895 | Effect.mapError( |
| 973 | 896 | (err) => |
| 974 | 897 | new OAuthCompleteError({ |
| 975 | − message: `Token exchange failed: ${err.message}`, | |
| 898 | + message: "Token exchange failed", | |
| 976 | 899 | code: err.error, |
| 977 | − | |
| 978 | 900 | }), |
| 979 | 901 | ), |
| 980 | 902 | ); |
| @@ -988,28 +910,21 @@ export const makeOAuth2Service = ( · 7 unmodified lines | ||
| 988 | 910 | payload: Extract<OAuthSessionPayload, { kind: "authorization-code" }>, |
| 989 | 911 | code: string, |
| 990 | 912 | redirectUrl: string, |
| 991 | − ): Effect.Effect< | |
| 992 | − ExchangeResult, | |
| 993 | − OAuthCompleteError | StorageFailure | |
| 994 | − > => | |
| 913 | + ): Effect.Effect<ExchangeResult, OAuthCompleteError | StorageFailure> => | |
| 995 | 914 | Effect.gen(function* () { |
| 996 | 915 | const clientId = yield* deps.secretsGet(payload.clientIdSecretId); |
| 997 | 916 | if (clientId === null) { |
| 998 | − return yield* Effect.fail( | |
| 999 | − new OAuthCompleteError({ | |
| 1000 | − message: `client_id secret "${payload.clientIdSecretId}" not found`, | |
| 1001 | − }), | |
| 1002 | − ); | |
| 917 | + return yield* new OAuthCompleteError({ | |
| 918 | + message: `client_id secret "${payload.clientIdSecretId}" not found`, | |
| 919 | + }); | |
| 1003 | 920 | } |
| 1004 | 921 | const clientSecret = payload.clientSecretSecretId |
| 1005 | 922 | ? yield* deps.secretsGet(payload.clientSecretSecretId) |
| 1006 | 923 | : null; |
| 1007 | 924 | if (payload.clientSecretSecretId && clientSecret === null) { |
| 1008 | − return yield* Effect.fail( | |
| 1009 | − new OAuthCompleteError({ | |
| 1010 | − message: `client_secret secret "${payload.clientSecretSecretId}" not found`, | |
| 1011 | − }), | |
| 1012 | − ); | |
| 925 | + return yield* new OAuthCompleteError({ | |
| 926 | + message: `client_secret secret "${payload.clientSecretSecretId}" not found`, | |
| 927 | + }); | |
| 1013 | 928 | } |
| 1014 | 929 | |
| 1015 | 930 | const tokens = yield* exchangeAuthorizationCode({ |
| @@ -1025,9 +940,8 @@ export const makeOAuth2Service = ( · 9 unmodified lines | ||
| 1025 | 940 | Effect.mapError( |
| 1026 | 941 | (err) => |
| 1027 | 942 | new OAuthCompleteError({ |
| 1028 | − message: `Token exchange failed: ${err.message}`, | |
| 943 | + message: "Token exchange failed", | |
| 1029 | 944 | code: err.error, |
| 1030 | − | |
| 1031 | 945 | }), |
| 1032 | 946 | ), |
| 1033 | 947 | ); |
| @@ -1048,7 +962,7 @@ export const makeOAuth2Service = ( · 14 unmodified lines | ||
| 1048 | 962 | model: "oauth2_session", |
| 1049 | 963 | where: [ |
| 1050 | 964 | { field: "id", value: sessionId }, |
| 1051 | − { field: "scope_id", value: row.scope_id as string }, | |
| 965 | + { field: "scope_id", value: row.scope_id }, | |
| 1052 | 966 | ], |
| 1053 | 967 | }); |
| 1054 | 968 | }); |
| @@ -1071,9 +985,7 @@ export const makeOAuth2Service = ( · 16 unmodified lines | ||
| 1071 | 985 | catch: (cause) => |
| 1072 | 986 | new ConnectionRefreshError({ |
| 1073 | 987 | connectionId: input.connectionId, |
| 1074 | − message: `oauth2 providerState is malformed: ${ | |
| 1075 | − cause instanceof Error ? cause.message : String(cause) | |
| 1076 | − }`, | |
| 988 | + message: "oauth2 providerState is malformed", | |
| 1077 | 989 | cause, |
| 1078 | 990 | }), |
| 1079 | 991 | }); |
| @@ -1095,22 +1007,16 @@ export const makeOAuth2Service = ( · 15 unmodified lines | ||
| 1095 | 1007 | case "dynamic-dcr": |
| 1096 | 1008 | return Effect.gen(function* () { |
| 1097 | 1009 | const csec = state.clientSecretSecretId |
| 1098 | − ? yield* deps | |
| 1099 | − .secretsGet(state.clientSecretSecretId) | |
| 1100 | − .pipe( | |
| 1101 | − Effect.mapError( | |
| 1102 | − (cause) => | |
| 1103 | − new ConnectionRefreshError({ | |
| 1104 | − connectionId: input.connectionId, | |
| 1105 | − message: `Failed to resolve DCR client_secret: ${ | |
| 1106 | − cause instanceof Error | |
| 1107 | − ? cause.message | |
| 1108 | − : String(cause) | |
| 1109 | − }`, | |
| 1110 | − cause, | |
| 1111 | − }), | |
| 1112 | − ), | |
| 1113 | − ) | |
| 1010 | + ? yield* deps.secretsGet(state.clientSecretSecretId).pipe( | |
| 1011 | + Effect.mapError( | |
| 1012 | + (cause) => | |
| 1013 | + new ConnectionRefreshError({ | |
| 1014 | + connectionId: input.connectionId, | |
| 1015 | + message: "Failed to resolve DCR client_secret", | |
| 1016 | + cause, | |
| 1017 | + }), | |
| 1018 | + ), | |
| 1019 | + ) | |
| 1114 | 1020 | : null; |
| 1115 | 1021 | if (state.clientSecretSecretId && csec === null) { |
| 1116 | 1022 | return yield* new ConnectionRefreshError({ |
| @@ -1124,22 +1030,16 @@ export const makeOAuth2Service = ( · 7 unmodified lines | ||
| 1124 | 1030 | case "authorization-code": |
| 1125 | 1031 | case "client-credentials": |
| 1126 | 1032 | return Effect.gen(function* () { |
| 1127 | − const cid = yield* deps | |
| 1128 | − .secretsGet(state.clientIdSecretId) | |
| 1129 | − .pipe( | |
| 1130 | − Effect.mapError( | |
| 1131 | − (cause) => | |
| 1132 | − new ConnectionRefreshError({ | |
| 1133 | − connectionId: input.connectionId, | |
| 1134 | − message: `Failed to resolve client_id secret: ${ | |
| 1135 | − cause instanceof Error | |
| 1136 | − ? cause.message | |
| 1137 | − : String(cause) | |
| 1138 | − }`, | |
| 1139 | − cause, | |
| 1140 | − }), | |
| 1141 | − ), | |
| 1142 | − ); | |
| 1033 | + const cid = yield* deps.secretsGet(state.clientIdSecretId).pipe( | |
| 1034 | + Effect.mapError( | |
| 1035 | + (cause) => | |
| 1036 | + new ConnectionRefreshError({ | |
| 1037 | + connectionId: input.connectionId, | |
| 1038 | + message: "Failed to resolve client_id secret", | |
| 1039 | + cause, | |
| 1040 | + }), | |
| 1041 | + ), | |
| 1042 | + ); | |
| 1143 | 1043 | if (cid === null) { |
| 1144 | 1044 | return yield* new ConnectionRefreshError({ |
| 1145 | 1045 | connectionId: input.connectionId, |
| @@ -1148,22 +1048,16 @@ export const makeOAuth2Service = ( · 2 unmodified lines | ||
| 1148 | 1048 | }); |
| 1149 | 1049 | } |
| 1150 | 1050 | const csec = state.clientSecretSecretId |
| 1151 | − ? yield* deps | |
| 1152 | − .secretsGet(state.clientSecretSecretId) | |
| 1153 | − .pipe( | |
| 1154 | − Effect.mapError( | |
| 1155 | − (cause) => | |
| 1156 | − new ConnectionRefreshError({ | |
| 1157 | − connectionId: input.connectionId, | |
| 1158 | − message: `Failed to resolve client_secret: ${ | |
| 1159 | − cause instanceof Error | |
| 1160 | − ? cause.message | |
| 1161 | − : String(cause) | |
| 1162 | − }`, | |
| 1163 | − cause, | |
| 1164 | − }), | |
| 1165 | − ), | |
| 1166 | − ) | |
| 1051 | + ? yield* deps.secretsGet(state.clientSecretSecretId).pipe( | |
| 1052 | + Effect.mapError( | |
| 1053 | + (cause) => | |
| 1054 | + new ConnectionRefreshError({ | |
| 1055 | + connectionId: input.connectionId, | |
| 1056 | + message: "Failed to resolve client_secret", | |
| 1057 | + cause, | |
| 1058 | + }), | |
| 1059 | + ), | |
| 1060 | + ) | |
| 1167 | 1061 | : null; |
| 1168 | 1062 | if (state.clientSecretSecretId && csec === null) { |
| 1169 | 1063 | return yield* new ConnectionRefreshError({ |
| @@ -1179,32 +1073,25 @@ export const makeOAuth2Service = ( · 9 unmodified lines | ||
| 1179 | 1073 | |
| 1180 | 1074 | const tokenEndpoint = yield* (() => { |
| 1181 | 1075 | if (state.tokenEndpoint) return Effect.succeed(state.tokenEndpoint); |
| 1182 | − if ( | |
| 1183 | − state.kind === "dynamic-dcr" && | |
| 1184 | − state.authorizationServerUrl | |
| 1185 | − ) { | |
| 1186 | − return discoverAuthorizationServerMetadata( | |
| 1187 | − state.authorizationServerUrl, | |
| 1188 | − ).pipe( | |
| 1076 | + if (state.kind === "dynamic-dcr" && state.authorizationServerUrl) { | |
| 1077 | + return discoverAuthorizationServerMetadata(state.authorizationServerUrl).pipe( | |
| 1189 | 1078 | Effect.flatMap((metadata) => |
| 1190 | 1079 | metadata?.metadata.token_endpoint |
| 1191 | 1080 | ? Effect.succeed(metadata.metadata.token_endpoint) |
| 1192 | 1081 | : Effect.fail( |
| 1193 | 1082 | new ConnectionRefreshError({ |
| 1194 | 1083 | connectionId: input.connectionId, |
| 1195 | − message: | |
| 1196 | − "oauth2 legacy MCP providerState is missing token endpoint", | |
| 1084 | + message: "oauth2 legacy MCP providerState is missing token endpoint", | |
| 1197 | 1085 | reauthRequired: true, |
| 1198 | 1086 | }), |
| 1199 | 1087 | ), |
| 1200 | 1088 | ), |
| 1201 | 1089 | Effect.mapError((cause) => |
| 1202 | − cause instanceof ConnectionRefreshError | |
| 1090 | + Predicate.isTagged("ConnectionRefreshError")(cause) | |
| 1203 | 1091 | ? cause |
| 1204 | 1092 | : new ConnectionRefreshError({ |
| 1205 | 1093 | connectionId: input.connectionId, |
| 1206 | − message: | |
| 1207 | − "Failed to discover token endpoint for legacy MCP OAuth connection", | |
| 1094 | + message: "Failed to discover token endpoint for legacy MCP OAuth connection", | |
| 1208 | 1095 | reauthRequired: true, |
| 1209 | 1096 | cause, |
| 1210 | 1097 | }), |
| @@ -1220,56 +1107,51 @@ export const makeOAuth2Service = ( · 9 unmodified lines | ||
| 1220 | 1107 | ); |
| 1221 | 1108 | })(); |
| 1222 | 1109 | |
| 1223 | − const tokens = yield* (state.kind === "client-credentials" | |
| 1224 | − ? exchangeClientCredentials({ | |
| 1225 | − tokenUrl: tokenEndpoint, | |
| 1226 | − clientId, | |
| 1227 | − clientSecret: clientSecret ?? "", | |
| 1228 | − scopes: state.scopes, | |
| 1229 | − scopeSeparator: state.scopeSeparator, | |
| 1230 | − clientAuth: state.clientAuth, | |
| 1231 | − }) | |
| 1232 | − : refreshAccessToken({ | |
| 1233 | − tokenUrl: tokenEndpoint, | |
| 1234 | − issuerUrl: | |
| 1235 | − state.kind === "dynamic-dcr" || state.kind === "authorization-code" | |
| 1236 | − ? (state.issuerUrl ?? undefined) | |
| 1237 | − : undefined, | |
| 1238 | − clientId, | |
| 1239 | − clientSecret: clientSecret ?? undefined, | |
| 1240 | − refreshToken: input.refreshToken!, | |
| 1241 | − scopes: | |
| 1242 | − state.kind === "dynamic-dcr" || state.kind === "authorization-code" | |
| 1243 | − ? state.scopes | |
| 1244 | − : undefined, | |
| 1245 | − scopeSeparator: | |
| 1246 | − state.kind === "dynamic-dcr" || state.kind === "authorization-code" | |
| 1247 | − ? state.scopeSeparator | |
| 1248 | − : undefined, | |
| 1249 | − clientAuth: state.clientAuth, | |
| 1250 | − idTokenSigningAlgValuesSupported: | |
| 1251 | − state.kind === "dynamic-dcr" | |
| 1252 | − ? state.idTokenSigningAlgValuesSupported | |
| 1253 | − : undefined, | |
| 1254 | − })).pipe( | |
| 1110 | + const tokens = yield* ( | |
| 1111 | + state.kind === "client-credentials" | |
| 1112 | + ? exchangeClientCredentials({ | |
| 1113 | + tokenUrl: tokenEndpoint, | |
| 1114 | + clientId, | |
| 1115 | + clientSecret: clientSecret ?? "", | |
| 1116 | + scopes: state.scopes, | |
| 1117 | + scopeSeparator: state.scopeSeparator, | |
| 1118 | + clientAuth: state.clientAuth, | |
| 1119 | + }) | |
| 1120 | + : refreshAccessToken({ | |
| 1121 | + tokenUrl: tokenEndpoint, | |
| 1122 | + issuerUrl: | |
| 1123 | + state.kind === "dynamic-dcr" || state.kind === "authorization-code" | |
| 1124 | + ? (state.issuerUrl ?? undefined) | |
| 1125 | + : undefined, | |
| 1126 | + clientId, | |
| 1127 | + clientSecret: clientSecret ?? undefined, | |
| 1128 | + refreshToken: input.refreshToken!, | |
| 1129 | + scopes: | |
| 1130 | + state.kind === "dynamic-dcr" || state.kind === "authorization-code" | |
| 1131 | + ? state.scopes | |
| 1132 | + : undefined, | |
| 1133 | + scopeSeparator: | |
| 1134 | + state.kind === "dynamic-dcr" || state.kind === "authorization-code" | |
| 1135 | + ? state.scopeSeparator | |
| 1136 | + : undefined, | |
| 1137 | + clientAuth: state.clientAuth, | |
| 1138 | + idTokenSigningAlgValuesSupported: | |
| 1139 | + state.kind === "dynamic-dcr" ? state.idTokenSigningAlgValuesSupported : undefined, | |
| 1140 | + }) | |
| 1141 | + ).pipe( | |
| 1255 | 1142 | Effect.mapError( |
| 1256 | 1143 | (err) => |
| 1257 | 1144 | new ConnectionRefreshError({ |
| 1258 | 1145 | connectionId: input.connectionId, |
| 1259 | − message: `OAuth refresh failed: ${err.message}`, | |
| 1146 | + message: "OAuth refresh failed", | |
| 1260 | 1147 | // Terminal RFC 6749 §5.2 errors mean retrying won't heal it. |
| 1261 | − reauthRequired: err.error | |
| 1262 | − ? terminalRefreshErrors.has(err.error) | |
| 1263 | − : false, | |
| 1264 | − | |
| 1148 | + reauthRequired: err.error ? terminalRefreshErrors.has(err.error) : false, | |
| 1265 | 1149 | }), |
| 1266 | 1150 | ), |
| 1267 | 1151 | ); |
| 1268 | 1152 | |
| 1269 | 1153 | const expiresAt = |
| 1270 | − typeof tokens.expires_in === "number" | |
| 1271 | − ? now() + tokens.expires_in * 1000 | |
| 1272 | − : null; | |
| 1154 | + typeof tokens.expires_in === "number" ? now() + tokens.expires_in * 1000 : null; | |
| 1273 | 1155 | |
| 1274 | 1156 | const result: ConnectionRefreshResult = { |
| 1275 | 1157 | accessToken: tokens.access_token, |
| @@ -1293,9 +1175,5 @@ export const makeOAuth2Service = ( · 17 unmodified lines | ||
| 1293 | 1175 | |
| 1294 | 1176 | const safeHostname = (value: string | null): string | null => { |
| 1295 | 1177 | if (!value) return null; |
| 1296 | − try { | |
| 1297 | − return new URL(value).host; | |
| 1298 | − } catch { | |
| 1299 | − return value; | |
| 1300 | − } | |
| 1178 | + return parseUrlOption(value)?.host ?? value; | |
| 1301 | 1179 | }; |
| @@ -13,8 +13,14 @@ · 12 unmodified lines | ||
| 13 | 13 | // --------------------------------------------------------------------------- |
| 14 | 14 | |
| 15 | 15 | import { afterEach, expect, layer } from "@effect/vitest"; |
| 16 | −import { Effect, Layer, Schema } from "effect"; | |
| 17 | −import { HttpApi, HttpApiBuilder, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"; | |
| 16 | +import { Effect, Layer, Predicate, Schema } from "effect"; | |
| 17 | +import { | |
| 18 | + HttpApi, | |
| 19 | + HttpApiBuilder, | |
| 20 | + HttpApiEndpoint, | |
| 21 | + HttpApiGroup, | |
| 22 | + OpenApi, | |
| 23 | +} from "effect/unstable/httpapi"; | |
| 18 | 24 | import { FetchHttpClient, HttpRouter, HttpServer, HttpServerRequest } from "effect/unstable/http"; |
| 19 | 25 | import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; |
| 20 | 26 | |
| @@ -40,13 +46,6 @@ import { OAuth2Auth } from "./types"; · 19 unmodified lines | ||
| 40 | 46 | |
| 41 | 47 | const autoApprove: InvokeOptions = { onElicitation: "accept-all" }; |
| 42 | 48 | |
| 43 | −class OpenApiOauthTestSetupError extends Schema.TaggedErrorClass<OpenApiOauthTestSetupError>()( | |
| 44 | − "OpenApiOauthTestSetupError", | |
| 45 | − { | |
| 46 | − message: Schema.String, | |
| 47 | − }, | |
| 48 | −) {} | |
| 49 | − | |
| 50 | 49 | // --------------------------------------------------------------------------- |
| 51 | 50 | // Test API — one endpoint that echoes the Authorization header so we can |
| 52 | 51 | // prove which access token was in flight at invoke time. |
| @@ -96,23 +95,26 @@ const mockTokenFetch = ( · 43 unmodified lines | ||
| 96 | 95 | handler: (body: URLSearchParams) => Effect.Effect<Response, never, never> | Promise<Response>, |
| 97 | 96 | ) => { |
| 98 | 97 | const calls: TokenCall[] = []; |
| 99 | − globalThis.fetch = Object.assign(async (_input: RequestInfo | URL, init?: RequestInit) => { | |
| 100 | − const url = typeof _input === "string" ? _input : _input.toString(); | |
| 101 | − if (!url.includes("token.example.com")) { | |
| 102 | − return originalFetch(_input, init); | |
| 103 | − } | |
| 104 | − const bodyText = | |
| 105 | − init?.body instanceof URLSearchParams | |
| 106 | − ? init.body.toString() | |
| 107 | − : typeof init?.body === "string" | |
| 108 | − ? init.body | |
| 109 | − : ""; | |
| 110 | − const body = new URLSearchParams(bodyText); | |
| 111 | − calls.push({ body }); | |
| 112 | − const out = handler(body); | |
| 113 | − if (Effect.isEffect(out)) return await Effect.runPromise(out); | |
| 114 | − return await out; | |
| 115 | − }, { preconnect: originalFetch.preconnect }); | |
| 98 | + globalThis.fetch = Object.assign( | |
| 99 | + async (_input: RequestInfo | URL, init?: RequestInit) => { | |
| 100 | + const url = typeof _input === "string" ? _input : _input.toString(); | |
| 101 | + if (!url.includes("token.example.com")) { | |
| 102 | + return originalFetch(_input, init); | |
| 103 | + } | |
| 104 | + const bodyText = | |
| 105 | + init?.body instanceof URLSearchParams | |
| 106 | + ? init.body.toString() | |
| 107 | + : typeof init?.body === "string" | |
| 108 | + ? init.body | |
| 109 | + : ""; | |
| 110 | + const body = new URLSearchParams(bodyText); | |
| 111 | + calls.push({ body }); | |
| 112 | + const out = handler(body); | |
| 113 | + if (Effect.isEffect(out)) return await Effect.runPromise(out); | |
| 114 | + return await out; | |
| 115 | + }, | |
| 116 | + { preconnect: originalFetch.preconnect }, | |
| 117 | + ); | |
| 116 | 118 | return { calls }; |
| 117 | 119 | }; |
| 118 | 120 | |
| @@ -133,14 +135,12 @@ const makeExecutor = () => · 14 unmodified lines | ||
| 133 | 135 | const memoryProvider: SecretProvider = { |
| 134 | 136 | key: "memory", |
| 135 | 137 | writable: true, |
| 136 | − get: (id, scope) => | |
| 137 | − Effect.sync(() => secretStore.get(keyOf(scope, id)) ?? null), | |
| 138 | + get: (id, scope) => Effect.sync(() => secretStore.get(keyOf(scope, id)) ?? null), | |
| 138 | 139 | set: (id, value, scope) => |
| 139 | 140 | Effect.sync(() => { |
| 140 | 141 | secretStore.set(keyOf(scope, id), value); |
| 141 | 142 | }), |
| 142 | − delete: (id, scope) => | |
| 143 | − Effect.sync(() => secretStore.delete(keyOf(scope, id))), | |
| 143 | + delete: (id, scope) => Effect.sync(() => secretStore.delete(keyOf(scope, id))), | |
| 144 | 144 | }; |
| 145 | 145 | const memorySecretsPlugin = definePlugin(() => ({ |
| 146 | 146 | id: "memory-secrets" as const, |
| @@ -150,10 +150,9 @@ const makeExecutor = () => · 3 unmodified lines | ||
| 150 | 150 | const clientLayer = FetchHttpClient.layer; |
| 151 | 151 | const server = yield* HttpServer.HttpServer; |
| 152 | 152 | const address = server.address; |
| 153 | − if (!("port" in address)) { | |
| 154 | − return yield* new OpenApiOauthTestSetupError({ | |
| 155 | − message: "Test server must bind to TCP", | |
| 156 | − }); | |
| 153 | + if (!Predicate.isTagged("TcpAddress")(address)) { | |
| 154 | + // oxlint-disable-next-line executor/no-effect-escape-hatch -- boundary: test harness cannot continue without a TCP test server address | |
| 155 | + return yield* Effect.die("test server must bind to TCP"); | |
| 157 | 156 | } |
| 158 | 157 | const baseUrl = `http://127.0.0.1:${address.port}`; |
| 159 | 158 | const plugins = [ |
| @@ -208,11 +207,7 @@ type ExecutorValue = EffectSuccess<ReturnType<typeof makeExecutor>>["executor"]; · 48 unmodified lines | ||
| 208 | 207 | // Seed an authorizationCode Connection with an already-expired access |
| 209 | 208 | // token and a stored refresh token. The test's mock token endpoint |
| 210 | 209 | // decides what comes back on `grant_type=refresh_token`. |
| 211 | −const seedExpiredConnection = ( | |
| 212 | − executor: ExecutorValue, | |
| 213 | − scopeId: ScopeId, | |
| 214 | − connectionId: string, | |
| 215 | −) => | |
| 210 | +const seedExpiredConnection = (executor: ExecutorValue, scopeId: ScopeId, connectionId: string) => | |
| 216 | 211 | Effect.gen(function* () { |
| 217 | 212 | yield* executor.connections.create( |
| 218 | 213 | new CreateConnectionInput({ |
| @@ -259,177 +254,144 @@ const seedExpiredConnection = ( · 40 unmodified lines | ||
| 259 | 254 | // --------------------------------------------------------------------------- |
| 260 | 255 | |
| 261 | 256 | layer(TestLayer)("OpenAPI oauth refresh", (it) => { |
| 262 | − it.effect( | |
| 263 | − "expired access_token is refreshed via grant_type=refresh_token before invoke", | |
| 264 | − () => | |
| 265 | − Effect.gen(function* () { | |
| 266 | − const { executor, scopeId, baseUrl } = yield* makeExecutor(); | |
| 267 | − const { calls } = mockTokenFetch( | |
| 268 | − () => | |
| 269 | − Effect.succeed( | |
| 270 | − new Response( | |
| 271 | − JSON.stringify({ | |
| 272 | − access_token: "fresh-access-v2", | |
| 273 | − token_type: "Bearer", | |
| 274 | − refresh_token: "refresh-v2", | |
| 275 | − expires_in: 3600, | |
| 276 | − }), | |
| 277 | − { status: 200, headers: { "content-type": "application/json" } }, | |
| 278 | − ), | |
| 279 | − ), | |
| 280 | − ); | |
| 281 | − | |
| 282 | − const auth = yield* seedExpiredConnection( | |
| 283 | − executor, | |
| 284 | − scopeId, | |
| 285 | − "conn-refresh-ok", | |
| 286 | − ); | |
| 287 | − | |
| 288 | − yield* executor.openapi.addSpec({ | |
| 289 | − spec: specJson, | |
| 290 | − scope: scopeId, | |
| 291 | − namespace: "petstore", | |
| 292 | − baseUrl, | |
| 293 | − oauth2: auth, | |
| 294 | − }); | |
| 295 | − | |
| 296 | − const result = (yield* executor.tools.invoke( | |
| 297 | − "petstore.items.echoHeaders", | |
| 298 | − {}, | |
| 299 | − autoApprove, | |
| 300 | − )) as { data: { authorization?: string } | null; error: unknown }; | |
| 301 | − | |
| 302 | − expect(result.error).toBeNull(); | |
| 303 | − // Proves the refresh landed: invoke carried the fresh token, | |
| 304 | − // not the expired one we seeded. | |
| 305 | − expect(result.data?.authorization).toBe("Bearer fresh-access-v2"); | |
| 306 | − expect(calls).toHaveLength(1); | |
| 307 | − expect(calls[0]!.body.get("grant_type")).toBe("refresh_token"); | |
| 308 | − expect(calls[0]!.body.get("refresh_token")).toBe("refresh-v1"); | |
| 309 | − | |
| 310 | − // Connection row is patched with the new expiry so the next | |
| 311 | − // invoke in-window doesn't trip a second refresh. | |
| 312 | − const conn = yield* executor.connections.get("conn-refresh-ok"); | |
| 313 | − expect(conn).not.toBeNull(); | |
| 314 | − expect(conn!.expiresAt).not.toBeNull(); | |
| 315 | − expect(conn!.expiresAt!).toBeGreaterThan(Date.now() + 3_000_000); | |
| 316 | − }), | |
| 257 | + it.effect("expired access_token is refreshed via grant_type=refresh_token before invoke", () => | |
| 258 | + Effect.gen(function* () { | |
| 259 | + const { executor, scopeId, baseUrl } = yield* makeExecutor(); | |
| 260 | + const { calls } = mockTokenFetch(() => | |
| 261 | + Effect.succeed( | |
| 262 | + new Response( | |
| 263 | + JSON.stringify({ | |
| 264 | + access_token: "fresh-access-v2", | |
| 265 | + token_type: "Bearer", | |
| 266 | + refresh_token: "refresh-v2", | |
| 267 | + expires_in: 3600, | |
| 268 | + }), | |
| 269 | + { status: 200, headers: { "content-type": "application/json" } }, | |
| 270 | + ), | |
| 271 | + ), | |
| 272 | + ); | |
| 273 | + | |
| 274 | + const auth = yield* seedExpiredConnection(executor, scopeId, "conn-refresh-ok"); | |
| 275 | + | |
| 276 | + yield* executor.openapi.addSpec({ | |
| 277 | + spec: specJson, | |
| 278 | + scope: String(scopeId), | |
| 279 | + namespace: "petstore", | |
| 280 | + baseUrl, | |
| 281 | + oauth2: auth, | |
| 282 | + }); | |
| 283 | + | |
| 284 | + const result = (yield* executor.tools.invoke( | |
| 285 | + "petstore.items.echoHeaders", | |
| 286 | + {}, | |
| 287 | + autoApprove, | |
| 288 | + )) as { data: { authorization?: string } | null; error: unknown }; | |
| 289 | + | |
| 290 | + expect(result.error).toBeNull(); | |
| 291 | + // Proves the refresh landed: invoke carried the fresh token, | |
| 292 | + // not the expired one we seeded. | |
| 293 | + expect(result.data?.authorization).toBe("Bearer fresh-access-v2"); | |
| 294 | + expect(calls).toHaveLength(1); | |
| 295 | + expect(calls[0]!.body.get("grant_type")).toBe("refresh_token"); | |
| 296 | + expect(calls[0]!.body.get("refresh_token")).toBe("refresh-v1"); | |
| 297 | + | |
| 298 | + // Connection row is patched with the new expiry so the next | |
| 299 | + // invoke in-window doesn't trip a second refresh. | |
| 300 | + const conn = yield* executor.connections.get("conn-refresh-ok"); | |
| 301 | + expect(conn).not.toBeNull(); | |
| 302 | + expect(conn!.expiresAt).not.toBeNull(); | |
| 303 | + expect(conn!.expiresAt!).toBeGreaterThan(Date.now() + 3_000_000); | |
| 304 | + }), | |
| 317 | 305 | ); |
| 318 | 306 | |
| 319 | − it.effect( | |
| 320 | − "concurrent invokes with an expired token issue exactly one refresh", | |
| 321 | − () => | |
| 322 | − Effect.gen(function* () { | |
| 323 | − const { executor, scopeId, baseUrl } = yield* makeExecutor(); | |
| 324 | − const { calls } = mockTokenFetch( | |
| 325 | − () => | |
| 326 | − Effect.succeed( | |
| 327 | − new Response( | |
| 328 | − JSON.stringify({ | |
| 329 | − access_token: "fresh-access-v2", | |
| 330 | − token_type: "Bearer", | |
| 331 | − refresh_token: "refresh-v2", | |
| 332 | − expires_in: 3600, | |
| 333 | − }), | |
| 334 | − { status: 200, headers: { "content-type": "application/json" } }, | |
| 335 | − ), | |
| 336 | − ), | |
| 337 | − ); | |
| 338 | − | |
| 339 | − const auth = yield* seedExpiredConnection( | |
| 340 | − executor, | |
| 341 | − scopeId, | |
| 342 | − "conn-refresh-concurrent", | |
| 343 | − ); | |
| 344 | − | |
| 345 | − yield* executor.openapi.addSpec({ | |
| 346 | − spec: specJson, | |
| 347 | − scope: scopeId, | |
| 348 | − namespace: "petstore", | |
| 349 | − baseUrl, | |
| 350 | − oauth2: auth, | |
| 351 | − }); | |
| 352 | − | |
| 353 | − const invokes = yield* Effect.all( | |
| 354 | − [1, 2, 3, 4, 5].map(() => | |
| 355 | − executor.tools.invoke( | |
| 356 | − "petstore.items.echoHeaders", | |
| 357 | − {}, | |
| 358 | − autoApprove, | |
| 359 | − ), | |
| 307 | + it.effect("concurrent invokes with an expired token issue exactly one refresh", () => | |
| 308 | + Effect.gen(function* () { | |
| 309 | + const { executor, scopeId, baseUrl } = yield* makeExecutor(); | |
| 310 | + const { calls } = mockTokenFetch(() => | |
| 311 | + Effect.succeed( | |
| 312 | + new Response( | |
| 313 | + JSON.stringify({ | |
| 314 | + access_token: "fresh-access-v2", | |
| 315 | + token_type: "Bearer", | |
| 316 | + refresh_token: "refresh-v2", | |
| 317 | + expires_in: 3600, | |
| 318 | + }), | |
| 319 | + { status: 200, headers: { "content-type": "application/json" } }, | |
| 360 | 320 | ), |
| 361 | − { concurrency: "unbounded" }, | |
| 362 | − ); | |
| 363 | − | |
| 364 | − for (const r of invokes) { | |
| 365 | − const res = r as { | |
| 366 | − data: { authorization?: string } | null; | |
| 367 | − error: unknown; | |
| 368 | − }; | |
| 369 | − expect(res.error).toBeNull(); | |
| 370 | − expect(res.data?.authorization).toBe("Bearer fresh-access-v2"); | |
| 371 | − } | |
| 372 | − // Critical assertion: the SDK's dedup collapses every parallel | |
| 373 | − // invoke into one call to the token endpoint. Anything more | |
| 374 | − // means we're hammering the AS under load. | |
| 375 | − expect(calls).toHaveLength(1); | |
| 376 | − }), | |
| 321 | + ), | |
| 322 | + ); | |
| 323 | + | |
| 324 | + const auth = yield* seedExpiredConnection(executor, scopeId, "conn-refresh-concurrent"); | |
| 325 | + | |
| 326 | + yield* executor.openapi.addSpec({ | |
| 327 | + spec: specJson, | |
| 328 | + scope: String(scopeId), | |
| 329 | + namespace: "petstore", | |
| 330 | + baseUrl, | |
| 331 | + oauth2: auth, | |
| 332 | + }); | |
| 333 | + | |
| 334 | + const invokes = yield* Effect.all( | |
| 335 | + [1, 2, 3, 4, 5].map(() => | |
| 336 | + executor.tools.invoke("petstore.items.echoHeaders", {}, autoApprove), | |
| 337 | + ), | |
| 338 | + { concurrency: "unbounded" }, | |
| 339 | + ); | |
| 340 | + | |
| 341 | + for (const r of invokes) { | |
| 342 | + const res = r as { | |
| 343 | + data: { authorization?: string } | null; | |
| 344 | + error: unknown; | |
| 345 | + }; | |
| 346 | + expect(res.error).toBeNull(); | |
| 347 | + expect(res.data?.authorization).toBe("Bearer fresh-access-v2"); | |
| 348 | + } | |
| 349 | + // Critical assertion: the SDK's dedup collapses every parallel | |
| 350 | + // invoke into one call to the token endpoint. Anything more | |
| 351 | + // means we're hammering the AS under load. | |
| 352 | + expect(calls).toHaveLength(1); | |
| 353 | + }), | |
| 377 | 354 | ); |
| 378 | 355 | |
| 379 | − it.effect( | |
| 380 | − "invalid_grant from refresh surfaces as ConnectionReauthRequiredError", | |
| 381 | − () => | |
| 382 | − Effect.gen(function* () { | |
| 383 | − const { executor, scopeId, baseUrl } = yield* makeExecutor(); | |
| 384 | − mockTokenFetch( | |
| 385 | − () => | |
| 386 | − Effect.succeed( | |
| 387 | − new Response( | |
| 388 | − JSON.stringify({ | |
| 389 | − error: "invalid_grant", | |
| 390 | − error_description: "Refresh token revoked", | |
| 391 | − }), | |
| 392 | − { status: 400, headers: { "content-type": "application/json" } }, | |
| 393 | − ), | |
| 394 | − ), | |
| 395 | − ); | |
| 396 | − | |
| 397 | − const auth = yield* seedExpiredConnection( | |
| 398 | − executor, | |
| 399 | − scopeId, | |
| 400 | − "conn-refresh-dead", | |
| 401 | − ); | |
| 402 | − | |
| 403 | − yield* executor.openapi.addSpec({ | |
| 404 | − spec: specJson, | |
| 405 | − scope: scopeId, | |
| 406 | − namespace: "petstore", | |
| 407 | − baseUrl, | |
| 408 | − oauth2: auth, | |
| 409 | − }); | |
| 410 | − | |
| 411 | − // Tool invocation currently wraps connection errors in a | |
| 412 | − // generic Error (see openapi invokeTool), so we assert against | |
| 413 | − // the `accessToken` call directly too — that's the surface | |
| 414 | − // the UI bridges use to trigger re-auth. | |
| 415 | − const reauthRequired = yield* executor.connections | |
| 416 | − .accessToken("conn-refresh-dead") | |
| 417 | − .pipe( | |
| 418 | − Effect.flatMap(() => | |
| 419 | − Effect.fail( | |
| 420 | − new OpenApiOauthTestSetupError({ | |
| 421 | − message: "Expected refresh to require re-auth", | |
| 422 | − }), | |
| 423 | − ), | |
| 424 | − ), | |
| 425 | − Effect.catchTag("ConnectionReauthRequiredError", Effect.succeed), | |
| 426 | − ); | |
| 427 | − expect(reauthRequired.provider).toBe( | |
| 428 | − "openapi:oauth2", | |
| 429 | − ); | |
| 430 | − expect( | |
| 431 | − reauthRequired.message, | |
| 432 | − ).toMatch(/invalid_grant|revoked/i); | |
| 433 | − }), | |
| 356 | + it.effect("invalid_grant from refresh surfaces as ConnectionReauthRequiredError", () => | |
| 357 | + Effect.gen(function* () { | |
| 358 | + const { executor, scopeId, baseUrl } = yield* makeExecutor(); | |
| 359 | + mockTokenFetch(() => | |
| 360 | + Effect.succeed( | |
| 361 | + new Response( | |
| 362 | + JSON.stringify({ | |
| 363 | + error: "invalid_grant", | |
| 364 | + error_description: "Refresh token revoked", | |
| 365 | + }), | |
| 366 | + { status: 400, headers: { "content-type": "application/json" } }, | |
| 367 | + ), | |
| 368 | + ), | |
| 369 | + ); | |
| 370 | + | |
| 371 | + const auth = yield* seedExpiredConnection(executor, scopeId, "conn-refresh-dead"); | |
| 372 | + | |
| 373 | + yield* executor.openapi.addSpec({ | |
| 374 | + spec: specJson, | |
| 375 | + scope: String(scopeId), | |
| 376 | + namespace: "petstore", | |
| 377 | + baseUrl, | |
| 378 | + oauth2: auth, | |
| 379 | + }); | |
| 380 | + | |
| 381 | + // Tool invocation currently wraps connection errors in a | |
| 382 | + // generic Error (see openapi invokeTool), so we assert against | |
| 383 | + // the `accessToken` call directly too — that's the surface | |
| 384 | + // the UI bridges use to trigger re-auth. | |
| 385 | + const flipped = yield* executor.connections.accessToken("conn-refresh-dead").pipe( | |
| 386 | + Effect.flip, | |
| 387 | + Effect.flatMap((error) => | |
| 388 | + Predicate.isTagged("ConnectionReauthRequiredError")(error) | |
| 389 | + ? Effect.succeed(error) | |
| 390 | + : Effect.fail(error), | |
| 391 | + ), | |
| 392 | + ); | |
| 393 | + expect(flipped.provider).toBe("openapi:oauth2"); | |
| 394 | + expect(flipped.message).toBe("OAuth refresh failed"); | |
| 395 | + }), | |
| 434 | 396 | ); |
| 435 | 397 | }); |