A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
core: fix token refresh race between TokenManager instances (#10540)
Abdullah Atta6e2b478c7029
diff --git a/packages/core/__tests__/token-manager.test.ts b/packages/core/__tests__/token-manager.test.tsnew file mode 100644index 000000000..bb021b8aa--- /dev/null+++ b/packages/core/__tests__/token-manager.test.ts@@ -0,0 +1,72 @@+/*+This file is part of the Notesnook project (https://notesnook.com/)++Copyright (C) 2023 Streetwriters (Private) Limited++This program 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 of the License, or+(at your option) any later version.++This program 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 this program. If not, see <http://www.gnu.org/licenses/>.+*/+++import { test, expect, vi } from "vitest";+import { databaseTest } from "./utils/index.ts";+import http from "../src/utils/http.js";++vi.mock("../src/utils/http.js", () => ({+ default: {+ get: vi.fn().mockResolvedValue(undefined),+ post: vi.fn().mockResolvedValue(undefined),+ patch: { json: vi.fn().mockResolvedValue(undefined) },+ delete: vi.fn().mockResolvedValue(undefined)+ }+}));++test("sync connection and db share the token refresh lock", () =>+ databaseTest().then(async (db) => {+ await db.kv().write("token", {+ access_token: "access-1",+ refresh_token: "refresh-1",+ scope: "notesnook.sync offline_access IdentityServerApi",+ token_type: "Bearer",+ expires_in: 3600,+ t: Date.now() - 2 * 3600 * 1000+ });++ let refreshes = 0;+ vi.mocked(http.post).mockImplementation(async (url: string) => {+ if (!url.endsWith("/connect/token")) return;+ const n = ++refreshes;+ await new Promise((r) => setTimeout(r, 50));+ return {+ access_token: `access-${n + 1}`,+ refresh_token: `refresh-${n + 1}`,+ scope: "notesnook.sync offline_access IdentityServerApi",+ token_type: "Bearer",+ expires_in: 3600+ };+ });++ const { HubConnectionBuilder } = await import("@microsoft/signalr");+ const withUrl = vi.spyOn(HubConnectionBuilder.prototype, "withUrl");+ await (db.syncer.sync as any).createConnection({});+ const options = withUrl.mock.calls[0][1] as {+ accessTokenFactory: () => Promise<string>;+ };++ await Promise.all([+ db.tokenManager.getAccessToken(),+ options.accessTokenFactory()+ ]);++ expect(refreshes).toBe(1);+ }));diff --git a/packages/core/src/api/sync/index.ts b/packages/core/src/api/sync/index.tsindex 66a34d98e..40f9263f9 100644--- a/packages/core/src/api/sync/index.ts+++ b/packages/core/src/api/sync/index.ts@@ -25,7 +25,6 @@ import {SYNC_CHECK_IDS} from "../../common.js";import Constants from "../../utils/constants.js";-import TokenManager from "../token-manager.js";import Collector from "./collector.js";import { type HubConnection } from "@microsoft/signalr";import Merger, { handleInboxItems } from "./merger.js";@@ -488,7 +487,7 @@ export class Sync {const { HubConnectionBuilder, HttpTransportType, JsonHubProtocol } =await import("@microsoft/signalr");- const tokenManager = new TokenManager(this.db.kv, this.db.eventManager);+ const tokenManager = this.db.tokenManager;this.connection = new HubConnectionBuilder().withUrl(`${Constants.API_HOST}/hubs/sync/v2`, {accessTokenFactory: async () => {diff --git a/packages/core/src/api/user-manager.ts b/packages/core/src/api/user-manager.tsindex 9af6579e0..5f9149708 100644--- a/packages/core/src/api/user-manager.ts+++ b/packages/core/src/api/user-manager.ts@@ -52,7 +52,7 @@ class UserManager {private keyManager: KeyManager;constructor(private readonly db: Database) {this.keyManager = new KeyManager(db);- this.tokenManager = new TokenManager(db.kv, db.eventManager);+ this.tokenManager = db.tokenManager;EV.subscribe(EVENTS.userUnauthorized, async (url: string) => {if (url.includes("/connect/token") || !(await HealthCheck.auth())) return;