@@ -9,10 +9,23 @@ import * as Path from "effect/Path";
99import * as PlatformError from "effect/PlatformError" ;
1010import * as Schema from "effect/Schema" ;
1111import * as Stream from "effect/Stream" ;
12- import { HttpClient , HttpClientResponse , HttpServer , type HttpClientRequest } from "effect/http" ;
12+ import * as TestClock from "effect/testing/TestClock" ;
13+ import {
14+ HttpClient ,
15+ HttpClientResponse ,
16+ HttpServer ,
17+ HttpServerRequest ,
18+ type HttpClientRequest ,
19+ } from "effect/http" ;
20+ import { RELAY_LINK_PROOF_TYP , signRelayJwt , verifyRelayJwt } from "@t3tools/shared/relayJwt" ;
1321import * as NetAddress from "effect/net/NetAddress" ;
1422
15- import { EnvironmentId } from "@t3tools/contracts" ;
23+ import {
24+ AuthSessionId ,
25+ type AuthEnvironmentScope ,
26+ EnvironmentAuthenticatedPrincipal ,
27+ EnvironmentId ,
28+ } from "@t3tools/contracts" ;
1629import * as EnvironmentKeys from "./environmentKeys.ts" ;
1730import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts" ;
1831import * as ServerSecretStore from "../auth/ServerSecretStore.ts" ;
@@ -28,7 +41,10 @@ import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
2841import * as AgentAwarenessRelay from "../relay/AgentAwarenessRelay.ts" ;
2942import { CLOUD_CLI_DESIRED_LINK_SECRET } from "./CliState.ts" ;
3043import * as CliTokenManager from "./CliTokenManager.ts" ;
31- import { RelayManagedEndpointRecoveryRegistrationRequest } from "@t3tools/contracts/relay" ;
44+ import {
45+ RelayEnvironmentLinkProofPayload ,
46+ RelayManagedEndpointRecoveryRegistrationRequest ,
47+ } from "@t3tools/contracts/relay" ;
3248import {
3349 CLOUD_ENDPOINT_CONFIRMED_ORIGIN ,
3450 CLOUD_ENDPOINT_RUNTIME_CONFIG ,
@@ -186,7 +202,13 @@ describe("releaseManagedTunnelOnShutdown", () => {
186202 ServerEnvironment . ServerEnvironment ,
187203 ServerEnvironment . ServerEnvironment . of ( {
188204 getEnvironmentId : Effect . succeed ( EnvironmentId . make ( "env_123" ) ) ,
189- getDescriptor : Effect . die ( "unused" ) ,
205+ getDescriptor : Effect . succeed ( {
206+ environmentId : EnvironmentId . make ( "env_123" ) ,
207+ label : "Test environment" ,
208+ platform : { os : "linux" , arch : "x64" } ,
209+ serverVersion : "0.0.0-test" ,
210+ capabilities : { repositoryIdentity : true } ,
211+ } ) ,
190212 } ) ,
191213 ) ,
192214 Effect . provideService (
@@ -252,6 +274,84 @@ describe("releaseManagedTunnelOnShutdown", () => {
252274 Effect . scoped ,
253275 ) ;
254276
277+ it . effect . each ( [
278+ { direction : "ahead" , skewSeconds : 600 } ,
279+ { direction : "behind" , skewSeconds : - 600 } ,
280+ ] ) (
281+ "signs a link proof accepted at relay time when the host clock is $direction" ,
282+ ( { skewSeconds } ) => {
283+ const { store } = makeMemorySecretStore ( ) ;
284+
285+ return Effect . gen ( function * ( ) {
286+ const relayNow = 1_800_000_000 ;
287+ const relayIssuer = "https://relay.example.test" ;
288+ const relayKeys = yield * EnvironmentKeys . getOrCreateEnvironmentKeyPairFromSecretStore (
289+ makeMemorySecretStore ( ) . store ,
290+ ) ;
291+ const challenge = yield * signRelayJwt ( {
292+ privateKey : relayKeys . privateKey ,
293+ typ : "t3-link-challenge+jwt" ,
294+ payload : {
295+ kind : "environment_link_challenge" ,
296+ iss : relayIssuer ,
297+ aud : relayIssuer ,
298+ sub : "user-123" ,
299+ jti : "challenge-jti" ,
300+ iat : relayNow ,
301+ exp : relayNow + 300 ,
302+ notificationsEnabled : true ,
303+ liveActivitiesEnabled : false ,
304+ managedTunnelsEnabled : false ,
305+ } ,
306+ } ) ;
307+ yield * TestClock . setTime ( ( relayNow + skewSeconds ) * 1_000 ) ;
308+ const link = yield * CloudLink . CloudLink ;
309+ const proof = yield * link
310+ . linkProof (
311+ {
312+ challenge,
313+ relayIssuer,
314+ endpoint : {
315+ httpBaseUrl : "http://127.0.0.1:3773" ,
316+ wsBaseUrl : "ws://127.0.0.1:3773" ,
317+ providerKind : "manual" ,
318+ } ,
319+ origin : { localHttpHost : "127.0.0.1" , localHttpPort : 3773 } ,
320+ } ,
321+ HttpServerRequest . fromWeb (
322+ new Request ( "http://127.0.0.1:3773/api/t3-connect/link-proof" ) ,
323+ ) ,
324+ )
325+ . pipe (
326+ Effect . provideService ( EnvironmentAuthenticatedPrincipal , {
327+ sessionId : AuthSessionId . make ( "test-session" ) ,
328+ subject : "test" ,
329+ method : "bearer-access-token" ,
330+ scopes : new Set < AuthEnvironmentScope > ( ) ,
331+ } ) ,
332+ ) ;
333+ const environmentKeys =
334+ yield * EnvironmentKeys . getOrCreateEnvironmentKeyPairFromSecretStore ( store ) ;
335+ yield * TestClock . setTime ( relayNow * 1_000 ) ;
336+ const verified = yield * verifyRelayJwt ( RelayEnvironmentLinkProofPayload , {
337+ publicKey : environmentKeys . publicKey ,
338+ token : proof ,
339+ typ : RELAY_LINK_PROOF_TYP ,
340+ issuer : "t3-env:env_123" ,
341+ audience : relayIssuer ,
342+ } ) ;
343+
344+ expect ( verified ) . toMatchObject ( {
345+ iat : relayNow ,
346+ exp : relayNow + 300 ,
347+ challenge,
348+ environmentId : "env_123" ,
349+ scopes : [ "agent_activity_notifications" ] ,
350+ } ) ;
351+ } ) . pipe ( provideReleaseHarness ( { store, applyConfigCalls : [ ] , requests : [ ] } ) ) ;
352+ } ,
353+ ) ;
354+
255355 const managedLinkSecrets = [
256356 [ CLOUD_ENDPOINT_RUNTIME_CONFIG , "runtime-config" ] ,
257357 [ CLOUD_ENDPOINT_CONFIRMED_ORIGIN , "confirmed-origin" ] ,
0 commit comments