Let Google Workspace groups grant admin access

Site settings → Access names a group for each of admin, editor and
viewer (migration 047). Someone in one who signs in with Google gets a
Google-only account with their highest group's role, and the role
follows the groups: at every sign-in, hourly while signed in, and on
Panel → Google groups → Re-sync now. Out of every group, the account
is disabled and signed out until they're back in one.

Group accounts' roles are locked in the Panel; a superadmin's Disable
sticks. Superadmins and admin-cli accounts are never changed. If
Google can't be asked, sign-ins are refused but the hourly check
leaves accounts alone. Panel → Check their groups tests an email.

Lookups use the service account through the Directory API as
GOOGLE_DIRECTORY_AS. Setup: docs/google-sign-in.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-29 17:44:04 -05:00
parent d7835dd5d4
commit 80877a4659
13 changed files with 563 additions and 36 deletions

View file

@ -19,12 +19,14 @@
A failed login still runs a hash. Otherwise the response time
itself says whether the account exists.
Nothing here creates accounts. See admin-cli.js. Google sign-in
only lets in an account that already exists: matched by its
linked Google id, or on first use by its email, which then links
it. A matching email already linked to a different Google
account is refused rather than relinked. googleAuth.js checks
the identity itself.
Accounts come from admin-cli.js, with one exception: with access
groups set up (accessGroups.js), a Google sign-in by someone in
one makes a group account, whose role then follows the group.
Otherwise Google sign-in only lets in an account that already
exists: matched by its linked Google id, or on first use by its
email, which then links it. A matching email already linked to a
different Google account is refused rather than relinked.
googleAuth.js checks the identity itself.
═══════════════════════════════════════════════════════════════ */
import { Hono } from "hono";
@ -41,6 +43,7 @@ import {
verifyPassword,
} from "../auth.js";
import { GoogleLoginError, finishLogin, googleConfig, startLogin } from "../googleAuth.js";
import { accessEnabled, applyGroupRole, groupRole } from "../accessGroups.js";
import { failureLimit } from "../rateLimit.js";
import { NO_STORE } from "../shape.js";
@ -208,24 +211,65 @@ auth.get("/google/callback", async (c) => {
}
const db = c.get("db");
const columns = `id, email, name, role, is_active, google_sub`;
const columns = `id, email, name, role, is_active, google_sub, access_source, group_disabled`;
const byId = (id) => db.prepare(`SELECT ${columns} FROM admin_users WHERE id = ?`).get(id);
let user = db.prepare(`SELECT ${columns} FROM admin_users WHERE google_sub = ?`).get(identity.sub);
// What the access groups give this person (accessGroups.js), asked
// only when it matters. Unanswerable is a refusal: a sign-in has to
// be shown to be allowed.
const roleFromGroups = async () => {
try {
return { role: await groupRole(db, identity.email) };
} catch (err) {
console.warn(`google login: groups unreadable for ${identity.email}: ${err.message}`);
return { failed: true };
}
};
if (!user) {
const byEmail = db.prepare(`SELECT ${columns} FROM admin_users WHERE email = ?`).get(identity.email);
if (!byEmail) {
console.warn(`google login: no account for ${identity.email}`);
return backToLogin(c, "no_account");
}
if (byEmail.google_sub) {
if (!accessEnabled(db)) {
console.warn(`google login: no account for ${identity.email}`);
return backToLogin(c, "no_account");
}
// In an access group: the account is made now, Google only.
const found = await roleFromGroups();
if (found.failed) return backToLogin(c, "groups_unreachable");
if (!found.role) {
console.warn(`google login: ${identity.email} is in no access group`);
return backToLogin(c, "not_in_group");
}
const id = db
.prepare(
`INSERT INTO admin_users (email, name, role, google_sub, access_source, groups_checked_at)
VALUES (?, ?, ?, ?, 'group', datetime('now'))`,
)
.run(identity.email, identity.name, found.role, identity.sub).lastInsertRowid;
console.log(`account ${identity.email} made from access group (${found.role})`);
user = byId(id);
} else if (byEmail.google_sub) {
console.warn(`google login: ${identity.email} is linked to another Google account`);
return backToLogin(c, "linked_elsewhere");
} else {
// Before linking: a refused sign-in mustn't change the account.
if (byEmail.is_active !== 1) return backToLogin(c, "disabled");
db.prepare(`UPDATE admin_users SET google_sub = ? WHERE id = ?`).run(identity.sub, byEmail.id);
console.log(`google account linked to ${byEmail.email}`);
user = byEmail;
}
// Before linking: a refused sign-in mustn't change the account.
if (byEmail.is_active !== 1) return backToLogin(c, "disabled");
db.prepare(`UPDATE admin_users SET google_sub = ? WHERE id = ?`).run(identity.sub, byEmail.id);
console.log(`google account linked to ${byEmail.email}`);
user = byEmail;
}
// A group account's role follows its groups, checked every time.
// One a superadmin disabled stays disabled whatever its groups say.
if (user.access_source === "group" && user.role !== "superadmin" && accessEnabled(db)) {
if (user.is_active !== 1 && user.group_disabled !== 1) return backToLogin(c, "disabled");
const found = await roleFromGroups();
if (found.failed) return backToLogin(c, "groups_unreachable");
applyGroupRole(db, user, found.role);
if (!found.role) return backToLogin(c, "not_in_group");
user = byId(user.id);
}
if (user.is_active !== 1) return backToLogin(c, "disabled");