/* This file is part of GNU Taler (C) 2022-2024 Taler Systems S.A. 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 */ import { ChallengerApi, HttpStatusCode, assertUnreachable, } from "@gnu-taler/taler-util"; import { Attention, Button, LocalNotificationBanner, ShowInputErrorLabel, useChallengerApiContext, useLocalNotificationHandler, useTranslationContext, } from "@gnu-taler/web-util/browser"; import { Fragment, VNode, h } from "preact"; import { useState } from "preact/hooks"; import { useSessionState } from "../hooks/session.js"; export const EMAIL_REGEX = /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/; type Props = { nonce: string; onComplete: () => void; }; export function AnswerChallenge({ nonce, onComplete }: Props): VNode { const { lib } = useChallengerApiContext(); const { i18n } = useTranslationContext(); const { state, accepted, completed } = useSessionState(); const [notification, withErrorHandler] = useLocalNotificationHandler(); const [pin, setPin] = useState(); const [lastTryError, setLastTryError] = useState(); const errors = undefinedIfEmpty({ pin: !pin ? i18n.str`Can't be empty` : undefined, }); const lastEmail = !state ? undefined : !state.lastStatus ? undefined : ((state.lastStatus.last_address as any)["email"] as string); const onSendAgain = !state || lastEmail === undefined ? undefined : withErrorHandler( async () => { if (!lastEmail) return; return await lib.bank.challenge(nonce, { email: lastEmail }); }, (ok) => { if ("redirectURL" in ok.body) { completed(ok.body.redirectURL); } else { accepted({ attemptsLeft: ok.body.attempts_left, nextSend: ok.body.next_tx_time, transmitted: ok.body.transmitted, }); } return undefined; }, (fail) => { switch (fail.case) { case HttpStatusCode.BadRequest: return i18n.str``; case HttpStatusCode.NotFound: return i18n.str``; case HttpStatusCode.NotAcceptable: return i18n.str``; case HttpStatusCode.TooManyRequests: return i18n.str``; case HttpStatusCode.InternalServerError: return i18n.str``; } }, ); const onCheck = lastTryError && lastTryError.exhausted ? undefined : withErrorHandler( async () => { return lib.bank.solve(nonce, { pin: pin! }); }, (ok) => { completed(ok.body.redirectURL as URL); onComplete(); }, (fail) => { switch (fail.case) { case HttpStatusCode.BadRequest: return i18n.str`Invalid request`; case HttpStatusCode.Forbidden: { setLastTryError(fail.body); return i18n.str`Invalid pin`; } case HttpStatusCode.NotFound: return i18n.str``; case HttpStatusCode.NotAcceptable: return i18n.str``; case HttpStatusCode.TooManyRequests: return i18n.str``; case HttpStatusCode.InternalServerError: return i18n.str``; default: assertUnreachable(fail); } }, ); if (!state) { return
no state
; } if (!state.lastTry) { return
you should do a challenge first
; } return (

Please enter the TAN you received to authenticate.

{state.lastTry.transmitted ? ( A TAN was sent to your address "{lastEmail}". ) : ( We recently already sent a TAN to your address " {lastEmail}". A new TAN will not be transmitted again before "{state.lastTry.nextSend}". )}

{!lastTryError ? undefined : (

You can try another PIN but just{" "} {lastTryError.auth_attempts_left} times more.

)}
{ e.preventDefault(); }} >
{ setPin(e.currentTarget.value); }} placeholder="12345678" class="block w-full rounded-md border-0 px-3.5 py-2 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6" />

You have {state.lastTry.attemptsLeft} attempts left.

); } export function undefinedIfEmpty(obj: T): T | undefined { return Object.keys(obj).some( (k) => (obj as Record)[k] !== undefined, ) ? obj : undefined; }