/* 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 */ import { AuthMethod, ReducerStateBackup } from "@gnu-taler/anastasis-core"; import { Fragment, h, VNode } from "preact"; import { useState } from "preact/hooks"; import { useAnastasisContext } from "../../context/anastasis.js"; import AddingProviderScreen from "./AddingProviderScreen/index.js"; import { authMethods, AuthMethodSetupProps, AuthMethodWithRemove, isKnownAuthMethods, KnownAuthMethods, } from "./authMethod/index.js"; import { ConfirmModal } from "./ConfirmModal.js"; import { AnastasisClientFrame } from "./index.js"; const getKeys = Object.keys as (obj: T) => Array; export function AuthenticationEditorScreen(): VNode { const [noProvidersAck, setNoProvidersAck] = useState(false); const [selectedMethod, setSelectedMethod] = useState< KnownAuthMethods | undefined >(undefined); const [tooFewAuths, setTooFewAuths] = useState(false); const [manageProvider, setManageProvider] = useState( undefined, ); // const [addingProvider, setAddingProvider] = useState(undefined) const reducer = useAnastasisContext(); if (!reducer) { return
no reducer in context
; } if (reducer.currentReducerState?.reducer_type !== "backup") { return
invalid state
; } const configuredAuthMethods: AuthMethod[] = reducer.currentReducerState.authentication_methods ?? []; function removeByIndex(index: number): void { if (reducer) reducer.transition("delete_authentication", { authentication_method: index, }); } const camByType: { [s: string]: AuthMethodWithRemove[] } = {}; for (let index = 0; index < configuredAuthMethods.length; index++) { const cam = { ...configuredAuthMethods[index], remove: () => removeByIndex(index), }; const prevValue = camByType[cam.type] || []; prevValue.push(cam); camByType[cam.type] = prevValue; } const providers = reducer.currentReducerState.authentication_providers!; const authAvailableSet = new Set(); for (const provKey of Object.keys(providers)) { const p = providers[provKey]; if (p.status === "ok") { for (const meth of p.methods) { authAvailableSet.add(meth.type); } } } if (manageProvider !== undefined) { return ( setManageProvider(undefined)} providerType={ isKnownAuthMethods(manageProvider) ? manageProvider : undefined } /> ); } if (selectedMethod) { const cancel = (): void => setSelectedMethod(undefined); const addMethod = (args: any): void => { reducer.transition("add_authentication", args); setSelectedMethod(undefined); }; const AuthSetup = authMethods[selectedMethod].setup ?? AuthMethodNotImplemented; return ( {!authAvailableSet.has(selectedMethod) && ( { setManageProvider(selectedMethod); }} >

We have found no Anastasis providers that support this authentication method. You can add a provider manually. To add a provider you must know the provider URL (e.g. https://provider.com)

Learn more about Anastasis providers

)}
); } function MethodButton(props: { method: KnownAuthMethods }): VNode { if (authMethods[props.method].skip) return
; return (
); } const errors = configuredAuthMethods.length < 2 ? "There is not enough authentication methods." : undefined; const handleNext = async () => { const st = reducer.currentReducerState as ReducerStateBackup; if ((st.authentication_methods ?? []).length <= 2) { setTooFewAuths(true); } else { await reducer.transition("next", {}); } }; return (
{getKeys(authMethods).map((method) => ( ))}
{tooFewAuths ? ( setTooFewAuths(false)} description="Too few auth methods configured" label="Proceed anyway" onConfirm={() => reducer.transition("next", {})} > You have selected fewer than 3 authentication methods. We recommend that you add at least 3. ) : null} {authAvailableSet.size === 0 && ( setNoProvidersAck(true)} description="No providers found" label="Add a provider manually" onConfirm={async () => { setManageProvider(""); }} >

We have found no Anastasis providers for your chosen country / currency. You can add a providers manually. To add a provider you must know the provider URL (e.g. https://provider.com)

Learn more about Anastasis providers

)}

When recovering your secret data, you will be asked to verify your identity via the methods you configure here. The list of authentication method is defined by the backup provider list.

{authAvailableSet.size > 0 && (

We couldn't find provider for some of the authentication methods.

)}
); } function AuthMethodNotImplemented(props: AuthMethodSetupProps): VNode { return (

This auth method is not implemented yet, please choose another one.

); }