diff options
Diffstat (limited to 'packages/taler-util')
-rw-r--r-- | packages/taler-util/src/http-client/challenger.ts | 39 | ||||
-rw-r--r-- | packages/taler-util/src/http-client/types.ts | 55 | ||||
-rw-r--r-- | packages/taler-util/src/http-impl.node.ts | 5 |
3 files changed, 46 insertions, 53 deletions
diff --git a/packages/taler-util/src/http-client/challenger.ts b/packages/taler-util/src/http-client/challenger.ts index 1c1ad28d9..e7b128b02 100644 --- a/packages/taler-util/src/http-client/challenger.ts +++ b/packages/taler-util/src/http-client/challenger.ts @@ -1,30 +1,25 @@ import { HttpRequestLibrary, readTalerErrorResponse } from "../http-common.js"; import { HttpStatusCode } from "../http-status-codes.js"; import { createPlatformHttpLib } from "../http.js"; -import { - codecForChallengeSolveResponse, - codecForOAuthErrorResponse, -} from "../index.js"; import { LibtoolVersion } from "../libtool-version.js"; import { FailCasesByMethod, - RedirectResult, ResultByMethod, - opFixedSuccess, opKnownAlternativeFailure, opKnownHttpFailure, opSuccessFromHttp, - opUnknownFailure, + opUnknownFailure } from "../operation.js"; import { AccessToken, - codecForChallengeCreateResponse, + codecForChallengeInvalidPinResponse, + codecForChallengeResponse, codecForChallengeSetupResponse, + codecForChallengeSolveResponse, codecForChallengeStatus, codecForChallengerAuthResponse, codecForChallengerInfoResponse, - codecForChallengerTermsOfServiceResponse, - codecForInvalidPinResponse, + codecForChallengerTermsOfServiceResponse } from "./types.js"; import { CacheEvictor, @@ -165,21 +160,12 @@ export class ChallengerHttpClient { await this.cacheEvictor.notifySuccess( ChallengerCacheEviction.CREATE_CHALLENGE, ); - return opSuccessFromHttp(resp, codecForChallengeCreateResponse()); + return opSuccessFromHttp(resp, codecForChallengeResponse()); } - // case HttpStatusCode.Found: - // const redirect = resp.headers.get("Location")!; - // return opFixedSuccess<RedirectResult>({ - // redirectURL: new URL(redirect), - // }); case HttpStatusCode.BadRequest: return opKnownHttpFailure(resp.status, resp); case HttpStatusCode.Forbidden: - return opKnownAlternativeFailure( - resp, - resp.status, - codecForOAuthErrorResponse(), - ); + return opKnownHttpFailure(resp.status, resp); case HttpStatusCode.NotFound: return opKnownHttpFailure(resp.status, resp); case HttpStatusCode.NotAcceptable: @@ -216,19 +202,10 @@ export class ChallengerHttpClient { ); return opSuccessFromHttp(resp, codecForChallengeSolveResponse()); } - // case HttpStatusCode.Found: - // const redirect = resp.headers.get("Location")!; - // return opFixedSuccess<RedirectResult>({ - // redirectURL: new URL(redirect), - // }); case HttpStatusCode.BadRequest: return opKnownHttpFailure(resp.status, resp); case HttpStatusCode.Forbidden: - return opKnownAlternativeFailure( - resp, - resp.status, - codecForInvalidPinResponse(), - ); + return opKnownHttpFailure(resp.status, resp); case HttpStatusCode.NotFound: return opKnownHttpFailure(resp.status, resp); case HttpStatusCode.NotAcceptable: diff --git a/packages/taler-util/src/http-client/types.ts b/packages/taler-util/src/http-client/types.ts index 3069db7e8..6e758773c 100644 --- a/packages/taler-util/src/http-client/types.ts +++ b/packages/taler-util/src/http-client/types.ts @@ -1583,26 +1583,38 @@ export const codecForChallengeStatus = .property("last_address", codecOptional(codecForMap(codecForAny()))) .property("changes_left", codecForNumber()) .build("ChallengerApi.ChallengeStatus"); + +export const codecForChallengeResponse = + (): Codec<ChallengerApi.ChallengeResponse> => + buildCodecForUnion<ChallengerApi.ChallengeResponse>() + .discriminateOn("type") + .alternative("completed", codecForChallengeRedirect()) + .alternative("created", codecForChallengeCreateResponse()) + .build("ChallengerApi.ChallengeResponse"); + export const codecForChallengeCreateResponse = (): Codec<ChallengerApi.ChallengeCreateResponse> => buildCodecForObject<ChallengerApi.ChallengeCreateResponse>() .property("attempts_left", codecForNumber()) .property("address", codecForAny()) + .property("type", codecForConstString("created")) .property("transmitted", codecForBoolean()) .property("next_tx_time", codecForString()) .build("ChallengerApi.ChallengeCreateResponse"); -export const codecForChallengeSolveResponse = - (): Codec<ChallengerApi.ChallengeSolveResponse> => - buildCodecForObject<ChallengerApi.ChallengeSolveResponse>() +export const codecForChallengeRedirect = + (): Codec<ChallengerApi.ChallengeRedirect> => + buildCodecForObject<ChallengerApi.ChallengeRedirect>() + .property("type", codecForConstString("completed")) .property("redirect_url", codecForString()) - .build("ChallengerApi.ChallengeSolveResponse"); + .build("ChallengerApi.ChallengeRedirect"); -export const codecForInvalidPinResponse = +export const codecForChallengeInvalidPinResponse = (): Codec<ChallengerApi.InvalidPinResponse> => buildCodecForObject<ChallengerApi.InvalidPinResponse>() .property("ec", codecOptional(codecForNumber())) .property("hint", codecForAny()) + .property("type", codecForConstString("pending")) .property("addresses_left", codecForNumber()) .property("pin_transmissions_left", codecForNumber()) .property("auth_attempts_left", codecForNumber()) @@ -1610,14 +1622,13 @@ export const codecForInvalidPinResponse = .property("no_challenge", codecForBoolean()) .build("ChallengerApi.InvalidPinResponse"); -export const codecForOAuthErrorResponse = - (): Codec<ChallengerApi.OAuthErrorResponse> => - buildCodecForObject<ChallengerApi.OAuthErrorResponse>() - .property("state", codecForString()) - .property("error", codecForString()) - .property("description", codecOptional(codecForString())) - .property("uri", codecOptional(codecForString())) - .build("ChallengerApi.OAuthErrorResponse"); +export const codecForChallengeSolveResponse = + (): Codec<ChallengerApi.ChallengeSolveResponse> => + buildCodecForUnion<ChallengerApi.ChallengeSolveResponse>() + .discriminateOn("type") + .alternative("completed", codecForChallengeRedirect()) + .alternative("pending", codecForChallengeInvalidPinResponse()) + .build("ChallengerApi.ChallengeSolveResponse"); export const codecForChallengerAuthResponse = (): Codec<ChallengerApi.ChallengerAuthResponse> => @@ -5411,12 +5422,16 @@ export namespace ChallengerApi { changes_left: Integer; } - export interface ChallengeSolveResponse { - // location where the user should be redirected + export type ChallengeResponse = ChallengeRedirect | ChallengeCreateResponse; + + export interface ChallengeRedirect { + type: "completed"; + // challenge is completed, use should redirect here redirect_url: string; } export interface ChallengeCreateResponse { + type: "created"; // how many more attempts are allowed, might be shown to the user, // highlighting might be appropriate for low values such as 1 or 2 (the // form will never be used if the value is zero) @@ -5435,13 +5450,11 @@ export namespace ChallengerApi { next_tx_time: string; } - export interface OAuthErrorResponse { - state: string; - error: string; - description: string | undefined; - uri: string | undefined; - } + export type ChallengeSolveResponse = ChallengeRedirect | InvalidPinResponse; + export interface InvalidPinResponse { + type: "pending"; + // numeric Taler error code, should be shown to indicate the error // compactly for reporting to developers ec?: number; diff --git a/packages/taler-util/src/http-impl.node.ts b/packages/taler-util/src/http-impl.node.ts index 77bdf575a..d27fd878d 100644 --- a/packages/taler-util/src/http-impl.node.ts +++ b/packages/taler-util/src/http-impl.node.ts @@ -181,7 +181,7 @@ export class HttpLibImpl implements HttpRequestLibrary { return arg + " '" + String(v) + "'"; } console.log( - `curl -X ${options.method} "${parsedUrl.href}" ${headers} ${ifUndefined( + `TALER_API_DEBUG: curl -X ${options.method} "${parsedUrl.href}" ${headers} ${ifUndefined( "-d", payload, )}`, @@ -236,6 +236,9 @@ export class HttpLibImpl implements HttpRequestLibrary { }, }; doCleanup(); + if (SHOW_CURL_HTTP_REQUEST) { + console.log(`TALER_API_DEBUG: ${textDecoder.decode(data)}`) + } resolve(resp); }); res.on("error", (e) => { |