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): surface provider connection failures on the login screen
Elian Doran468e1a1638be
diff --git a/apps/client/src/login.spec.tsx b/apps/client/src/login.spec.tsxindex 02a5517397..781c4a2f10 100644--- a/apps/client/src/login.spec.tsx+++ b/apps/client/src/login.spec.tsx@@ -155,4 +155,17 @@ describe("login App — SSO branch", () => {const c = renderInto(<App />);expect(c.textContent).toContain("login.sso-wrong-account");});++ it("shows the generic connection-failure message when the provider was unreachable", () => {+ window.glob.login = { ssoEnabled: true, ssoIssuerName: "Google", totpEnabled: false, ssoConnectionFailed: true };+ const c = renderInto(<App />);+ expect(c.textContent).toContain("login.sso-connection-failed");+ });++ it("prefers the specific SSO error over the connection-failure message when both are set", () => {+ window.glob.login = { ssoEnabled: true, ssoIssuerName: "Google", totpEnabled: false, ssoError: "wrong_account", ssoConnectionFailed: true };+ const c = renderInto(<App />);+ expect(c.textContent).toContain("login.sso-wrong-account");+ expect(c.textContent).not.toContain("login.sso-connection-failed");+ });});diff --git a/apps/client/src/login.tsx b/apps/client/src/login.tsxindex ef502fa0ce..1250e54c3a 100644--- a/apps/client/src/login.tsx+++ b/apps/client/src/login.tsx@@ -29,7 +29,7 @@ export function App() {const config = window.glob.login;const illustration = <img src={logo} alt="" className="illustration-logo" />;- const [ error, setError ] = useState<string | null>(ssoErrorMessage(config?.ssoError));+ const [ error, setError ] = useState<string | null>(initialSsoError(config));const [ errorId, setErrorId ] = useState(0);function raiseError(message: string) {@@ -188,11 +188,16 @@ export function PasswordLogin({ illustration, totpEnabled, error, errorId, onErr);}-function ssoErrorMessage(ssoError: string | false | undefined): string | null {- if (!ssoError) {- return null;+function initialSsoError(config: typeof window.glob.login): string | null {+ // `ssoError` is the outcome of a round-trip that actually reached the provider, so it is the more+ // specific message and wins over the generic connection failure.+ if (config?.ssoError) {+ return config.ssoError === "wrong_account" ? t("login.sso-wrong-account") : t("login.sso-not-enrolled");}- return ssoError === "wrong_account" ? t("login.sso-wrong-account") : t("login.sso-not-enrolled");+ if (config?.ssoConnectionFailed) {+ return t("login.sso-connection-failed");+ }+ return null;}// Skip the bootstrap render under test, where the components are imported directly.diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex a19a8645f9..bc8363bad3 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3041,7 +3041,8 @@"connection-error": "Couldn't reach the server. Check your connection and try again.","sign_in_with_sso": "Sign in with {{ssoIssuerName}}","sso-wrong-account": "That account isn't the one linked to this Trilium instance. Sign in with the linked account.",- "sso-not-enrolled": "Single sign-on isn't set up yet. Sign in with your password first, then link your account from the settings."+ "sso-not-enrolled": "Single sign-on isn't set up yet. Sign in with your password first, then link your account from the settings.",+ "sso-connection-failed": "Couldn't reach the authentication provider. Try again, or check the server logs for details."},"set_password": {"heading": "Set password",diff --git a/apps/server/src/routes/index.ts b/apps/server/src/routes/index.tsindex 924daafc1b..edfd4a3c38 100644--- a/apps/server/src/routes/index.ts+++ b/apps/server/src/routes/index.ts@@ -92,9 +92,12 @@ export function bootstrap(req: Request, res: Response) {delete req.session.ssoError;}// A round-trip that failed before the user was ever logged in lands here rather than on the- // authenticated bootstrap below, so consume its one-shot detail too. It isn't surfaced on the- // login screen (which has its own `ssoError` messaging); leaving it would otherwise fire as a- // stale, confusing toast after a later successful password login.+ // authenticated bootstrap below, so consume its one-shot detail too and surface it as a bare+ // flag — in SSO-only mode the login screen is the only place left to explain the bounce. The+ // technical detail is deliberately NOT forwarded: this payload is served pre-auth, and the+ // failure reason (TLS trust, DNS, refused connection) would leak infrastructure details to+ // anonymous visitors. It's already in the server log.+ const ssoConnectionFailed = Boolean(req.session.ssoConnectionFailed);delete req.session.ssoConnectionFailed;res.send({...commonItems,@@ -104,7 +107,8 @@ export function bootstrap(req: Request, res: Response) {ssoIssuerName: openID.getSSOIssuerName(),ssoIssuerIcon: openID.getSSOIssuerIcon(),totpEnabled: totp.isTotpEnabled(),- ssoError+ ssoError,+ ssoConnectionFailed},hasNativeTitleBar: false,hasBackgroundEffects: false,diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.tsindex 9c0782fe87..ff98a71750 100644--- a/packages/commons/src/lib/server_api.ts+++ b/packages/commons/src/lib/server_api.ts@@ -417,6 +417,12 @@ export type BootstrapDefinition = {totpEnabled: boolean;/** One-shot SSO error from a failed OIDC round-trip ("wrong_account" / "not_enrolled"). */ssoError?: string | false;+ /**+ * One-shot flag set when the OIDC provider couldn't be reached at all during an+ * unauthenticated round-trip, so the login screen can explain the bounce back. Deliberately+ * a boolean: the technical detail stays in the server log rather than being exposed pre-auth.+ */+ ssoConnectionFailed?: boolean;};baseApiUrl: string;assetPath: string;