1+ // @effect -diagnostics-next-line nodeBuiltinImport:off -- Effect's Crypto has no generateKeyPairSync.
2+ import * as NodeCrypto from "node:crypto" ;
13import { EnvironmentId } from "@t3tools/contracts" ;
4+ import { RELAY_MINT_REQUEST_TYP , signRelayJwt } from "@t3tools/shared/relayJwt" ;
25import { assert , it } from "@effect/vitest" ;
6+ import * as DateTime from "effect/DateTime" ;
37import * as Effect from "effect/Effect" ;
48import * as Fiber from "effect/Fiber" ;
59import * as Layer from "effect/Layer" ;
610import * as Option from "effect/Option" ;
711import * as FetchHttpClient from "effect/http/FetchHttpClient" ;
812import { HttpClient , HttpServer } from "effect/http" ;
913import * as NetAddress from "effect/net/NetAddress" ;
10- import * as NodeCrypto from "@effect/platform-node/NodeCrypto" ;
14+ import * as NodePlatformCrypto from "@effect/platform-node/NodeCrypto" ;
1115import * as NodeServices from "@effect/platform-node/NodeServices" ;
1216
1317import * as EnvironmentKeys from "./environmentKeys.ts" ;
@@ -22,6 +26,7 @@ import * as ConnectReachability from "./ConnectReachability.ts";
2226import * as ManagedEndpointRuntime from "./ManagedEndpointRuntime.ts" ;
2327import {
2428 CLOUD_LINKED_USER_ID ,
29+ CLOUD_MINT_PUBLIC_KEY ,
2530 HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET ,
2631 PUBLISH_AGENT_ACTIVITY_SECRET ,
2732 RELAY_ENVIRONMENT_CREDENTIAL_SECRET ,
@@ -31,6 +36,11 @@ import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
3136
3237const encode = ( value : string ) => new TextEncoder ( ) . encode ( value ) ;
3338
39+ const cloudMintKeys = NodeCrypto . generateKeyPairSync ( "ed25519" , {
40+ privateKeyEncoding : { format : "pem" , type : "pkcs8" } ,
41+ publicKeyEncoding : { format : "pem" , type : "spki" } ,
42+ } ) ;
43+
3444const withService = < A , E > (
3545 options : {
3646 readonly failHoldWrite ?: boolean ;
@@ -45,8 +55,10 @@ const withService = <A, E>(
4555 } ,
4656 body : ( input : {
4757 readonly preferences : { readonly update : CloudLink . CloudLink [ "Service" ] [ "updatePreferences" ] } ;
58+ readonly mintCredential : CloudLink . CloudLink [ "Service" ] [ "mintCredential" ] ;
4859 readonly stored : Map < string , Uint8Array > ;
4960 readonly relayCalls : Array < boolean > ;
61+ readonly pairingLinkLabels : Array < string | undefined > ;
5062 } ) => Effect . Effect < A , E , never > ,
5163) =>
5264 Effect . gen ( function * ( ) {
@@ -55,8 +67,11 @@ const withService = <A, E>(
5567 [ RELAY_ENVIRONMENT_CREDENTIAL_SECRET , encode ( "credential" ) ] ,
5668 [ HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET , encode ( "false" ) ] ,
5769 [ PUBLISH_AGENT_ACTIVITY_SECRET , encode ( "false" ) ] ,
70+ [ CLOUD_MINT_PUBLIC_KEY , encode ( cloudMintKeys . publicKey ) ] ,
71+ [ CLOUD_LINKED_USER_ID , encode ( "cloud-user-1" ) ] ,
5872 ] ) ;
5973 const relayCalls : Array < boolean > = [ ] ;
74+ const pairingLinkLabels : Array < string | undefined > = [ ] ;
6075 const fetch : typeof globalThis . fetch = Object . assign (
6176 ( _input : Parameters < typeof globalThis . fetch > [ 0 ] , init ?: RequestInit ) => {
6277 const payload = JSON . parse ( String ( init ?. body ) ) as { holdWebhooksWhileOffline : boolean } ;
@@ -93,6 +108,7 @@ const withService = <A, E>(
93108 } ) ,
94109 )
95110 : Effect . sync ( ( ) => void stored . set ( name , value ) ) ,
111+ create : ( name , value ) => Effect . sync ( ( ) => void stored . set ( name , value ) ) ,
96112 } ) ,
97113 Layer . mock ( ServerEnvironment . ServerEnvironment ) ( {
98114 getEnvironmentId : Effect . succeed ( EnvironmentId . make ( "environment-1" ) ) ,
@@ -103,7 +119,20 @@ const withService = <A, E>(
103119 Layer . mock ( ConnectReachability . ConnectReachability ) ( {
104120 latest : Effect . succeedNone ,
105121 } ) ,
106- Layer . mock ( EnvironmentAuth . EnvironmentAuth ) ( { } ) ,
122+ Layer . mock ( EnvironmentAuth . EnvironmentAuth ) ( {
123+ createPairingLink : ( input ) =>
124+ Effect . sync ( ( ) => {
125+ pairingLinkLabels . push ( input ?. label ) ;
126+ return {
127+ id : "pairing-1" ,
128+ credential : "pairing-credential" ,
129+ scopes : input ?. scopes ?? [ ] ,
130+ subject : input ?. subject ?? "" ,
131+ createdAt : DateTime . makeUnsafe ( 0 ) ,
132+ expiresAt : DateTime . makeUnsafe ( 120_000 ) ,
133+ } ;
134+ } ) ,
135+ } ) ,
107136 Layer . mock ( CliTokenManager . CloudCliTokenManager ) ( { } ) ,
108137 Layer . mock ( HttpServer . HttpServer ) ( {
109138 address : NetAddress . inetAddressFromIpStringUnsafe ( "127.0.0.1" , 3773 ) ,
@@ -114,16 +143,27 @@ const withService = <A, E>(
114143 HttpClient . make ( ( ) => Effect . die ( "unused" ) ) ,
115144 ) ,
116145 NodeServices . layer ,
117- NodeCrypto . layer ,
146+ NodePlatformCrypto . layer ,
118147 ) ;
119148 return yield * Effect . gen ( function * ( ) {
120149 const link = yield * CloudLink . CloudLink ;
121- return yield * body ( { preferences : { update : link . updatePreferences } , stored, relayCalls } ) ;
150+ return yield * body ( {
151+ preferences : { update : link . updatePreferences } ,
152+ mintCredential : link . mintCredential ,
153+ stored,
154+ relayCalls,
155+ pairingLinkLabels,
156+ } ) ;
122157 } ) . pipe (
123158 Effect . provide (
124159 CloudLink . layer . pipe (
125- // These tests never sign proofs; the mocked secret store cannot create a key pair.
126- Layer . provide ( Layer . mock ( EnvironmentKeys . EnvironmentSigningKey ) ( { publicKey : "" } ) ) ,
160+ // The mocked secret store cannot create a key pair, and no test reads the signature.
161+ Layer . provide (
162+ Layer . mock ( EnvironmentKeys . EnvironmentSigningKey ) ( {
163+ publicKey : "" ,
164+ sign : ( ) => Effect . succeed ( "environment-signed-proof" ) ,
165+ } ) ,
166+ ) ,
127167 Layer . provide ( layerDependencies ) ,
128168 ) ,
129169 ) ,
@@ -257,6 +297,32 @@ it.effect("an update the client walks away from still finishes", () => {
257297 ) ;
258298} ) ;
259299
300+ it . effect ( 'names T3 Connect sessions "T3 Connect" in authorized clients' , ( ) =>
301+ withService ( { } , ( { mintCredential, pairingLinkLabels } ) =>
302+ Effect . gen ( function * ( ) {
303+ const proof = yield * signRelayJwt ( {
304+ privateKey : cloudMintKeys . privateKey ,
305+ typ : RELAY_MINT_REQUEST_TYP ,
306+ payload : {
307+ iss : "https://relay.test" ,
308+ aud : "t3-env:environment-1" ,
309+ sub : "cloud-user-1" ,
310+ jti : "mint-jti-1" ,
311+ iat : 0 ,
312+ exp : 60 ,
313+ environmentId : "environment-1" ,
314+ clientProofKeyThumbprint : "client-jkt" ,
315+ cnf : { jkt : "client-jkt" } ,
316+ nonce : "mint-nonce-1" ,
317+ scope : [ "environment:connect" ] ,
318+ } ,
319+ } ) . pipe ( Effect . provide ( NodePlatformCrypto . layerSignature ) ) ;
320+ yield * mintCredential ( { proof } ) ;
321+ assert . deepEqual ( pairingLinkLabels , [ "T3 Connect" ] ) ;
322+ } ) ,
323+ ) ,
324+ ) ;
325+
260326it . effect ( "unlinks on startup and refuses to link when the policy turns Connect off" , ( ) =>
261327 Effect . gen ( function * ( ) {
262328 const stored = new Map < string , Uint8Array > ( [
@@ -297,7 +363,7 @@ it.effect("unlinks on startup and refuses to link when the policy turns Connect
297363 HttpClient . make ( ( ) => Effect . die ( "unused" ) ) ,
298364 ) ,
299365 NodeServices . layer ,
300- NodeCrypto . layer ,
366+ NodePlatformCrypto . layer ,
301367 ) ;
302368 yield * Effect . gen ( function * ( ) {
303369 const link = yield * CloudLink . CloudLink ;
0 commit comments