Skip to content

Sign in with Google One Tap

For: an app developer who shows the One Tap prompt of Google Identity Services.

What it does: One Tap gives the page an ID token in place of a code. POST /social/google/id-token verifies it and opens the session, as the Google callback does.

  1. Keep social.idToken on. It is on by default with social.google.
  2. Add the client id of each Android or iOS app to social.google.audiences.
  3. Post {} to POST /social/google/id-token/nonce. Keep nonce in the page.
  4. Give providerNonce to google.accounts.id.initialize as nonce. Never give Google the raw nonce.
  5. In the callback, post { idToken, nonce } to POST /social/google/id-token.
Google with the ID-token route
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
/** Google sign-in with One Tap, beside the redirect flow. */
export function createOneTapAuth(
deps: { storage: StorageAdapter; mail: MailPort; secret: string },
// Tests serve their own key set through this fetch. An app leaves it out.
fetchFn?: typeof fetch,
) {
return createAuth({
baseURL: "https://api.example.com",
appURL: "https://app.example.com",
trustedOrigins: ["https://app.example.com"],
secret: deps.secret,
storage: deps.storage,
mail: deps.mail,
social: {
google: {
clientId: "web-client.apps.googleusercontent.com",
clientSecret: "google-client-secret",
},
// On by default with google or apple. requireNonce: true is the default: keep it.
idToken: { requireNonce: true },
...(fetchFn ? { fetch: fetchFn } : {}),
},
});
}
The One Tap prompt in the page
/** The part of Google Identity Services that the page uses. */
export interface GoogleId {
initialize(options: {
client_id: string;
nonce: string;
callback: (answer: { credential: string }) => void;
}): void;
prompt(): void;
}
const API = "https://api.example.com/auth";
/** Shows One Tap. The nonce goes to Google as its hash, and to core as itself. */
export async function showOneTap(google: GoogleId, done: (ok: boolean) => void): Promise<void> {
const res = await fetch(`${API}/social/google/id-token/nonce`, {
method: "POST",
headers: { "content-type": "application/json" },
body: "{}",
});
const { nonce, providerNonce } = (await res.json()) as { nonce: string; providerNonce: string };
google.initialize({
client_id: "web-client.apps.googleusercontent.com",
// Google puts this value in the token. Never give it the raw nonce.
nonce: providerNonce,
callback: async ({ credential }) => {
const signIn = await fetch(`${API}/social/google/id-token`, {
method: "POST",
credentials: "include",
headers: { "content-type": "application/json" },
body: JSON.stringify({ idToken: credential, nonce }),
});
done(signIn.ok);
},
});
google.prompt();
}
The One Tap sign-in as a test
import { exportJWK, generateKeyPair, SignJWT } from "jose";
import { describe, expect, it } from "vitest";
import { call, harness, sessionCookieOf } from "../support/harness.ts";
import { createOneTapAuth } from "./one-tap.ts";
// A test key set in place of Google's. No test calls Google.
const { publicKey, privateKey } = await generateKeyPair("RS256");
const jwk = { ...(await exportJWK(publicKey)), kid: "test-key", alg: "RS256", use: "sig" };
const googleKeys: typeof fetch = async (input) =>
String(input) === "https://www.googleapis.com/oauth2/v3/certs"
? Response.json({ keys: [jwk] })
: new Response("not found", { status: 404 });
/** The ID token that One Tap gives the page, signed by the test key. */
function oneTapToken(nonce: string): Promise<string> {
const now = Math.floor(Date.now() / 1000);
return new SignJWT({
iss: "https://accounts.google.com",
aud: "web-client.apps.googleusercontent.com",
sub: "google-user-1",
email: "ada@example.com",
email_verified: true,
name: "Ada",
nonce,
iat: now,
exp: now + 3600,
})
.setProtectedHeader({ alg: "RS256", kid: "test-key" })
.sign(privateKey);
}
describe("sign in with Google One Tap", () => {
it("signs in once with the token and the raw nonce", async () => {
const auth = createOneTapAuth(harness(), googleKeys);
const res = await call(auth, "POST", "/auth/social/google/id-token/nonce", {});
const { nonce, providerNonce } = (await res.json()) as {
nonce: string;
providerNonce: string;
};
// Google gets providerNonce and writes it in the token.
const idToken = await oneTapToken(providerNonce);
const signIn = await call(auth, "POST", "/auth/social/google/id-token", { idToken, nonce });
expect(signIn.status).toBe(200);
expect(sessionCookieOf(signIn)).not.toBe("");
// Each token signs in once.
const again = await call(auth, "POST", "/auth/social/google/id-token", { idToken, nonce });
expect(await again.json()).toEqual({ error: "invalid_token" });
});
it("refuses a token with the raw nonce in it", async () => {
const auth = createOneTapAuth(harness(), googleKeys);
const res = await call(auth, "POST", "/auth/social/google/id-token/nonce", {});
const { nonce } = (await res.json()) as { nonce: string };
const idToken = await oneTapToken(nonce);
const signIn = await call(auth, "POST", "/auth/social/google/id-token", { idToken, nonce });
expect(await signIn.json()).toEqual({ error: "invalid_token" });
});
});
  • The nonce is 32 random bytes as base64url, and it lives 5 minutes. providerNonce is its lowercase SHA-256 hex. Core stores only that hex.
  • The token’s nonce claim must equal the SHA-256 hex of the posted nonce. A party that holds only the token sees only the hash, so it cannot post the nonce.
  • The token must verify against Google’s key set with RS256 or ES256. It needs Google’s issuer, an aud of clientId or of audiences, and an exp not past.
  • Its iat is at most 60 seconds ahead, and its life is at most one day.
  • Core checks the token before it spends the nonce. So a bad token cannot use the nonce up. The nonce is spent once.
  • Each token signs in once, on every instance: core writes a row for it until its exp.
  • The account rules, the gate of the second factor, and the anonymous upgrade are those of the Google callback.
  • The route takes JSON only, under the Origin check. So core takes no form post of the redirect mode of One Tap.
  • The limits: id-token-nonce:ip 60 and id-token:ip 30, each per 15 minutes.
  • social.idToken: false removes both routes.
  • With social.idToken.requireNonce: false, a token posted with no nonce skips the hash check. A party that holds only such a token can then sign in before the app. Only the one-use row of the token stops a second use. Keep requireNonce on.
  • The Google reference does not say that a One Tap token holds the nonce. Core’s tests use test keys, not real tokens.

Sign in once with One Tap on a real Google account. Give it providerNonce, and confirm that the token’s nonce claim is that value.

  • invalid_token: the token did not verify, its nonce does not match, the nonce is spent, or the token signed in before. Ask for a new nonce and a new token.
  • rate_limited: too many nonces or sign-ins from the IP.
  • email_taken_unverified: an account with the address exists, and Google did not verify the address.
  • sign_up_closed: the sign-up rule refused a new user.
  • account_banned: an operator banned the account.
  • invalid_body: the body is not JSON, or a field has the wrong type.
  • oauth_failed: the subject (sub) of the token is empty, too long, or holds a control character. Core writes the line social.bad_subject.
  • email_required: the token holds no address.
  • sso_required: the address is of a domain that signs in by SSO only. Send the browser to the start URL in detail.
  • hook_refused and hook_failed: a hook of your app refused the sign-in or threw.
  • conflict: another write changed the user at the same moment. Ask for a new nonce and a new token, and try again.