The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
Accounts: a provider's first sign-in to an account whose email was never confirmed ends its password and its other sign-ins
Adarshfa27ddc4ddf8
diff --git a/docs/public-signup.md b/docs/public-signup.mdindex c058457d..673ece25 100644--- a/docs/public-signup.md+++ b/docs/public-signup.md@@ -196,6 +196,11 @@ so the callback address matches the one registered.user id is kept (table `auth_oauth_links`), never a code or a token. The account's email then countsas confirmed, and an optional billing service hears `emailVerified: true` and `signIn: "google"` or`"github"` ([billing-hook.md](billing-hook.md)).+- **An unconfirmed email is taken over.** Email sign-up doesn't confirm the address, so someone else+ may have set an account's password. When a provider's user first reaches an account by its email+ and that email was never confirmed, the account's password stops working and its other sign-ins+ end; it then reads as an account made with the provider (below). An account whose email was+ confirmed keeps its password.- **No password at first.** An account made with a provider has no password: no password logs in toit. **Settings → Account → Set a password** gives it one (`PATCH /api/auth/me` with`{ "next" }`), and deleting it asks for no password. Both need a sign-in with the provider in thediff --git a/src/app/api/auth/oauth/[provider]/callback/route.ts b/src/app/api/auth/oauth/[provider]/callback/route.tsindex 4286499e..360bd36e 100644--- a/src/app/api/auth/oauth/[provider]/callback/route.ts+++ b/src/app/api/auth/oauth/[provider]/callback/route.ts@@ -7,6 +7,7 @@ import {accountWithEmail,createOAuthAccount,createSession,+ claimUnconfirmedAccount,linkOAuth,LoginGuard,noteSignIn,@@ -77,7 +78,8 @@ export async function GET(request: Request, ctx: RouteContext<"/api/auth/oauth/[// Public sign-up may have been switched off while the visitor was at the provider.if (!publicSignup(request.headers.get("host"))) return back("failed");- let account: Account | undefined = oauthLinkedAccount(provider, who.subject) ?? accountWithEmail(who.email);+ const linked = oauthLinkedAccount(provider, who.subject);+ let account: Account | undefined = linked ?? accountWithEmail(who.email);let made = false;if (!account) {const refused = beginSignup(key);@@ -98,6 +100,9 @@ export async function GET(request: Request, ctx: RouteContext<"/api/auth/oauth/[// An optional billing service hears of it (none set: nothing is sent).accountEvent("created", accountFacts(account));} else {+ // Found by its email, never confirmed: the provider's verified user takes it over, and a password someone else may+ // have set stops working (claimUnconfirmedAccount).+ if (!linked) account = claimUnconfirmedAccount(account, provider, who.subject);linkOAuth(account.id, provider, who.subject);recordLogin(account.id);// Signed in with the provider, whose email is verified: the account's email counts as confirmed from now on.diff --git a/src/server/auth.ts b/src/server/auth.tsindex c4ef6916..b4cf7369 100644--- a/src/server/auth.ts+++ b/src/server/auth.ts@@ -443,6 +443,29 @@ export function createOAuthAccount(email: string, provider: OAuthProvider, subjereturn account;}+/**+ * A provider's first sign-in to an account found by its email, when that email was never confirmed (email sign-up+ * doesn't confirm it): the provider's verified user owns the address, and whoever set the password may not be them. The+ * password stops working (a hash of random bytes nobody keeps), every other sign-in of the account ends, and the account+ * reads as one made with the provider, so its owner sets a password of their own (Settings → Account) if they want one.+ * An account whose email is confirmed, or that has no password, is left as it is. Returns the account as it is now.+ */+export function claimUnconfirmedAccount(account: Account, provider: OAuthProvider, subject: string): Account {+ if (account.emailVerifiedAt || !accountHasPassword(account)) return account;+ const at = Date.now();+ const hash = hashPassword(crypto.randomBytes(32).toString("hex"));+ // Sessions issued up to now end; the one made for this sign-in, after it, is kept.+ const after = at - 1;+ authWrite(() => {+ sql("UPDATE auth_accounts SET password_hash = ?, password_changed_at = NULL, sessions_after = ? WHERE id = ?").run(hash, after, account.id);+ sql(+ `INSERT INTO auth_oauth_links (provider, subject, account_id, made_account, created_at, last_used_at) VALUES (?, ?, ?, 1, ?, ?)+ ON CONFLICT (provider, subject) DO UPDATE SET account_id = excluded.account_id, made_account = 1, last_used_at = excluded.last_used_at`,+ ).run(provider, subject, account.id, at, at);+ });+ return { ...account, hash, passwordChangedAt: undefined, sessionsAfter: after };+}+/*** Inside authWrite: merge fields into an account's extra_json (the fields auth.json kept that have no column); undefined* removes one. Writes only when something changes. False when there is no such account.diff --git a/tests/oauth-signin.test.ts b/tests/oauth-signin.test.tsindex 8568296c..3d0bc510 100644--- a/tests/oauth-signin.test.ts+++ b/tests/oauth-signin.test.ts@@ -306,14 +306,18 @@ describe("the callback", () => {expect(body.account).toEqual({ email: "[email protected]", providers: ["google"], noPassword: true });});+ /** An account whose email is confirmed already (as a provider's sign-in or the billing side would say). */+ const confirmed = (id: string) => authDb().prepare("UPDATE auth_accounts SET extra_json = json_set(COALESCE(extra_json, '{}'), '$.emailVerifiedAt', 1) WHERE id = ?").run(id);+it("signs in to the account with the same email, and later finds it by the provider's id", async () => {const a = auth.createAccount("[email protected]", "long enough");+ confirmed(a.id);who.github = { id: 777, emails: [{ email: "[email protected]", primary: true, verified: true }] };const r = await signIn("github", { next: "/shorts" });expect(location(r).pathname + location(r).search).toBe("/shorts");expect(sessionAccount()?.id).toBe(a.id);expect(authDb().prepare("SELECT provider, subject, made_account FROM auth_oauth_links WHERE account_id = ?").all(a.id)).toEqual([{ provider: "github", subject: "777", made_account: 0 }]);- // Its password still works, and it still has one.+ // Its email was confirmed: its password still works, and it still has one.expect((await auth.verifyAccount("[email protected]", "long enough"))?.id).toBe(a.id);expect(auth.accountHasPassword(auth.accountById(a.id)!)).toBe(true);// The owner changes its email: GitHub's user still signs in to it.@@ -323,6 +327,29 @@ describe("the callback", () => {expect(sessionAccount()?.id).toBe(a.id);});+ it("takes over an account whose email was never confirmed: its password stops working and its other sign-ins end", async () => {+ // Someone signed up with this address and a password before its owner came with Google.+ const a = auth.createAccount("[email protected]", "someone else's");+ const before = auth.createSession(a.id).value;+ expect(auth.sessionViewer(before)?.kind).toBe("account");+ who.google = { sub: "g-real-owner", email: "[email protected]", email_verified: true };+ await signIn("google");+ expect(sessionAccount()?.id).toBe(a.id);+ // The password set before stops working, and the sign-in made with it ends.+ expect(await auth.verifyAccount("[email protected]", "someone else's")).toBeNull();+ expect(auth.sessionViewer(before)).toBeNull();+ // It reads as an account made with Google: no password, its email confirmed, and its owner can set a password.+ const now = auth.accountById(a.id)!;+ expect(auth.accountHasPassword(now)).toBe(false);+ expect(accountFacts(now)).toMatchObject({ emailVerified: true, signIn: "google" });+ // Google's user signs in again (found by its id now): nothing more changes, and that sign-in stays.+ const kept = setCookie(auth.SESSION_COOKIE)![1];+ jar.set.mockClear();+ await signIn("google");+ expect(sessionAccount()?.id).toBe(a.id);+ expect(auth.sessionViewer(kept)?.kind).toBe("account");+ });+it("keeps a switched-off account out, found by email or by id", async () => {const a = auth.createAccount("[email protected]", "long enough");auth.setAccountDisabled(a.id, true);@@ -401,6 +428,8 @@ describe("what the billing service hears", () => {it("a password account that signs in with a provider: its email counts as confirmed; a password log-in says password again", async () => {const a = auth.createAccount("[email protected]", "long enough");expect(accountFacts(a)).toMatchObject({ emailVerified: false, signIn: "password" });+ // Confirmed before (an unconfirmed one is taken over instead, and loses its password).+ authDb().prepare("UPDATE auth_accounts SET extra_json = json_set(COALESCE(extra_json, '{}'), '$.emailVerifiedAt', 1) WHERE id = ?").run(a.id);who.google = { sub: "g-pw", email: "[email protected]", email_verified: true };await signIn("google");expect(events.accountEvent).toHaveBeenLastCalledWith("signed-in", expect.objectContaining({ id: a.id, emailVerified: true, signIn: "google" }));