From fbf501e727ffe37c30a0ebe038dfb8ae900be749 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 13 Oct 2021 14:26:18 -0300 Subject: fixing tos request --- packages/taler-wallet-core/src/common.ts | 1 + .../taler-wallet-core/src/operations/exchanges.ts | 32 +++++++++++++++---- .../taler-wallet-core/src/operations/refresh.ts | 2 +- .../taler-wallet-core/src/operations/withdraw.ts | 37 ---------------------- packages/taler-wallet-core/src/wallet.ts | 22 +++++++------ 5 files changed, 40 insertions(+), 54 deletions(-) (limited to 'packages/taler-wallet-core/src') diff --git a/packages/taler-wallet-core/src/common.ts b/packages/taler-wallet-core/src/common.ts index b0b975e7b..dd8542def 100644 --- a/packages/taler-wallet-core/src/common.ts +++ b/packages/taler-wallet-core/src/common.ts @@ -71,6 +71,7 @@ export interface ExchangeOperations { updateExchangeFromUrl( ws: InternalWalletState, baseUrl: string, + acceptedFormat?: string[], forceNow?: boolean, ): Promise<{ exchange: ExchangeRecord; diff --git a/packages/taler-wallet-core/src/operations/exchanges.ts b/packages/taler-wallet-core/src/operations/exchanges.ts index fc776c81f..629957efb 100644 --- a/packages/taler-wallet-core/src/operations/exchanges.ts +++ b/packages/taler-wallet-core/src/operations/exchanges.ts @@ -278,6 +278,7 @@ async function downloadExchangeWithWireInfo( export async function updateExchangeFromUrl( ws: InternalWalletState, baseUrl: string, + acceptedFormat?: string[], forceNow = false, ): Promise<{ exchange: ExchangeRecord; @@ -286,7 +287,7 @@ export async function updateExchangeFromUrl( const onOpErr = (e: TalerErrorDetails): Promise => handleExchangeUpdateError(ws, baseUrl, e); return await guardOperationException( - () => updateExchangeFromUrlImpl(ws, baseUrl, forceNow), + () => updateExchangeFromUrlImpl(ws, baseUrl, acceptedFormat, forceNow), onOpErr, ); } @@ -411,6 +412,7 @@ async function downloadKeysInfo( async function updateExchangeFromUrlImpl( ws: InternalWalletState, baseUrl: string, + acceptedFormat?: string[], forceNow = false, ): Promise<{ exchange: ExchangeRecord; @@ -468,12 +470,28 @@ async function updateExchangeFromUrlImpl( logger.info("finished validating exchange /wire info"); - const tosDownload = await downloadExchangeWithTermsOfService( - baseUrl, - ws.http, - timeout, - "text/plain" - ); + let tosFound: ExchangeTosDownloadResult | undefined; + //Remove this when exchange supports multiple content-type in accept header + if (acceptedFormat) for (const format of acceptedFormat) { + const resp = await downloadExchangeWithTermsOfService( + baseUrl, + ws.http, + timeout, + format + ); + if (resp.tosContentType === format) { + tosFound = resp + break + } + } + // If none of the specified format was found try text/plain + const tosDownload = tosFound !== undefined ? tosFound : + await downloadExchangeWithTermsOfService( + baseUrl, + ws.http, + timeout, + "text/plain" + ); let recoupGroupId: string | undefined = undefined; diff --git a/packages/taler-wallet-core/src/operations/refresh.ts b/packages/taler-wallet-core/src/operations/refresh.ts index 85de813dc..144514e1c 100644 --- a/packages/taler-wallet-core/src/operations/refresh.ts +++ b/packages/taler-wallet-core/src/operations/refresh.ts @@ -920,7 +920,7 @@ export async function autoRefresh( exchangeBaseUrl: string, ): Promise { logger.info(`doing auto-refresh check for '${exchangeBaseUrl}'`); - await updateExchangeFromUrl(ws, exchangeBaseUrl, true); + await updateExchangeFromUrl(ws, exchangeBaseUrl, undefined, true); let minCheckThreshold = timestampAddDuration( getTimestampNow(), durationFromSpec({ days: 1 }), diff --git a/packages/taler-wallet-core/src/operations/withdraw.ts b/packages/taler-wallet-core/src/operations/withdraw.ts index f63723535..620ad88be 100644 --- a/packages/taler-wallet-core/src/operations/withdraw.ts +++ b/packages/taler-wallet-core/src/operations/withdraw.ts @@ -66,8 +66,6 @@ import { WALLET_BANK_INTEGRATION_PROTOCOL_VERSION, WALLET_EXCHANGE_PROTOCOL_VERSION, } from "../versions.js"; -import { stringify } from "querystring"; -import { downloadExchangeWithTermsOfService, ExchangeTosDownloadResult } from "./exchanges"; /** * Logger for this file. @@ -133,11 +131,6 @@ export interface ExchangeWithdrawDetails { */ termsOfServiceAccepted: boolean; - /** - * Tos - */ - tosRequested: ExchangeTosDownloadResult | undefined - /** * The exchange is trusted directly. */ @@ -937,7 +930,6 @@ export async function getExchangeWithdrawalInfo( ws: InternalWalletState, baseUrl: string, amount: AmountJson, - tosAcceptedFormat?: string[], ): Promise { const { exchange, @@ -1004,34 +996,6 @@ export async function getExchangeWithdrawalInfo( } } - const noTosDownloaded = - exchangeDetails.termsOfServiceContentType === undefined || - exchangeDetails.termsOfServiceAcceptedEtag === undefined || - exchangeDetails.termsOfServiceText === undefined; - - let tosFound: ExchangeTosDownloadResult | undefined = noTosDownloaded ? undefined : { - tosContentType: exchangeDetails.termsOfServiceContentType!, - tosEtag: exchangeDetails.termsOfServiceAcceptedEtag!, - tosText: exchangeDetails.termsOfServiceText!, - }; - - try { - if (tosAcceptedFormat) for (const format of tosAcceptedFormat) { - const resp = await downloadExchangeWithTermsOfService( - exchangeDetails.exchangeBaseUrl, - ws.http, - { d_ms: 1000 }, - format - ); - if (resp.tosContentType === format) { - tosFound = resp - break - } - } - } catch (e) { - tosFound = undefined - } - const withdrawFee = Amounts.sub( selectedDenoms.totalWithdrawCost, selectedDenoms.totalCoinValue, @@ -1054,7 +1018,6 @@ export async function getExchangeWithdrawalInfo( walletVersion: WALLET_EXCHANGE_PROTOCOL_VERSION, withdrawFee, termsOfServiceAccepted: tosAccepted, - tosRequested: tosFound }; return ret; } diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts index a0da3d356..253a69df3 100644 --- a/packages/taler-wallet-core/src/wallet.ts +++ b/packages/taler-wallet-core/src/wallet.ts @@ -217,7 +217,7 @@ async function processOnePendingOperation( logger.trace(`running pending ${JSON.stringify(pending, undefined, 2)}`); switch (pending.type) { case PendingTaskType.ExchangeUpdate: - await updateExchangeFromUrl(ws, pending.exchangeBaseUrl, forceNow); + await updateExchangeFromUrl(ws, pending.exchangeBaseUrl, undefined, forceNow); break; case PendingTaskType.Refresh: await processRefreshGroup(ws, pending.refreshGroupId, forceNow); @@ -452,20 +452,24 @@ async function acceptManualWithdrawal( async function getExchangeTos( ws: InternalWalletState, exchangeBaseUrl: string, + acceptedFormat?: string[], ): Promise { - const { exchange, exchangeDetails } = await updateExchangeFromUrl( + const { exchangeDetails } = await updateExchangeFromUrl( ws, exchangeBaseUrl, + acceptedFormat, ); - const tos = exchangeDetails.termsOfServiceText; + const content = exchangeDetails.termsOfServiceText; const currentEtag = exchangeDetails.termsOfServiceLastEtag; - if (!tos || !currentEtag) { + const contentType = exchangeDetails.termsOfServiceContentType; + if (content === undefined || currentEtag === undefined || contentType === undefined) { throw Error("exchange is in invalid state"); } return { acceptedEtag: exchangeDetails.termsOfServiceAcceptedEtag, currentEtag, - tos, + content, + contentType, }; } @@ -485,7 +489,7 @@ async function getExchanges( if (!dp) { continue; } - const { currency, masterPublicKey } = dp; + const { currency } = dp; const exchangeDetails = await getExchangeDetails(tx, r.baseUrl); if (!exchangeDetails) { continue; @@ -684,7 +688,7 @@ async function dispatchRequestInternal( } case "addExchange": { const req = codecForAddExchangeRequest().decode(payload); - await updateExchangeFromUrl(ws, req.exchangeBaseUrl, req.forceUpdate); + await updateExchangeFromUrl(ws, req.exchangeBaseUrl, undefined, req.forceUpdate); return {}; } case "listExchanges": { @@ -696,7 +700,7 @@ async function dispatchRequestInternal( } case "getExchangeWithdrawalInfo": { const req = codecForGetExchangeWithdrawalInfo().decode(payload); - return await getExchangeWithdrawalInfo(ws, req.exchangeBaseUrl, req.amount, req.tosAcceptedFormat); + return await getExchangeWithdrawalInfo(ws, req.exchangeBaseUrl, req.amount); } case "acceptManualWithdrawal": { const req = codecForAcceptManualWithdrawalRequet().decode(payload); @@ -744,7 +748,7 @@ async function dispatchRequestInternal( } case "getExchangeTos": { const req = codecForGetExchangeTosRequest().decode(payload); - return getExchangeTos(ws, req.exchangeBaseUrl); + return getExchangeTos(ws, req.exchangeBaseUrl , req.acceptedFormat); } case "retryPendingNow": { await runPending(ws, true); -- cgit v1.2.3