Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(web-clipper): log in to servers with TOTP enabled (closes #9286)
Elian Doran13a976d1daf5
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.htmlindex 58a181a90c..fb2ead793d 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html@@ -61,6 +61,13 @@<p>It's also possible to configure the <a href="#root/_help_WOcw2SLH6tbX">server</a> addressif you don't run the desktop application, or want it to work without thedesktop application running.</p>+<p>To connect to a server, enter its address and your password in the extension's+ options and press <em>Login to the server instance</em>. If the server uses <a+ href="#root/_help_7DAiwaf8Z7Rz">multi-factor authentication</a>, also fill+ in <em>Authentication code</em> with the current code from your authenticator+ app (or one of your recovery codes); otherwise leave it empty. The password+ and the code are used only once, to obtain a token for the extension, and+ are not stored.</p><h3>When a clipping fails</h3><p>After each clipping, a notification in the corner of the page confirmsthat it was saved, with a link to open the new note in Trilium. If thediff --git a/apps/server/src/routes/api/login.spec.ts b/apps/server/src/routes/api/login.spec.tsindex 28709a08ad..2ae79b5542 100644--- a/apps/server/src/routes/api/login.spec.ts+++ b/apps/server/src/routes/api/login.spec.ts@@ -91,7 +91,9 @@ describe("Login (sync) API (integration)", () => {describe("Login (token) API", () => {it("rejects an incorrect password", async () => {const req = { body: { password: "wrongpassword" } } as unknown as Request;- expect(await loginApiRoute.token(req)).toEqual([401, "Incorrect credential"]);+ expect(await loginApiRoute.token(req)).toEqual(+ [401, { message: "Incorrect credential", factor: "password" }]+ );});it("issues an ETAPI token for the correct password", async () => {@@ -106,6 +108,8 @@ describe("Login (token) API", () => {});const req = { body: { password: "demo1234", totpToken: "000000" } } as unknown as Request;- expect(await cls.init(() => loginApiRoute.token(req))).toEqual([401, "Incorrect credential"]);+ expect(await cls.init(() => loginApiRoute.token(req))).toEqual(+ [401, { message: "Incorrect credential", factor: "totp" }]+ );});});diff --git a/apps/server/src/routes/api/login.ts b/apps/server/src/routes/api/login.tsindex 5cc2860e81..043003356d 100644--- a/apps/server/src/routes/api/login.ts+++ b/apps/server/src/routes/api/login.ts@@ -132,8 +132,9 @@ async function token(req: Request) {const password = req.body.password;const submittedTotpToken = req.body.totpToken;- if (await verifyLoginCredentials(password, submittedTotpToken)) {- return [401, "Incorrect credential"];+ const failedFactor = await verifyLoginCredentials(password, submittedTotpToken);+ if (failedFactor) {+ return [401, { message: "Incorrect credential", factor: failedFactor }];}// for backwards compatibility with Sender which does not send the namediff --git a/apps/web-clipper/entrypoints/options/index.html b/apps/web-clipper/entrypoints/options/index.htmlindex abe09d4458..0fb2adab95 100644--- a/apps/web-clipper/entrypoints/options/index.html+++ b/apps/web-clipper/entrypoints/options/index.html@@ -45,6 +45,10 @@<th>Password:</th><td><input type="password" id="trilium-server-password"/></td></tr>+ <tr>+ <th>Authentication code:</th>+ <td><input type="text" id="trilium-server-totp" autocomplete="one-time-code"/> (only if two-factor authentication is enabled)</td>+ </tr><tr><th></th><td><input type="submit" value="Login to the server instance"/></td>diff --git a/apps/web-clipper/entrypoints/options/index.ts b/apps/web-clipper/entrypoints/options/index.tsindex f230ab574d..bef6be6376 100644--- a/apps/web-clipper/entrypoints/options/index.ts+++ b/apps/web-clipper/entrypoints/options/index.ts@@ -2,6 +2,7 @@ import $ from "cash-dom";const $triliumServerUrl = $("#trilium-server-url");const $triliumServerPassword = $("#trilium-server-password");+const $triliumServerTotp = $("#trilium-server-totp");const $errorMessage = $("#error-message");const $successMessage = $("#success-message");@@ -38,7 +39,8 @@ async function saveTriliumServerSetup(e) {'Content-Type': 'application/json'},body: JSON.stringify({- password: $triliumServerPassword.val()+ password: $triliumServerPassword.val(),+ totpToken: ($triliumServerTotp.val() as string | undefined)?.trim()})});}@@ -49,7 +51,17 @@ async function saveTriliumServerSetup(e) {}if (resp.status === 401) {- showError("Incorrect credentials.");+ const { factor } = await resp.json().catch(() => ({}));++ if (factor === "totp") {+ const totpWasEntered = ($triliumServerTotp.val() as string | undefined)?.trim();+ $triliumServerTotp.val("").trigger("focus");+ showError(totpWasEntered+ ? "Incorrect authentication code."+ : "Two-factor authentication is enabled. Enter the code from your authenticator app or a recovery code.");+ } else {+ showError("Incorrect credentials.");+ }}else if (resp.status !== 200) {showError(`Unrecognised response with status code ${ resp.status}`);@@ -118,6 +130,7 @@ async function restoreOptions() {$triliumServerUrl.val('');$triliumServerPassword.val('');+ $triliumServerTotp.val('');if (triliumServerUrl && authToken) {$triliumServerSetupForm.hide();diff --git a/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md b/docs/User Guide/User Guide/Installation & Setup/Web Clipper.mdindex e61273da9f..8b2e3dea16 100644--- a/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md+++ b/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md@@ -55,6 +55,8 @@ The extension needs to connect to a running Trilium instance. By default, it scaIt's also possible to configure the [server](Server%20Installation.md) address if you don't run the desktop application, or want it to work without the desktop application running.+To connect to a server, enter its address and your password in the extension's options and press _Login to the server instance_. If the server uses [multi-factor authentication](Server%20Installation/Multi-factor%20authentication%20with%20TOTP.md), also fill in _Authentication code_ with the current code from your authenticator app (or one of your recovery codes); otherwise leave it empty. The password and the code are used only once, to obtain a token for the extension, and are not stored.+### When a clipping failsAfter each clipping, a notification in the corner of the page confirms that it was saved, with a link to open the new note in Trilium. If the clipping could not be saved, the notification says why instead: