aboutsummaryrefslogtreecommitdiff
path: root/packages/exchange-backoffice-ui/src/pages/Officer.tsx
blob: 39e368b375ba9a1b5a0cbf94ec7d2f0942054d86 (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
import { AbsoluteTime, TranslatedString } from "@gnu-taler/taler-util";
import {
  notifyError,
  notifyInfo,
  useLocalStorage,
  useMemoryStorage,
  useTranslationContext,
} from "@gnu-taler/web-util/browser";
import { VNode, h } from "preact";
import { useEffect, useState } from "preact/hooks";
import {
  Account,
  UnwrapKeyError,
  createNewAccount,
  unlockAccount,
} from "../account.js";
import { createNewForm } from "../handlers/forms.js";
import { Officer, codecForOfficer } from "../Dashboard.js";

export function Officer() {
  const password = useMemoryStorage("password");
  const officer = useLocalStorage("officer", {
    codec: codecForOfficer(),
  });
  const [keys, setKeys] = useState<Account>();

  useEffect(() => {
    if (officer.value === undefined || password.value === undefined) {
      return;
    }

    unlockAccount(officer.value.salt, officer.value.key, password.value)
      .then((keys) => setKeys(keys ?? { accountId: "", pub: "" }))
      .catch((e) => {
        if (e instanceof UnwrapKeyError) {
          console.log(e);
        }
      });
  }, [officer.value, password.value]);

  if (
    officer.value === undefined ||
    !officer.value.key ||
    !officer.value.salt
  ) {
    return (
      <CreateAccount
        onNewAccount={(salt, key, pwd) => {
          password.update(pwd);
          officer.update({ salt, when: AbsoluteTime.now(), key });
        }}
      />
    );
  }

  if (password.value === undefined) {
    return (
      <UnlockAccount
        salt={officer.value.salt}
        sealedKey={officer.value.key}
        onAccountUnlocked={(pwd) => {
          password.update(pwd);
        }}
      />
    );
  }

  return (
    <div>
      <h1 class="my-2 text-3xl font-bold tracking-tight text-gray-900 ">
        Public key
      </h1>
      <div class="max-w-xl text-base leading-7 text-gray-700 lg:max-w-lg">
        <p class="mt-6 font-mono break-all">{keys?.accountId}</p>
      </div>
      <p>
        <a
          href={`mailto:aml@exchange.taler.net?body=${encodeURIComponent(
            `I want my AML account\n\n\nPubKey: ${keys?.accountId}`,
          )}`}
          target="_blank"
          rel="noreferrer"
          class="m-4 block rounded-md w-fit border-0 px-3 py-2 text-center text-sm bg-indigo-700 text-white shadow-sm hover:bg-indigo-700"
        >
          Request account activation
        </a>
      </p>
      <p>
        <button
          type="button"
          onClick={() => {
            password.reset();
          }}
          class="m-4 block rounded-md border-0 bg-gray-200 px-3 py-2 text-center text-sm text-black shadow-sm "
        >
          Lock account
        </button>
      </p>
      <p>
        <button
          type="button"
          onClick={() => {
            officer.reset();
          }}
          class="m-4 block rounded-md bg-red-600 px-3 py-2 text-center text-sm  text-white shadow-sm hover:bg-red-500 "
        >
          Remove account
        </button>
      </p>
    </div>
  );
}

function CreateAccount({
  onNewAccount,
}: {
  onNewAccount: (salt: string, accountId: string, password: string) => void;
}): VNode {
  const { i18n } = useTranslationContext();
  const Form = createNewForm<{
    password: string;
    repeat: string;
  }>();

  return (
    <div class="flex min-h-full flex-col ">
      <div class="sm:mx-auto sm:w-full sm:max-w-md">
        <h2 class="mt-6 text-center text-2xl font-bold leading-9 tracking-tight text-gray-900">
          Create account
        </h2>
      </div>

      <div class="mt-10 sm:mx-auto sm:w-full sm:max-w-[480px] ">
        <div class="bg-gray-100 px-6 py-6 shadow sm:rounded-lg sm:px-12">
          <Form.Provider
            computeFormState={(v) => {
              return {
                password: {
                  error: !v.password
                    ? i18n.str`required`
                    : v.password.length < 8
                    ? i18n.str`should have at least 8 characters`
                    : !v.password.match(/[a-z]/) && v.password.match(/[A-Z]/)
                    ? i18n.str`should have lowercase and uppercase characters`
                    : !v.password.match(/\d/)
                    ? i18n.str`should have numbers`
                    : !v.password.match(/[^a-zA-Z\d]/)
                    ? i18n.str`should have at least one character which is not a number or letter`
                    : undefined,
                },
                repeat: {
                  // error: !v.repeat
                  //   ? i18n.str`required`
                  //   // : v.repeat !== v.password
                  //   // ? i18n.str`doesn't match`
                  //   : undefined,
                },
              };
            }}
            onSubmit={async (v) => {
              const keys = await createNewAccount(v.password);
              onNewAccount(keys.salt, keys.accountId, v.password);
            }}
          >
            <div class="mb-4">
              <Form.InputLine
                label={"Password" as TranslatedString}
                name="password"
                type="password"
                help={
                  "lower and upper case letters, number and special character" as TranslatedString
                }
                required
              />
            </div>
            <div class="mb-4">
              <Form.InputLine
                label={"Repeat password" as TranslatedString}
                name="repeat"
                type="password"
                required
              />
            </div>

            <div class="mt-8">
              <button
                type="submit"
                class="flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 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"
              >
                Create
              </button>
            </div>
          </Form.Provider>
        </div>
      </div>
    </div>
  );
}

function UnlockAccount({
  salt,
  sealedKey,
  onAccountUnlocked,
}: {
  salt: string;
  sealedKey: string;
  onAccountUnlocked: (password: string) => void;
}): VNode {
  const Form = createNewForm<{
    password: string;
  }>();

  return (
    <div class="flex min-h-full flex-col ">
      <div class="sm:mx-auto sm:w-full sm:max-w-md">
        <h2 class="mt-6 text-center text-2xl font-bold leading-9 tracking-tight text-gray-900">
          Account locked
        </h2>
        <p class="mt-6 text-lg leading-8 text-gray-600">
          Your account is normally locked anytime you reload. To unlock type
          your password again.
        </p>
      </div>

      <div class="mt-10 sm:mx-auto sm:w-full sm:max-w-[480px] ">
        <div class="bg-gray-100 px-6 py-6 shadow sm:rounded-lg sm:px-12">
          <Form.Provider
            onSubmit={async (v) => {
              try {
                // test login
                await unlockAccount(salt, sealedKey, v.password);

                onAccountUnlocked(v.password ?? "");
                notifyInfo("Account unlocked" as TranslatedString);
              } catch (e) {
                if (e instanceof UnwrapKeyError) {
                  notifyError(
                    "Could not unlock account" as any,
                    e.message as any,
                  );
                } else {
                  throw e;
                }
              }
            }}
          >
            <div class="mb-4">
              <Form.InputLine
                label={"Password" as TranslatedString}
                name="password"
                type="password"
                required
              />
            </div>

            <div class="mt-8">
              <button
                type="submit"
                class="flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 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"
              >
                Unlock
              </button>
            </div>
          </Form.Provider>
        </div>
      </div>
    </div>
  );
}