11import assert from 'assert'
2+ import { createHash } from 'crypto'
23import type { MockASE } from 'test-lib'
34import { parseCookies , urlWithoutTenantId } from '../utils'
45import { WalletAddress , PendingGrant } from '@interledger/open-payments'
@@ -11,14 +12,21 @@ export interface TestActionsDeps {
1112 receivingASE : MockASE
1213}
1314
15+ interface InteractionArgs {
16+ clientNonce : string
17+ initialGrantUrl : string
18+ finishUri : string
19+ }
20+
1421export interface TestActions {
1522 consentInteraction (
1623 outgoingPaymentGrant : PendingGrant ,
1724 senderWalletAddress : WalletAddress
1825 ) : Promise < void >
1926 consentInteractionWithInteractRef (
2027 outgoingPaymentGrant : PendingGrant ,
21- senderWalletAddress : WalletAddress
28+ senderWalletAddress : WalletAddress ,
29+ args : InteractionArgs
2230 ) : Promise < string >
2331 admin : AdminActions
2432 openPayments : OpenPaymentsActions
@@ -31,12 +39,14 @@ export function createTestActions(deps: TestActionsDeps): TestActions {
3139 consentInteraction ( deps , outgoingPaymentGrant , senderWalletAddress ) ,
3240 consentInteractionWithInteractRef : (
3341 outgoingPaymentGrant ,
34- senderWalletAddress
42+ senderWalletAddress ,
43+ args
3544 ) =>
3645 consentInteractionWithInteractRef (
3746 deps ,
3847 outgoingPaymentGrant ,
39- senderWalletAddress
48+ senderWalletAddress ,
49+ args
4050 ) ,
4151 admin : createAdminActions ( deps ) ,
4252 openPayments : createOpenPaymentsActions ( deps ) ,
@@ -73,7 +83,8 @@ async function consentInteraction(
7383async function consentInteractionWithInteractRef (
7484 deps : TestActionsDeps ,
7585 outgoingPaymentGrant : PendingGrant ,
76- senderWalletAddress : WalletAddress
86+ senderWalletAddress : WalletAddress ,
87+ interactionArgs : InteractionArgs
7788) : Promise < string > {
7889 const { idpSecret } = deps . sendingASE . config
7990 const { interactId, nonce, cookie } = await _startAndAcceptInteraction (
@@ -98,14 +109,49 @@ async function consentInteractionWithInteractRef(
98109
99110 const redirectURI = finishResponse . headers . get ( 'location' )
100111 assert ( redirectURI )
112+ expect ( redirectURI . startsWith ( interactionArgs . finishUri ) )
101113
102114 const url = new URL ( redirectURI )
103115 const interact_ref = url . searchParams . get ( 'interact_ref' )
116+ const hash = url . searchParams . get ( 'hash' )
117+
118+ assert ( hash )
119+ assert ( interact_ref )
120+
121+ verifyHash ( {
122+ initialGrantUrl : interactionArgs . initialGrantUrl ,
123+ clientNonce : interactionArgs . clientNonce ,
124+ interactNonce : nonce ,
125+ receivedHash : hash ,
126+ interactRef : interact_ref
127+ } )
104128 assert ( interact_ref )
105129
106130 return interact_ref
107131}
108132
133+ interface VerifyHashArgs {
134+ clientNonce : string
135+ initialGrantUrl : string
136+ receivedHash : string
137+ interactNonce : string
138+ interactRef : string
139+ }
140+
141+ async function verifyHash ( args : VerifyHashArgs ) {
142+ const {
143+ clientNonce,
144+ interactNonce,
145+ interactRef,
146+ initialGrantUrl,
147+ receivedHash
148+ } = args
149+ const data = `${ clientNonce } \n${ interactNonce } \n${ interactRef } \n${ initialGrantUrl } `
150+ const hash = createHash ( 'sha-256' ) . update ( data ) . digest ( 'base64' )
151+
152+ expect ( hash ) . toBe ( receivedHash )
153+ }
154+
109155async function _startAndAcceptInteraction (
110156 deps : TestActionsDeps ,
111157 outgoingPaymentGrant : PendingGrant ,
0 commit comments