diff --git a/packages/client-runtime/src/authorization/layer.test.ts b/packages/client-runtime/src/authorization/layer.test.ts index 8250999bbe..fa77ab2c13 100644 --- a/packages/client-runtime/src/authorization/layer.test.ts +++ b/packages/client-runtime/src/authorization/layer.test.ts @@ -231,12 +231,12 @@ describe("RemoteEnvironmentAuthorization", () => { }), ); - it.effect("reuses a valid persisted environment token without contacting the relay", () => + it.effect("refreshes the descriptor while reusing a valid environment token", () => Effect.gen(function* () { + const refreshedServerVersion = "0.0.1"; const cached = new TokenStore.RemoteDpopAccessToken({ environmentId: ENVIRONMENT_ID, label: DESCRIPTOR.label, - serverVersion: DESCRIPTOR.serverVersion, endpoint: ENDPOINT, accessToken: "cached-access-token", expiresAtEpochMs: Number.MAX_SAFE_INTEGER, @@ -244,7 +244,10 @@ describe("RemoteEnvironmentAuthorization", () => { }); const harness = yield* makeHarness({ initialToken: cached, - responses: [websocketTicket("cached-ticket")], + responses: [ + Response.json({ ...DESCRIPTOR, serverVersion: refreshedServerVersion }), + websocketTicket("cached-ticket"), + ], }); const authorized = yield* Effect.gen(function* () { @@ -256,10 +259,13 @@ describe("RemoteEnvironmentAuthorization", () => { }).pipe(Effect.provide(harness.layer)); expect(authorized.socketUrl).toContain("wsTicket=cached-ticket"); - expect(authorized.serverVersion).toBe(DESCRIPTOR.serverVersion); + expect(authorized.serverVersion).toBe(refreshedServerVersion); expect(yield* Ref.get(harness.bootstrapCalls)).toBe(0); - expect(harness.fetch.calls).toHaveLength(1); + expect(harness.fetch.calls).toHaveLength(2); expect(String(harness.fetch.calls[0]?.[0])).toBe( + "https://environment.example.test/.well-known/t3/environment", + ); + expect(String(harness.fetch.calls[1]?.[0])).toBe( "https://environment.example.test/api/auth/websocket-ticket", ); }), @@ -299,7 +305,6 @@ describe("RemoteEnvironmentAuthorization", () => { expect.objectContaining({ accessToken: "fresh-access-token", dpopThumbprint: "thumbprint-1", - serverVersion: DESCRIPTOR.serverVersion, }), ); expect(harness.fetch.calls).toHaveLength(3); @@ -319,6 +324,7 @@ describe("RemoteEnvironmentAuthorization", () => { const harness = yield* makeHarness({ initialToken: cached, responses: [ + Response.json(DESCRIPTOR), authInvalid(), Response.json(DESCRIPTOR), accessToken("replacement-access-token"), @@ -341,7 +347,7 @@ describe("RemoteEnvironmentAuthorization", () => { accessToken: "replacement-access-token", }), ); - expect(harness.fetch.calls).toHaveLength(4); + expect(harness.fetch.calls).toHaveLength(5); }), ); @@ -358,6 +364,7 @@ describe("RemoteEnvironmentAuthorization", () => { const harness = yield* makeHarness({ initialToken: cached, responses: [ + Response.json(DESCRIPTOR), new Response("endpoint unavailable", { status: 503 }), Response.json(DESCRIPTOR), accessToken("replacement-access-token"), @@ -380,7 +387,7 @@ describe("RemoteEnvironmentAuthorization", () => { accessToken: "replacement-access-token", }), ); - expect(harness.fetch.calls).toHaveLength(4); + expect(harness.fetch.calls).toHaveLength(5); }), ); diff --git a/packages/client-runtime/src/authorization/service.ts b/packages/client-runtime/src/authorization/service.ts index 9fbf712014..6676681333 100644 --- a/packages/client-runtime/src/authorization/service.ts +++ b/packages/client-runtime/src/authorization/service.ts @@ -209,17 +209,24 @@ export const make = Effect.gen(function* () { yield* Effect.annotateCurrentSpan({ "connection.remote_token_cache": "hit", }); + const descriptor = yield* fetchDescriptor(cached.value.endpoint.httpBaseUrl).pipe( + Effect.provideService(HttpClient.HttpClient, httpClient), + ); + if (descriptor.environmentId !== input.expectedEnvironmentId) { + return yield* environmentMismatchError({ + expected: input.expectedEnvironmentId, + actual: descriptor.environmentId, + }); + } const cachedSocket = yield* createDpopSocketUrl( cached.value, CACHED_ENDPOINT_SOCKET_TIMEOUT_MS, ).pipe(Effect.result); if (Result.isSuccess(cachedSocket)) { return { - environmentId: cached.value.environmentId, - label: cached.value.label, - ...(cached.value.serverVersion === undefined - ? {} - : { serverVersion: cached.value.serverVersion }), + environmentId: descriptor.environmentId, + label: descriptor.label, + serverVersion: descriptor.serverVersion, httpBaseUrl: cached.value.endpoint.httpBaseUrl, socketUrl: cachedSocket.success, httpAuthorization: { @@ -279,7 +286,6 @@ export const make = Effect.gen(function* () { const token = new TokenStore.RemoteDpopAccessToken({ environmentId: descriptor.environmentId, label: descriptor.label, - serverVersion: descriptor.serverVersion, endpoint: bootstrap.endpoint, accessToken: access.access_token, expiresAtEpochMs: issuedAt + access.expires_in * 1_000, diff --git a/packages/client-runtime/src/authorization/tokenStore.ts b/packages/client-runtime/src/authorization/tokenStore.ts index 315a6cd9f8..c490a22da1 100644 --- a/packages/client-runtime/src/authorization/tokenStore.ts +++ b/packages/client-runtime/src/authorization/tokenStore.ts @@ -13,7 +13,6 @@ export class RemoteDpopAccessToken extends Schema.Class( )({ environmentId: EnvironmentId, label: Schema.String, - serverVersion: Schema.optionalKey(Schema.String), endpoint: RelayManagedEndpoint, accessToken: Schema.String, expiresAtEpochMs: Schema.Number,