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(oauth): send Google credentials via client_secret_post
Elian Doran37ec0d34ff05
diff --git a/apps/server/src/services/open_id.spec.ts b/apps/server/src/services/open_id.spec.tsindex 3f8b701e89..34440b9068 100644--- a/apps/server/src/services/open_id.spec.ts+++ b/apps/server/src/services/open_id.spec.ts@@ -124,6 +124,18 @@ describe("open_id", () => {expect(typeof cfg.afterCallback).toBe("function");});+ it("auto-selects the token-endpoint auth method by issuer", () => {+ setOauthConfig(true);++ // Non-Google issuer (the setOauthConfig default) → spec-default client_secret_basic.+ expect(openID.generateOAuthConfig().clientAuthMethod).toBe("client_secret_basic");++ // Google issuer (trailing slash tolerated) → client_secret_post, since Google rejects the+ // RFC-encoded Basic credentials (client_ids contain "-"/"." which become %2D/%2E).+ mfa.oauthIssuerBaseUrl = "https://accounts.google.com/";+ expect(openID.generateOAuthConfig().clientAuthMethod).toBe("client_secret_post");+ });+it("returns the session unchanged when the DB is not initialized", async () => {const cfg = buildConfig();const spy = vi.spyOn(sqlInit, "isDbInitialized").mockReturnValue(false);diff --git a/apps/server/src/services/open_id.ts b/apps/server/src/services/open_id.tsindex 1d61a23535..142cdc497f 100644--- a/apps/server/src/services/open_id.ts+++ b/apps/server/src/services/open_id.ts@@ -113,6 +113,7 @@ function generateOAuthConfig() {issuerBaseURL: config.MultiFactorAuthentication.oauthIssuerBaseUrl,secret: config.MultiFactorAuthentication.oauthClientSecret,clientSecret: config.MultiFactorAuthentication.oauthClientSecret,+ clientAuthMethod: resolveClientAuthMethod(),authorizationParams: {response_type: "code",scope: "openid profile email",@@ -158,3 +159,23 @@ export default {isTokenValid,isUserSaved,};++const GOOGLE_ISSUER = "https://accounts.google.com";++/**+ * Chooses the token-endpoint client authentication method based on the issuer.+ *+ * - **Google** → `client_secret_post`. express-openid-connect defaults to `client_secret_basic`,+ * whose oauth4webapi implementation form-url-encodes the client_id/secret per RFC 6749 §2.3.1+ * ("-" → %2D, "." → %2E) inside the HTTP Basic header. Google does not decode Basic credentials, so+ * a Google client_id (which always contains "-" and ".") arrives corrupted and the token exchange+ * fails with "invalid_client: The OAuth client was not found." Posting the credentials in the body+ * sidesteps the Basic-auth encoding.+ * - **Any other issuer** → `client_secret_basic`, the OIDC default a spec-compliant provider expects+ * (e.g. Authelia/Keycloak register confidential clients as `client_secret_basic` and reject a+ * mismatched method).+ */+function resolveClientAuthMethod() {+ const issuer = config.MultiFactorAuthentication.oauthIssuerBaseUrl.replace(/\/+$/, "");+ return issuer === GOOGLE_ISSUER ? "client_secret_post" : "client_secret_basic";+}