From d9c0332004d01642bc5158d7a81b5357ef4aff6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20=C3=81valos?= Date: Mon, 27 May 2024 14:10:40 -0600 Subject: anastasis: switch to new http fetch function --- packages/anastasis-core/src/index.ts | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'packages') diff --git a/packages/anastasis-core/src/index.ts b/packages/anastasis-core/src/index.ts index 05fa4a49f..c4ead05a7 100644 --- a/packages/anastasis-core/src/index.ts +++ b/packages/anastasis-core/src/index.ts @@ -43,7 +43,7 @@ import { URL, j2s, } from "@gnu-taler/taler-util"; -import { HttpResponse } from "@gnu-taler/taler-util/http"; +import { HttpResponse, createPlatformHttpLib } from "@gnu-taler/taler-util/http"; import { anastasisData } from "./anastasis-data.js"; import { codecForChallengeInstructionMessage, @@ -139,6 +139,11 @@ export * from "./challenge-feedback-types.js"; const logger = new Logger("anastasis-core:index.ts"); +const http = createPlatformHttpLib({ + enableThrottling: true, + requireTls: false, +}); + const ANASTASIS_HTTP_HEADER_POLICY_META_DATA = "Anastasis-Policy-Meta-Data"; function getContinents(): ContinentInfo[] { @@ -279,9 +284,9 @@ async function getProviderInfo( providerBaseUrl: string, ): Promise { // FIXME: Use a reasonable timeout here. - let resp: Response; + let resp: HttpResponse; try { - resp = await fetch(new URL("config", providerBaseUrl).href); + resp = await http.fetch(new URL("config", providerBaseUrl).href); } catch (e) { console.warn( "Encountered an HTTP error whilst trying to get the provider's config: ", @@ -293,7 +298,7 @@ async function getProviderInfo( hint: "request to anastasis provider failed", }; } - if (!resp.ok) { + if (resp.status < 200 && resp.status >= 300) { console.warn("Got bad response code whilst getting provider config", resp); return { status: "error", @@ -556,7 +561,7 @@ async function uploadSecret( // FIXME: Get this from the params reqUrl.searchParams.set("timeout_ms", "500"); } - const resp = await fetch(reqUrl.href, { + const resp = await http.fetch(reqUrl.href, { method: "POST", headers: { "content-type": "application/json", @@ -646,7 +651,7 @@ async function uploadSecret( reqUrl.searchParams.set("timeout_ms", "500"); } logger.info(`uploading policy to ${prov.provider_url}`); - const resp = await fetch(reqUrl.href, { + const resp = await http.fetch(reqUrl.href, { method: "POST", headers: { "Anastasis-Policy-Signature": encodeCrock(sig), @@ -757,14 +762,14 @@ async function downloadPolicyFromProvider( const acctKeypair = accountKeypairDerive(userId); const reqUrl = new URL(`policy/${acctKeypair.pub}`, providerUrl); reqUrl.searchParams.set("version", `${version}`); - const resp = await fetch(reqUrl.href); + const resp = await http.fetch(reqUrl.href); if (resp.status !== 200) { logger.info( `Could not download policy from provider ${providerUrl}, status ${resp.status}`, ); return undefined; } - const body = await resp.arrayBuffer(); + const body = await resp.bytes(); const bodyDecrypted = await decryptRecoveryDocument( userId, encodeCrock(body), @@ -981,10 +986,10 @@ async function requestTruth( const hresp = await getResponseHash(truth, solveRequest); - let resp: Response; + let resp: HttpResponse; try { - resp = await fetch(url.href, { + resp = await http.fetch(url.href, { method: "POST", headers: { Accept: "application/json", @@ -1022,7 +1027,7 @@ async function requestTruth( truth.provider_salt, ); - const respBody = new Uint8Array(await resp.arrayBuffer()); + const respBody = new Uint8Array(await resp.bytes()); const keyShare = await decryptKeyShare( encodeCrock(respBody), userId, @@ -1138,10 +1143,10 @@ async function selectChallenge( } } - let resp: Response; + let resp: HttpResponse; try { - resp = await fetch(url.href, { + resp = await http.fetch(url.href, { method: "POST", headers: { Accept: "application/json", @@ -1859,7 +1864,7 @@ export async function discoverPolicies( ); const acctKeypair = accountKeypairDerive(userId); const reqUrl = new URL(`policy/${acctKeypair.pub}/meta`, providerUrl); - const resp = await fetch(reqUrl.href); + const resp = await http.fetch(reqUrl.href); if (resp.status !== 200) { logger.warn(`Could not fetch policy metadate from ${reqUrl.href}`); continue; -- cgit v1.2.3