@jskit-ai/auth-web 0.2.35 → 0.2.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ - The package manifest no longer requires `runtime.actions`: its feature declares no actions, so since the kernel's 9646ecf21 it does not require the action catalogue, and the catalog's manifest check reported the requirement as one no server provider has.
6
+ - `SecondFactorSettings` emits `codes-saved` when the person says they saved a new set of recovery codes (after setting an authenticator up, or replacing its codes), so a page that needs an authenticator before it goes on, such as a console's way in, goes on then; and its recovery codes step has **Download codes** beside **Copy codes**, which saves them as `recovery-codes.txt`, one a line (new message key `jskit.auth.secondFactor.codes.download`). `SecondFactorDialog` gives focus back to the element that had it when it opened, the control that sent the refused request, after the code as after a cancel: the dialog opens for a request and has no activator, so focus used to fall to the page's body.
5
7
  - **Second factor.** The routes `GET /api/second-factor`, `POST /api/second-factor/setup`, `/setup/confirm`, `/verify` and `/recovery-codes`, and `DELETE /api/second-factor` run the `auth.secondFactor.*` actions for the signed-in session, each with its own per-address limit in `AUTH_RATE_LIMITS` (`secondFactorSetup`, `secondFactorSetupConfirm` and `secondFactorVerify` 10 a minute, `secondFactorRecoveryCodes` and `secondFactorRemove` 5 a minute).
6
8
  - Client: `SecondFactorSettings` sets an authenticator app up (its QR code and key, then a first code), shows the recovery codes once, replaces them and removes the authenticator with a code or a recovery code, for an account settings section; `SecondFactorPrompt` proves the session's second factor; `SecondFactorDialog` with `useSecondFactorStepUp()` runs a request again after the person proves it, when its route refused it with `auth.secondFactor.required` (`run(request)`, binding `open`, `verified` and `cancel` to the dialog). A refused code shows beside its field; other failures go to the shell's snackbar. The three views come from their own paths (`@jskit-ai/auth-web/client/views/SecondFactorSettings`, `.../SecondFactorPrompt`, `.../SecondFactorDialog`), not `@jskit-ai/auth-web/client`, so only a page that shows one loads it; `useSecondFactorStepUp` comes from `@jskit-ai/auth-web/client`. New dependency `uqr` (MIT) draws the QR code, on the settings page alone. New message keys under `jskit.auth.secondFactor` (an app with other locales adds them with `npx jskit i18n export <locale>`).
7
9
  - The sign-in screen's action rows wrap: on a phone, "Confirm your email"'s three actions (go to the main screen, resend the confirmation email, back to sign in) reflow onto a second line inside the card, as Material 3 reflows actions that do not fit, where the row clipped its last button at 390px. The reset and one-time-code rows and the forgot-password links wrap the same way, keeping their 12px gap.
@@ -74,6 +74,16 @@ const HomePage = {
74
74
  ]);
75
75
  }
76
76
  };
77
+ // An account settings section with the authenticator, which counts the times the person said they saved new codes.
78
+ const SecondFactorPage = {
79
+ setup() {
80
+ const codesSaved = ref(0);
81
+ return () => h("div", [
82
+ h(SecondFactorSettings, { onCodesSaved: () => { codesSaved.value += 1; } }),
83
+ h("p", { "data-testid": "codes-saved" }, `Codes saved: ${codesSaved.value}`)
84
+ ]);
85
+ }
86
+ };
77
87
  // An application's console: its refund route asks for a code proved within five minutes, through the step-up dialog.
