summaryrefslogtreecommitdiff
path: root/packages/challenger-ui/src/pages/AnswerChallenge.tsx
blob: 73a79c51f06b7da06a87d5369157c891356e0b6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 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 <http://www.gnu.org/licenses/>
 */
import {
  ChallengerApi,
  HttpStatusCode,
  assertUnreachable,
} from "@gnu-taler/taler-util";
import {
  Attention,
  Button,
  LocalNotificationBanner,
  RouteDefinition,
  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;
  focus?: boolean;
  onComplete: () => void;
  routeAsk: RouteDefinition<{ nonce: string }>;
};

export function AnswerChallenge({ focus, nonce, onComplete, routeAsk }: Props): VNode {
  const { lib } = useChallengerApiContext();
  const { i18n } = useTranslationContext();
  const { state, accepted, completed } = useSessionState();
  const [notification, withErrorHandler] = useLocalNotificationHandler();
  const [pin, setPin] = useState<string | undefined>();
  const [lastTryError, setLastTryError] =
    useState<ChallengerApi.InvalidPinResponse>();
  const errors = undefinedIfEmpty({
    pin: !pin ? i18n.str`Can't be empty` : undefined,
  });

  const lastEmail = !state
    ? undefined
    : !state.lastStatus
      ? undefined
      : !state.lastStatus.last_address
        ? undefined
        : state.lastStatus.last_address["email"];

  const onSendAgain =
    !state || lastEmail === undefined
      ? undefined
      : withErrorHandler(
          async () => {
            if (!lastEmail) return;
            return await lib.challenger.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 =
    errors !== undefined || (lastTryError && lastTryError.exhausted)
      ? undefined
      : withErrorHandler(
          async () => {
            return lib.challenger.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 <div>no state</div>;
  }

  if (!state.lastTry) {
    return <div>you should do a challenge first</div>;
  }

  return (
    <Fragment>
      <LocalNotificationBanner notification={notification} />

      <div class="isolate bg-white px-6 py-12">
        <div class="mx-auto max-w-2xl text-center">
          <h2 class="text-3xl font-bold tracking-tight text-gray-900 sm:text-4xl">
            <i18n.Translate>
              Enter the TAN you received to authenticate.
            </i18n.Translate>
          </h2>
          <p class="mt-2 text-lg leading-8 text-gray-600">
            {state.lastTry.transmitted ? (
              <i18n.Translate>
                A TAN was sent to your address &quot;{lastEmail}&quot;.
              </i18n.Translate>
            ) : (
              <Attention title={i18n.str`Resend failed`} type="warning">
                <i18n.Translate>
                  We recently already sent a TAN to your address &quot;
                  {lastEmail}&quot;. A new TAN will not be transmitted again
                  before &quot;{state.lastTry.nextSend}&quot;.
                </i18n.Translate>
              </Attention>
            )}
          </p>
          {!lastTryError ? undefined : (
            <p class="mt-2 text-lg leading-8 text-gray-600">
              <i18n.Translate>
                You can try another PIN but just{" "}
                {lastTryError.auth_attempts_left} times more.
              </i18n.Translate>
            </p>
          )}
        </div>
        <form
          method="POST"
          class="mx-auto mt-16 max-w-xl sm:mt-20"
          onSubmit={(e) => {
            e.preventDefault();
          }}
        >
          <div class="grid grid-cols-1 gap-x-8 gap-y-6">
            <div class="sm:col-span-2">
              <label
                for="pin"
                class="block text-sm font-semibold leading-6 text-gray-900"
              >
                <i18n.Translate>TAN code</i18n.Translate>
              </label>
              <div class="mt-2.5">
                <input
                  autoFocus
                  ref={focus ? doAutoFocus : undefined}
                  type="number"
                  name="pin"
                  id="pin"
                  maxLength={64}
                  value={pin}
                  onChange={(e) => {
                    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"
                />
                <ShowInputErrorLabel
                  message={errors?.pin}
                  isDirty={pin !== undefined}
                />
              </div>
            </div>

            <p class="mt-3 text-sm leading-6 text-gray-400">
              <i18n.Translate>
                You have {state.lastTry.attemptsLeft} attempts left.
              </i18n.Translate>
            </p>
          </div>

          <div class="mt-10">
            <Button
              type="submit"
              class="block w-full disabled:bg-gray-300 rounded-md bg-indigo-600 px-3.5 py-2.5 text-center text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
              disabled={!onCheck}
              handler={onCheck}
            >
              <i18n.Translate>Check</i18n.Translate>
            </Button>
          </div>
          <div class="mt-10 flex justify-between">
            <div>
              <a
                href={routeAsk.url({ nonce })}
                class="relative disabled:bg-gray-100 disabled:text-gray-500 inline-flex items-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus-visible:outline-offset-0"
              >
                <i18n.Translate>Change email</i18n.Translate>
              </a>
            </div>
            <div>
              <Button
                type="submit"
                disabled={!onSendAgain}
                class="block w-full disabled:bg-gray-300 rounded-md bg-indigo-600 px-3.5 py-2.5 text-center text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
                handler={onSendAgain}
              >
                <i18n.Translate>Send code again</i18n.Translate>
              </Button>
            </div>
          </div>
        </form>
      </div>
    </Fragment>
  );
}

/**
 * Show the element when the load ended
 * @param element
 */
export function doAutoFocus(element: HTMLElement | null): void {
  if (element) {
    setTimeout(() => {
      element.focus({ preventScroll: true });
      element.scrollIntoView({
        behavior: "smooth",
        block: "center",
        inline: "center",
      });
    }, 100);
  }
}

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