140 lines
7.5 KiB
TypeScript
140 lines
7.5 KiB
TypeScript
// PEP-90: Google login against mock-google: first login registers, repeat finds the account,
|
||
// disabled user refused, forged/replayed/foreign-browser state refused, unverified e-mail, link/unlink.
|
||
import assert from "node:assert/strict";
|
||
import { after, before, describe, test } from "node:test";
|
||
import { core, db, teardown } from "./helpers.ts";
|
||
import { MockGoogle } from "../src/mock-google.ts";
|
||
|
||
process.env.PEPPA_SECRET = "mock-secret-for-tests";
|
||
const google = new MockGoogle();
|
||
const REDIRECT = "https://peppa.test/auth/google/callback";
|
||
|
||
before(async () => {
|
||
process.env.GOOGLE_OAUTH_BASE = await google.start();
|
||
process.env.GOOGLE_CLIENT_ID = google.clientId;
|
||
process.env.GOOGLE_CLIENT_SECRET = google.clientSecret;
|
||
});
|
||
after(async () => {
|
||
await google.stop();
|
||
await teardown();
|
||
});
|
||
|
||
/** The "browser": opens Google's authorize URL, which consents and redirects back with code+state. */
|
||
async function consent(url: string) {
|
||
const res = await fetch(url, { redirect: "manual" });
|
||
assert.equal(res.status, 302);
|
||
const back = new URL(res.headers.get("location")!);
|
||
return { code: back.searchParams.get("code")!, state: back.searchParams.get("state")! };
|
||
}
|
||
async function login(purpose: "login" | "link" = "login", userId: number | null = null) {
|
||
const f = await core.startGoogleFlow(db, purpose, REDIRECT, userId);
|
||
const { code, state } = await consent(f.url);
|
||
return { f, code, state };
|
||
}
|
||
|
||
describe("Google login", () => {
|
||
test("disabled without a client", async () => {
|
||
delete process.env.GOOGLE_CLIENT_SECRET;
|
||
assert.equal(core.googleEnabled(), false);
|
||
await assert.rejects(core.startGoogleFlow(db, "login", REDIRECT), /не настроен/);
|
||
process.env.GOOGLE_CLIENT_SECRET = google.clientSecret;
|
||
assert.equal(core.googleEnabled(), true);
|
||
});
|
||
|
||
test("first login registers, the next one finds the same account", async () => {
|
||
const a = await login();
|
||
const r = await core.completeGoogleFlow(db, { state: a.state, code: a.code, cookieState: a.f.state });
|
||
assert.ok(r.purpose === "login" && r.created);
|
||
assert.equal(r.user.login, "anna.petrova");
|
||
assert.equal(r.user.name, "Anna Petrova");
|
||
assert.equal(r.user.googleSub, "g-1001");
|
||
assert.ok(r.user.giteaId < 0 && !r.user.passwordHash, "no Gitea, no password");
|
||
assert.ok(core.isLocalUser(r.user));
|
||
assert.equal(await core.mayUseMachineLogin(db, r.user.id), false, "no pool → no machine login, like eGov");
|
||
assert.ok(google.seen.auth[0]!.get("code_challenge"), "PKCE");
|
||
|
||
const b = await login();
|
||
const r2 = await core.completeGoogleFlow(db, { state: b.state, code: b.code, cookieState: b.f.state });
|
||
assert.ok(r2.purpose === "login" && !r2.created);
|
||
assert.equal(r2.user.id, r.user.id);
|
||
});
|
||
|
||
test("same e-mail local part, other Google account → suffixed login, no merge by e-mail", async () => {
|
||
google.account = { sub: "g-1002", email: "anna.petrova@other.org", email_verified: true, name: "Another Anna" };
|
||
const a = await login();
|
||
const r = await core.completeGoogleFlow(db, { state: a.state, code: a.code, cookieState: a.f.state });
|
||
assert.equal(r.user.login, "anna.petrova-2");
|
||
google.account = { sub: "g-1001", email: "anna.petrova@example.com", email_verified: true, name: "Anna Petrova" };
|
||
});
|
||
|
||
test("replayed callback is refused", async () => {
|
||
const a = await login();
|
||
await core.completeGoogleFlow(db, { state: a.state, code: a.code, cookieState: a.f.state });
|
||
await assert.rejects(core.completeGoogleFlow(db, { state: a.state, code: a.code, cookieState: a.f.state }), /устарел/);
|
||
});
|
||
|
||
test("forged state and a state from another browser are refused, without calling Google", async () => {
|
||
const calls = google.seen.token.length;
|
||
const a = await login();
|
||
await assert.rejects(core.completeGoogleFlow(db, { state: "forged", code: a.code, cookieState: "forged" }), /устарел/);
|
||
await assert.rejects(core.completeGoogleFlow(db, { state: a.state, code: a.code, cookieState: "" }), /заново/);
|
||
await assert.rejects(core.completeGoogleFlow(db, { state: a.state, code: a.code, cookieState: "other-browser" }), /заново/);
|
||
assert.equal(google.seen.token.length, calls, "no token exchange");
|
||
// The failed attempts did not spend the flow: the real browser still completes.
|
||
const ok = await core.completeGoogleFlow(db, { state: a.state, code: a.code, cookieState: a.f.state });
|
||
assert.equal(ok.user.googleSub, "g-1001");
|
||
});
|
||
|
||
test("a stolen code without the PKCE verifier is useless", async () => {
|
||
const a = await login();
|
||
const res = await fetch(`${process.env.GOOGLE_OAUTH_BASE}/token`, {
|
||
method: "POST",
|
||
body: new URLSearchParams({ grant_type: "authorization_code", code: a.code, redirect_uri: REDIRECT, client_id: google.clientId, client_secret: google.clientSecret, code_verifier: "guess" }),
|
||
});
|
||
assert.equal(res.status, 400);
|
||
});
|
||
|
||
test("unverified e-mail is refused and creates nothing", async () => {
|
||
google.account = { sub: "g-3003", email: "fake@example.com", email_verified: false };
|
||
const a = await login();
|
||
await assert.rejects(core.completeGoogleFlow(db, { state: a.state, code: a.code, cookieState: a.f.state }), /не подтверждена/);
|
||
assert.equal(await core.getUserByLogin(db, "fake"), null);
|
||
google.account = { sub: "g-1001", email: "anna.petrova@example.com", email_verified: true, name: "Anna Petrova" };
|
||
});
|
||
|
||
test("a disabled user is not allowed", async () => {
|
||
const a = await login();
|
||
const r = await core.completeGoogleFlow(db, { state: a.state, code: a.code, cookieState: a.f.state });
|
||
await core.setUserDisabled(db, r.user.id, true);
|
||
const b = await login();
|
||
const r2 = await core.completeGoogleFlow(db, { state: b.state, code: b.code, cookieState: b.f.state });
|
||
assert.equal(await core.isUserAllowed(db, r2.user), false);
|
||
await core.setUserDisabled(db, r.user.id, false);
|
||
});
|
||
|
||
test("link Google to an eGov account, taken account refused, unlink guard", async () => {
|
||
const egov = await core.registerEgovUser(db, { iin: "900101300126", name: "ТЕСТОВА АСЕЛЬ" });
|
||
await assert.rejects(core.startGoogleFlow(db, "link", REDIRECT, null), /после входа/);
|
||
google.account = { sub: "g-2002", email: "asel@example.com", email_verified: true };
|
||
const l = await login("link", egov.id);
|
||
const r = await core.completeGoogleFlow(db, { state: l.state, code: l.code, cookieState: l.f.state });
|
||
assert.equal(r.purpose, "link");
|
||
assert.equal(r.user.id, egov.id);
|
||
assert.equal(r.user.googleSub, "g-2002");
|
||
const again = await login();
|
||
const r2 = await core.completeGoogleFlow(db, { state: again.state, code: again.code, cookieState: again.f.state });
|
||
assert.equal(r2.user.id, egov.id, "Google now leads into the eGov account");
|
||
|
||
// g-1001 belongs to anna.petrova: the eGov account cannot take it.
|
||
google.account = { sub: "g-1001", email: "anna.petrova@example.com", email_verified: true };
|
||
const t = await login("link", egov.id);
|
||
await assert.rejects(core.completeGoogleFlow(db, { state: t.state, code: t.code, cookieState: t.f.state }), /уже привязан/);
|
||
|
||
const unlinked = await core.unlinkGoogle(db, egov.id);
|
||
assert.equal(unlinked.googleSub, null);
|
||
// Google-only account cannot drop its only way in.
|
||
const [anna] = await db.select().from((await import("@peppa/db")).users).where((await import("drizzle-orm")).eq((await import("@peppa/db")).users.googleSub, "g-1001"));
|
||
await assert.rejects(core.unlinkGoogle(db, anna!.id), /единственный/);
|
||
});
|
||
});
|