Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

fix(auth): show email confirmation guidance for unlinked SSO accounts (#244)

Kevin Rose3f5cd791b3e1
diff --git a/src/components/auth/login-shell.tsx b/src/components/auth/login-shell.tsx
index 04c27e4..fcdcf43 100644
--- a/src/components/auth/login-shell.tsx
+++ b/src/components/auth/login-shell.tsx
@@ -143,7 +143,7 @@ function LiveLoginShell({
resetError === "INVALID_TOKEN"
? "This reset link is invalid or has expired."
: ssoError
- ? describeSsoError(ssoError)
+ ? describeSsoError(ssoError, { passwordSignIn })
: null,
);
// With password sign-in off, the form is only shown on request, for the
diff --git a/src/components/auth/sso-button.tsx b/src/components/auth/sso-button.tsx
index a008d2a..802ac4c 100644
--- a/src/components/auth/sso-button.tsx
+++ b/src/components/auth/sso-button.tsx
@@ -3,34 +3,16 @@
import { useState } from "react";
import { ShellButton } from "@/components/shared/form-shell";
+import { describeSsoError } from "@/lib/auth/sso-errors";
// "Continue with <provider>": hands the visitor to the organization's
// identity provider (lib/auth/sso.ts). Better Auth sends them back to
// /api/auth/callback/sso, and from there to the dashboard, or to the sign-in
// page with ?error= when it did not work.
+export { describeSsoError } from "@/lib/auth/sso-errors";
export type SsoOption = { name: string };
-/** What the sign-in page says for an ?error= the provider callback sent. */
-export function describeSsoError(code: string): string {
- switch (code) {
- case "signup_closed":
- case "signup_domain_not_allowed":
- return "That address does not have an account here, and this site's sign-up rule does not let it make one. Ask an administrator.";
- case "access_denied":
- return "Sign-in was cancelled.";
- case "email_not_found":
- return "Your identity provider did not share an email address, so you could not be signed in. Ask your IT team to include the email claim.";
- case "unable_to_link_account":
- case "email_not_verified":
- return "An account with this address already exists and its address is not confirmed yet. Sign in with your password once, or ask an administrator.";
- case "account_already_linked_to_different_user":
- return "This sign-in is already tied to a different account here. Ask an administrator.";
- default:
- return "Single sign-on did not finish. Please try again.";
- }
-}
-
export function SsoButton({
label,
onError,
diff --git a/src/lib/auth/sso-errors.ts b/src/lib/auth/sso-errors.ts
new file mode 100644
index 0000000..3a61554
--- /dev/null
+++ b/src/lib/auth/sso-errors.ts
@@ -0,0 +1,31 @@
+// Error messages for single sign-on redirects back to /login?error=<code>.
+// Kept free of server-only imports so both client components and unit tests
+// can use it directly.
+
+export type PasswordSignInMode = "fallback" | "off" | "on";
+
+/** What the sign-in page says for an ?error= the provider callback sent. */
+export function describeSsoError(
+ code: string,
+ options?: { passwordSignIn?: PasswordSignInMode },
+): string {
+ switch (code) {
+ case "signup_closed":
+ case "signup_domain_not_allowed":
+ return "That address does not have an account here, and this site's sign-up rule does not let it make one. Ask an administrator.";
+ case "access_denied":
+ return "Sign-in was cancelled.";
+ case "email_not_found":
+ return "Your identity provider did not share an email address, so you could not be signed in. Ask your IT team to include the email claim.";
+ case "account_not_linked":
+ case "unable_to_link_account":
+ case "email_not_verified":
+ return (options?.passwordSignIn ?? "on") === "on"
+ ? "An account with this address already exists here, and its email address must be confirmed before you can use single sign-on. Open your verification email to confirm your address, or log in with your password to send a fresh link, then try single sign-on again. If your address is already confirmed, ask an administrator."
+ : "An account with this address already exists here, and its email address must be confirmed before you can use single sign-on. Open your verification email to confirm your address, then try single sign-on again. If your address is already confirmed, ask an administrator.";
+ case "account_already_linked_to_different_user":
+ return "This sign-in is already tied to a different account here. Ask an administrator.";
+ default:
+ return "Single sign-on did not finish. Please try again.";
+ }
+}
diff --git a/src/lib/auth/sso.dbtest.ts b/src/lib/auth/sso.dbtest.ts
index 16ed257..cc84441 100644
--- a/src/lib/auth/sso.dbtest.ts
+++ b/src/lib/auth/sso.dbtest.ts
@@ -9,6 +9,7 @@ import { exportJWK, generateKeyPair, SignJWT, type JWK } from "jose";
import type pg from "pg";
import type { createAppAuth as CreateAppAuth } from "./server.ts";
+import { describeSsoError } from "./sso-errors.ts";
import { replaceDbPoolForTests } from "../db/connection.ts";
import { openTestDatabase } from "../db/test-database.ts";
import { clearRateLimitStoreForTests } from "../security/rate-limit.ts";
@@ -383,6 +384,72 @@ test("someone with a password account is signed in to it, not given a second one
assert.equal(rows[0]?.count, "1");
});
+test("someone with an unconfirmed password account stays signed out on SSO until they confirm their address (#244)", async () => {
+ const auth = await makeAuth();
+ const context = await auth.$context;
+ const id = randomUUID();
+
+ await database.query(
+ `insert into "user" (id, name, email, "emailVerified", role) values ($1, 'Pending Sender', '[email protected]', false, 'user')`,
+ [id],
+ );
+ await database.query(
+ `insert into account (id, "accountId", "providerId", "userId", password) values ($1, $2, 'credential', $2, $3)`,
+ [randomUUID(), id, await context.password.hash(PASSWORD)],
+ );
+
+ const refused = await signInWithSso(auth, {
+ email: "[email protected]",
+ email_verified: true,
+ name: "Pending Sender",
+ sub: "okta-pending",
+ });
+
+ assert.equal(refused.location.pathname, "/login");
+
+ const errorCode = refused.location.searchParams.get("error") ?? "";
+
+ assert.equal(errorCode, "account_not_linked");
+ assert.equal(await sessionUser(auth, refused.cookies), null);
+ assert.match(describeSsoError(errorCode), /email address must be confirmed/i);
+ assert.notEqual(
+ describeSsoError(errorCode),
+ "Single sign-on did not finish. Please try again.",
+ );
+
+ const failed = (await listSystemAuditEvents({ limit: 100 }, database)).find(
+ (event) => event.event === "sign_in_failed",
+ );
+
+ assert.deepEqual(failed?.metadata, {
+ method: "sso",
+ provider: "sso",
+ reason: "account_not_linked",
+ });
+
+ const { rows: linkedBefore } = await database.query<{ count: string }>(
+ `select count(*)::text as count from account where "userId" = $1 and "providerId" = 'sso'`,
+ [id],
+ );
+
+ assert.equal(linkedBefore[0]?.count, "0");
+
+ await database.query(
+ `update "user" set "emailVerified" = true where id = $1`,
+ [id],
+ );
+
+ const allowed = await signInWithSso(auth, {
+ email: "[email protected]",
+ email_verified: true,
+ name: "Pending Sender",
+ sub: "okta-pending",
+ });
+
+ assert.equal(allowed.location.pathname, "/dashboard");
+ assert.equal((await sessionUser(auth, allowed.cookies))?.id, id);
+});
+
test("the sign-up rule holds for single sign-on too", async () => {
await saveSettings(
{ allowedEmailDomains: ["acme.com"], signupMode: "domains" },
diff --git a/src/lib/auth/sso.test.ts b/src/lib/auth/sso.test.ts
index 51698c1..f6a1ec2 100644
--- a/src/lib/auth/sso.test.ts
+++ b/src/lib/auth/sso.test.ts
@@ -2,10 +2,12 @@ import assert from "node:assert/strict";
import { test } from "node:test";
import {
+ isSsoEmailVerified,
mayUsePassword,
readPasswordSignInPolicy,
readSsoConfig,
} from "./sso.ts";
+import { describeSsoError } from "./sso-errors.ts";
const sso = {
SSO_CLIENT_ID: "client",
@@ -63,3 +65,75 @@ test("password sign-in can only be turned off while single sign-on is set up", (
true,
);
});
+
+test("isSsoEmailVerified accepts only explicit email_verified or Entra xms_edov affirmations", () => {
+ assert.equal(isSsoEmailVerified({ email_verified: true }), true);
+ assert.equal(isSsoEmailVerified({ email_verified: "true" }), true);
+ assert.equal(isSsoEmailVerified({ xms_edov: true }), true);
+ assert.equal(isSsoEmailVerified({ xms_edov: "true" }), true);
+ assert.equal(isSsoEmailVerified({ xms_edov: 1 }), true);
+ assert.equal(isSsoEmailVerified({ email_verified: false }), false);
+ assert.equal(isSsoEmailVerified({ email_verified: "yes" }), false);
+ assert.equal(isSsoEmailVerified({}), false);
+});
+
+test("describeSsoError explains pending local email confirmation for account_not_linked (#244)", () => {
+ for (const code of [
+ "account_not_linked",
+ "unable_to_link_account",
+ "email_not_verified",
+ ]) {
+ const withPasswordsOn = describeSsoError(code);
+
+ assert.notEqual(
+ withPasswordsOn,
+ "Single sign-on did not finish. Please try again.",
+ );
+ assert.match(withPasswordsOn, /email address must be confirmed/i);
+ assert.match(withPasswordsOn, /verification email/i);
+ assert.match(
+ withPasswordsOn,
+ /log in with your password to send a fresh link/i,
+ );
+ assert.equal(
+ describeSsoError(code, { passwordSignIn: "on" }),
+ withPasswordsOn,
+ );
+
+ for (const passwordSignIn of ["off", "fallback"] as const) {
+ const withoutPasswords = describeSsoError(code, { passwordSignIn });
+
+ assert.match(withoutPasswords, /email address must be confirmed/i);
+ assert.match(withoutPasswords, /verification email/i);
+ assert.doesNotMatch(withoutPasswords, /log in with your password/i);
+ }
+ }
+});
+
+test("describeSsoError keeps cancellation, policy, and fallback messages accurate", () => {
+ assert.equal(describeSsoError("access_denied"), "Sign-in was cancelled.");
+ assert.match(
+ describeSsoError("signup_closed"),
+ /sign-up rule does not let it make one/,
+ );
+ assert.match(
+ describeSsoError("signup_domain_not_allowed"),
+ /sign-up rule does not let it make one/,
+ );
+ assert.match(
+ describeSsoError("email_not_found"),
+ /did not share an email address/,
+ );
+ assert.match(
+ describeSsoError("account_already_linked_to_different_user"),
+ /already tied to a different account/,
+ );
+ assert.equal(
+ describeSsoError("unknown_error"),
+ "Single sign-on did not finish. Please try again.",
+ );
+ assert.equal(
+ describeSsoError(""),
+ "Single sign-on did not finish. Please try again.",
+ );
+});