From 4489e11005556511ca6fc798aaef2b1e1ad9bce1 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 28 Oct 2021 15:51:14 -0300 Subject: fix for issue #7056, not all the actions/state has been checked --- packages/anastasis-core/src/index.ts | 92 ++++++++++++++++++++-------- packages/anastasis-core/src/reducer-types.ts | 6 +- 2 files changed, 71 insertions(+), 27 deletions(-) diff --git a/packages/anastasis-core/src/index.ts b/packages/anastasis-core/src/index.ts index c9e2bcf36..a3367a3e6 100644 --- a/packages/anastasis-core/src/index.ts +++ b/packages/anastasis-core/src/index.ts @@ -2,8 +2,6 @@ import { AmountString, buildSigPS, bytesToString, - Codec, - codecForAny, decodeCrock, eddsaSign, encodeCrock, @@ -34,7 +32,6 @@ import { BackupStates, ContinentInfo, CountryInfo, - MethodSpec, Policy, PolicyProvider, RecoveryInformation, @@ -61,8 +58,6 @@ import { PolicySalt, TruthSalt, secureAnswerHash, - TruthKey, - TruthUuid, UserIdentifier, userIdentifierDerive, typedArrayConcat, @@ -74,7 +69,7 @@ import { import { unzlibSync, zlibSync } from "fflate"; import { EscrowMethod, RecoveryDocument } from "./recovery-document-types.js"; -const { fetch, Request, Response, Headers } = fetchPonyfill({}); +const { fetch } = fetchPonyfill({}); export * from "./reducer-types.js"; export * as validators from './validators.js'; @@ -119,7 +114,7 @@ async function backupSelectCountry( state: ReducerStateBackup, countryCode: string, currencies: string[], -): Promise { +): Promise { const country = anastasisData.countriesList.countries.find( (x) => x.code === countryCode, ); @@ -130,26 +125,51 @@ async function backupSelectCountry( }; } + return { + ...state, + selected_country: countryCode, + currencies, + }; +} + +async function backupSelectCountryNext( + state: ReducerStateBackup, +): Promise { + + if (!state.selected_country) { + return { + code: TalerErrorCode.ANASTASIS_REDUCER_STATE_INVALID, + hint: "no country selected", + }; + } + if (!state.currencies) { + return { + code: TalerErrorCode.ANASTASIS_REDUCER_STATE_INVALID, + hint: "no currencies selected", + }; + } + const providers: { [x: string]: {} } = {}; for (const prov of anastasisData.providersList.anastasis_provider) { - if (currencies.includes(prov.currency)) { + if (state.currencies.includes(prov.currency)) { providers[prov.url] = {}; } } - const ra = (anastasisData.countryDetails as any)[countryCode] + const ra = (anastasisData.countryDetails as any)[state.selected_country] .required_attributes; return { ...state, backup_state: BackupStates.UserAttributesCollecting, - selected_country: countryCode, - currencies, + selected_country: state.selected_country, + currencies: state.currencies, required_attributes: ra, authentication_providers: providers, }; } + async function recoverySelectCountry( state: ReducerStateRecovery, countryCode: string, @@ -165,21 +185,43 @@ async function recoverySelectCountry( }; } + return { + ...state, + selected_country: countryCode, + currencies, + }; +} + +async function recoverySelectCountryNext( + state: ReducerStateRecovery, +): Promise { + + if (!state.selected_country) { + return { + code: TalerErrorCode.ANASTASIS_REDUCER_STATE_INVALID, + hint: "no country selected", + }; + } + if (!state.currencies) { + return { + code: TalerErrorCode.ANASTASIS_REDUCER_STATE_INVALID, + hint: "no currencies selected", + }; + } + const providers: { [x: string]: {} } = {}; for (const prov of anastasisData.providersList.anastasis_provider) { - if (currencies.includes(prov.currency)) { + if (state.currencies.includes(prov.currency)) { providers[prov.url] = {}; } } - const ra = (anastasisData.countryDetails as any)[countryCode] + const ra = (anastasisData.countryDetails as any)[state.selected_country] .required_attributes; return { ...state, recovery_state: RecoveryStates.UserAttributesCollecting, - selected_country: countryCode, - currencies, required_attributes: ra, authentication_providers: providers, }; @@ -419,7 +461,7 @@ async function compressRecoveryDoc(rd: any): Promise { } async function uncompressRecoveryDoc(zippedRd: Uint8Array): Promise { - const header = zippedRd.slice(0, 4); + // const header = zippedRd.slice(0, 4); const data = zippedRd.slice(4); const res = unzlibSync(data); return JSON.parse(bytesToString(res)); @@ -601,13 +643,13 @@ async function uploadSecret( let policyExpiration: Timestamp = { t_ms: 0 }; try { policyVersion = Number(resp.headers.get("Anastasis-Version") ?? "0"); - } catch (e) {} + } catch (e) { } try { policyExpiration = { t_ms: 1000 * Number(resp.headers.get("Anastasis-Policy-Expiration") ?? "0"), }; - } catch (e) {} + } catch (e) { } successDetails[prov.provider_url] = { policy_version: policyVersion, policy_expiration: policyExpiration, @@ -629,8 +671,8 @@ async function downloadPolicy( state: ReducerStateRecovery, ): Promise { const providerUrls = Object.keys(state.authentication_providers ?? {}); - let foundRecoveryInfo: RecoveryInternalData | undefined = undefined; - let recoveryDoc: RecoveryDocument | undefined = undefined; + let foundRecoveryInfo: RecoveryInternalData | undefined; + let recoveryDoc: RecoveryDocument | undefined; const newProviderStatus: { [url: string]: AuthenticationProviderStatusOk } = {}; const userAttributes = state.identity_attributes!; @@ -666,7 +708,7 @@ async function downloadPolicy( let policyVersion = 0; try { policyVersion = Number(resp.headers.get("Anastasis-Version") ?? "0"); - } catch (e) {} + } catch (e) { } foundRecoveryInfo = { provider_url: url, secret_name: rd.secret_name ?? "", @@ -906,7 +948,9 @@ export async function reduceAction( }; } const currencies = args.currencies; - return backupSelectCountry(state, countryCode, currencies); + return backupSelectCountry(state, countryCode, currencies) + } else if (action === "next") { + return backupSelectCountryNext(state); } else { return { code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID, @@ -1098,6 +1142,8 @@ export async function reduceAction( } const currencies = args.currencies; return recoverySelectCountry(state, countryCode, currencies); + } else if (action === "next") { + return recoverySelectCountryNext(state); } else { return { code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID, @@ -1170,7 +1216,6 @@ export async function reduceAction( if (state.recovery_state === RecoveryStates.ChallengeSolving) { if (action === "back") { - const ta: ActionArgsSelectChallenge = args; return { ...state, selected_challenge_uuid: undefined, @@ -1189,7 +1234,6 @@ export async function reduceAction( if (state.recovery_state === RecoveryStates.RecoveryFinished) { if (action === "back") { - const ta: ActionArgsSelectChallenge = args; return { ...state, selected_challenge_uuid: undefined, diff --git a/packages/anastasis-core/src/reducer-types.ts b/packages/anastasis-core/src/reducer-types.ts index 2e1154fca..354d1024d 100644 --- a/packages/anastasis-core/src/reducer-types.ts +++ b/packages/anastasis-core/src/reducer-types.ts @@ -47,7 +47,7 @@ export interface ReducerStateBackup { code?: undefined; currencies?: string[]; continents?: ContinentInfo[]; - countries?: any; + countries?: CountryInfo[]; identity_attributes?: { [n: string]: string }; authentication_providers?: { [url: string]: AuthenticationProviderStatus }; authentication_methods?: AuthMethod[]; @@ -129,8 +129,8 @@ export interface ReducerStateRecovery { identity_attributes?: { [n: string]: string }; - continents?: any; - countries?: any; + continents?: ContinentInfo[]; + countries?: CountryInfo[]; selected_continent?: string; selected_country?: string; -- cgit v1.2.3