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
chore(import): hardcode the public OneNote Graph client id
Elian Doran4901af8942d0
diff --git a/apps/server/src/routes/api/onenote_import.ts b/apps/server/src/routes/api/onenote_import.tsindex 493935d1d0..988bd09787 100644--- a/apps/server/src/routes/api/onenote_import.ts+++ b/apps/server/src/routes/api/onenote_import.ts@@ -20,10 +20,6 @@ import oauth from "../../services/import/onenote/oauth.js";function getAuthUrl(req: Request) {const clientId = oauth.getClientId();- if (!clientId) {- return [400, "OneNote import is not configured. Set the TRILIUM_ONENOTE_CLIENT_ID environment variable to the client id of a registered Microsoft Entra application."];- }-const { verifier, challenge } = oauth.generatePkce();const state = oauth.generateState();const redirectUri = getRedirectUri(req);@@ -48,10 +44,6 @@ async function callback(req: Request, res: Response) {}const clientId = oauth.getClientId();- if (!clientId) {- return sendHtml(res, "OneNote import is not configured on the server.");- }-const tokens = await oauth.exchangeCodeForToken({ clientId, code, verifier: pending.verifier, redirectUri: pending.redirectUri });const account = await graph.getAccount(tokens.access_token);@@ -122,9 +114,8 @@ async function getValidAccessToken(req: Request): Promise<string | null> {return session.accessToken;}- const clientId = oauth.getClientId();- if (session.refreshToken && clientId) {- const tokens = await oauth.refreshAccessToken({ clientId, refreshToken: session.refreshToken });+ if (session.refreshToken) {+ const tokens = await oauth.refreshAccessToken({ clientId: oauth.getClientId(), refreshToken: session.refreshToken });session.accessToken = tokens.access_token;session.refreshToken = tokens.refresh_token ?? session.refreshToken;session.expiresAt = Date.now() + tokens.expires_in * 1000;diff --git a/apps/server/src/services/import/onenote/oauth.ts b/apps/server/src/services/import/onenote/oauth.tsindex 854266842d..f2ad641f44 100644--- a/apps/server/src/services/import/onenote/oauth.ts+++ b/apps/server/src/services/import/onenote/oauth.ts@@ -6,10 +6,10 @@* the Trilium user (OIDC), whereas this one authorizes the app to call the Graph API on the user's* behalf and therefore needs to keep (and refresh) the resulting access/refresh tokens.*- * It is a PUBLIC client (no client secret), so PKCE is mandatory. Register an app of type- * "Mobile and desktop applications" in the Microsoft Entra admin center, allow personal + work/school- * accounts, add the redirect URI shown in the dialog, grant delegated Graph permissions- * (Notes.Read, User.Read, offline_access), and expose its client id via TRILIUM_ONENOTE_CLIENT_ID.+ * It is a PUBLIC client (no client secret), so PKCE is mandatory. The backing app registration is of+ * type "Mobile and desktop applications" in the Microsoft Entra admin center, allows personal ++ * work/school accounts, and grants delegated Graph permissions (Notes.Read, User.Read,+ * offline_access). Its client id is a public identifier (see getClientId), so it is hardcoded.*/import { createHash, randomBytes } from "node:crypto";@@ -35,9 +35,13 @@ export interface Pkce {challenge: string;}-/** The public client id of the registered Microsoft Entra app, or null if not configured. */-export function getClientId(): string | null {- return process.env.TRILIUM_ONENOTE_CLIENT_ID || null;+/**+ * The public client id of the registered Microsoft Entra app. A public-client (PKCE) client id is not+ * a secret — the flow is protected by PKCE, not by hiding this value — so it is hardcoded, the same+ * way other open-source apps (e.g. Obsidian's OneNote importer) ship theirs.+ */+export function getClientId(): string {+ return "47e34695-b922-4c23-8519-303fa39284c8";}export function generatePkce(): Pkce {