@@ -13,6 +13,7 @@ import {
1313 type RelayLinkProofRequest ,
1414} from "@t3tools/contracts/relay" ;
1515import * as Effect from "effect/Effect" ;
16+ import * as TestClock from "effect/testing/TestClock" ;
1617import * as Layer from "effect/Layer" ;
1718import * as Schema from "effect/Schema" ;
1819import { HttpClient , HttpServer } from "effect/http" ;
@@ -22,8 +23,7 @@ import * as HttpRouter from "effect/http/HttpRouter";
2223import * as HttpApi from "effect/http-api/HttpApi" ;
2324import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder" ;
2425import * as NetAddress from "effect/net/NetAddress" ;
25- import { decodeJwt , importSPKI , jwtVerify } from "jose" ;
26- import { RELAY_LINK_PROOF_TYP } from "@t3tools/shared/relayJwt" ;
26+ import { decodeRelayJwt , RELAY_LINK_PROOF_TYP , verifyRelayJwt } from "@t3tools/shared/relayJwt" ;
2727
2828import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts" ;
2929import * as AuthHttp from "../auth/http.ts" ;
@@ -52,7 +52,7 @@ const proofRequest: RelayLinkProofRequest = {
5252 } ,
5353 origin : { localHttpHost : "127.0.0.1" , localHttpPort : 3773 } ,
5454} ;
55- const decodeLinkProofPayload = Schema . decodeUnknownSync ( RelayEnvironmentLinkProofPayload ) ;
55+ const decodeLinkProofPayload = Schema . decodeUnknownEffect ( RelayEnvironmentLinkProofPayload ) ;
5656
5757class LinkProofTestApi extends HttpApi . make ( "environment" ) . add (
5858 EnvironmentHttpApi . groups . auth ,
@@ -112,7 +112,7 @@ const layerLinkProofRoutes = HttpApiBuilder.layer(LinkProofTestApi).pipe(
112112 Layer . provideMerge ( Layer . mergeAll ( NodeServices . layer , NodeCrypto . layer ) ) ,
113113) ;
114114
115- const withLinkProofClient = async (
115+ const withLinkProofClient = async < A = void > (
116116 body : ( client : {
117117 post : (
118118 path : string ,
@@ -121,8 +121,8 @@ const withLinkProofClient = async (
121121 origin ?: string ,
122122 ) => Promise < Response > ;
123123 exchange : ( credential : string , scope ?: string ) => Promise < string > ;
124- } ) => Promise < void > ,
125- ) => {
124+ } ) => Promise < A > ,
125+ ) : Promise < A > => {
126126 const { handler, dispose } = HttpRouter . toWebHandler ( layerLinkProofRoutes , {
127127 disableLogger : true ,
128128 } ) ;
@@ -157,39 +157,50 @@ const withLinkProofClient = async (
157157 return payload . access_token ;
158158 } ;
159159 try {
160- await body ( { post, exchange } ) ;
160+ return await body ( { post, exchange } ) ;
161161 } finally {
162162 await dispose ( ) ;
163163 }
164164} ;
165165
166166describe ( "link proof over a non-loopback desktop transport" , ( ) => {
167- it ( "signs a loopback tunnel origin after desktop bootstrap token exchange over a non-loopback host" , ( ) =>
168- withLinkProofClient ( async ( { post, exchange } ) => {
169- const token = await exchange ( DESKTOP_BOOTSTRAP_TOKEN ) ;
170- const response = await post ( "/api/connect/link-proof" , proofRequest , {
171- authorization : `Bearer ${ token } ` ,
172- } ) ;
173- expect ( response . status ) . toBe ( 200 ) ;
174- const proof = ( await response . json ( ) ) as string ;
175- const payload = decodeLinkProofPayload ( decodeJwt ( proof ) ) ;
176- const verified = await jwtVerify (
177- proof ,
178- await importSPKI ( payload . environmentPublicKey , "EdDSA" ) ,
179- {
180- algorithms : [ "EdDSA" ] ,
167+ it . effect (
168+ "signs a loopback tunnel origin after desktop bootstrap token exchange over a non-loopback host" ,
169+ ( ) =>
170+ Effect . gen ( function * ( ) {
171+ const response = yield * Effect . promise ( ( ) =>
172+ withLinkProofClient ( async ( { post, exchange } ) => {
173+ const token = await exchange ( DESKTOP_BOOTSTRAP_TOKEN ) ;
174+ const response = await post ( "/api/connect/link-proof" , proofRequest , {
175+ authorization : `Bearer ${ token } ` ,
176+ } ) ;
177+ return {
178+ status : response . status ,
179+ cacheControl : response . headers . get ( "cache-control" ) ,
180+ proof : ( await response . json ( ) ) as string ,
181+ } ;
182+ } ) ,
183+ ) ;
184+ expect ( response . status ) . toBe ( 200 ) ;
185+ expect ( response . cacheControl ) . toBe ( "no-store" ) ;
186+ const unverified = yield * decodeRelayJwt ( response . proof ) . pipe (
187+ Effect . flatMap ( decodeLinkProofPayload ) ,
188+ ) ;
189+ yield * TestClock . setTime ( unverified . iat * 1_000 ) ;
190+ const verified = yield * verifyRelayJwt ( RelayEnvironmentLinkProofPayload , {
191+ publicKey : unverified . environmentPublicKey ,
192+ token : response . proof ,
181193 typ : RELAY_LINK_PROOF_TYP ,
182194 issuer : "t3-env:wsl-environment" ,
183195 audience : proofRequest . relayIssuer ,
184- } ,
185- ) ;
186- expect ( verified . payload ) . toMatchObject ( {
187- origin : proofRequest . origin ,
188- endpoint : proofRequest . endpoint ,
189- challenge : proofRequest . challenge ,
190- } ) ;
191- expect ( response . headers . get ( "cache-control" ) ) . toBe ( "no-store" ) ;
192- } ) ) ;
196+ } ) ;
197+ expect ( verified ) . toMatchObject ( {
198+ origin : proofRequest . origin ,
199+ endpoint : proofRequest . endpoint ,
200+ challenge : proofRequest . challenge ,
201+ } ) ;
202+ } ) . pipe ( Effect . provide ( NodeCrypto . layerSignature ) ) ,
203+ ) ;
193204
194205 it ( "does not pass desktop trust to a paired client or accept client-supplied provenance" , ( ) =>
195206 withLinkProofClient ( async ( { post, exchange } ) => {
0 commit comments