From af57a404d070500ce63ed9ed0dfce721e82e4801 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Sat, 18 Jan 2020 23:32:03 +0100 Subject: [PATCH] fix and test case for "insecure" taler://refund URIs --- src/headless/integrationtest.ts | 2 ++ src/operations/pay.ts | 1 + src/operations/refund.ts | 4 ++-- src/util/taleruri-test.ts | 15 +++++++++++++-- src/util/taleruri.ts | 17 ++++++++++------- src/wallet.ts | 25 +++++++++++++++++++++++++ 6 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/headless/integrationtest.ts b/src/headless/integrationtest.ts index b00fa5ccb..ee52c4c46 100644 --- a/src/headless/integrationtest.ts +++ b/src/headless/integrationtest.ts @@ -171,6 +171,8 @@ export async function runIntegrationTest(args: IntegrationTestArgs) { Amounts.toString(refundAmount), ); + console.log("refund URI", refundUri); + await myWallet.applyRefund(refundUri); // Wait until the refund is done diff --git a/src/operations/pay.ts b/src/operations/pay.ts index 098e6e18c..c5a344186 100644 --- a/src/operations/pay.ts +++ b/src/operations/pay.ts @@ -1093,6 +1093,7 @@ export async function confirmPay( ); logger.trace("confirmPay: submitting payment after creating purchase record"); + logger.trace("purchaseRecord:", purchase); return submitPay(ws, proposalId); } diff --git a/src/operations/refund.ts b/src/operations/refund.ts index 6a96868a3..27d83230a 100644 --- a/src/operations/refund.ts +++ b/src/operations/refund.ts @@ -271,7 +271,7 @@ export async function applyRefund( ): Promise { const parseResult = parseRefundUri(talerRefundUri); - console.log("applying refund"); + console.log("applying refund", parseResult); if (!parseResult) { throw Error("invalid refund URI"); @@ -283,7 +283,7 @@ export async function applyRefund( ]); if (!purchase) { - throw Error("no purchase for the taler://refund/ URI was found"); + throw Error(`no purchase for the taler://refund/ URI (${talerRefundUri}) was found`); } console.log("processing purchase for refund"); diff --git a/src/util/taleruri-test.ts b/src/util/taleruri-test.ts index de4a90697..052581a91 100644 --- a/src/util/taleruri-test.ts +++ b/src/util/taleruri-test.ts @@ -96,7 +96,7 @@ test("taler pay url parsing: complex path prefix", t => { t.is(r1.sessionId, undefined); }); -test("taler pay url parsing: complex path prefix and instance", t => { +test("taler pay uri parsing: complex path prefix and instance", t => { const url1 = "taler://pay/example.com/mypfx%2Fpublic/foo/myorder"; const r1 = parsePayUri(url1); if (!r1) { @@ -107,7 +107,18 @@ test("taler pay url parsing: complex path prefix and instance", t => { t.is(r1.orderId, "myorder"); }); -test("taler pay url parsing: non-https #1", t => { +test("taler refund uri parsing: non-https #1", t => { + const url1 = "taler://refund/example.com/-/-/myorder?insecure=1"; + const r1 = parseRefundUri(url1); + if (!r1) { + t.fail(); + return; + } + t.is(r1.merchantBaseUrl, "http://example.com/public/"); + t.is(r1.orderId, "myorder") +}); + +test("taler pay uri parsing: non-https #1", t => { const url1 = "taler://pay/example.com/-/-/myorder?insecure=1"; const r1 = parsePayUri(url1); if (!r1) { diff --git a/src/util/taleruri.ts b/src/util/taleruri.ts index 1b4e4352b..8ad79669f 100644 --- a/src/util/taleruri.ts +++ b/src/util/taleruri.ts @@ -95,13 +95,12 @@ export function classifyTalerUri(s: string): TalerUriType { return TalerUriType.TalerNotifyReserve; } return TalerUriType.Unknown; - } export function getOrderDownloadUrl(merchantBaseUrl: string, orderId: string) { const u = new URL("proposal", merchantBaseUrl); u.searchParams.set("order_id", orderId); - return u.href + return u.href; } export function parsePayUri(s: string): PayUriResult | undefined { @@ -208,7 +207,7 @@ export function parseRefundUri(s: string): RefundUriResult | undefined { return undefined; } - const path = s.slice(pfx.length); + const [path, search] = s.slice(pfx.length).split("?"); let [host, maybePath, maybeInstance, orderId] = path.split("/"); @@ -236,10 +235,14 @@ export function parseRefundUri(s: string): RefundUriResult | undefined { maybeInstancePath = `instances/${maybeInstance}/`; } - const merchantBaseUrl = "https://" + host + - "/" + - maybePath + - maybeInstancePath + let protocol = "https"; + const searchParams = new URLSearchParams(search); + if (searchParams.get("insecure") === "1") { + protocol = "http"; + } + + const merchantBaseUrl = + `${protocol}://${host}/` + maybePath + maybeInstancePath; return { merchantBaseUrl, diff --git a/src/wallet.ts b/src/wallet.ts index a89d2d567..32a92cee0 100644 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -241,6 +241,31 @@ export class Wallet { * returns without resolving to an exception. */ public async runUntilDone(): Promise { + const p = new Promise((resolve, reject) => { + // Run this asynchronously + this.addNotificationListener(n => { + if ( + n.type === NotificationType.WaitingForRetry && + n.numGivingLiveness == 0 + ) { + logger.trace("no liveness-giving operations left, returning"); + resolve(); + } + }); + this.runRetryLoop().catch(e => { + console.log("exception in wallet retry loop"); + reject(e); + }); + }); + await p; + } + + /** + * Run the wallet until there are no more pending operations that give + * liveness left. The wallet will be in a stopped state when this function + * returns without resolving to an exception. + */ + public async runUntilDoneAndStop(): Promise { const p = new Promise((resolve, reject) => { // Run this asynchronously this.addNotificationListener(n => {