diff options
author | Florian Dold <florian@dold.me> | 2024-07-15 17:32:26 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2024-07-15 17:32:32 +0200 |
commit | 87066659bf4c62a06bfcca2fbd6c3a81ed4fef40 (patch) | |
tree | 5300143eebd707d1f881e6c12df7a6c2922357da /packages/taler-util/src/http-client/exchange.ts | |
parent | be412f419efd37dd558753a5f79a687b4672d90f (diff) | |
download | wallet-core-87066659bf4c62a06bfcca2fbd6c3a81ed4fef40.tar.xz |
harness,util,others: check version in /config responses
Diffstat (limited to 'packages/taler-util/src/http-client/exchange.ts')
-rw-r--r-- | packages/taler-util/src/http-client/exchange.ts | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/packages/taler-util/src/http-client/exchange.ts b/packages/taler-util/src/http-client/exchange.ts index 7e145e0b6..866a8577d 100644 --- a/packages/taler-util/src/http-client/exchange.ts +++ b/packages/taler-util/src/http-client/exchange.ts @@ -1,10 +1,16 @@ -import { HttpRequestLibrary, readTalerErrorResponse } from "../http-common.js"; +import { + HttpRequestLibrary, + readSuccessResponseJsonOrThrow, + readTalerErrorResponse, +} from "../http-common.js"; import { HttpStatusCode } from "../http-status-codes.js"; import { createPlatformHttpLib } from "../http.js"; import { LibtoolVersion } from "../libtool-version.js"; import { hash } from "../nacl-fast.js"; import { FailCasesByMethod, + OperationFail, + OperationOk, ResultByMethod, opEmptySuccess, opFixedSuccess, @@ -27,6 +33,7 @@ import { OfficerAccount, PaginationParams, SigningKey, + codecForTalerCommonConfigResponse, } from "../types-taler-common.js"; import { codecForAmlDecisionDetails, @@ -36,6 +43,8 @@ import { } from "../types-taler-exchange.js"; import { CacheEvictor, addPaginationParams, nullEvictor } from "./utils.js"; +import { TalerError } from "../errors.js"; +import { TalerErrorCode } from "../taler-error-codes.js"; import * as TalerExchangeApi from "../types-taler-exchange.js"; export type TalerExchangeResultByMethod< @@ -94,14 +103,47 @@ export class TalerExchangeHttpClient { * https://docs.taler.net/core/api-exchange.html#get--config * */ - async getConfig() { + async getConfig(): Promise< + | OperationFail<HttpStatusCode.NotFound> + | OperationOk<TalerExchangeApi.ExchangeVersionResponse> + > { const url = new URL(`config`, this.baseUrl); const resp = await this.httpLib.fetch(url.href, { method: "GET", }); switch (resp.status) { - case HttpStatusCode.Ok: - return opSuccessFromHttp(resp, codecForExchangeConfig()); + case HttpStatusCode.Ok: { + const minBody = await readSuccessResponseJsonOrThrow( + resp, + codecForTalerCommonConfigResponse(), + ); + const expectedName = "taler-exchange"; + if (minBody.name !== expectedName) { + throw TalerError.fromUncheckedDetail({ + code: TalerErrorCode.GENERIC_UNEXPECTED_REQUEST_ERROR, + requestUrl: resp.requestUrl, + httpStatusCode: resp.status, + detail: `Unexpected server component name (got ${minBody.name}, expected ${expectedName}})`, + }); + } + if (!this.isCompatible(minBody.version)) { + throw TalerError.fromUncheckedDetail({ + code: TalerErrorCode.GENERIC_CLIENT_UNSUPPORTED_PROTOCOL_VERSION, + requestUrl: resp.requestUrl, + httpStatusCode: resp.status, + detail: `Unsupported protocol version, client supports ${this.PROTOCOL_VERSION}, server supports ${minBody.version}`, + }); + } + // Now that we've checked the basic body, re-parse the full response. + const body = await readSuccessResponseJsonOrThrow( + resp, + codecForExchangeConfig(), + ); + return { + type: "ok", + body, + }; + } case HttpStatusCode.NotFound: return opKnownHttpFailure(resp.status, resp); default: |