From df87ab8dbbe78e6bbb03d81f03babfc58c5ca25e Mon Sep 17 00:00:00 2001 From: David Dada Date: Sun, 21 Jun 2026 21:29:09 +0100 Subject: [PATCH] fix(auth): request Google `email` scope so id_token carries the email claim Enoki's createAuthorizationURL defaults the OAuth scope to "openid" only, and Google emits `email`/`email_verified` claims only when the `email` scope is requested. Without it the bind-google route saw no email claim and rejected sign-in with the misleading "Google email not verified" (#96). Add `extraParams: { scope: ["email"] }`. Scope doesn't affect zkLogin address derivation (iss/aud/sub), so existing Google users are unaffected; `email` is non-sensitive (no Google re-review). Adds a regression test pinning the scope. Co-Authored-By: Claude Opus 4.8 (1M context) --- web/lib/__tests__/auth.test.ts | 35 ++++++++++++++++++++++++++++++++++ web/lib/auth.ts | 7 +++++++ 2 files changed, 42 insertions(+) create mode 100644 web/lib/__tests__/auth.test.ts diff --git a/web/lib/__tests__/auth.test.ts b/web/lib/__tests__/auth.test.ts new file mode 100644 index 0000000..b029835 --- /dev/null +++ b/web/lib/__tests__/auth.test.ts @@ -0,0 +1,35 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; +import { renderHook } from "@testing-library/react"; + +// Capture what useGoogleSignIn hands to Enoki's createAuthorizationURL. +const createAuthorizationURL = vi.fn( + (_input: { provider: string; extraParams?: { scope?: string[] } }) => + Promise.resolve("https://accounts.google.com/o/oauth2/v2/auth?stub"), +); + +vi.mock("@mysten/enoki/react", () => ({ + useEnokiFlow: () => ({ createAuthorizationURL, logout: vi.fn() }), + useZkLogin: () => ({ address: null }), +})); +vi.mock("@mysten/dapp-kit-react", () => ({ + useDAppKit: () => ({ disconnectWallet: vi.fn() }), +})); + +import { useGoogleSignIn } from "../auth"; + +describe("useGoogleSignIn", () => { + beforeEach(() => createAuthorizationURL.mockClear()); + + // Regression for #96: Enoki defaults the scope to just "openid", and Google + // only emits `email`/`email_verified` claims when the `email` scope is asked + // for. Without it, /api/email/bind-google rejects with "Google email not + // verified". This pins the call site so the scope can't be dropped again. + it("requests the Google `email` scope", async () => { + const { result } = renderHook(() => useGoogleSignIn()); + await result.current(); + expect(createAuthorizationURL).toHaveBeenCalledTimes(1); + const arg = createAuthorizationURL.mock.calls[0][0]; + expect(arg.provider).toBe("google"); + expect(arg.extraParams?.scope).toContain("email"); + }); +}); diff --git a/web/lib/auth.ts b/web/lib/auth.ts index 3605b79..d4dea70 100644 --- a/web/lib/auth.ts +++ b/web/lib/auth.ts @@ -38,6 +38,13 @@ export function useGoogleSignIn() { clientId: GOOGLE_CLIENT_ID, redirectUrl: authUrl.toString(), network: ENOKI_NETWORK, + // Ask for the `email` scope so Google puts `email` + `email_verified` + // in the id_token. Enoki defaults to "openid" only; without this the + // token has no email claim and /api/email/bind-google rejects it with + // "Google email not verified" (#96). Scope doesn't affect the zkLogin + // address (derived from iss/aud/sub), so existing Google users are + // unaffected; `email` is a non-sensitive scope (no Google re-review). + extraParams: { scope: ["email"] }, }); window.location.href = url; },