anastasis-core: provide reducer CLI, refactor state machine
This commit is contained in:
parent
a4cdc02e50
commit
aa78c1105e
@ -8,8 +8,8 @@
|
||||
"check": "pnpm run --filter '{packages}' --if-present test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@linaria/esbuild": "^3.0.0-beta.7",
|
||||
"@linaria/shaker": "^3.0.0-beta.7",
|
||||
"esbuild": "^0.12.21"
|
||||
"@linaria/esbuild": "^3.0.0-beta.13",
|
||||
"@linaria/shaker": "^3.0.0-beta.13",
|
||||
"esbuild": "^0.12.29"
|
||||
}
|
||||
}
|
||||
|
14
packages/anastasis-core/bin/anastasis-ts-reducer.js
Executable file
14
packages/anastasis-core/bin/anastasis-ts-reducer.js
Executable file
@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
async function r() {
|
||||
try {
|
||||
(await import("source-map-support")).install();
|
||||
} catch (e) {
|
||||
console.warn("can't load souremaps");
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
(await import("../dist/anastasis-cli.js")).reducerCliMain();
|
||||
}
|
||||
|
||||
r();
|
@ -6,8 +6,8 @@
|
||||
"module": "./lib/index.js",
|
||||
"types": "./lib/index.d.ts",
|
||||
"scripts": {
|
||||
"prepare": "tsc",
|
||||
"compile": "tsc",
|
||||
"prepare": "tsc && rollup -c",
|
||||
"compile": "tsc && rollup -c",
|
||||
"pretty": "prettier --write src",
|
||||
"test": "tsc && ava",
|
||||
"coverage": "tsc && nyc ava",
|
||||
@ -17,15 +17,23 @@
|
||||
"license": "AGPL-3-or-later",
|
||||
"type": "module",
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-commonjs": "^21.0.1",
|
||||
"@rollup/plugin-json": "^4.1.0",
|
||||
"@rollup/plugin-node-resolve": "^13.0.6",
|
||||
"ava": "^3.15.0",
|
||||
"typescript": "^4.4.3"
|
||||
"rimraf": "^3.0.2",
|
||||
"rollup": "^2.59.0",
|
||||
"rollup-plugin-sourcemaps": "^0.6.3",
|
||||
"source-map-support": "^0.5.19",
|
||||
"typescript": "^4.4.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@gnu-taler/taler-util": "workspace:^0.8.3",
|
||||
"fetch-ponyfill": "^7.1.0",
|
||||
"fflate": "^0.6.0",
|
||||
"hash-wasm": "^4.9.0",
|
||||
"node-fetch": "^3.0.0"
|
||||
"node-fetch": "^3.0.0",
|
||||
"tslib": "^2.1.0"
|
||||
},
|
||||
"ava": {
|
||||
"files": [
|
||||
|
30
packages/anastasis-core/rollup.config.js
Normal file
30
packages/anastasis-core/rollup.config.js
Normal file
@ -0,0 +1,30 @@
|
||||
// rollup.config.js
|
||||
import commonjs from "@rollup/plugin-commonjs";
|
||||
import nodeResolve from "@rollup/plugin-node-resolve";
|
||||
import json from "@rollup/plugin-json";
|
||||
import builtins from "builtin-modules";
|
||||
import sourcemaps from "rollup-plugin-sourcemaps";
|
||||
|
||||
export default {
|
||||
input: "lib/index.node.js",
|
||||
output: {
|
||||
file: "dist/anastasis-cli.js",
|
||||
format: "es",
|
||||
sourcemap: true,
|
||||
},
|
||||
external: builtins,
|
||||
plugins: [
|
||||
nodeResolve({
|
||||
preferBuiltins: true,
|
||||
}),
|
||||
|
||||
sourcemaps(),
|
||||
|
||||
commonjs({
|
||||
sourceMap: true,
|
||||
transformMixedEsModules: true,
|
||||
}),
|
||||
|
||||
json(),
|
||||
],
|
||||
};
|
64
packages/anastasis-core/src/cli.ts
Normal file
64
packages/anastasis-core/src/cli.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import { clk } from "@gnu-taler/taler-util";
|
||||
import {
|
||||
getBackupStartState,
|
||||
getRecoveryStartState,
|
||||
reduceAction,
|
||||
} from "./index.js";
|
||||
import fs from "fs";
|
||||
|
||||
export const reducerCli = clk
|
||||
.program("reducer", {
|
||||
help: "Command line interface for the GNU Taler wallet.",
|
||||
})
|
||||
.flag("initBackup", ["-b", "--backup"])
|
||||
.flag("initRecovery", ["-r", "--restore"])
|
||||
.maybeOption("argumentsJson", ["-a", "--arguments"], clk.STRING)
|
||||
.maybeArgument("action", clk.STRING)
|
||||
.maybeArgument("stateFile", clk.STRING);
|
||||
|
||||
async function read(stream: NodeJS.ReadStream): Promise<string> {
|
||||
const chunks = [];
|
||||
for await (const chunk of stream) {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
return Buffer.concat(chunks).toString("utf8");
|
||||
}
|
||||
|
||||
reducerCli.action(async (x) => {
|
||||
if (x.reducer.initBackup) {
|
||||
console.log(JSON.stringify(await getBackupStartState()));
|
||||
return;
|
||||
} else if (x.reducer.initRecovery) {
|
||||
console.log(JSON.stringify(await getRecoveryStartState()));
|
||||
return;
|
||||
}
|
||||
|
||||
const action = x.reducer.action;
|
||||
if (!action) {
|
||||
console.log("action required");
|
||||
return;
|
||||
}
|
||||
|
||||
let lastState: any;
|
||||
if (x.reducer.stateFile) {
|
||||
const s = fs.readFileSync(x.reducer.stateFile, { encoding: "utf-8" });
|
||||
lastState = JSON.parse(s);
|
||||
} else {
|
||||
const s = await read(process.stdin);
|
||||
lastState = JSON.parse(s);
|
||||
}
|
||||
|
||||
let args: any;
|
||||
if (x.reducer.argumentsJson) {
|
||||
args = JSON.parse(x.reducer.argumentsJson);
|
||||
} else {
|
||||
args = {};
|
||||
}
|
||||
|
||||
const nextState = await reduceAction(lastState, action, args);
|
||||
console.log(JSON.stringify(nextState));
|
||||
});
|
||||
|
||||
export function reducerCliMain() {
|
||||
reducerCli.run();
|
||||
}
|
2
packages/anastasis-core/src/index.node.ts
Normal file
2
packages/anastasis-core/src/index.node.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from "./index.js";
|
||||
export { reducerCliMain } from "./cli.js";
|
@ -9,6 +9,8 @@ import {
|
||||
encodeCrock,
|
||||
getRandomBytes,
|
||||
hash,
|
||||
j2s,
|
||||
Logger,
|
||||
stringToBytes,
|
||||
TalerErrorCode,
|
||||
TalerSignaturePurpose,
|
||||
@ -26,12 +28,22 @@ import {
|
||||
ActionArgEnterSecret,
|
||||
ActionArgEnterSecretName,
|
||||
ActionArgEnterUserAttributes,
|
||||
ActionArgsAddPolicy,
|
||||
ActionArgSelectContinent,
|
||||
ActionArgSelectCountry,
|
||||
ActionArgsSelectChallenge,
|
||||
ActionArgsSolveChallengeRequest,
|
||||
ActionArgsUpdateExpiration,
|
||||
AuthenticationProviderStatus,
|
||||
AuthenticationProviderStatusOk,
|
||||
AuthMethod,
|
||||
BackupStates,
|
||||
codecForActionArgEnterUserAttributes,
|
||||
codecForActionArgsAddPolicy,
|
||||
codecForActionArgSelectChallenge,
|
||||
codecForActionArgSelectContinent,
|
||||
codecForActionArgSelectCountry,
|
||||
codecForActionArgsUpdateExpiration,
|
||||
ContinentInfo,
|
||||
CountryInfo,
|
||||
MethodSpec,
|
||||
@ -46,6 +58,7 @@ import {
|
||||
ReducerStateError,
|
||||
ReducerStateRecovery,
|
||||
SuccessDetails,
|
||||
UserAttributeSpec,
|
||||
} from "./reducer-types.js";
|
||||
import fetchPonyfill from "fetch-ponyfill";
|
||||
import {
|
||||
@ -61,8 +74,6 @@ import {
|
||||
PolicySalt,
|
||||
TruthSalt,
|
||||
secureAnswerHash,
|
||||
TruthKey,
|
||||
TruthUuid,
|
||||
UserIdentifier,
|
||||
userIdentifierDerive,
|
||||
typedArrayConcat,
|
||||
@ -74,10 +85,12 @@ 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';
|
||||
export * as validators from "./validators.js";
|
||||
|
||||
const logger = new Logger("anastasis-core:index.ts");
|
||||
|
||||
function getContinents(): ContinentInfo[] {
|
||||
const continentSet = new Set<string>();
|
||||
@ -95,10 +108,40 @@ function getContinents(): ContinentInfo[] {
|
||||
return continents;
|
||||
}
|
||||
|
||||
interface ErrorDetails {
|
||||
code: TalerErrorCode;
|
||||
message?: string;
|
||||
hint?: string;
|
||||
}
|
||||
|
||||
export class ReducerError extends Error {
|
||||
constructor(public errorJson: ErrorDetails) {
|
||||
super(
|
||||
errorJson.message ??
|
||||
errorJson.hint ??
|
||||
`${TalerErrorCode[errorJson.code]}`,
|
||||
);
|
||||
|
||||
// Set the prototype explicitly.
|
||||
Object.setPrototypeOf(this, ReducerError.prototype);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get countries for a continent, abort with ReducerError
|
||||
* exception when continent doesn't exist.
|
||||
*/
|
||||
function getCountries(continent: string): CountryInfo[] {
|
||||
return anastasisData.countriesList.countries.filter(
|
||||
const countries = anastasisData.countriesList.countries.filter(
|
||||
(x) => x.continent === continent,
|
||||
);
|
||||
if (countries.length <= 0) {
|
||||
throw new ReducerError({
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_INPUT_INVALID,
|
||||
hint: "continent not found",
|
||||
});
|
||||
}
|
||||
return countries;
|
||||
}
|
||||
|
||||
export async function getBackupStartState(): Promise<ReducerStateBackup> {
|
||||
@ -115,19 +158,27 @@ export async function getRecoveryStartState(): Promise<ReducerStateRecovery> {
|
||||
};
|
||||
}
|
||||
|
||||
async function backupSelectCountry(
|
||||
state: ReducerStateBackup,
|
||||
countryCode: string,
|
||||
currencies: string[],
|
||||
): Promise<ReducerStateError | ReducerStateBackupUserAttributesCollecting> {
|
||||
async function selectCountry(
|
||||
selectedContinent: string,
|
||||
args: ActionArgSelectCountry,
|
||||
): Promise<Partial<ReducerStateBackup> & Partial<ReducerStateRecovery>> {
|
||||
const countryCode = args.country_code;
|
||||
const currencies = args.currencies;
|
||||
const country = anastasisData.countriesList.countries.find(
|
||||
(x) => x.code === countryCode,
|
||||
);
|
||||
if (!country) {
|
||||
return {
|
||||
throw new ReducerError({
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: "invalid country selected",
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
if (country.continent !== selectedContinent) {
|
||||
throw new ReducerError({
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: "selected country is not in selected continent",
|
||||
});
|
||||
}
|
||||
|
||||
const providers: { [x: string]: {} } = {};
|
||||
@ -141,8 +192,6 @@ async function backupSelectCountry(
|
||||
.required_attributes;
|
||||
|
||||
return {
|
||||
...state,
|
||||
backup_state: BackupStates.UserAttributesCollecting,
|
||||
selected_country: countryCode,
|
||||
currencies,
|
||||
required_attributes: ra,
|
||||
@ -150,38 +199,25 @@ async function backupSelectCountry(
|
||||
};
|
||||
}
|
||||
|
||||
async function backupSelectCountry(
|
||||
state: ReducerStateBackup,
|
||||
args: ActionArgSelectCountry,
|
||||
): Promise<ReducerStateError | ReducerStateBackup> {
|
||||
return {
|
||||
...state,
|
||||
...(await selectCountry(state.selected_continent!, args)),
|
||||
backup_state: BackupStates.UserAttributesCollecting,
|
||||
};
|
||||
}
|
||||
|
||||
async function recoverySelectCountry(
|
||||
state: ReducerStateRecovery,
|
||||
countryCode: string,
|
||||
currencies: string[],
|
||||
args: ActionArgSelectCountry,
|
||||
): Promise<ReducerStateError | ReducerStateRecovery> {
|
||||
const country = anastasisData.countriesList.countries.find(
|
||||
(x) => x.code === countryCode,
|
||||
);
|
||||
if (!country) {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: "invalid country selected",
|
||||
};
|
||||
}
|
||||
|
||||
const providers: { [x: string]: {} } = {};
|
||||
for (const prov of anastasisData.providersList.anastasis_provider) {
|
||||
if (currencies.includes(prov.currency)) {
|
||||
providers[prov.url] = {};
|
||||
}
|
||||
}
|
||||
|
||||
const ra = (anastasisData.countryDetails as any)[countryCode]
|
||||
.required_attributes;
|
||||
|
||||
return {
|
||||
...state,
|
||||
recovery_state: RecoveryStates.UserAttributesCollecting,
|
||||
selected_country: countryCode,
|
||||
currencies,
|
||||
required_attributes: ra,
|
||||
authentication_providers: providers,
|
||||
...(await selectCountry(state.selected_continent!, args)),
|
||||
};
|
||||
}
|
||||
|
||||
@ -231,8 +267,9 @@ async function getProviderInfo(
|
||||
|
||||
async function backupEnterUserAttributes(
|
||||
state: ReducerStateBackup,
|
||||
attributes: Record<string, string>,
|
||||
args: ActionArgEnterUserAttributes,
|
||||
): Promise<ReducerStateBackup> {
|
||||
const attributes = args.identity_attributes;
|
||||
const providerUrls = Object.keys(state.authentication_providers ?? {});
|
||||
const newProviders = state.authentication_providers ?? {};
|
||||
for (const url of providerUrls) {
|
||||
@ -336,7 +373,7 @@ function suggestPolicies(
|
||||
}
|
||||
const policies: Policy[] = [];
|
||||
const selections = enumerateSelections(numSel, numMethods);
|
||||
console.log("selections", selections);
|
||||
logger.info(`selections: ${j2s(selections)}`);
|
||||
for (const sel of selections) {
|
||||
const p = assignProviders(methods, providers, sel);
|
||||
if (p) {
|
||||
@ -409,7 +446,7 @@ async function getTruthValue(
|
||||
* Compress the recovery document and add a size header.
|
||||
*/
|
||||
async function compressRecoveryDoc(rd: any): Promise<Uint8Array> {
|
||||
console.log("recovery document", rd);
|
||||
logger.info(`recovery document: ${j2s(rd)}`);
|
||||
const docBytes = stringToBytes(JSON.stringify(rd));
|
||||
const sizeHeaderBuf = new ArrayBuffer(4);
|
||||
const dvbuf = new DataView(sizeHeaderBuf);
|
||||
@ -509,10 +546,6 @@ async function uploadSecret(
|
||||
? bytesToString(decodeCrock(authMethod.challenge))
|
||||
: undefined,
|
||||
);
|
||||
console.log(
|
||||
"encrypted key share len",
|
||||
decodeCrock(encryptedKeyShare).length,
|
||||
);
|
||||
const tur: TruthUploadRequest = {
|
||||
encrypted_truth: encryptedTruth,
|
||||
key_share_data: encryptedKeyShare,
|
||||
@ -550,8 +583,6 @@ async function uploadSecret(
|
||||
// the state, since it's possible that we'll run into
|
||||
// a provider that requests a payment.
|
||||
|
||||
console.log("policy UUIDs", policyUuids);
|
||||
|
||||
const rd: RecoveryDocument = {
|
||||
secret_name: secretName,
|
||||
encrypted_core_secret: csr.encCoreSecret,
|
||||
@ -662,7 +693,6 @@ async function downloadPolicy(
|
||||
const rd: RecoveryDocument = await uncompressRecoveryDoc(
|
||||
decodeCrock(bodyDecrypted),
|
||||
);
|
||||
console.log("rd", rd);
|
||||
let policyVersion = 0;
|
||||
try {
|
||||
policyVersion = Number(resp.headers.get("Anastasis-Version") ?? "0");
|
||||
@ -683,7 +713,6 @@ async function downloadPolicy(
|
||||
}
|
||||
const recoveryInfo: RecoveryInformation = {
|
||||
challenges: recoveryDoc.escrow_methods.map((x) => {
|
||||
console.log("providers", newProviderStatus);
|
||||
const prov = newProviderStatus[x.url] as AuthenticationProviderStatusOk;
|
||||
return {
|
||||
cost: prov.methods.find((m) => m.type === x.escrow_type)?.usage_fee!,
|
||||
@ -777,8 +806,6 @@ async function solveChallenge(
|
||||
},
|
||||
});
|
||||
|
||||
console.log(resp);
|
||||
|
||||
if (resp.status !== 200) {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_TRUTH_CHALLENGE_FAILED,
|
||||
@ -825,12 +852,12 @@ async function solveChallenge(
|
||||
|
||||
async function recoveryEnterUserAttributes(
|
||||
state: ReducerStateRecovery,
|
||||
attributes: Record<string, string>,
|
||||
args: ActionArgEnterUserAttributes,
|
||||
): Promise<ReducerStateRecovery | ReducerStateError> {
|
||||
// FIXME: validate attributes
|
||||
const st: ReducerStateRecovery = {
|
||||
...state,
|
||||
identity_attributes: attributes,
|
||||
identity_attributes: args.identity_attributes,
|
||||
};
|
||||
return downloadPolicy(st);
|
||||
}
|
||||
@ -853,8 +880,6 @@ async function selectChallenge(
|
||||
},
|
||||
});
|
||||
|
||||
console.log(resp);
|
||||
|
||||
return {
|
||||
...state,
|
||||
recovery_state: RecoveryStates.ChallengeSolving,
|
||||
@ -862,98 +887,140 @@ async function selectChallenge(
|
||||
};
|
||||
}
|
||||
|
||||
export async function reduceAction(
|
||||
state: ReducerState,
|
||||
async function backupSelectContinent(
|
||||
state: ReducerStateBackup,
|
||||
args: ActionArgSelectContinent,
|
||||
): Promise<ReducerStateBackup | ReducerStateError> {
|
||||
const countries = getCountries(args.continent);
|
||||
if (countries.length <= 0) {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_INPUT_INVALID,
|
||||
hint: "continent not found",
|
||||
};
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
backup_state: BackupStates.CountrySelecting,
|
||||
countries,
|
||||
selected_continent: args.continent,
|
||||
};
|
||||
}
|
||||
|
||||
async function recoverySelectContinent(
|
||||
state: ReducerStateRecovery,
|
||||
args: ActionArgSelectContinent,
|
||||
): Promise<ReducerStateRecovery | ReducerStateError> {
|
||||
const countries = getCountries(args.continent);
|
||||
return {
|
||||
...state,
|
||||
recovery_state: RecoveryStates.CountrySelecting,
|
||||
countries,
|
||||
selected_continent: args.continent,
|
||||
};
|
||||
}
|
||||
|
||||
interface TransitionImpl<S, T> {
|
||||
argCodec: Codec<T>;
|
||||
handler: (s: S, args: T) => Promise<S | ReducerStateError>;
|
||||
}
|
||||
|
||||
interface Transition<S, T> {
|
||||
[x: string]: TransitionImpl<S, T>;
|
||||
}
|
||||
|
||||
function transition<S, T>(
|
||||
action: string,
|
||||
args: any,
|
||||
): Promise<ReducerState> {
|
||||
console.log(`ts reducer: handling action ${action}`);
|
||||
if (state.backup_state === BackupStates.ContinentSelecting) {
|
||||
if (action === "select_continent") {
|
||||
const continent: string = args.continent;
|
||||
if (typeof continent !== "string") {
|
||||
argCodec: Codec<T>,
|
||||
handler: (s: S, args: T) => Promise<S | ReducerStateError>,
|
||||
): Transition<S, T> {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: "continent required",
|
||||
[action]: {
|
||||
argCodec,
|
||||
handler,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function transitionBackupJump(
|
||||
action: string,
|
||||
st: BackupStates,
|
||||
): Transition<ReducerStateBackup, void> {
|
||||
return {
|
||||
...state,
|
||||
backup_state: BackupStates.CountrySelecting,
|
||||
countries: getCountries(continent),
|
||||
selected_continent: continent,
|
||||
[action]: {
|
||||
argCodec: codecForAny(),
|
||||
handler: async (s, a) => ({ ...s, backup_state: st }),
|
||||
},
|
||||
};
|
||||
} else {
|
||||
}
|
||||
|
||||
function transitionRecoveryJump(
|
||||
action: string,
|
||||
st: RecoveryStates,
|
||||
): Transition<ReducerStateRecovery, void> {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}'`,
|
||||
[action]: {
|
||||
argCodec: codecForAny(),
|
||||
handler: async (s, a) => ({ ...s, recovery_state: st }),
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
if (state.backup_state === BackupStates.CountrySelecting) {
|
||||
if (action === "back") {
|
||||
return {
|
||||
...state,
|
||||
backup_state: BackupStates.ContinentSelecting,
|
||||
countries: undefined,
|
||||
};
|
||||
} else if (action === "select_country") {
|
||||
const countryCode = args.country_code;
|
||||
if (typeof countryCode !== "string") {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: "country_code required",
|
||||
};
|
||||
}
|
||||
const currencies = args.currencies;
|
||||
return backupSelectCountry(state, countryCode, currencies);
|
||||
} else {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}'`,
|
||||
};
|
||||
}
|
||||
}
|
||||
if (state.backup_state === BackupStates.UserAttributesCollecting) {
|
||||
if (action === "back") {
|
||||
return {
|
||||
...state,
|
||||
backup_state: BackupStates.CountrySelecting,
|
||||
};
|
||||
} else if (action === "enter_user_attributes") {
|
||||
const ta = args as ActionArgEnterUserAttributes;
|
||||
return backupEnterUserAttributes(state, ta.identity_attributes);
|
||||
} else {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}'`,
|
||||
};
|
||||
}
|
||||
}
|
||||
if (state.backup_state === BackupStates.AuthenticationsEditing) {
|
||||
if (action === "back") {
|
||||
return {
|
||||
...state,
|
||||
backup_state: BackupStates.UserAttributesCollecting,
|
||||
};
|
||||
} else if (action === "add_authentication") {
|
||||
const ta = args as ActionArgAddAuthentication;
|
||||
}
|
||||
|
||||
async function addAuthentication(
|
||||
state: ReducerStateBackup,
|
||||
args: ActionArgAddAuthentication,
|
||||
): Promise<ReducerStateBackup> {
|
||||
return {
|
||||
...state,
|
||||
authentication_methods: [
|
||||
...(state.authentication_methods ?? []),
|
||||
ta.authentication_method,
|
||||
args.authentication_method,
|
||||
],
|
||||
};
|
||||
} else if (action === "delete_authentication") {
|
||||
const ta = args as ActionArgDeleteAuthentication;
|
||||
}
|
||||
|
||||
async function deleteAuthentication(
|
||||
state: ReducerStateBackup,
|
||||
args: ActionArgDeleteAuthentication,
|
||||
): Promise<ReducerStateBackup> {
|
||||
const m = state.authentication_methods ?? [];
|
||||
m.splice(ta.authentication_method, 1);
|
||||
m.splice(args.authentication_method, 1);
|
||||
return {
|
||||
...state,
|
||||
authentication_methods: m,
|
||||
};
|
||||
} else if (action === "next") {
|
||||
}
|
||||
|
||||
async function deletePolicy(
|
||||
state: ReducerStateBackup,
|
||||
args: ActionArgDeletePolicy,
|
||||
): Promise<ReducerStateBackup> {
|
||||
const policies = [...(state.policies ?? [])];
|
||||
policies.splice(args.policy_index, 1);
|
||||
return {
|
||||
...state,
|
||||
policies,
|
||||
};
|
||||
}
|
||||
|
||||
async function addPolicy(
|
||||
state: ReducerStateBackup,
|
||||
args: ActionArgsAddPolicy,
|
||||
): Promise<ReducerStateBackup> {
|
||||
return {
|
||||
...state,
|
||||
policies: [
|
||||
...(state.policies ?? []),
|
||||
{
|
||||
methods: args.policy,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
async function nextFromAuthenticationsEditing(
|
||||
state: ReducerStateBackup,
|
||||
args: {},
|
||||
): Promise<ReducerStateBackup | ReducerStateError> {
|
||||
const methods = state.authentication_methods ?? [];
|
||||
const providers: ProviderInfo[] = [];
|
||||
for (const provUrl of Object.keys(state.authentication_providers ?? {})) {
|
||||
@ -974,184 +1041,43 @@ export async function reduceAction(
|
||||
});
|
||||
}
|
||||
const pol = suggestPolicies(methods, providers);
|
||||
console.log("policies", pol);
|
||||
return {
|
||||
...state,
|
||||
backup_state: BackupStates.PoliciesReviewing,
|
||||
...pol,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}'`,
|
||||
};
|
||||
}
|
||||
|
||||
async function updateUploadFees(
|
||||
state: ReducerStateBackup,
|
||||
): Promise<ReducerStateBackup | ReducerStateError> {
|
||||
for (const prov of state.policy_providers ?? []) {
|
||||
const info = state.authentication_providers![prov.provider_url];
|
||||
if (!("currency" in info)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (state.backup_state === BackupStates.PoliciesReviewing) {
|
||||
if (action === "back") {
|
||||
return { ...state, upload_fees: [] };
|
||||
}
|
||||
|
||||
async function enterSecret(
|
||||
state: ReducerStateBackup,
|
||||
args: ActionArgEnterSecret,
|
||||
): Promise<ReducerStateBackup | ReducerStateError> {
|
||||
return {
|
||||
...state,
|
||||
backup_state: BackupStates.AuthenticationsEditing,
|
||||
};
|
||||
} else if (action === "delete_policy") {
|
||||
const ta = args as ActionArgDeletePolicy;
|
||||
const policies = [...(state.policies ?? [])];
|
||||
policies.splice(ta.policy_index, 1);
|
||||
return {
|
||||
...state,
|
||||
policies,
|
||||
};
|
||||
} else if (action === "next") {
|
||||
return {
|
||||
...state,
|
||||
backup_state: BackupStates.SecretEditing,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}'`,
|
||||
};
|
||||
}
|
||||
}
|
||||
if (state.backup_state === BackupStates.SecretEditing) {
|
||||
if (action === "back") {
|
||||
return {
|
||||
...state,
|
||||
backup_state: BackupStates.PoliciesReviewing,
|
||||
};
|
||||
} else if (action === "enter_secret_name") {
|
||||
const ta = args as ActionArgEnterSecretName;
|
||||
return {
|
||||
...state,
|
||||
secret_name: ta.name,
|
||||
};
|
||||
} else if (action === "enter_secret") {
|
||||
const ta = args as ActionArgEnterSecret;
|
||||
return {
|
||||
...state,
|
||||
expiration: ta.expiration,
|
||||
expiration: args.expiration,
|
||||
core_secret: {
|
||||
mime: ta.secret.mime ?? "text/plain",
|
||||
value: ta.secret.value,
|
||||
mime: args.secret.mime ?? "text/plain",
|
||||
value: args.secret.value,
|
||||
},
|
||||
};
|
||||
} else if (action === "next") {
|
||||
return uploadSecret(state);
|
||||
} else {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}'`,
|
||||
};
|
||||
}
|
||||
}
|
||||
if (state.backup_state === BackupStates.BackupFinished) {
|
||||
if (action === "back") {
|
||||
return {
|
||||
...state,
|
||||
backup_state: BackupStates.SecretEditing,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}'`,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (state.recovery_state === RecoveryStates.ContinentSelecting) {
|
||||
if (action === "select_continent") {
|
||||
const continent: string = args.continent;
|
||||
if (typeof continent !== "string") {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: "continent required",
|
||||
};
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
recovery_state: RecoveryStates.CountrySelecting,
|
||||
countries: getCountries(continent),
|
||||
selected_continent: continent,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}'`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (state.recovery_state === RecoveryStates.CountrySelecting) {
|
||||
if (action === "back") {
|
||||
return {
|
||||
...state,
|
||||
recovery_state: RecoveryStates.ContinentSelecting,
|
||||
countries: undefined,
|
||||
};
|
||||
} else if (action === "select_country") {
|
||||
const countryCode = args.country_code;
|
||||
if (typeof countryCode !== "string") {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: "country_code required",
|
||||
};
|
||||
}
|
||||
const currencies = args.currencies;
|
||||
return recoverySelectCountry(state, countryCode, currencies);
|
||||
} else {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}'`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (state.recovery_state === RecoveryStates.UserAttributesCollecting) {
|
||||
if (action === "back") {
|
||||
return {
|
||||
...state,
|
||||
recovery_state: RecoveryStates.CountrySelecting,
|
||||
};
|
||||
} else if (action === "enter_user_attributes") {
|
||||
const ta = args as ActionArgEnterUserAttributes;
|
||||
return recoveryEnterUserAttributes(state, ta.identity_attributes);
|
||||
} else {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}'`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (state.recovery_state === RecoveryStates.SecretSelecting) {
|
||||
if (action === "back") {
|
||||
return {
|
||||
...state,
|
||||
recovery_state: RecoveryStates.UserAttributesCollecting,
|
||||
};
|
||||
} else if (action === "next") {
|
||||
return {
|
||||
...state,
|
||||
recovery_state: RecoveryStates.ChallengeSelecting,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}'`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (state.recovery_state === RecoveryStates.ChallengeSelecting) {
|
||||
if (action === "select_challenge") {
|
||||
const ta: ActionArgsSelectChallenge = args;
|
||||
return selectChallenge(state, ta);
|
||||
} else if (action === "back") {
|
||||
return {
|
||||
...state,
|
||||
recovery_state: RecoveryStates.SecretSelecting,
|
||||
};
|
||||
} else if (action === "next") {
|
||||
async function nextFromChallengeSelecting(
|
||||
state: ReducerStateRecovery,
|
||||
args: void,
|
||||
): Promise<ReducerStateRecovery | ReducerStateError> {
|
||||
const s2 = await tryRecoverSecret(state);
|
||||
if (s2.recovery_state === RecoveryStates.RecoveryFinished) {
|
||||
return s2;
|
||||
@ -1160,54 +1086,187 @@ export async function reduceAction(
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: "Not enough challenges solved",
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}'`,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (state.recovery_state === RecoveryStates.ChallengeSolving) {
|
||||
if (action === "back") {
|
||||
const ta: ActionArgsSelectChallenge = args;
|
||||
async function enterSecretName(
|
||||
state: ReducerStateBackup,
|
||||
args: ActionArgEnterSecretName,
|
||||
): Promise<ReducerStateBackup | ReducerStateError> {
|
||||
return {
|
||||
...state,
|
||||
selected_challenge_uuid: undefined,
|
||||
recovery_state: RecoveryStates.ChallengeSelecting,
|
||||
};
|
||||
} else if (action === "solve_challenge") {
|
||||
const ta: ActionArgsSolveChallengeRequest = args;
|
||||
return solveChallenge(state, ta);
|
||||
} else {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}'`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (state.recovery_state === RecoveryStates.RecoveryFinished) {
|
||||
if (action === "back") {
|
||||
const ta: ActionArgsSelectChallenge = args;
|
||||
return {
|
||||
...state,
|
||||
selected_challenge_uuid: undefined,
|
||||
recovery_state: RecoveryStates.ChallengeSelecting,
|
||||
};
|
||||
} else if (action === "solve_challenge") {
|
||||
const ta: ActionArgsSolveChallengeRequest = args;
|
||||
return solveChallenge(state, ta);
|
||||
} else {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}'`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: "Reducer action invalid",
|
||||
secret_name: args.name,
|
||||
};
|
||||
}
|
||||
|
||||
async function updateSecretExpiration(
|
||||
state: ReducerStateBackup,
|
||||
args: ActionArgsUpdateExpiration,
|
||||
): Promise<ReducerStateBackup | ReducerStateError> {
|
||||
// FIXME: implement!
|
||||
return {
|
||||
...state,
|
||||
expiration: args.expiration,
|
||||
};
|
||||
}
|
||||
|
||||
const backupTransitions: Record<
|
||||
BackupStates,
|
||||
Transition<ReducerStateBackup, any>
|
||||
> = {
|
||||
[BackupStates.ContinentSelecting]: {
|
||||
...transition(
|
||||
"select_continent",
|
||||
codecForActionArgSelectContinent(),
|
||||
backupSelectContinent,
|
||||
),
|
||||
},
|
||||
[BackupStates.CountrySelecting]: {
|
||||
...transitionBackupJump("back", BackupStates.ContinentSelecting),
|
||||
...transition(
|
||||
"select_country",
|
||||
codecForActionArgSelectCountry(),
|
||||
backupSelectCountry,
|
||||
),
|
||||
...transition(
|
||||
"select_continent",
|
||||
codecForActionArgSelectContinent(),
|
||||
backupSelectContinent,
|
||||
),
|
||||
},
|
||||
[BackupStates.UserAttributesCollecting]: {
|
||||
...transitionBackupJump("back", BackupStates.CountrySelecting),
|
||||
...transition(
|
||||
"enter_user_attributes",
|
||||
codecForActionArgEnterUserAttributes(),
|
||||
backupEnterUserAttributes,
|
||||
),
|
||||
},
|
||||
[BackupStates.AuthenticationsEditing]: {
|
||||
...transitionBackupJump("back", BackupStates.UserAttributesCollecting),
|
||||
...transition("add_authentication", codecForAny(), addAuthentication),
|
||||
...transition("delete_authentication", codecForAny(), deleteAuthentication),
|
||||
...transition("next", codecForAny(), nextFromAuthenticationsEditing),
|
||||
},
|
||||
[BackupStates.PoliciesReviewing]: {
|
||||
...transitionBackupJump("back", BackupStates.AuthenticationsEditing),
|
||||
...transitionBackupJump("next", BackupStates.SecretEditing),
|
||||
...transition("add_policy", codecForActionArgsAddPolicy(), addPolicy),
|
||||
...transition("delete_policy", codecForAny(), deletePolicy),
|
||||
},
|
||||
[BackupStates.SecretEditing]: {
|
||||
...transitionBackupJump("back", BackupStates.PoliciesPaying),
|
||||
...transition("next", codecForAny(), uploadSecret),
|
||||
...transition("enter_secret", codecForAny(), enterSecret),
|
||||
...transition(
|
||||
"update_expiration",
|
||||
codecForActionArgsUpdateExpiration(),
|
||||
updateSecretExpiration,
|
||||
),
|
||||
...transition("enter_secret_name", codecForAny(), enterSecretName),
|
||||
},
|
||||
[BackupStates.PoliciesPaying]: {},
|
||||
[BackupStates.TruthsPaying]: {},
|
||||
[BackupStates.PoliciesPaying]: {},
|
||||
[BackupStates.BackupFinished]: {
|
||||
...transitionBackupJump("back", BackupStates.SecretEditing),
|
||||
},
|
||||
};
|
||||
|
||||
const recoveryTransitions: Record<
|
||||
RecoveryStates,
|
||||
Transition<ReducerStateRecovery, any>
|
||||
> = {
|
||||
[RecoveryStates.ContinentSelecting]: {
|
||||
...transition(
|
||||
"select_continent",
|
||||
codecForActionArgSelectContinent(),
|
||||
recoverySelectContinent,
|
||||
),
|
||||
},
|
||||
[RecoveryStates.CountrySelecting]: {
|
||||
...transitionRecoveryJump("back", RecoveryStates.ContinentSelecting),
|
||||
...transition(
|
||||
"select_country",
|
||||
codecForActionArgSelectCountry(),
|
||||
recoverySelectCountry,
|
||||
),
|
||||
...transition(
|
||||
"select_continent",
|
||||
codecForActionArgSelectContinent(),
|
||||
recoverySelectContinent,
|
||||
),
|
||||
},
|
||||
[RecoveryStates.UserAttributesCollecting]: {
|
||||
...transitionRecoveryJump("back", RecoveryStates.CountrySelecting),
|
||||
...transition(
|
||||
"enter_user_attributes",
|
||||
codecForActionArgEnterUserAttributes(),
|
||||
recoveryEnterUserAttributes,
|
||||
),
|
||||
},
|
||||
[RecoveryStates.SecretSelecting]: {
|
||||
...transitionRecoveryJump("back", RecoveryStates.UserAttributesCollecting),
|
||||
...transitionRecoveryJump("next", RecoveryStates.ChallengeSelecting),
|
||||
},
|
||||
[RecoveryStates.ChallengeSelecting]: {
|
||||
...transitionRecoveryJump("back", RecoveryStates.SecretSelecting),
|
||||
...transition(
|
||||
"select_challenge",
|
||||
codecForActionArgSelectChallenge(),
|
||||
selectChallenge,
|
||||
),
|
||||
...transition("next", codecForAny(), nextFromChallengeSelecting),
|
||||
},
|
||||
[RecoveryStates.ChallengeSolving]: {
|
||||
...transitionRecoveryJump("back", RecoveryStates.ChallengeSelecting),
|
||||
...transition("solve_challenge", codecForAny(), solveChallenge),
|
||||
},
|
||||
[RecoveryStates.ChallengePaying]: {},
|
||||
[RecoveryStates.RecoveryFinished]: {},
|
||||
};
|
||||
|
||||
export async function reduceAction(
|
||||
state: ReducerState,
|
||||
action: string,
|
||||
args: any,
|
||||
): Promise<ReducerState> {
|
||||
let h: TransitionImpl<any, any>;
|
||||
let stateName: string;
|
||||
if ("backup_state" in state && state.backup_state) {
|
||||
stateName = state.backup_state;
|
||||
h = backupTransitions[state.backup_state][action];
|
||||
} else if ("recovery_state" in state && state.recovery_state) {
|
||||
stateName = state.recovery_state;
|
||||
h = recoveryTransitions[state.recovery_state][action];
|
||||
} else {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Invalid state (needs backup_state or recovery_state)`,
|
||||
};
|
||||
}
|
||||
if (!h) {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_ACTION_INVALID,
|
||||
hint: `Unsupported action '${action}' in state '${stateName}'`,
|
||||
};
|
||||
}
|
||||
let parsedArgs: any;
|
||||
try {
|
||||
parsedArgs = h.argCodec.decode(args);
|
||||
} catch (e: any) {
|
||||
return {
|
||||
code: TalerErrorCode.ANASTASIS_REDUCER_INPUT_INVALID,
|
||||
hint: "argument validation failed",
|
||||
message: e.toString(),
|
||||
};
|
||||
}
|
||||
try {
|
||||
return await h.handler(state, parsedArgs);
|
||||
} catch (e) {
|
||||
logger.error("action handler failed");
|
||||
if (e instanceof ReducerError) {
|
||||
return e.errorJson;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,14 @@
|
||||
import { Duration, Timestamp } from "@gnu-taler/taler-util";
|
||||
import {
|
||||
AmountString,
|
||||
buildCodecForObject,
|
||||
codecForAny,
|
||||
codecForList,
|
||||
codecForNumber,
|
||||
codecForString,
|
||||
codecForTimestamp,
|
||||
Duration,
|
||||
Timestamp,
|
||||
} from "@gnu-taler/taler-util";
|
||||
import { KeyShare } from "./crypto.js";
|
||||
import { RecoveryDocument } from "./recovery-document-types.js";
|
||||
|
||||
@ -70,7 +80,9 @@ export interface ReducerStateBackup {
|
||||
|
||||
core_secret?: CoreSecret;
|
||||
|
||||
expiration?: Duration;
|
||||
expiration?: Timestamp;
|
||||
|
||||
upload_fees?: AmountString[];
|
||||
}
|
||||
|
||||
export interface AuthMethod {
|
||||
@ -94,8 +106,8 @@ export interface UserAttributeSpec {
|
||||
uuid: string;
|
||||
widget: string;
|
||||
optional?: boolean;
|
||||
'validation-regex': string | undefined;
|
||||
'validation-logic': string | undefined;
|
||||
"validation-regex": string | undefined;
|
||||
"validation-logic": string | undefined;
|
||||
}
|
||||
|
||||
export interface RecoveryInternalData {
|
||||
@ -244,6 +256,11 @@ export interface ActionArgEnterUserAttributes {
|
||||
identity_attributes: Record<string, string>;
|
||||
}
|
||||
|
||||
export const codecForActionArgEnterUserAttributes = () =>
|
||||
buildCodecForObject<ActionArgEnterUserAttributes>()
|
||||
.property("identity_attributes", codecForAny())
|
||||
.build("ActionArgEnterUserAttributes");
|
||||
|
||||
export interface ActionArgAddAuthentication {
|
||||
authentication_method: {
|
||||
type: string;
|
||||
@ -270,15 +287,69 @@ export interface ActionArgEnterSecret {
|
||||
value: string;
|
||||
mime?: string;
|
||||
};
|
||||
expiration: Duration;
|
||||
expiration: Timestamp;
|
||||
}
|
||||
|
||||
export interface ActionArgSelectContinent {
|
||||
continent: string;
|
||||
}
|
||||
|
||||
export const codecForActionArgSelectContinent = () =>
|
||||
buildCodecForObject<ActionArgSelectContinent>()
|
||||
.property("continent", codecForString())
|
||||
.build("ActionArgSelectContinent");
|
||||
|
||||
export interface ActionArgSelectCountry {
|
||||
country_code: string;
|
||||
currencies: string[];
|
||||
}
|
||||
|
||||
export const codecForActionArgSelectCountry = () =>
|
||||
buildCodecForObject<ActionArgSelectCountry>()
|
||||
.property("country_code", codecForString())
|
||||
.property("currencies", codecForList(codecForString()))
|
||||
.build("ActionArgSelectCountry");
|
||||
|
||||
export interface ActionArgsSelectChallenge {
|
||||
uuid: string;
|
||||
}
|
||||
|
||||
export const codecForActionArgSelectChallenge = () =>
|
||||
buildCodecForObject<ActionArgsSelectChallenge>()
|
||||
.property("uuid", codecForString())
|
||||
.build("ActionArgSelectChallenge");
|
||||
|
||||
export type ActionArgsSolveChallengeRequest = SolveChallengeAnswerRequest;
|
||||
|
||||
export interface SolveChallengeAnswerRequest {
|
||||
answer: string;
|
||||
}
|
||||
|
||||
export interface PolicyMember {
|
||||
authentication_method: number;
|
||||
provider: string;
|
||||
}
|
||||
|
||||
export interface ActionArgsAddPolicy {
|
||||
policy: PolicyMember[];
|
||||
}
|
||||
|
||||
export const codecForPolicyMember = () =>
|
||||
buildCodecForObject<PolicyMember>()
|
||||
.property("authentication_method", codecForNumber())
|
||||
.property("provider", codecForString())
|
||||
.build("PolicyMember");
|
||||
|
||||
export const codecForActionArgsAddPolicy = () =>
|
||||
buildCodecForObject<ActionArgsAddPolicy>()
|
||||
.property("policy", codecForList(codecForPolicyMember()))
|
||||
.build("ActionArgsAddPolicy");
|
||||
|
||||
export interface ActionArgsUpdateExpiration {
|
||||
expiration: Timestamp;
|
||||
}
|
||||
|
||||
export const codecForActionArgsUpdateExpiration = () =>
|
||||
buildCodecForObject<ActionArgsUpdateExpiration>()
|
||||
.property("expiration", codecForTimestamp)
|
||||
.build("ActionArgsUpdateExpiration");
|
||||
|
620
packages/taler-util/src/clk.ts
Normal file
620
packages/taler-util/src/clk.ts
Normal file
@ -0,0 +1,620 @@
|
||||
/*
|
||||
This file is part of GNU Taler
|
||||
(C) 2019 GNUnet e.V.
|
||||
|
||||
GNU Taler is free software; you can redistribute it and/or modify it under the
|
||||
terms of the GNU General Public License as published by the Free Software
|
||||
Foundation; either version 3, or (at your option) any later version.
|
||||
|
||||
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
/**
|
||||
* Imports.
|
||||
*/
|
||||
import process from "process";
|
||||
import path from "path";
|
||||
import readline from "readline";
|
||||
|
||||
export namespace clk {
|
||||
class Converter<T> {}
|
||||
|
||||
export const INT = new Converter<number>();
|
||||
export const STRING: Converter<string> = new Converter<string>();
|
||||
|
||||
export interface OptionArgs<T> {
|
||||
help?: string;
|
||||
default?: T;
|
||||
onPresentHandler?: (v: T) => void;
|
||||
}
|
||||
|
||||
export interface ArgumentArgs<T> {
|
||||
metavar?: string;
|
||||
help?: string;
|
||||
default?: T;
|
||||
}
|
||||
|
||||
export interface SubcommandArgs {
|
||||
help?: string;
|
||||
}
|
||||
|
||||
export interface FlagArgs {
|
||||
help?: string;
|
||||
}
|
||||
|
||||
export interface ProgramArgs {
|
||||
help?: string;
|
||||
}
|
||||
|
||||
interface ArgumentDef {
|
||||
name: string;
|
||||
conv: Converter<any>;
|
||||
args: ArgumentArgs<any>;
|
||||
required: boolean;
|
||||
}
|
||||
|
||||
interface SubcommandDef {
|
||||
commandGroup: CommandGroup<any, any>;
|
||||
name: string;
|
||||
args: SubcommandArgs;
|
||||
}
|
||||
|
||||
type ActionFn<TG> = (x: TG) => void;
|
||||
|
||||
type SubRecord<S extends keyof any, N extends keyof any, V> = {
|
||||
[Y in S]: { [X in N]: V };
|
||||
};
|
||||
|
||||
interface OptionDef {
|
||||
name: string;
|
||||
flagspec: string[];
|
||||
/**
|
||||
* Converter, only present for options, not for flags.
|
||||
*/
|
||||
conv?: Converter<any>;
|
||||
args: OptionArgs<any>;
|
||||
isFlag: boolean;
|
||||
required: boolean;
|
||||
}
|
||||
|
||||
function splitOpt(opt: string): { key: string; value?: string } {
|
||||
const idx = opt.indexOf("=");
|
||||
if (idx == -1) {
|
||||
return { key: opt };
|
||||
}
|
||||
return { key: opt.substring(0, idx), value: opt.substring(idx + 1) };
|
||||
}
|
||||
|
||||
function formatListing(key: string, value?: string): string {
|
||||
const res = " " + key;
|
||||
if (!value) {
|
||||
return res;
|
||||
}
|
||||
if (res.length >= 25) {
|
||||
return res + "\n" + " " + value;
|
||||
} else {
|
||||
return res.padEnd(24) + " " + value;
|
||||
}
|
||||
}
|
||||
|
||||
export class CommandGroup<GN extends keyof any, TG> {
|
||||
private shortOptions: { [name: string]: OptionDef } = {};
|
||||
private longOptions: { [name: string]: OptionDef } = {};
|
||||
private subcommandMap: { [name: string]: SubcommandDef } = {};
|
||||
private subcommands: SubcommandDef[] = [];
|
||||
private options: OptionDef[] = [];
|
||||
private arguments: ArgumentDef[] = [];
|
||||
|
||||
private myAction?: ActionFn<TG>;
|
||||
|
||||
constructor(
|
||||
private argKey: string,
|
||||
private name: string | null,
|
||||
private scArgs: SubcommandArgs,
|
||||
) {}
|
||||
|
||||
action(f: ActionFn<TG>): void {
|
||||
if (this.myAction) {
|
||||
throw Error("only one action supported per command");
|
||||
}
|
||||
this.myAction = f;
|
||||
}
|
||||
|
||||
requiredOption<N extends keyof any, V>(
|
||||
name: N,
|
||||
flagspec: string[],
|
||||
conv: Converter<V>,
|
||||
args: OptionArgs<V> = {},
|
||||
): CommandGroup<GN, TG & SubRecord<GN, N, V>> {
|
||||
const def: OptionDef = {
|
||||
args: args,
|
||||
conv: conv,
|
||||
flagspec: flagspec,
|
||||
isFlag: false,
|
||||
required: true,
|
||||
name: name as string,
|
||||
};
|
||||
this.options.push(def);
|
||||
for (const flag of flagspec) {
|
||||
if (flag.startsWith("--")) {
|
||||
const flagname = flag.substring(2);
|
||||
this.longOptions[flagname] = def;
|
||||
} else if (flag.startsWith("-")) {
|
||||
const flagname = flag.substring(1);
|
||||
this.shortOptions[flagname] = def;
|
||||
} else {
|
||||
throw Error("option must start with '-' or '--'");
|
||||
}
|
||||
}
|
||||
return this as any;
|
||||
}
|
||||
|
||||
maybeOption<N extends keyof any, V>(
|
||||
name: N,
|
||||
flagspec: string[],
|
||||
conv: Converter<V>,
|
||||
args: OptionArgs<V> = {},
|
||||
): CommandGroup<GN, TG & SubRecord<GN, N, V | undefined>> {
|
||||
const def: OptionDef = {
|
||||
args: args,
|
||||
conv: conv,
|
||||
flagspec: flagspec,
|
||||
isFlag: false,
|
||||
required: false,
|
||||
name: name as string,
|
||||
};
|
||||
this.options.push(def);
|
||||
for (const flag of flagspec) {
|
||||
if (flag.startsWith("--")) {
|
||||
const flagname = flag.substring(2);
|
||||
this.longOptions[flagname] = def;
|
||||
} else if (flag.startsWith("-")) {
|
||||
const flagname = flag.substring(1);
|
||||
this.shortOptions[flagname] = def;
|
||||
} else {
|
||||
throw Error("option must start with '-' or '--'");
|
||||
}
|
||||
}
|
||||
return this as any;
|
||||
}
|
||||
|
||||
requiredArgument<N extends keyof any, V>(
|
||||
name: N,
|
||||
conv: Converter<V>,
|
||||
args: ArgumentArgs<V> = {},
|
||||
): CommandGroup<GN, TG & SubRecord<GN, N, V>> {
|
||||
const argDef: ArgumentDef = {
|
||||
args: args,
|
||||
conv: conv,
|
||||
name: name as string,
|
||||
required: true,
|
||||
};
|
||||
this.arguments.push(argDef);
|
||||
return this as any;
|
||||
}
|
||||
|
||||
maybeArgument<N extends keyof any, V>(
|
||||
name: N,
|
||||
conv: Converter<V>,
|
||||
args: ArgumentArgs<V> = {},
|
||||
): CommandGroup<GN, TG & SubRecord<GN, N, V | undefined>> {
|
||||
const argDef: ArgumentDef = {
|
||||
args: args,
|
||||
conv: conv,
|
||||
name: name as string,
|
||||
required: false,
|
||||
};
|
||||
this.arguments.push(argDef);
|
||||
return this as any;
|
||||
}
|
||||
|
||||
flag<N extends string, V>(
|
||||
name: N,
|
||||
flagspec: string[],
|
||||
args: OptionArgs<V> = {},
|
||||
): CommandGroup<GN, TG & SubRecord<GN, N, boolean>> {
|
||||
const def: OptionDef = {
|
||||
args: args,
|
||||
flagspec: flagspec,
|
||||
isFlag: true,
|
||||
required: false,
|
||||
name: name as string,
|
||||
};
|
||||
this.options.push(def);
|
||||
for (const flag of flagspec) {
|
||||
if (flag.startsWith("--")) {
|
||||
const flagname = flag.substring(2);
|
||||
this.longOptions[flagname] = def;
|
||||
} else if (flag.startsWith("-")) {
|
||||
const flagname = flag.substring(1);
|
||||
this.shortOptions[flagname] = def;
|
||||
} else {
|
||||
throw Error("option must start with '-' or '--'");
|
||||
}
|
||||
}
|
||||
return this as any;
|
||||
}
|
||||
|
||||
subcommand<GN extends keyof any>(
|
||||
argKey: GN,
|
||||
name: string,
|
||||
args: SubcommandArgs = {},
|
||||
): CommandGroup<GN, TG> {
|
||||
const cg = new CommandGroup<GN, {}>(argKey as string, name, args);
|
||||
const def: SubcommandDef = {
|
||||
commandGroup: cg,
|
||||
name: name as string,
|
||||
args: args,
|
||||
};
|
||||
cg.flag("help", ["-h", "--help"], {
|
||||
help: "Show this message and exit.",
|
||||
});
|
||||
this.subcommandMap[name as string] = def;
|
||||
this.subcommands.push(def);
|
||||
this.subcommands = this.subcommands.sort((x1, x2) => {
|
||||
const a = x1.name;
|
||||
const b = x2.name;
|
||||
if (a === b) {
|
||||
return 0;
|
||||
} else if (a < b) {
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
});
|
||||
return cg as any;
|
||||
}
|
||||
|
||||
printHelp(progName: string, parents: CommandGroup<any, any>[]): void {
|
||||
let usageSpec = "";
|
||||
for (const p of parents) {
|
||||
usageSpec += (p.name ?? progName) + " ";
|
||||
if (p.arguments.length >= 1) {
|
||||
usageSpec += "<ARGS...> ";
|
||||
}
|
||||
}
|
||||
usageSpec += (this.name ?? progName) + " ";
|
||||
if (this.subcommands.length != 0) {
|
||||
usageSpec += "COMMAND ";
|
||||
}
|
||||
for (const a of this.arguments) {
|
||||
const argName = a.args.metavar ?? a.name;
|
||||
usageSpec += `<${argName}> `;
|
||||
}
|
||||
usageSpec = usageSpec.trimRight();
|
||||
console.log(`Usage: ${usageSpec}`);
|
||||
if (this.scArgs.help) {
|
||||
console.log();
|
||||
console.log(this.scArgs.help);
|
||||
}
|
||||
if (this.options.length != 0) {
|
||||
console.log();
|
||||
console.log("Options:");
|
||||
for (const opt of this.options) {
|
||||
let optSpec = opt.flagspec.join(", ");
|
||||
if (!opt.isFlag) {
|
||||
optSpec = optSpec + "=VALUE";
|
||||
}
|
||||
console.log(formatListing(optSpec, opt.args.help));
|
||||
}
|
||||
}
|
||||
|
||||
if (this.subcommands.length != 0) {
|
||||
console.log();
|
||||
console.log("Commands:");
|
||||
for (const subcmd of this.subcommands) {
|
||||
console.log(formatListing(subcmd.name, subcmd.args.help));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the (sub-)command with the given command line parameters.
|
||||
*/
|
||||
run(
|
||||
progname: string,
|
||||
parents: CommandGroup<any, any>[],
|
||||
unparsedArgs: string[],
|
||||
parsedArgs: any,
|
||||
): void {
|
||||
let posArgIndex = 0;
|
||||
let argsTerminated = false;
|
||||
let i;
|
||||
let foundSubcommand: CommandGroup<any, any> | undefined = undefined;
|
||||
const myArgs: any = (parsedArgs[this.argKey] = {});
|
||||
const foundOptions: { [name: string]: boolean } = {};
|
||||
const currentName = this.name ?? progname;
|
||||
for (i = 0; i < unparsedArgs.length; i++) {
|
||||
const argVal = unparsedArgs[i];
|
||||
if (argsTerminated == false) {
|
||||
if (argVal === "--") {
|
||||
argsTerminated = true;
|
||||
continue;
|
||||
}
|
||||
if (argVal.startsWith("--")) {
|
||||
const opt = argVal.substring(2);
|
||||
const r = splitOpt(opt);
|
||||
const d = this.longOptions[r.key];
|
||||
if (!d) {
|
||||
console.error(
|
||||
`error: unknown option '--${r.key}' for ${currentName}`,
|
||||
);
|
||||
process.exit(-1);
|
||||
throw Error("not reached");
|
||||
}
|
||||
if (d.isFlag) {
|
||||
if (r.value !== undefined) {
|
||||
console.error(`error: flag '--${r.key}' does not take a value`);
|
||||
process.exit(-1);
|
||||
throw Error("not reached");
|
||||
}
|
||||
foundOptions[d.name] = true;
|
||||
myArgs[d.name] = true;
|
||||
} else {
|
||||
if (r.value === undefined) {
|
||||
if (i === unparsedArgs.length - 1) {
|
||||
console.error(`error: option '--${r.key}' needs an argument`);
|
||||
process.exit(-1);
|
||||
throw Error("not reached");
|
||||
}
|
||||
myArgs[d.name] = unparsedArgs[i + 1];
|
||||
i++;
|
||||
} else {
|
||||
myArgs[d.name] = r.value;
|
||||
}
|
||||
foundOptions[d.name] = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (argVal.startsWith("-") && argVal != "-") {
|
||||
const optShort = argVal.substring(1);
|
||||
for (let si = 0; si < optShort.length; si++) {
|
||||
const chr = optShort[si];
|
||||
const opt = this.shortOptions[chr];
|
||||
if (!opt) {
|
||||
console.error(`error: option '-${chr}' not known`);
|
||||
process.exit(-1);
|
||||
}
|
||||
if (opt.isFlag) {
|
||||
myArgs[opt.name] = true;
|
||||
foundOptions[opt.name] = true;
|
||||
} else {
|
||||
if (si == optShort.length - 1) {
|
||||
if (i === unparsedArgs.length - 1) {
|
||||
console.error(`error: option '-${chr}' needs an argument`);
|
||||
process.exit(-1);
|
||||
throw Error("not reached");
|
||||
} else {
|
||||
myArgs[opt.name] = unparsedArgs[i + 1];
|
||||
i++;
|
||||
}
|
||||
} else {
|
||||
myArgs[opt.name] = optShort.substring(si + 1);
|
||||
}
|
||||
foundOptions[opt.name] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (this.subcommands.length != 0) {
|
||||
const subcmd = this.subcommandMap[argVal];
|
||||
if (!subcmd) {
|
||||
console.error(`error: unknown command '${argVal}'`);
|
||||
process.exit(-1);
|
||||
throw Error("not reached");
|
||||
}
|
||||
foundSubcommand = subcmd.commandGroup;
|
||||
break;
|
||||
} else {
|
||||
const d = this.arguments[posArgIndex];
|
||||
if (!d) {
|
||||
console.error(`error: too many arguments for ${currentName}`);
|
||||
process.exit(-1);
|
||||
throw Error("not reached");
|
||||
}
|
||||
myArgs[d.name] = unparsedArgs[i];
|
||||
posArgIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
if (parsedArgs[this.argKey].help) {
|
||||
this.printHelp(progname, parents);
|
||||
process.exit(0);
|
||||
throw Error("not reached");
|
||||
}
|
||||
|
||||
for (let i = posArgIndex; i < this.arguments.length; i++) {
|
||||
const d = this.arguments[i];
|
||||
if (d.required) {
|
||||
if (d.args.default !== undefined) {
|
||||
myArgs[d.name] = d.args.default;
|
||||
} else {
|
||||
console.error(
|
||||
`error: missing positional argument '${d.name}' for ${currentName}`,
|
||||
);
|
||||
process.exit(-1);
|
||||
throw Error("not reached");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const option of this.options) {
|
||||
if (option.isFlag == false && option.required == true) {
|
||||
if (!foundOptions[option.name]) {
|
||||
if (option.args.default !== undefined) {
|
||||
myArgs[option.name] = option.args.default;
|
||||
} else {
|
||||
const name = option.flagspec.join(",");
|
||||
console.error(`error: missing option '${name}'`);
|
||||
process.exit(-1);
|
||||
throw Error("not reached");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const option of this.options) {
|
||||
const ph = option.args.onPresentHandler;
|
||||
if (ph && foundOptions[option.name]) {
|
||||
ph(myArgs[option.name]);
|
||||
}
|
||||
}
|
||||
|
||||
if (foundSubcommand) {
|
||||
foundSubcommand.run(
|
||||
progname,
|
||||
Array.prototype.concat(parents, [this]),
|
||||
unparsedArgs.slice(i + 1),
|
||||
parsedArgs,
|
||||
);
|
||||
} else if (this.myAction) {
|
||||
let r;
|
||||
try {
|
||||
r = this.myAction(parsedArgs);
|
||||
} catch (e) {
|
||||
console.error(`An error occurred while running ${currentName}`);
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
}
|
||||
Promise.resolve(r).catch((e) => {
|
||||
console.error(`An error occurred while running ${currentName}`);
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
} else {
|
||||
this.printHelp(progname, parents);
|
||||
process.exit(-1);
|
||||
throw Error("not reached");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class Program<PN extends keyof any, T> {
|
||||
private mainCommand: CommandGroup<any, any>;
|
||||
|
||||
constructor(argKey: string, args: ProgramArgs = {}) {
|
||||
this.mainCommand = new CommandGroup<any, any>(argKey, null, {
|
||||
help: args.help,
|
||||
});
|
||||
this.mainCommand.flag("help", ["-h", "--help"], {
|
||||
help: "Show this message and exit.",
|
||||
});
|
||||
}
|
||||
|
||||
run(): void {
|
||||
const args = process.argv;
|
||||
if (args.length < 2) {
|
||||
console.error(
|
||||
"Error while parsing command line arguments: not enough arguments",
|
||||
);
|
||||
process.exit(-1);
|
||||
}
|
||||
const progname = path.basename(args[1]);
|
||||
const rest = args.slice(2);
|
||||
|
||||
this.mainCommand.run(progname, [], rest, {});
|
||||
}
|
||||
|
||||
subcommand<GN extends keyof any>(
|
||||
argKey: GN,
|
||||
name: string,
|
||||
args: SubcommandArgs = {},
|
||||
): CommandGroup<GN, T> {
|
||||
const cmd = this.mainCommand.subcommand(argKey, name as string, args);
|
||||
return cmd as any;
|
||||
}
|
||||
|
||||
requiredOption<N extends keyof any, V>(
|
||||
name: N,
|
||||
flagspec: string[],
|
||||
conv: Converter<V>,
|
||||
args: OptionArgs<V> = {},
|
||||
): Program<PN, T & SubRecord<PN, N, V>> {
|
||||
this.mainCommand.requiredOption(name, flagspec, conv, args);
|
||||
return this as any;
|
||||
}
|
||||
|
||||
maybeOption<N extends keyof any, V>(
|
||||
name: N,
|
||||
flagspec: string[],
|
||||
conv: Converter<V>,
|
||||
args: OptionArgs<V> = {},
|
||||
): Program<PN, T & SubRecord<PN, N, V | undefined>> {
|
||||
this.mainCommand.maybeOption(name, flagspec, conv, args);
|
||||
return this as any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a flag (option without value) to the program.
|
||||
*/
|
||||
flag<N extends string>(
|
||||
name: N,
|
||||
flagspec: string[],
|
||||
args: OptionArgs<boolean> = {},
|
||||
): Program<PN, T & SubRecord<PN, N, boolean>> {
|
||||
this.mainCommand.flag(name, flagspec, args);
|
||||
return this as any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a required positional argument to the program.
|
||||
*/
|
||||
requiredArgument<N extends keyof any, V>(
|
||||
name: N,
|
||||
conv: Converter<V>,
|
||||
args: ArgumentArgs<V> = {},
|
||||
): Program<PN, T & SubRecord<PN, N, V>> {
|
||||
this.mainCommand.requiredArgument(name, conv, args);
|
||||
return this as any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an optional argument to the program.
|
||||
*/
|
||||
maybeArgument<N extends keyof any, V>(
|
||||
name: N,
|
||||
conv: Converter<V>,
|
||||
args: ArgumentArgs<V> = {},
|
||||
): Program<PN, T & SubRecord<PN, N, V | undefined>> {
|
||||
this.mainCommand.maybeArgument(name, conv, args);
|
||||
return this as any;
|
||||
}
|
||||
|
||||
action(f: ActionFn<T>): void {
|
||||
this.mainCommand.action(f);
|
||||
}
|
||||
}
|
||||
|
||||
export type GetArgType<T> = T extends Program<any, infer AT>
|
||||
? AT
|
||||
: T extends CommandGroup<any, infer AT>
|
||||
? AT
|
||||
: any;
|
||||
|
||||
export function program<PN extends keyof any>(
|
||||
argKey: PN,
|
||||
args: ProgramArgs = {},
|
||||
): Program<PN, {}> {
|
||||
return new Program(argKey as string, args);
|
||||
}
|
||||
|
||||
export function prompt(question: string): Promise<string> {
|
||||
const stdinReadline = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
});
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
stdinReadline.question(question, (res) => {
|
||||
resolve(res);
|
||||
stdinReadline.close();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
@ -21,3 +21,4 @@ initNodePrng();
|
||||
export * from "./index.js";
|
||||
export * from "./talerconfig.js";
|
||||
export * from "./globbing/minimatch.js";
|
||||
export { clk } from "./clk.js";
|
||||
|
1209
pnpm-lock.yaml
1209
pnpm-lock.yaml
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user