wallet-core/packages/anastasis-webui/src/pages/home/ChallengeOverviewScreen.tsx

297 lines
9.7 KiB
TypeScript
Raw Normal View History

2022-06-06 16:46:49 +02:00
/*
This file is part of GNU Anastasis
(C) 2021-2022 Anastasis SARL
GNU Anastasis is free software; you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Anastasis 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with
GNU Anastasis; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
2022-01-24 18:39:27 +01:00
import {
ChallengeFeedback,
ChallengeFeedbackStatus,
} from "@gnu-taler/anastasis-core";
2022-08-26 17:59:00 +02:00
import { Fragment, h, VNode } from "preact";
2022-06-06 05:54:55 +02:00
import { AsyncButton } from "../../components/AsyncButton.js";
import { useAnastasisContext } from "../../context/anastasis.js";
import { authMethods, KnownAuthMethods } from "./authMethod/index.js";
import { AnastasisClientFrame } from "./index.js";
2021-10-22 06:31:46 +02:00
2022-08-26 17:59:00 +02:00
function OverviewFeedbackDisplay(props: {
feedback?: ChallengeFeedback;
}): VNode {
const { feedback } = props;
if (!feedback) {
2022-08-26 17:59:00 +02:00
return <Fragment />;
}
2022-08-26 17:59:00 +02:00
switch (feedback.state) {
2021-11-05 18:56:03 +01:00
case ChallengeFeedbackStatus.Solved:
2021-11-10 14:20:52 +01:00
return <div />;
2022-04-14 14:14:02 +02:00
case ChallengeFeedbackStatus.IbanInstructions:
2022-08-26 17:59:00 +02:00
return <div class="block has-text-info">Payment required.</div>;
2021-11-05 18:56:03 +01:00
case ChallengeFeedbackStatus.ServerFailure:
return <div class="block has-text-danger">Server error.</div>;
case ChallengeFeedbackStatus.RateLimitExceeded:
2021-11-10 14:20:52 +01:00
return (
<div class="block has-text-danger">
There were to many failed attempts.
</div>
);
case ChallengeFeedbackStatus.Unsupported:
2021-11-10 14:20:52 +01:00
return (
<div class="block has-text-danger">
2022-06-06 05:54:55 +02:00
This client doesn&apos;t support solving this type of challenge. Use
2021-11-10 14:20:52 +01:00
another version or contact the provider.
</div>
);
case ChallengeFeedbackStatus.TruthUnknown:
2021-11-10 14:20:52 +01:00
return (
<div class="block has-text-danger">
2022-08-26 17:59:00 +02:00
Provider doesn&apos;t recognize the type of challenge. Use another
version or contact the provider.
2021-11-10 14:20:52 +01:00
</div>
);
2022-08-26 17:59:00 +02:00
case ChallengeFeedbackStatus.IncorrectAnswer:
return (
<div class="block has-text-danger">The answer was not correct.</div>
);
case ChallengeFeedbackStatus.CodeInFile:
return <div class="block has-text-info">code in file</div>;
case ChallengeFeedbackStatus.CodeSent:
return <div class="block has-text-info">Code sent</div>;
case ChallengeFeedbackStatus.TalerPayment:
return <div class="block has-text-info">Payment required</div>;
}
}
2021-10-22 06:31:46 +02:00
export function ChallengeOverviewScreen(): VNode {
2021-11-03 18:26:57 +01:00
const reducer = useAnastasisContext();
2021-10-22 06:31:46 +02:00
if (!reducer) {
2021-11-03 18:26:57 +01:00
return <div>no reducer in context</div>;
2021-10-22 06:31:46 +02:00
}
2022-04-13 08:44:37 +02:00
if (reducer.currentReducerState?.reducer_type !== "recovery") {
2021-11-03 18:26:57 +01:00
return <div>invalid state</div>;
2021-10-22 06:31:46 +02:00
}
2021-11-03 18:26:57 +01:00
const policies =
reducer.currentReducerState.recovery_information?.policies ?? [];
const knownChallengesArray =
reducer.currentReducerState.recovery_information?.challenges ?? [];
const challengeFeedback =
reducer.currentReducerState?.challenge_feedback ?? {};
2021-10-19 15:56:52 +02:00
2021-10-27 20:13:35 +02:00
const knownChallengesMap: {
2021-10-19 15:56:52 +02:00
[uuid: string]: {
type: string;
instructions: string;
2021-10-27 20:13:35 +02:00
feedback: ChallengeFeedback | undefined;
2021-10-19 15:56:52 +02:00
};
} = {};
2021-10-27 20:13:35 +02:00
for (const ch of knownChallengesArray) {
knownChallengesMap[ch.uuid] = {
2021-10-19 15:56:52 +02:00
type: ch.type,
instructions: ch.instructions,
2021-11-03 18:26:57 +01:00
feedback: challengeFeedback[ch.uuid],
2021-10-19 15:56:52 +02:00
};
}
2021-11-10 14:20:52 +01:00
const policiesWithInfo = policies
.map((row) => {
let isPolicySolved = true;
const challenges = row
.map(({ uuid }) => {
const info = knownChallengesMap[uuid];
const isChallengeSolved = info?.feedback?.state === "solved";
isPolicySolved = isPolicySolved && isChallengeSolved;
return { info, uuid, isChallengeSolved };
})
.filter((ch) => ch.info !== undefined);
2021-10-27 20:13:35 +02:00
2021-11-10 14:20:52 +01:00
return {
isPolicySolved,
challenges,
corrupted: row.length > challenges.length,
};
})
.filter((p) => !p.corrupted);
2021-10-27 20:13:35 +02:00
2021-11-03 18:26:57 +01:00
const atLeastThereIsOnePolicySolved =
policiesWithInfo.find((p) => p.isPolicySolved) !== undefined;
2021-10-27 20:13:35 +02:00
2021-11-03 18:26:57 +01:00
const errors = !atLeastThereIsOnePolicySolved
? "Solve one policy before proceeding"
: undefined;
2021-10-19 15:56:52 +02:00
return (
<AnastasisClientFrame hideNext={errors} title="Recovery: Solve challenges">
2021-11-10 14:20:52 +01:00
{!policiesWithInfo.length ? (
2021-11-03 18:26:57 +01:00
<p class="block">
No policies found, try with another version of the secret
</p>
2021-11-10 14:20:52 +01:00
) : policiesWithInfo.length === 1 ? (
2021-11-03 18:26:57 +01:00
<p class="block">
One policy found for this secret. You need to solve all the challenges
in order to recover your secret.
</p>
) : (
<p class="block">
2021-11-10 14:20:52 +01:00
We have found {policiesWithInfo.length} polices. You need to solve all
the challenges from one policy in order to recover your secret.
2021-11-03 18:26:57 +01:00
</p>
)}
{policiesWithInfo.map((policy, policy_index) => {
const tableBody = policy.challenges.map(({ info, uuid }) => {
2021-11-03 18:26:57 +01:00
const method = authMethods[info.type as KnownAuthMethods];
2021-11-05 18:56:03 +01:00
if (!method) {
2021-11-10 14:20:52 +01:00
return (
<div
key={uuid}
class="block"
style={{ display: "flex", justifyContent: "space-between" }}
>
<div style={{ display: "flex", alignItems: "center" }}>
<span>unknown challenge</span>
</div>
2021-11-05 18:56:03 +01:00
</div>
2021-11-10 14:20:52 +01:00
);
2021-11-05 18:56:03 +01:00
}
2021-11-10 14:20:52 +01:00
function ChallengeButton({
id,
feedback,
}: {
id: string;
feedback?: ChallengeFeedback;
}): VNode {
async function selectChallenge(): Promise<void> {
if (reducer) {
return reducer.transition("select_challenge", { uuid: id });
}
2021-11-05 18:56:03 +01:00
}
if (!feedback) {
2021-11-10 14:20:52 +01:00
return (
<div>
<AsyncButton
2021-11-10 14:20:52 +01:00
class="button"
disabled={
atLeastThereIsOnePolicySolved && !policy.isPolicySolved
}
onClick={selectChallenge}
>
Solve
</AsyncButton>
2021-11-10 14:20:52 +01:00
</div>
);
2021-11-05 18:56:03 +01:00
}
switch (feedback.state) {
case ChallengeFeedbackStatus.ServerFailure:
case ChallengeFeedbackStatus.Unsupported:
case ChallengeFeedbackStatus.TruthUnknown:
2021-11-10 14:20:52 +01:00
case ChallengeFeedbackStatus.RateLimitExceeded:
return <div />;
2022-04-14 14:14:02 +02:00
case ChallengeFeedbackStatus.IbanInstructions:
case ChallengeFeedbackStatus.TalerPayment:
2021-11-10 14:20:52 +01:00
return (
<div>
<AsyncButton
2021-11-10 14:20:52 +01:00
class="button"
disabled={
atLeastThereIsOnePolicySolved && !policy.isPolicySolved
}
onClick={selectChallenge}
>
Pay
</AsyncButton>
2021-11-10 14:20:52 +01:00
</div>
);
case ChallengeFeedbackStatus.Solved:
return (
<div>
<div class="tag is-success is-large">Solved</div>
</div>
);
default:
return (
<div>
<AsyncButton
2021-11-10 14:20:52 +01:00
class="button"
disabled={
atLeastThereIsOnePolicySolved && !policy.isPolicySolved
}
onClick={selectChallenge}
>
Solve
</AsyncButton>
2021-11-10 14:20:52 +01:00
</div>
);
2021-11-05 18:56:03 +01:00
}
}
2021-10-27 20:13:35 +02:00
return (
2021-11-03 18:26:57 +01:00
<div
key={uuid}
class="block"
style={{ display: "flex", justifyContent: "space-between" }}
>
<div
style={{
display: "flex",
flexDirection: "column",
}}
>
<div style={{ display: "flex", alignItems: "center" }}>
<span class="icon">{method?.icon}</span>
<span>{info.instructions}</span>
</div>
<OverviewFeedbackDisplay feedback={info.feedback} />
</div>
2021-11-05 18:56:03 +01:00
<ChallengeButton id={uuid} feedback={info.feedback} />
</div>
2021-10-27 20:13:35 +02:00
);
2021-11-03 18:26:57 +01:00
});
const policyName = policy.challenges
.map((x) => x.info.type)
.join(" + ");
2021-11-10 14:20:52 +01:00
2021-11-03 18:26:57 +01:00
const opa = !atLeastThereIsOnePolicySolved
? undefined
: policy.isPolicySolved
2021-11-10 14:20:52 +01:00
? undefined
: "0.6";
2021-10-19 15:56:52 +02:00
return (
2021-11-03 18:26:57 +01:00
<div
key={policy_index}
class="box"
style={{
opacity: opa,
}}
>
<h3 class="subtitle">
Policy #{policy_index + 1}: {policyName}
</h3>
2021-11-03 18:26:57 +01:00
{policy.challenges.length === 0 && (
2022-06-06 05:54:55 +02:00
<p>This policy doesn&apos;t have any challenges.</p>
2021-11-03 18:26:57 +01:00
)}
{policy.challenges.length === 1 && (
<p>This policy has one challenge.</p>
2021-11-03 18:26:57 +01:00
)}
{policy.challenges.length > 1 && (
<p>This policy has {policy.challenges.length} challenges.</p>
2021-11-03 18:26:57 +01:00
)}
{tableBody}
2021-10-19 15:56:52 +02:00
</div>
);
})}
</AnastasisClientFrame>
);
}