@jskit-ai/auth-web 0.2.33 → 0.2.35

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
+ - **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
+ - 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>`).
5
7
  - 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.
6
8
  - The sign-in, registration and reset-password fields have no margin under them, since each keeps its supporting-text line (`@jskit-ai/material-web` no longer sets `hideDetails: "auto"`): an email or password error shown on blur appears in place instead of pushing the form down.
7
9
  - `loginRequestAsExistingUser(request, options)` (new, `@jskit-ai/auth-web/test/playwright`) signs a Playwright APIRequestContext in, as `loginAsExistingUser()` signs a page's context in: a browser test `signIn` module uses it to sign each worker in once.
@@ -1,4 +1,4 @@
1
- import { createApp, h } from "vue";
1
+ import { createApp, h, ref } from "vue";
2
2
  import { createPinia } from "pinia";
3
3
  import { createRouter, createWebHistory, RouterView } from "vue-router";
4
4
  import { QueryClient, VueQueryPlugin } from "@tanstack/vue-query";
@@ -14,6 +14,10 @@ import AuthProfileWidget from "../../src/client/views/AuthProfileWidget.vue";
14
14
  import DefaultLoginView from "../../src/client/views/DefaultLoginView.vue";
15
15
  import DefaultResetPasswordView from "../../src/client/views/DefaultResetPasswordView.vue";
16
16
  import DefaultSignOutView from "../../src/client/views/DefaultSignOutView.vue";
17
+ import SecondFactorDialog from "../../src/client/views/SecondFactorDialog.vue";
18
+ import SecondFactorSettings from "../../src/client/views/SecondFactorSettings.vue";
19
+ import { useSecondFactorStepUp } from "../../src/client/composables/secondFactor/useSecondFactorStepUp.js";
20
+ import { authHttpRequest } from "../../src/client/runtime/authHttpClient.js";
17
21
  import { AuthWebClientProvider } from "../../src/client/providers/AuthWebClientProvider.js";
18
22
  import { useAuthStore } from "../../src/client/stores/useAuthStore.js";
19
23
  import PatternLoginPage from "../../patterns/auth-surface/example/src/pages/auth/login.vue";
@@ -70,6 +74,26 @@ const HomePage = {
70
74
  ]);
71
75
  }
72
76
  };
77
+ // An application's console: its refund route asks for a code proved within five minutes, through the step-up dialog.
78
+ const ConsolePage = {
79
+ setup() {
80
+ const { open, run, verified, cancel } = useSecondFactorStepUp();
81
+ const outcome = ref("");
82
+ async function refund() {
83
+ try {
84
+ await run(() => authHttpRequest("/api/console/refund", { method: "POST" }));
85
+ outcome.value = "Refunded";
86
+ } catch (error) {
87
+ outcome.value = `Refund failed: ${error.message}`;
88
+ }
89
+ }
90
+ return () => h("div", [
91
+ h(components.VBtn, { onClick: refund }, () => "Refund"),
92
+ h("p", { "data-testid": "console-outcome" }, outcome.value),
93
+ h(SecondFactorDialog, { open: open.value, onVerified: verified, onCancel: cancel })
94
+ ]);
95
+ }
96
+ };
73
97
  // An app's brand in the auth views' brand slot: a pane's wordmark and line on wide windows, a compact row on phones.
74
98
  function branded(View) {
75
99
  return {
@@ -87,6 +111,8 @@ const router = createRouter({
87
111
  history: createWebHistory(),
88
112
  routes: [
89
113
  { path: "/home", component: HomePage },
114
+ { path: "/account/second-factor", component: SecondFactorSettings },
115
+ { path: "/console", component: ConsolePage },
90
116
  { path: "/auth/login", component: DefaultLoginView },
91
117
  { path: "/auth/reset-password", component: DefaultResetPasswordView },
92
118
  { path: "/auth/signout", component: DefaultSignOutView },
@@ -1,11 +1,16 @@
1
+ import process from "node:process";
1
2
  import { fileURLToPath } from "node:url";
2
3
  import { defineConfig } from "vite";
3
4
  import vue from "@vitejs/plugin-vue";
4
5
 
6
+ // A test with a real auth server behind the page names it here, and /api goes to it; the other tests answer /api in
7
+ // the page themselves.
8
+ const apiServer = process.env.JSKIT_AUTH_WEB_FIXTURE_API_SERVER;
9
+
5
10
  export default defineConfig({
6
11
  root: fileURLToPath(new URL("./", import.meta.url)),
7
12
  // The auth-surface pattern's pages and view wrappers import the app's own files through "@/", as in an app.
8
13
  resolve: { alias: { "@": fileURLToPath(new URL("../../patterns/auth-surface/example/src", import.meta.url)) } },
9
14
  plugins: [vue()],
10
- server: { host: "127.0.0.1", strictPort: true }
15
+ server: { host: "127.0.0.1", strictPort: true, ...(apiServer ? { proxy: { "/api": apiServer } } : {}) }
11
16
  });
package/locales/en.json CHANGED
@@ -124,6 +124,57 @@
124
124
  },
125
125
  "profile": {
126
126
  "guest": "Guest"
127
+ },
128
+ "secondFactor": {
129
+ "confirm": "Confirm",
130
+ "cancel": "Cancel",
131
+ "copied": "Copied.",
132
+ "copyFailed": "Could not copy. Select the text and copy it instead.",
133
+ "form": {
134
+ "code": "6-digit code",
135
+ "recoveryCode": "Recovery code",
136
+ "useRecoveryCode": "Use a recovery code",
137
+ "useCode": "Use a code from the app",
138
+ "failed": "Could not check the code."
139
+ },
140
+ "prompt": {
141
+ "title": "Confirm it's you",
142
+ "explain": "Enter the code your authenticator app shows now."
143
+ },
144
+ "settings": {
145
+ "off": "No authenticator app is set up. Set one up to confirm it's you with a code when a page asks for one.",
146
+ "setUp": "Set up authenticator",
147
+ "startFailed": "Could not start setting up the authenticator.",
148
+ "on": "Authenticator app set up on {date}.",
149
+ "codesLeft": "No recovery codes left: create new ones. | 1 recovery code left. | {count} recovery codes left.",
150
+ "newCodes": "New recovery codes",
151
+ "remove": "Remove authenticator",
152
+ "removed": "Authenticator removed.",
153
+ "loadFailed": "Could not load your authenticator settings.",
154
+ "retry": "Retry"
155
+ },
156
+ "setup": {
157
+ "title": "Set up an authenticator app",
158
+ "scan": "Scan this QR code with your authenticator app.",
159
+ "qrLabel": "QR code to add {account} to an authenticator app",
160
+ "typeKey": "Can't scan it? Enter this key in the app instead:",
161
+ "copyKey": "Copy key",
162
+ "enterCode": "Then enter the 6-digit code the app shows."
163
+ },
164
+ "codes": {
165
+ "title": "Save your recovery codes",
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
+ "listLabel": "Recovery codes",
168
+ "copy": "Copy codes",
169
+ "saved": "I've saved them"
170
+ },
171
+ "replace": {
172
+ "explain": "Enter a code to create new recovery codes. Your old recovery codes stop working.",
173
+ "submit": "Create new codes"
174
+ },
175
+ "remove": {
176
+ "explain": "Enter a code to remove the authenticator app. Pages that ask for a code won't open until you set one up again."
177
+ }
127
178
  }
128
179
  }
129
180
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/auth-web",
3
- "version": "0.2.33",
3
+ "version": "0.2.35",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -17,6 +17,9 @@
17
17
  "./client/views/DefaultLoginView": "./src/client/views/DefaultLoginView.vue",
18
18
  "./client/views/DefaultResetPasswordView": "./src/client/views/DefaultResetPasswordView.vue",
19
19
  "./client/views/DefaultSignOutView": "./src/client/views/DefaultSignOutView.vue",
20
+ "./client/views/SecondFactorDialog": "./src/client/views/SecondFactorDialog.vue",
21
+ "./client/views/SecondFactorPrompt": "./src/client/views/SecondFactorPrompt.vue",
22
+ "./client/views/SecondFactorSettings": "./src/client/views/SecondFactorSettings.vue",
20
23
  "./client/runtime/authGuardRuntime": "./src/client/runtime/authGuardRuntime.js",
21
24
  "./client/runtime/authHttpClient": "./src/client/runtime/authHttpClient.js",
22
25
  "./client/runtime/oauthCallbackRuntime": "./src/client/runtime/oauthCallbackRuntime.js",
@@ -24,11 +27,14 @@
24
27
  "./test/playwright": "./src/test/playwright.js",
25
28
  "./locales/en.json": "./locales/en.json"
26
29
  },
30
+ "dependencies": {
31
+ "uqr": "0.1.3"
32
+ },
27
33
  "peerDependencies": {
28
- "@jskit-ai/auth-core": "0.2.21",
29
- "@jskit-ai/http-runtime": "0.2.21",
30
- "@jskit-ai/kernel": "0.2.21",
31
- "@jskit-ai/shell-web": "0.2.30",
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",
32
38
  "@tanstack/vue-query": "^5.90.5",
33
39
  "pinia": "^4.0.3",
34
40
  "vue": "^3.5.44",
@@ -74,7 +80,7 @@
74
80
  "surfaces": [
75
81
  {
76
82
  "subpath": "./client",
77
- "summary": "Exports auth web client provider, default auth views, and route/provider registration surface."
83
+ "summary": "Exports auth web client provider, default auth views, the second-factor step-up composable, and route/provider registration surface."
78
84
  },
79
85
  {
80
86
  "subpath": "./client/types",
@@ -157,6 +163,36 @@
157
163
  "path": "/api/register",
158
164
  "summary": "Register a new user"
159
165
  },
166
+ {
167
+ "method": "DELETE",
168
+ "path": "/api/second-factor",
169
+ "summary": "Remove the authenticator, with a proof"
170
+ },
171
+ {
172
+ "method": "GET",
173
+ "path": "/api/second-factor",
174
+ "summary": "Read the account's second factor and when this session proved it"
175
+ },
176
+ {
177
+ "method": "POST",
178
+ "path": "/api/second-factor/recovery-codes",
179
+ "summary": "Replace the recovery codes, with a proof: the new codes, shown once"
180
+ },
181
+ {
182
+ "method": "POST",
183
+ "path": "/api/second-factor/setup",
184
+ "summary": "Start setting up an authenticator app: its secret, shown once"
185
+ },
186
+ {
187
+ "method": "POST",
188
+ "path": "/api/second-factor/setup/confirm",
189
+ "summary": "Confirm the authenticator with its first code: the recovery codes, shown once"
190
+ },
191
+ {
192
+ "method": "POST",
193
+ "path": "/api/second-factor/verify",
194
+ "summary": "Prove the second factor for this session with a code or a recovery code"
195
+ },
160
196
  {
161
197
  "method": "GET",
162
198
  "path": "/api/session",
@@ -18,6 +18,11 @@ function pageLocation() {
18
18
  return pageWindow()?.location || null;
19
19
  }
20
20
 
21
+ /** The page's clipboard; null outside a browser, and on a page the browser does not trust with it (one not on HTTPS). */
22
+ function pageClipboard() {
23
+ return pageWindow()?.navigator.clipboard || null;
24
+ }
25
+
21
26
  /**
22
27
  * Replaces the page with the one at `path`; does nothing outside a browser.
23
28
  * @param {string} path
@@ -30,6 +35,7 @@ function replacePage(path) {
30
35
  }
31
36
 
32
37
  export {
38
+ pageClipboard,
33
39
  pageDocument,
34
40
  pageLocation,
35
41
  pageWindow,
@@ -0,0 +1,79 @@
1
+ import { computed, ref } from "vue";
2
+ import { useShellWebErrorRuntime } from "@jskit-ai/shell-web/client/error";
3
+ import { useJskitText } from "../../i18n.js";
4
+
5
+ // Refusals of what the person typed (a wrong, used or missing code, or an account waiting after wrong codes) show
6
+ // beside the field; any other failure is reported as a failed command.
7
+ const FIELD_REFUSAL_STATUSES = Object.freeze([400, 409, 429]);
8
+ const SOURCE = "auth-web.second-factor";
9
+
10
+ /**
11
+ * A form for a code from the authenticator app or one of the recovery codes: what the person typed, the refusal shown
12
+ * beside the field, and the submission, which hands `submit`'s answer to `onDone`.
13
+ * @param {{
14
+ * submit: (proof: import("../../types.js").SecondFactorProof) => Promise<unknown>,
15
+ * onDone: (result: unknown) => void
16
+ * }} options
17
+ */
18
+ function useSecondFactorProof({ submit, onDone }) {
19
+ const { fieldErrorMessages, serverMessage, t } = useJskitText();
20
+ const errorRuntime = useShellWebErrorRuntime();
21
+ const usingRecoveryCode = ref(false);
22
+ const code = ref("");
23
+ const recoveryCode = ref("");
24
+ const fieldError = ref("");
25
+ const pending = ref(false);
26
+ const typed = computed(() => (usingRecoveryCode.value ? recoveryCode.value : code.value).trim());
27
+ const canSubmit = computed(() => typed.value.length > 0 && !pending.value);
28
+
29
+ function toggleRecoveryCode() {
30
+ usingRecoveryCode.value = !usingRecoveryCode.value;
31
+ fieldError.value = "";
32
+ }
33
+
34
+ async function submitProof() {
35
+ if (!canSubmit.value) {
36
+ return;
37
+ }
38
+ const field = usingRecoveryCode.value ? "recoveryCode" : "code";
39
+ pending.value = true;
40
+ fieldError.value = "";
41
+ let result;
42
+ try {
43
+ result = await submit({ [field]: typed.value });
44
+ } catch (error) {
45
+ const message = fieldErrorMessages(error)[field] || serverMessage(error, t("jskit.auth.secondFactor.form.failed"));
46
+ if (FIELD_REFUSAL_STATUSES.includes(error.status)) {
47
+ fieldError.value = message;
48
+ } else {
49
+ errorRuntime.report({
50
+ source: SOURCE,
51
+ message,
52
+ severity: "error",
53
+ channel: "snackbar",
54
+ dedupeKey: `${SOURCE}:${message}`,
55
+ dedupeWindowMs: 3000
56
+ });
57
+ }
58
+ return;
59
+ } finally {
60
+ pending.value = false;
61
+ }
62
+ code.value = "";
63
+ recoveryCode.value = "";
64
+ onDone(result);
65
+ }
66
+
67
+ return {
68
+ usingRecoveryCode,
69
+ code,
70
+ recoveryCode,
71
+ fieldError,
72
+ pending,
73
+ canSubmit,
74
+ toggleRecoveryCode,
75
+ submitProof
76
+ };
77
+ }
78
+
79
+ export { useSecondFactorProof };
@@ -0,0 +1,122 @@
1
+ import { computed, onMounted, ref } from "vue";
2
+ import { useShellWebErrorRuntime } from "@jskit-ai/shell-web/client/error";
3
+ import { pageClipboard } from "../../browser.js";
4
+ import { useJskitText } from "../../i18n.js";
5
+ import { qrCodePath } from "../../lib/qrCode.js";
6
+ import { readSecondFactor, startSecondFactorSetup } from "../../runtime/secondFactorRequests.js";
7
+
8
+ const SOURCE = "auth-web.second-factor-settings";
9
+
10
+ /**
11
+ * The signed-in person's authenticator app: its state, read on mount (`load` is "loading", "ready" or "error"), and
12
+ * the step shown: "status", "setup" (the secret, once, and its first code), "codes" (new recovery codes, once), or
13
+ * "replaceCodes" and "remove", which each ask for a proof.
14
+ */
15
+ function useSecondFactorSettings() {
16
+ const { serverMessage, t } = useJskitText();
17
+ const errorRuntime = useShellWebErrorRuntime();
18
+ const load = ref("loading");
19
+ /** @type {import("vue").Ref<import("../../types.js").SecondFactorStatus | null>} */
20
+ const status = ref(null);
21
+ const step = ref("status");
22
+ /** @type {import("vue").Ref<import("../../types.js").SecondFactorSetup | null>} */
23
+ const setup = ref(null);
24
+ /** @type {import("vue").Ref<string[]>} */
25
+ const recoveryCodes = ref([]);
26
+ const starting = ref(false);
27
+ const canCopy = Boolean(pageClipboard());
28
+ const qrCode = computed(() => (setup.value ? qrCodePath(setup.value.otpauthUrl) : null));
29
+ // The secret in groups of four, as a person reads it out to type it.
30
+ const secretText = computed(() => (setup.value ? setup.value.secret.replace(/(.{4})(?=.)/gu, "$1 ") : ""));
31
+
32
+ function report(message, severity) {
33
+ errorRuntime.report({
34
+ source: SOURCE,
35
+ message,
36
+ severity,
37
+ channel: "snackbar",
38
+ dedupeKey: `${SOURCE}:${message}`,
39
+ dedupeWindowMs: 3000
40
+ });
41
+ }
42
+
43
+ async function reload() {
44
+ load.value = "loading";
45
+ try {
46
+ status.value = await readSecondFactor();
47
+ load.value = "ready";
48
+ } catch {
49
+ load.value = "error";
50
+ }
51
+ }
52
+
53
+ async function startSetup() {
54
+ starting.value = true;
55
+ try {
56
+ setup.value = await startSecondFactorSetup();
57
+ step.value = "setup";
58
+ } catch (error) {
59
+ report(serverMessage(error, t("jskit.auth.secondFactor.settings.startFailed")), "error");
60
+ } finally {
61
+ starting.value = false;
62
+ }
63
+ }
64
+
65
+ /** Shows new recovery codes, once, and reads the state again behind them. */
66
+ function showRecoveryCodes(/** @type {import("../../types.js").SecondFactorRecoveryCodes} */ result) {
67
+ recoveryCodes.value = result.recoveryCodes;
68
+ setup.value = null;
69
+ step.value = "codes";
70
+ void reload();
71
+ }
72
+
73
+ function codesSaved() {
74
+ recoveryCodes.value = [];
75
+ step.value = "status";
76
+ }
77
+
78
+ function removed() {
79
+ step.value = "status";
80
+ report(t("jskit.auth.secondFactor.settings.removed"), "success");
81
+ void reload();
82
+ }
83
+
84
+ /** @param {"status" | "replaceCodes" | "remove"} name */
85
+ function showStep(name) {
86
+ setup.value = null;
87
+ step.value = name;
88
+ }
89
+
90
+ /** @param {string} text */
91
+ async function copy(text) {
92
+ try {
93
+ await pageClipboard().writeText(text);
94
+ report(t("jskit.auth.secondFactor.copied"), "success");
95
+ } catch {
96
+ report(t("jskit.auth.secondFactor.copyFailed"), "error");
97
+ }
98
+ }
99
+
100
+ onMounted(reload);
101
+
102
+ return {
103
+ load,
104
+ status,
105
+ step,
106
+ setup,
107
+ qrCode,
108
+ secretText,
109
+ recoveryCodes,
110
+ starting,
111
+ canCopy,
112
+ reload,
113
+ startSetup,
114
+ showRecoveryCodes,
115
+ codesSaved,
116
+ removed,
117
+ showStep,
118
+ copy
119
+ };
120
+ }
121
+
122
+ export { useSecondFactorSettings };
@@ -0,0 +1,51 @@
1
+ import { ref } from "vue";
2
+ import { isSecondFactorRequired } from "@jskit-ai/auth-core/shared/authSecondFactor";
3
+
4
+ /**
5
+ * Runs requests a route may refuse until the session proves its second factor. When one is refused for that, `open`
6
+ * turns true (bind it to `SecondFactorDialog`) and the request waits: after `verified()` it runs once more, and after
7
+ * `cancel()` its refusal is thrown to the caller.
8
+ */
9
+ function useSecondFactorStepUp() {
10
+ const open = ref(false);
11
+ /** @type {{ resolve: () => void, reject: () => void }[]} */
12
+ const waiting = [];
13
+
14
+ /**
15
+ * @template T
16
+ * @param {() => Promise<T>} request
17
+ * @returns {Promise<T>}
18
+ */
19
+ async function run(request) {
20
+ try {
21
+ return await request();
22
+ } catch (error) {
23
+ if (!isSecondFactorRequired(error)) {
24
+ throw error;
25
+ }
26
+ await new Promise((resolve, reject) => {
27
+ waiting.push({ resolve, reject: () => reject(error) });
28
+ open.value = true;
29
+ });
30
+ return request();
31
+ }
32
+ }
33
+
34
+ function verified() {
35
+ open.value = false;
36
+ for (const request of waiting.splice(0)) {
37
+ request.resolve();
38
+ }
39
+ }
40
+
41
+ function cancel() {
42
+ open.value = false;
43
+ for (const request of waiting.splice(0)) {
44
+ request.reject();
45
+ }
46
+ }
47
+
48
+ return { open, run, verified, cancel };
49
+ }
50
+
51
+ export { useSecondFactorStepUp };
@@ -8,6 +8,7 @@ export { default as DefaultSignOutView } from "./views/DefaultSignOutView.vue";
8
8
  export { default as DefaultResetPasswordView } from "./views/DefaultResetPasswordView.vue";
9
9
  export { default as AuthProfileWidget } from "./views/AuthProfileWidget.vue";
10
10
  export { default as AuthProfileMenuLinkItem } from "./views/AuthProfileMenuLinkItem.vue";
11
+ export { useSecondFactorStepUp } from "./composables/secondFactor/useSecondFactorStepUp.js";
11
12
  export { useAuthStore } from "./stores/useAuthStore.js";
12
13
  export { useAuthGuardRuntime } from "./runtime/inject.js";
13
14
  export {
@@ -0,0 +1,32 @@
1
+ import { encode } from "uqr";
2
+
3
+ // The quiet zone a QR code needs around it for a scanner to find it: four modules on each side.
4
+ const QUIET_ZONE = 4;
5
+
6
+ /**
7
+ * The QR code of `text` as one SVG path in a square of `size` modules, its quiet zone included: each run of dark
8
+ * modules along a row is one rectangle.
9
+ * @param {string} text
10
+ * @returns {{ size: number, path: string }}
11
+ */
12
+ function qrCodePath(text) {
13
+ const { size, data } = encode(text, { border: QUIET_ZONE });
14
+ let path = "";
15
+ data.forEach((row, y) => {
16
+ let x = 0;
17
+ while (x < size) {
18
+ if (!row[x]) {
19
+ x += 1;
20
+ continue;
21
+ }
22
+ const start = x;
23
+ while (x < size && row[x]) {
24
+ x += 1;
25
+ }
26
+ path += `M${start} ${y}h${x - start}v1h-${x - start}z`;
27
+ }
28
+ });
29
+ return { size, path };
30
+ }
31
+
32
+ export { qrCodePath };
@@ -0,0 +1,56 @@
1
+ import { AUTH_PATHS } from "@jskit-ai/auth-core/shared/authPaths";
2
+ import { authHttpRequest } from "./authHttpClient.js";
3
+
4
+ // The signed-in person's second factor, through auth-web's routes. A proof is a code from the authenticator app or one
5
+ // of the recovery codes.
6
+
7
+ /** @returns {Promise<import("../types.js").SecondFactorStatus>} */
8
+ function readSecondFactor() {
9
+ return authHttpRequest(AUTH_PATHS.SECOND_FACTOR, { method: "GET" });
10
+ }
11
+
12
+ /** @returns {Promise<import("../types.js").SecondFactorSetup>} */
13
+ function startSecondFactorSetup() {
14
+ return authHttpRequest(AUTH_PATHS.SECOND_FACTOR_SETUP, { method: "POST" });
15
+ }
16
+
17
+ /**
18
+ * @param {import("../types.js").SecondFactorProof} proof The authenticator's first code.
19
+ * @returns {Promise<import("../types.js").SecondFactorRecoveryCodes>}
20
+ */
21
+ function confirmSecondFactorSetup(proof) {
22
+ return authHttpRequest(AUTH_PATHS.SECOND_FACTOR_SETUP_CONFIRM, { method: "POST", body: proof });
23
+ }
24
+
25
+ /**
26
+ * @param {import("../types.js").SecondFactorProof} proof
27
+ * @returns {Promise<{ verifiedAt: string, method: "code" | "recoveryCode", recoveryCodesLeft: number }>}
28
+ */
29
+ function verifySecondFactor(proof) {
30
+ return authHttpRequest(AUTH_PATHS.SECOND_FACTOR_VERIFY, { method: "POST", body: proof });
31
+ }
32
+
33
+ /**
34
+ * @param {import("../types.js").SecondFactorProof} proof
35
+ * @returns {Promise<import("../types.js").SecondFactorRecoveryCodes>}
36
+ */
37
+ function replaceSecondFactorRecoveryCodes(proof) {
38
+ return authHttpRequest(AUTH_PATHS.SECOND_FACTOR_RECOVERY_CODES, { method: "POST", body: proof });
39
+ }
40
+
41
+ /**
42
+ * @param {import("../types.js").SecondFactorProof} proof
43
+ * @returns {Promise<{ ok: true }>}
44
+ */
45
+ function removeSecondFactor(proof) {
46
+ return authHttpRequest(AUTH_PATHS.SECOND_FACTOR, { method: "DELETE", body: proof });
47
+ }
48
+
49
+ export {
50
+ confirmSecondFactorSetup,
51
+ readSecondFactor,
52
+ removeSecondFactor,
53
+ replaceSecondFactorRecoveryCodes,
54
+ startSecondFactorSetup,
55
+ verifySecondFactor
56
+ };
@@ -77,6 +77,38 @@
77
77
  * @typedef {(path: string, options: { method?: string, body?: unknown }) => Promise<any>} AuthRequest
78
78
  */
79
79
 
80
+ /**
81
+ * A second-factor proof: a code from the authenticator app, or one of the recovery codes.
82
+ * @typedef {{ code: string } | { recoveryCode: string }} SecondFactorProof
83
+ */
84
+
85
+ /**
86
+ * The signed-in account's second factor, and when this session last proved it (ISO times, or null).
87
+ * @typedef {object} SecondFactorStatus
88
+ * @property {boolean} enrolled
89
+ * @property {string | null} enrolledAt
90
+ * @property {number} recoveryCodesLeft
91
+ * @property {string | null} verifiedAt
92
+ */
93
+
94
+ /**
95
+ * An authenticator being set up: its secret in base32, shown once, and the otpauth address its QR code carries.
96
+ * @typedef {object} SecondFactorSetup
97
+ * @property {string} secret
98
+ * @property {string} otpauthUrl
99
+ * @property {string} issuer
100
+ * @property {string} accountName
101
+ */
102
+
103
+ /**
104
+ * New recovery codes, shown once, and when the proof that made them was given.
105
+ * @typedef {object} SecondFactorRecoveryCodes
106
+ * @property {string[]} recoveryCodes
107
+ * @property {string} verifiedAt
108
+ */
109
+
110
+ /** @typedef {ReturnType<typeof import("./composables/secondFactor/useSecondFactorStepUp.js").useSecondFactorStepUp>} SecondFactorStepUp */
111
+
80
112
  /** @typedef {ReturnType<typeof import("./composables/loginView/useLoginViewState.js").useLoginViewState>} LoginViewState */
81
113
 
82
114
  /** @typedef {ReturnType<typeof import("./composables/loginView/useLoginViewValidation.js").useLoginViewValidation>} LoginViewValidation */
@@ -0,0 +1,37 @@
1
+ <template>
2
+ <v-dialog
3
+ :model-value="open"
4
+ max-width="440"
5
+ :aria-label="t('jskit.auth.secondFactor.prompt.title')"
6
+ @update:model-value="(shown) => shown || emit('cancel')"
7
+ >
8
+ <v-card>
9
+ <v-card-text class="second-factor-dialog">
10
+ <SecondFactorPrompt @verified="(result) => emit('verified', result)">
11
+ <template #actions>
12
+ <v-btn variant="text" @click="emit('cancel')">{{ t("jskit.auth.secondFactor.cancel") }}</v-btn>
13
+ </template>
14
+ </SecondFactorPrompt>
15
+ </v-card-text>
16
+ </v-card>
17
+ </v-dialog>
18
+ </template>
19
+
20
+ <script setup>
21
+ import SecondFactorPrompt from "./SecondFactorPrompt.vue";
22
+ import { useJskitText } from "../i18n.js";
23
+
24
+ // The step-up dialog: bind `open`, `verified` and `cancel` to useSecondFactorStepUp()'s. Escape and a click outside
25
+ // cancel, as the Cancel button does.
26
+ defineProps({
27
+ open: { type: Boolean, required: true }
28
+ });
29
+ const emit = defineEmits(["verified", "cancel"]);
30
+ const { t } = useJskitText();
31
+ </script>
32
+
33
+ <style scoped>
34
+ .second-factor-dialog {
35
+ padding: 24px;
36
+ }
37
+ </style>