2022-01-24 18:39:27 +01:00
|
|
|
import { UserAttributeSpec, validators } from "@gnu-taler/anastasis-core";
|
2021-11-08 17:12:44 +01:00
|
|
|
import { isAfter, parse } from "date-fns";
|
2022-04-13 19:32:12 +02:00
|
|
|
import { h, VNode } from "preact";
|
2021-10-19 15:56:52 +02:00
|
|
|
import { useState } from "preact/hooks";
|
2022-06-06 05:54:55 +02:00
|
|
|
import { DateInput } from "../../components/fields/DateInput.js";
|
|
|
|
import { PhoneNumberInput } from "../../components/fields/NumberInput.js";
|
|
|
|
import { TextInput } from "../../components/fields/TextInput.js";
|
|
|
|
import { useAnastasisContext } from "../../context/anastasis.js";
|
|
|
|
import { ConfirmModal } from "./ConfirmModal.js";
|
|
|
|
import { AnastasisClientFrame, withProcessLabel } from "./index.js";
|
2021-10-19 15:56:52 +02:00
|
|
|
|
2021-10-22 06:31:46 +02:00
|
|
|
export function AttributeEntryScreen(): VNode {
|
2021-11-10 14:20:52 +01:00
|
|
|
const reducer = useAnastasisContext();
|
|
|
|
const state = reducer?.currentReducerState;
|
|
|
|
const currentIdentityAttributes =
|
|
|
|
state && "identity_attributes" in state
|
|
|
|
? state.identity_attributes || {}
|
|
|
|
: {};
|
|
|
|
const [attrs, setAttrs] = useState<Record<string, string>>(
|
|
|
|
currentIdentityAttributes,
|
|
|
|
);
|
2022-04-13 08:44:37 +02:00
|
|
|
const isBackup = state?.reducer_type === "backup";
|
2021-11-10 22:10:45 +01:00
|
|
|
const [askUserIfSure, setAskUserIfSure] = useState(false);
|
2021-10-22 06:31:46 +02:00
|
|
|
|
|
|
|
if (!reducer) {
|
2021-11-10 14:20:52 +01:00
|
|
|
return <div>no reducer in context</div>;
|
2021-10-22 06:31:46 +02:00
|
|
|
}
|
2021-11-10 14:20:52 +01:00
|
|
|
if (
|
|
|
|
!reducer.currentReducerState ||
|
|
|
|
!("required_attributes" in reducer.currentReducerState)
|
|
|
|
) {
|
|
|
|
return <div>invalid state</div>;
|
2021-10-22 06:31:46 +02:00
|
|
|
}
|
2021-11-10 14:20:52 +01:00
|
|
|
const reqAttr = reducer.currentReducerState.required_attributes || [];
|
2021-11-01 20:10:49 +01:00
|
|
|
let hasErrors = false;
|
2021-10-26 17:08:03 +02:00
|
|
|
|
2021-11-01 20:10:49 +01:00
|
|
|
const fieldList: VNode[] = reqAttr.map((spec, i: number) => {
|
2021-11-10 14:20:52 +01:00
|
|
|
const value = attrs[spec.name];
|
|
|
|
const error = checkIfValid(value, spec);
|
2022-01-24 21:32:46 +01:00
|
|
|
|
|
|
|
function addAutocomplete(newValue: string): string {
|
|
|
|
const ac = spec.autocomplete;
|
2022-01-25 13:26:12 +01:00
|
|
|
if (!ac || ac.length <= newValue.length || ac[newValue.length] === "?")
|
2022-01-24 21:32:46 +01:00
|
|
|
return newValue;
|
|
|
|
|
|
|
|
if (!value || newValue.length < value.length) {
|
|
|
|
return newValue.slice(0, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newValue + ac[newValue.length];
|
|
|
|
}
|
|
|
|
|
2021-11-10 14:20:52 +01:00
|
|
|
hasErrors = hasErrors || error !== undefined;
|
2021-11-01 20:10:49 +01:00
|
|
|
return (
|
|
|
|
<AttributeEntryField
|
|
|
|
key={i}
|
|
|
|
isFirst={i == 0}
|
2022-01-24 21:32:46 +01:00
|
|
|
setValue={(v: string) =>
|
|
|
|
setAttrs({ ...attrs, [spec.name]: addAutocomplete(v) })
|
|
|
|
}
|
2021-11-01 20:10:49 +01:00
|
|
|
spec={spec}
|
|
|
|
errorMessage={error}
|
2021-11-12 17:12:27 +01:00
|
|
|
onConfirm={() => {
|
|
|
|
if (!hasErrors) {
|
2022-01-24 18:39:27 +01:00
|
|
|
setAskUserIfSure(true);
|
2021-11-12 17:12:27 +01:00
|
|
|
}
|
|
|
|
}}
|
2021-11-10 14:20:52 +01:00
|
|
|
value={value}
|
|
|
|
/>
|
2021-11-01 20:10:49 +01:00
|
|
|
);
|
2021-11-10 14:20:52 +01:00
|
|
|
});
|
2021-10-26 17:08:03 +02:00
|
|
|
|
2021-11-19 12:47:55 +01:00
|
|
|
const doConfirm = async () => {
|
|
|
|
await reducer.transition("enter_user_attributes", {
|
2022-04-13 19:32:12 +02:00
|
|
|
identity_attributes: {
|
|
|
|
application_id: "anastasis-standalone",
|
|
|
|
...attrs,
|
|
|
|
},
|
2021-11-19 12:47:55 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-10-19 15:56:52 +02:00
|
|
|
return (
|
|
|
|
<AnastasisClientFrame
|
2021-10-26 17:08:03 +02:00
|
|
|
title={withProcessLabel(reducer, "Who are you?")}
|
2021-11-01 20:10:49 +01:00
|
|
|
hideNext={hasErrors ? "Complete the form." : undefined}
|
2022-01-24 18:39:27 +01:00
|
|
|
onNext={async () => (isBackup ? setAskUserIfSure(true) : doConfirm())}
|
2021-10-19 15:56:52 +02:00
|
|
|
>
|
2021-11-10 22:10:45 +01:00
|
|
|
{askUserIfSure ? (
|
|
|
|
<ConfirmModal
|
|
|
|
active
|
|
|
|
onCancel={() => setAskUserIfSure(false)}
|
2021-11-11 17:22:14 +01:00
|
|
|
description="The values in the form must be correct"
|
2021-11-10 22:10:45 +01:00
|
|
|
label="I am sure"
|
|
|
|
cancelLabel="Wait, I want to check"
|
2022-01-24 18:39:27 +01:00
|
|
|
onConfirm={() => doConfirm().then(() => setAskUserIfSure(false))}
|
2021-11-10 22:10:45 +01:00
|
|
|
>
|
2022-01-24 18:39:27 +01:00
|
|
|
You personal information is used to define the location where your
|
|
|
|
secret will be safely stored. If you forget what you have entered or
|
2021-11-11 17:22:14 +01:00
|
|
|
if there is a misspell you will be unable to recover your secret.
|
2021-11-10 22:10:45 +01:00
|
|
|
</ConfirmModal>
|
|
|
|
) : null}
|
|
|
|
|
2021-11-10 14:20:52 +01:00
|
|
|
<div class="columns" style={{ maxWidth: "unset" }}>
|
|
|
|
<div class="column">{fieldList}</div>
|
2021-11-08 17:12:44 +01:00
|
|
|
<div class="column">
|
2021-11-01 20:10:49 +01:00
|
|
|
<p>This personal information will help to locate your secret.</p>
|
2021-11-03 21:30:11 +01:00
|
|
|
<h1 class="title">This stays private</h1>
|
|
|
|
<p>The information you have entered here:</p>
|
2021-10-26 17:08:03 +02:00
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
<span class="icon is-right">
|
|
|
|
<i class="mdi mdi-circle-small" />
|
|
|
|
</span>
|
|
|
|
Will be hashed, and therefore unreadable
|
|
|
|
</li>
|
2021-11-10 14:20:52 +01:00
|
|
|
<li>
|
|
|
|
<span class="icon is-right">
|
|
|
|
<i class="mdi mdi-circle-small" />
|
|
|
|
</span>
|
|
|
|
The non-hashed version is not shared
|
|
|
|
</li>
|
2021-10-26 17:08:03 +02:00
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-10-19 15:56:52 +02:00
|
|
|
</AnastasisClientFrame>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-26 17:08:03 +02:00
|
|
|
interface AttributeEntryFieldProps {
|
2021-10-19 15:56:52 +02:00
|
|
|
isFirst: boolean;
|
|
|
|
value: string;
|
|
|
|
setValue: (newValue: string) => void;
|
2021-10-22 06:31:46 +02:00
|
|
|
spec: UserAttributeSpec;
|
2021-11-01 20:10:49 +01:00
|
|
|
errorMessage: string | undefined;
|
2021-11-12 17:12:27 +01:00
|
|
|
onConfirm: () => void;
|
2021-10-19 15:56:52 +02:00
|
|
|
}
|
2021-11-10 14:20:52 +01:00
|
|
|
const possibleBirthdayYear: Array<number> = [];
|
2021-11-01 20:10:49 +01:00
|
|
|
for (let i = 0; i < 100; i++) {
|
2021-11-10 14:20:52 +01:00
|
|
|
possibleBirthdayYear.push(2020 - i);
|
2021-10-27 20:13:35 +02:00
|
|
|
}
|
2021-10-26 17:08:03 +02:00
|
|
|
function AttributeEntryField(props: AttributeEntryFieldProps): VNode {
|
2021-10-19 15:56:52 +02:00
|
|
|
return (
|
2022-01-24 20:47:37 +01:00
|
|
|
<div style={{ marginTop: 16 }}>
|
2022-01-24 18:39:27 +01:00
|
|
|
{props.spec.type === "date" && (
|
2021-10-26 17:08:03 +02:00
|
|
|
<DateInput
|
2021-10-27 20:13:35 +02:00
|
|
|
grabFocus={props.isFirst}
|
|
|
|
label={props.spec.label}
|
|
|
|
years={possibleBirthdayYear}
|
2021-11-12 17:12:27 +01:00
|
|
|
onConfirm={props.onConfirm}
|
2021-11-01 20:10:49 +01:00
|
|
|
error={props.errorMessage}
|
2021-10-27 20:13:35 +02:00
|
|
|
bind={[props.value, props.setValue]}
|
2021-11-10 14:20:52 +01:00
|
|
|
/>
|
2022-01-24 18:39:27 +01:00
|
|
|
)}
|
|
|
|
{props.spec.type === "number" && (
|
2021-11-09 23:14:44 +01:00
|
|
|
<PhoneNumberInput
|
2021-10-26 17:08:03 +02:00
|
|
|
grabFocus={props.isFirst}
|
|
|
|
label={props.spec.label}
|
2021-11-12 17:12:27 +01:00
|
|
|
onConfirm={props.onConfirm}
|
2021-11-01 20:10:49 +01:00
|
|
|
error={props.errorMessage}
|
2021-10-26 17:08:03 +02:00
|
|
|
bind={[props.value, props.setValue]}
|
2021-10-27 20:13:35 +02:00
|
|
|
/>
|
2022-01-24 18:39:27 +01:00
|
|
|
)}
|
2021-11-10 14:20:52 +01:00
|
|
|
{props.spec.type === "string" && (
|
2021-10-27 20:13:35 +02:00
|
|
|
<TextInput
|
2021-10-26 17:08:03 +02:00
|
|
|
grabFocus={props.isFirst}
|
|
|
|
label={props.spec.label}
|
2021-11-12 17:12:27 +01:00
|
|
|
onConfirm={props.onConfirm}
|
2021-11-01 20:10:49 +01:00
|
|
|
error={props.errorMessage}
|
2021-10-26 17:08:03 +02:00
|
|
|
bind={[props.value, props.setValue]}
|
|
|
|
/>
|
2021-11-10 14:20:52 +01:00
|
|
|
)}
|
2022-01-24 20:47:37 +01:00
|
|
|
{props.spec.type === "string" && (
|
|
|
|
<div>
|
|
|
|
This field is case-sensitive. You must enter exactly the same value
|
|
|
|
during recovery.
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{props.spec.name === "full_name" && (
|
|
|
|
<div>
|
2022-06-06 05:54:55 +02:00
|
|
|
If possible, use "LASTNAME, Firstname(s)" without
|
|
|
|
abbreviations.
|
2022-01-24 20:47:37 +01:00
|
|
|
</div>
|
|
|
|
)}
|
2021-11-03 21:30:11 +01:00
|
|
|
<div class="block">
|
|
|
|
This stays private
|
2021-10-26 17:08:03 +02:00
|
|
|
<span class="icon is-right">
|
|
|
|
<i class="mdi mdi-eye-off" />
|
|
|
|
</span>
|
2021-11-03 21:30:11 +01:00
|
|
|
</div>
|
2021-10-19 15:56:52 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2021-11-10 14:20:52 +01:00
|
|
|
const YEAR_REGEX = /^[0-9]+-[0-9]+-[0-9]+$/;
|
2021-11-01 20:10:49 +01:00
|
|
|
|
2021-11-10 14:20:52 +01:00
|
|
|
function checkIfValid(
|
|
|
|
value: string,
|
|
|
|
spec: UserAttributeSpec,
|
|
|
|
): string | undefined {
|
|
|
|
const pattern = spec["validation-regex"];
|
2021-11-01 20:10:49 +01:00
|
|
|
if (pattern) {
|
2021-11-10 14:20:52 +01:00
|
|
|
const re = new RegExp(pattern);
|
|
|
|
if (!re.test(value)) return "The value is invalid";
|
2021-11-01 20:10:49 +01:00
|
|
|
}
|
2021-11-10 14:20:52 +01:00
|
|
|
const logic = spec["validation-logic"];
|
2021-11-01 20:10:49 +01:00
|
|
|
if (logic) {
|
|
|
|
const func = (validators as any)[logic];
|
2021-11-10 14:20:52 +01:00
|
|
|
if (func && typeof func === "function" && !func(value))
|
|
|
|
return "Please check the value";
|
2021-11-01 20:10:49 +01:00
|
|
|
}
|
2021-11-10 14:20:52 +01:00
|
|
|
const optional = spec.optional;
|
2021-11-01 20:10:49 +01:00
|
|
|
if (!optional && !value) {
|
2021-11-10 14:20:52 +01:00
|
|
|
return "This value is required";
|
2021-11-01 20:10:49 +01:00
|
|
|
}
|
2021-11-03 21:30:11 +01:00
|
|
|
if ("date" === spec.type) {
|
|
|
|
if (!YEAR_REGEX.test(value)) {
|
2021-11-10 14:20:52 +01:00
|
|
|
return "The date doesn't follow the format";
|
2021-11-03 21:30:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-11-10 14:20:52 +01:00
|
|
|
const v = parse(value, "yyyy-MM-dd", new Date());
|
2021-11-03 21:30:11 +01:00
|
|
|
if (Number.isNaN(v.getTime())) {
|
2021-11-10 14:20:52 +01:00
|
|
|
return "Some numeric values seems out of range for a date";
|
2021-11-03 21:30:11 +01:00
|
|
|
}
|
|
|
|
if ("birthdate" === spec.name && isAfter(v, new Date())) {
|
2021-11-10 14:20:52 +01:00
|
|
|
return "A birthdate cannot be in the future";
|
2021-11-03 21:30:11 +01:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-11-10 14:20:52 +01:00
|
|
|
return "Could not parse the date";
|
2021-11-03 21:30:11 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-10 14:20:52 +01:00
|
|
|
return undefined;
|
2021-11-01 20:10:49 +01:00
|
|
|
}
|