78
88
  const ConsolePage = {
79
89
  setup() {
@@ -111,7 +121,7 @@ const router = createRouter({
111
121
  history: createWebHistory(),
112
122
  routes: [
113
123
  { path: "/home", component: HomePage },
114
- { path: "/account/second-factor", component: SecondFactorSettings },
124
+ { path: "/account/second-factor", component: SecondFactorPage },
115
125
  { path: "/console", component: ConsolePage },
116
126
  { path: "/auth/login", component: DefaultLoginView },
117
127
  { path: "/auth/reset-password", component: DefaultResetPasswordView },
package/locales/en.json CHANGED
@@ -166,6 +166,7 @@
166
166
  "explain": "If you lose your authenticator app, each of these codes confirms it's you once. They are shown only now: keep them somewhere safe.",
167
167
  "listLabel": "Recovery codes",
168
168
  "copy": "Copy codes",
169
+ "download": "Download codes",
169
170
  "saved": "I've saved them"
170
171
  },
171
172
  "replace": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/auth-web",
3
- "version": "0.2.35",
3
+ "version": "0.2.36",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -31,10 +31,10 @@
31
31
  "uqr": "0.1.3"
32
32
  },
33
33
  "peerDependencies": {
34
- "@jskit-ai/auth-core": "0.2.23",
35
- "@jskit-ai/http-runtime": "0.2.22",
36
- "@jskit-ai/kernel": "0.2.22",
37
- "@jskit-ai/shell-web": "0.2.31",
34
+ "@jskit-ai/auth-core": "0.2.24",
35
+ "@jskit-ai/http-runtime": "0.2.23",
36
+ "@jskit-ai/kernel": "0.2.23",
37
+ "@jskit-ai/shell-web": "0.2.32",
38
38
  "@tanstack/vue-query": "^5.90.5",
39
39
  "pinia": "^4.0.3",
40
40
  "vue": "^3.5.44",
@@ -50,7 +50,6 @@
50
50
  ],
51
51
  "requires": [
52
52
  "auth.service",
53
- "runtime.actions",
54
53
  "runtime.config",
55
54
  "runtime.env",
56
55
  "runtime.http"
@@ -23,6 +23,33 @@ function pageClipboard() {
23
23
  return pageWindow()?.navigator.clipboard || null;
24
24
  }
25
25
 
26
+ /** The element that has the page's focus; null outside a browser. */
27
+ function pageActiveElement() {
28
+ return pageDocument()?.activeElement || null;
29
+ }
30
+
31
+ /**
32
+ * Has the browser save `text` as a file named `fileName`; does nothing outside a browser.
33
+ * @param {string} fileName
34
+ * @param {string} text
35
+ */
36
+ function saveTextFile(fileName, text) {
37
+ const window = pageWindow();
38
+ const document = pageDocument();
39
+ if (!window || !document) {
40
+ return;
41
+ }
42
+ const url = window.URL.createObjectURL(new window.Blob([text], { type: "text/plain;charset=utf-8" }));
43
+ const link = document.createElement("a");
44
+ link.href = url;
45
+ link.download = fileName;
46
+ document.body.append(link);
47
+ link.click();
48
+ link.remove();
49
+ // After the click has started the download, which reads the file first.
50
+ window.setTimeout(() => window.URL.revokeObjectURL(url), 0);
51
+ }
52
+
26
53
  /**
27
54
  * Replaces the page with the one at `path`; does nothing outside a browser.
28
55
  * @param {string} path
@@ -35,9 +62,11 @@ function replacePage(path) {
35
62
  }
36
63
 
37
64
  export {
65
+ pageActiveElement,
38
66
  pageClipboard,
39
67
  pageDocument,
40
68
  pageLocation,
41
69
  pageWindow,
42
- replacePage
70
+ replacePage,
71
+ saveTextFile
43
72
  };
@@ -1,11 +1,12 @@
1
1
  import { computed, onMounted, ref } from "vue";
2
2
  import { useShellWebErrorRuntime } from "@jskit-ai/shell-web/client/error";
3
- import { pageClipboard } from "../../browser.js";
3
+ import { pageClipboard, saveTextFile } from "../../browser.js";
4
4
  import { useJskitText } from "../../i18n.js";
5
5
  import { qrCodePath } from "../../lib/qrCode.js";
6
6
  import { readSecondFactor, startSecondFactorSetup } from "../../runtime/secondFactorRequests.js";
7
7
 
8
8
  const SOURCE = "auth-web.second-factor-settings";
9
+ const RECOVERY_CODES_FILE = "recovery-codes.txt";
9
10
 
10
11
  /**
11
12
  * The signed-in person's authenticator app: its state, read on mount (`load` is "loading", "ready" or "error"), and
@@ -97,6 +98,11 @@ function useSecondFactorSettings() {
97
98
  }
98
99
  }
99
100
 
101
+ /** Saves the recovery codes shown as a text file, one code a line, for a person who keeps them as a file. */
102
+ function download() {
103
+ saveTextFile(RECOVERY_CODES_FILE, `${recoveryCodes.value.join("\n")}\n`);
104
+ }
105
+
100
106
  onMounted(reload);
101
107
 
102
108
  return {
@@ -115,7 +121,8 @@ function useSecondFactorSettings() {
115
121
  codesSaved,
116
122
  removed,
117
123
  showStep,
118
- copy
124
+ copy,
125
+ download
119
126
  };
120
127
  }
121
128
 
@@ -18,16 +18,38 @@
18
18
  </template>
19
19
 
20
20
  <script setup>
21
+ import { nextTick, watch } from "vue";
21
22
  import SecondFactorPrompt from "./SecondFactorPrompt.vue";
23
+ import { pageActiveElement } from "../browser.js";
22
24
  import { useJskitText } from "../i18n.js";
23
25
 
24
26
  // The step-up dialog: bind `open`, `verified` and `cancel` to useSecondFactorStepUp()'s. Escape and a click outside
25
27
  // cancel, as the Cancel button does.
26
- defineProps({
28
+ const props = defineProps({
27
29
  open: { type: Boolean, required: true }
28
30
  });
29
31
  const emit = defineEmits(["verified", "cancel"]);
30
32
  const { t } = useJskitText();
33
+
34
+ /**
35
+ * A request opens the dialog, not a button of its own, so the dialog has no activator to give focus back to: it keeps
36
+ * the element that had focus when it opened (the control that sent the request) and focuses it again once it closes,
37
+ * verified or cancelled, so a keyboard carries on where it was.
38
+ * @type {HTMLElement | null}
39
+ */
40
+ let returnFocusTo = null;
41
+ watch(() => props.open, async (open) => {
42
+ if (open) {
43
+ returnFocusTo = /** @type {HTMLElement | null} */ (pageActiveElement());
44
+ return;
45
+ }
46
+ const element = returnFocusTo;
47
+ returnFocusTo = null;
48
+ await nextTick();
49
+ if (element?.isConnected) {
50
+ element.focus({ preventScroll: true });
51
+ }
52
+ });
31
53
  </script>
32
54
 
33
55
  <style scoped>
@@ -10,6 +10,9 @@
10
10
  <v-btn v-if="canCopy" variant="text" prepend-icon="$contentCopy" @click="copy(recoveryCodes.join('\n'))">
11
11
  {{ t("jskit.auth.secondFactor.codes.copy") }}
12
12
  </v-btn>
13
+ <v-btn variant="text" prepend-icon="$download" @click="download">
14
+ {{ t("jskit.auth.secondFactor.codes.download") }}
15
+ </v-btn>
13
16
  <v-btn color="primary" variant="flat" @click="onCodesSaved">{{ t("jskit.auth.secondFactor.codes.saved") }}</v-btn>
14
17
  </div>
15
18
  </div>
@@ -126,7 +129,10 @@ import {
126
129
  } from "../runtime/secondFactorRequests.js";
127
130
 
128
131
  // The signed-in person's authenticator app: setting one up, its recovery codes, and removing it. An account settings
129
- // section places it; the section's title names it.
132
+ // section places it; the section's title names it. `codes-saved` tells the page that the person said they saved a new
133
+ // set of recovery codes (the last step of setting one up, or of replacing its codes), so a page that asked for an
134
+ // authenticator before it goes on, such as a console's way in, can go on.
135
+ const emit = defineEmits(["codes-saved"]);
130
136
  const { formatDate, t } = useJskitText();
131
137
  const {
132
138
  load,
@@ -144,7 +150,8 @@ const {
144
150
  codesSaved,
145
151
  removed,
146
152
  showStep,
147
- copy
153
+ copy,
154
+ download
148
155
  } = useSecondFactorSettings();
149
156
 
150
157
  // After a step ends, focus moves to the start of the next, so a keyboard or screen reader carries on from there.
@@ -163,6 +170,7 @@ function onRecoveryCodes(result) {
163
170
 
164
171
  function onCodesSaved() {
165
172
  codesSaved();
173
+ emit("codes-saved");
166
174
  void focusStep();
167
175
  }
168
176
 
@@ -1,4 +1,5 @@
1
1
  import assert from "node:assert/strict";
2
+ import { readFile } from "node:fs/promises";
2
3
  import process from "node:process";
3
4
  import { after, before, test } from "node:test";
4
5
  import { fileURLToPath } from "node:url";
@@ -95,7 +96,7 @@ async function finish(api) {
95
96
  await api.page.close();
96
97
  }
97
98
 
98
- test("on a phone, setting up an authenticator shows its QR code and key, a wrong code beside the field, and the recovery codes once", options, async () => {
99
+ test("on a phone, setting up an authenticator shows its QR code and key, a wrong code beside the field, and the recovery codes once, to download, telling the page when they are saved", options, async () => {
99
100
  const api = await openApp({
100
101
  "GET /api/second-factor": [refused(503, "", "Service unavailable."), { json: NOT_ENROLLED }, { json: ENROLLED }],
101
102
  "POST /api/second-factor/setup": [{
@@ -133,7 +134,14 @@ test("on a phone, setting up an authenticator shows its QR code and key, a wrong
133
134
  await expect(codesTitle).toBeFocused();
134
135
  await expect(page.getByRole("list", { name: "Recovery codes" }).getByRole("listitem")).toHaveText(RECOVERY_CODES);
135
136
  await fitsTheScreen(page);
137
+ // The codes download as a text file, one a line.
138
+ const [saved] = await Promise.all([page.waitForEvent("download"), page.getByRole("button", { name: "Download codes" }).click()]);
139
+ assert.equal(saved.suggestedFilename(), "recovery-codes.txt");
140
+ assert.equal(await readFile(await saved.path(), "utf8"), `${RECOVERY_CODES.join("\n")}\n`);
141
+ // The page hears that the codes are saved once the person says so, not before.
142
+ await expect(page.getByTestId("codes-saved")).toHaveText("Codes saved: 0");
136
143
  await page.getByRole("button", { name: "I've saved them" }).click();
144
+ await expect(page.getByTestId("codes-saved")).toHaveText("Codes saved: 1");
137
145
  await expect(page.getByText(/^Authenticator app set up on /u)).toBeFocused();
138
146
  await expect(page.getByText("10 recovery codes left.")).toBeVisible();
139
147
  await expect(page.getByText(RECOVERY_CODES[0])).toHaveCount(0);
@@ -182,7 +190,7 @@ test("removing the authenticator takes a code or a recovery code, shows a wait b
182
190
  await finish(api);
183
191
  });
184
192
 
185
- test("a route that asks for a fresh code opens the step-up dialog, runs again after the code, and Escape cancels it", options, async () => {
193
+ test("a route that asks for a fresh code opens the step-up dialog, runs again after the code, and Escape cancels it, focus returning to the control each time", options, async () => {
186
194
  const api = await openApp({
187
195
  "POST /api/console/refund": [REQUIRED, { json: { refunded: true } }, REQUIRED],
188
196
  "POST /api/second-factor/verify": [
@@ -205,12 +213,15 @@ test("a route that asks for a fresh code opens the step-up dialog, runs again af
205
213
  await page.keyboard.press("Enter");
206
214
  await expect(page.getByTestId("console-outcome")).toHaveText("Refunded");
207
215
  await expect(dialog).toBeHidden();
216
+ // Focus is back on the control that sent the request, after a code as after cancelling.
217
+ await expect(page.getByRole("button", { name: "Refund" })).toBeFocused();
208
218
 
209
- await page.getByRole("button", { name: "Refund" }).click();
219
+ await page.keyboard.press("Enter");
210
220
  await expect(dialog.getByLabel("6-digit code")).toBeFocused();
211
221
  await page.keyboard.press("Escape");
212
222
  await expect(dialog).toBeHidden();
213
223
  await expect(page.getByTestId("console-outcome")).toHaveText("Refund failed: Confirm it's you with a code from your authenticator app.");
224
+ await expect(page.getByRole("button", { name: "Refund" })).toBeFocused();
214
225
 
215
226
  assert.equal(bodiesOf(api, "POST /api/console/refund").length, 3);
216
227
  assert.deepEqual(bodiesOf(api, "POST /api/second-factor/verify"), [{ code: "000000" }, { code: "123456" }]);