@@ -6,6 +6,7 @@ import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hos
66
77import {
88 AuthAccessTokenType ,
9+ AuthStandardClientScopes ,
910 AuthEnvironmentBootstrapTokenType ,
1011 AuthTokenExchangeGrantType ,
1112 CommandId ,
@@ -63,6 +64,7 @@ import * as Option from "effect/Option";
6364import * as Path from "effect/Path" ;
6465import * as PubSub from "effect/PubSub" ;
6566import * as Ref from "effect/Ref" ;
67+ import * as Queue from "effect/Queue" ;
6668import * as Schema from "effect/Schema" ;
6769import * as Stream from "effect/Stream" ;
6870import * as TestClock from "effect/testing/TestClock" ;
@@ -152,6 +154,7 @@ import * as ReviewService from "./review/ReviewService.ts";
152154import * as SourceControlRepositoryService from "./sourceControl/SourceControlRepositoryService.ts" ;
153155import * as ServerSecretStore from "./auth/ServerSecretStore.ts" ;
154156import * as EnvironmentAuth from "./auth/EnvironmentAuth.ts" ;
157+ import * as PairingGrantStore from "./auth/PairingGrantStore.ts" ;
155158import * as CloudManagedEndpointRuntime from "./cloud/ManagedEndpointRuntime.ts" ;
156159import * as CloudCliTokenManager from "./cloud/CliTokenManager.ts" ;
157160import * as ProcessDiagnostics from "./diagnostics/ProcessDiagnostics.ts" ;
@@ -482,6 +485,7 @@ const makeBrowserOtlpPayload = (spanName: string) =>
482485 } ) ;
483486
484487const buildAppUnderTest = ( options ?: {
488+ onPairingChangesSubscribed ?: Effect . Effect < void > ;
485489 config ?: Partial < ServerConfig . ServerConfig [ "Service" ] > ;
486490 layers ?: {
487491 keybindings ?: Partial < Keybindings . Keybindings [ "Service" ] > ;
@@ -1108,6 +1112,24 @@ const buildAppUnderTest = (options?: {
11081112 ...options ?. layers ?. cloudCliTokenManager ,
11091113 } ) ,
11101114 ) ,
1115+ Layer . updateService ( PairingGrantStore . PairingGrantStore , ( grants ) => {
1116+ const subscribed = options ?. onPairingChangesSubscribed ;
1117+ if ( ! subscribed ) return grants ;
1118+ return {
1119+ ...grants ,
1120+ streamChanges : Stream . unwrap (
1121+ Effect . gen ( function * ( ) {
1122+ const changes = yield * Queue . unbounded < PairingGrantStore . BootstrapCredentialChange > ( ) ;
1123+ yield * grants . streamChanges . pipe (
1124+ Stream . runForEach ( ( change ) => Queue . offer ( changes , change ) ) ,
1125+ Effect . forkScoped ( { startImmediately : true } ) ,
1126+ ) ;
1127+ yield * subscribed ;
1128+ return Stream . fromQueue ( changes ) ;
1129+ } ) ,
1130+ ) ,
1131+ } ;
1132+ } ) ,
11111133 Layer . provideMerge ( makeAuthTestLayer ( ) ) ,
11121134 Layer . provideMerge ( ServerSecretStore . layer ) ,
11131135 Layer . provide ( workspaceAndProjectServicesLayer ) ,
@@ -1134,16 +1156,19 @@ const parseSessionCookieFromWsUrl = (
11341156 } ;
11351157} ;
11361158
1137- const wsRpcProtocolLayer = ( wsUrl : string ) => {
1159+ const wsRpcProtocolLayer = ( wsUrl : string , onMessage ?: ( message : string ) => void ) => {
11381160 const { cookie, url } = parseSessionCookieFromWsUrl ( wsUrl ) ;
11391161 const webSocketConstructorLayer = Layer . succeed (
11401162 Socket . WebSocketConstructor ,
1141- ( socketUrl , protocols ) =>
1142- new NodeSocket . NodeWS . WebSocket (
1163+ ( socketUrl , protocols ) => {
1164+ const socket = new NodeSocket . NodeWS . WebSocket (
11431165 socketUrl ,
11441166 protocols ,
11451167 cookie ? { headers : { cookie } } : undefined ,
1146- ) as unknown as globalThis . WebSocket ,
1168+ ) ;
1169+ if ( onMessage ) socket . on ( "message" , ( data ) => onMessage ( data . toString ( ) ) ) ;
1170+ return socket as unknown as globalThis . WebSocket ;
1171+ } ,
11471172 ) ;
11481173
11491174 return RpcClient . layerProtocolSocket ( ) . pipe (
@@ -1159,7 +1184,8 @@ type WsRpcClient =
11591184const withWsRpcClient = < A , E , R > (
11601185 wsUrl : string ,
11611186 f : ( client : WsRpcClient ) => Effect . Effect < A , E , R > ,
1162- ) => makeWsRpcClient . pipe ( Effect . flatMap ( f ) , Effect . provide ( wsRpcProtocolLayer ( wsUrl ) ) ) ;
1187+ onMessage ?: ( message : string ) => void ,
1188+ ) => makeWsRpcClient . pipe ( Effect . flatMap ( f ) , Effect . provide ( wsRpcProtocolLayer ( wsUrl , onMessage ) ) ) ;
11631189
11641190const appendSessionCookieToWsUrl = ( url : string , sessionCookieHeader : string ) => {
11651191 const isAbsoluteUrl = / ^ [ a - z A - Z ] [ a - z A - Z \d + . - ] * : / . test ( url ) ;
@@ -3935,6 +3961,128 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
39353961 } ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
39363962 ) ;
39373963
3964+ it . effect ( "returns only pairing metadata to access-read HTTP sessions" , ( ) =>
3965+ Effect . gen ( function * ( ) {
3966+ yield * buildAppUnderTest ( ) ;
3967+ const reader = yield * exchangeAccessToken ( defaultDesktopBootstrapToken , {
3968+ scope : "access:read" ,
3969+ } ) ;
3970+ assert . equal ( reader . response . status , 200 ) ;
3971+ assert . equal ( reader . body . scope , "access:read" ) ;
3972+ const createdResponse = yield * HttpClient . post ( "/api/auth/pairing-token" , {
3973+ headers : { cookie : yield * getAuthenticatedSessionCookieHeader ( ) } ,
3974+ body : yield * HttpBody . json ( { label : "Synthetic phone" } ) ,
3975+ } ) ;
3976+ const created = ( yield * createdResponse . json ) as { id : string ; credential : string } ;
3977+ assert . equal ( createdResponse . status , 200 ) ;
3978+ const response = yield * HttpClient . get ( "/api/auth/pairing-links" , {
3979+ headers : { authorization : `Bearer ${ reader . body . access_token ?? "" } ` } ,
3980+ } ) ;
3981+ assert . equal ( response . status , 200 ) ;
3982+ const responseText = yield * response . text ;
3983+ assert . notInclude ( responseText , '"credential"' ) ;
3984+ assert . notInclude ( responseText , created . credential ) ;
3985+ const links = yield * responseJsonEffect <
3986+ ReadonlyArray < {
3987+ readonly id : string ;
3988+ readonly label ?: string ;
3989+ readonly scopes : ReadonlyArray < string > ;
3990+ } >
3991+ > ( response ) ;
3992+ const listed = links . find ( ( link ) => link . id === created . id ) ;
3993+ assert . isDefined ( listed ) ;
3994+ assert . deepInclude ( listed , {
3995+ label : "Synthetic phone" ,
3996+ scopes : [ ...AuthStandardClientScopes ] ,
3997+ } ) ;
3998+
3999+ const unauthorizedCreate = yield * HttpClient . post ( "/api/auth/pairing-token" , {
4000+ headers : { authorization : `Bearer ${ reader . body . access_token ?? "" } ` } ,
4001+ body : yield * HttpBody . json ( { } ) ,
4002+ } ) ;
4003+ assert . equal ( unauthorizedCreate . status , 403 ) ;
4004+ const idExchange = yield * exchangeAccessToken ( created . id , { scope : "terminal:operate" } ) ;
4005+ assert . equal ( idExchange . response . status , 401 ) ;
4006+ const authorized = yield * exchangeAccessToken ( created . credential , {
4007+ scope : AuthStandardClientScopes . join ( " " ) ,
4008+ } ) ;
4009+ assert . equal ( authorized . response . status , 200 ) ;
4010+ assert . equal ( authorized . body . scope , AuthStandardClientScopes . join ( " " ) ) ;
4011+ const reused = yield * exchangeAccessToken ( created . credential , { scope : "terminal:operate" } ) ;
4012+ assert . equal ( reused . response . status , 401 ) ;
4013+ } ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
4014+ ) ;
4015+
4016+ it . effect ( "returns only pairing metadata in access-read WebSocket snapshots and updates" , ( ) =>
4017+ Effect . gen ( function * ( ) {
4018+ const changesSubscribed = yield * Deferred . make < void > ( ) ;
4019+ yield * buildAppUnderTest ( {
4020+ onPairingChangesSubscribed : Deferred . succeed ( changesSubscribed , undefined ) . pipe (
4021+ Effect . asVoid ,
4022+ ) ,
4023+ } ) ;
4024+ const ownerCookie = yield * getAuthenticatedSessionCookieHeader ( ) ;
4025+ const createLink = Effect . gen ( function * ( ) {
4026+ const response = yield * HttpClient . post ( "/api/auth/pairing-token" , {
4027+ headers : { cookie : ownerCookie } ,
4028+ body : yield * HttpBody . json ( { } ) ,
4029+ } ) ;
4030+ assert . equal ( response . status , 200 ) ;
4031+ return ( yield * response . json ) as { id : string ; credential : string } ;
4032+ } ) ;
4033+ const initialLink = yield * createLink ;
4034+ const reader = yield * exchangeAccessToken ( defaultDesktopBootstrapToken , {
4035+ scope : "access:read" ,
4036+ } ) ;
4037+ assert . equal ( reader . body . scope , "access:read" ) ;
4038+ const ticketResponse = yield * HttpClient . post ( "/api/auth/websocket-ticket" , {
4039+ headers : { authorization : `Bearer ${ reader . body . access_token ?? "" } ` } ,
4040+ } ) ;
4041+ assert . equal ( ticketResponse . status , 200 ) ;
4042+ const { ticket } = ( yield * ticketResponse . json ) as { ticket : string } ;
4043+ const wsUrl = `${ yield * getWsServerUrl ( "/ws" , { authenticated : false } ) } ?wsTicket=${ encodeURIComponent ( ticket ) } ` ;
4044+ const frames : string [ ] = [ ] ;
4045+ yield * withWsRpcClient (
4046+ wsUrl ,
4047+ ( client ) =>
4048+ Effect . gen ( function * ( ) {
4049+ const snapshotReceived = yield * Deferred . make < void > ( ) ;
4050+ const eventsFiber = yield * client . subscribeAuthAccess ( { } ) . pipe (
4051+ Stream . tap ( ( event ) =>
4052+ event . type === "snapshot"
4053+ ? Deferred . succeed ( snapshotReceived , undefined )
4054+ : Effect . void ,
4055+ ) ,
4056+ Stream . takeUntil ( ( event ) => event . type === "pairingLinkUpserted" ) ,
4057+ Stream . runCollect ,
4058+ Effect . forkChild ,
4059+ ) ;
4060+ yield * Deferred . await ( snapshotReceived ) ;
4061+ yield * Deferred . await ( changesSubscribed ) ;
4062+ const liveLink = yield * createLink ;
4063+ const events = yield * Fiber . join ( eventsFiber ) ;
4064+ const snapshot = events . find ( ( event ) => event . type === "snapshot" ) ;
4065+ const update = events . find ( ( event ) => event . type === "pairingLinkUpserted" ) ;
4066+ assert . isDefined ( snapshot ) ;
4067+ assert . isDefined ( update ) ;
4068+ assert . isTrue (
4069+ snapshot ?. payload . pairingLinks . some ( ( link ) => link . id === initialLink . id ) ,
4070+ ) ;
4071+ assert . equal ( update ?. payload . id , liveLink . id ) ;
4072+ // Inspect the wire frames so client schema decoding cannot hide a leak.
4073+ assert . notInclude ( frames . join ( "" ) , '"credential"' ) ;
4074+ assert . notInclude ( frames . join ( "" ) , initialLink . credential ) ;
4075+ assert . notInclude ( frames . join ( "" ) , liveLink . credential ) ;
4076+ const paired = yield * exchangeAccessToken ( liveLink . credential , {
4077+ scope : AuthStandardClientScopes . join ( " " ) ,
4078+ } ) ;
4079+ assert . equal ( paired . response . status , 200 ) ;
4080+ } ) ,
4081+ ( frame ) => frames . push ( frame ) ,
4082+ ) ;
4083+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeHttpServer . layerTest ) ) ,
4084+ ) ;
4085+
39384086 it . effect ( "lists and revokes pairing links for access management sessions" , ( ) =>
39394087 Effect . gen ( function * ( ) {
39404088 yield * buildAppUnderTest ( {
@@ -3962,7 +4110,6 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
39624110 } ) ;
39634111 const listedLinks = ( yield * listResponse . json ) as ReadonlyArray < {
39644112 readonly id : string ;
3965- readonly credential : string ;
39664113 } > ;
39674114
39684115 const revokeResponse = yield * HttpClient . post ( "/api/auth/pairing-links/revoke" , {
0 commit comments