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'
@@ -10,14 +11,21 @@ export interface TestActionsDeps {
1011 receivingASE : MockASE
1112}
1213
14+ interface InteractionArgs {
15+ clientNonce : string
16+ initialGrantUrl : string
17+ finishUri : string
18+ }
19+
1320export interface TestActions {
1421 consentInteraction (
1522 outgoingPaymentGrant : PendingGrant ,
1623 senderWalletAddress : WalletAddress
1724 ) : Promise < void >
1825 consentInteractionWithInteractRef (
1926 outgoingPaymentGrant : PendingGrant ,
20- senderWalletAddress : WalletAddress
27+ senderWalletAddress : WalletAddress ,
28+ args : InteractionArgs
2129 ) : Promise < string >
2230 admin : AdminActions
2331 openPayments : OpenPaymentsActions
@@ -29,12 +37,14 @@ export function createTestActions(deps: TestActionsDeps): TestActions {
2937 consentInteraction ( deps , outgoingPaymentGrant , senderWalletAddress ) ,
3038 consentInteractionWithInteractRef : (
3139 outgoingPaymentGrant ,
32- senderWalletAddress
40+ senderWalletAddress ,
41+ args
3342 ) =>
3443 consentInteractionWithInteractRef (
3544 deps ,
3645 outgoingPaymentGrant ,
37- senderWalletAddress
46+ senderWalletAddress ,
47+ args
3848 ) ,
3949 admin : createAdminActions ( deps ) ,
4050 openPayments : createOpenPaymentsActions ( deps )
@@ -70,7 +80,8 @@ async function consentInteraction(
7080async function consentInteractionWithInteractRef (
7181 deps : TestActionsDeps ,
7282 outgoingPaymentGrant : PendingGrant ,
73- senderWalletAddress : WalletAddress
83+ senderWalletAddress : WalletAddress ,
84+ interactionArgs : InteractionArgs
7485) : Promise < string > {
7586 const { idpSecret } = deps . sendingASE . config
7687 const { interactId, nonce, cookie } = await _startAndAcceptInteraction (
@@ -95,14 +106,49 @@ async function consentInteractionWithInteractRef(
95106
96107 const redirectURI = finishResponse . headers . get ( 'location' )
97108 assert ( redirectURI )
109+ expect ( redirectURI . startsWith ( interactionArgs . finishUri ) )
98110
99111 const url = new URL ( redirectURI )
100112 const interact_ref = url . searchParams . get ( 'interact_ref' )
113+ const hash = url . searchParams . get ( 'hash' )
114+
115+ assert ( hash )
116+ assert ( interact_ref )
117+
118+ verifyHash ( {
119+ initialGrantUrl : interactionArgs . initialGrantUrl ,
120+ clientNonce : interactionArgs . clientNonce ,
121+ interactNonce : nonce ,
122+ receivedHash : hash ,
123+ interactRef : interact_ref
124+ } )
101125 assert ( interact_ref )
102126
103127 return interact_ref
104128}
105129
130+ interface VerifyHashArgs {
131+ clientNonce : string
132+ initialGrantUrl : string
133+ receivedHash : string
134+ interactNonce : string
135+ interactRef : string
136+ }
137+
138+ async function verifyHash ( args : VerifyHashArgs ) {
139+ const {
140+ clientNonce,
141+ interactNonce,
142+ interactRef,
143+ initialGrantUrl,
144+ receivedHash
145+ } = args
146+ const data = `${ clientNonce } \n${ interactNonce } \n${ interactRef } \n${ initialGrantUrl } `
147+ const hash = createHash ( 'sha-256' ) . update ( data ) . digest ( 'base64' )
148+
149+ expect ( hash ) . toBe ( receivedHash )
150+ }
151+
106152async function _startAndAcceptInteraction (
107153 deps : TestActionsDeps ,
108154 outgoingPaymentGrant : PendingGrant ,
0 commit comments