mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-18 17:27:50 -05:00
feat(dashboard): unify secure staff administration
Provide a single /dashboard entry point for owner and staff tools, with Discord OAuth sign-in for approved owner accounts and active staff. Add owner-managed dashboard access grants, staff server health and update visibility, filtering, moderation improvements, item controls, and per-animal clothing restrictions. Replace staff-triggered password and 2FA resets with email-verified account recovery. Persist validated recovery addresses, use short-lived hashed recovery tokens, expose recovery flows in registration and account pages, and keep mail delivery disabled until its webhook configuration is present. Cover dashboard aliases, recovery restrictions, Discord access, monitoring, item locks, wear rules, and staff behavior with regression tests.
This commit is contained in:
1 parent
4997315239
commit
7dee29010a
27 files changed
+2303
-346
No files matched your search
+46
-23
@@ -43,6 +43,7 @@ const {activeAvatar} = require("./player.js");
|
||||
|
||||
const PASSWORD_SETTING = "admin_password"; // "salt:hash"
|
||||
const COOKIE = "ajc_admin";
|
||||
const DISCORD_COOKIE = "ajc_admin_discord";
|
||||
const SESSION_HOURS = 12;
|
||||
const MIN_PASSWORD_LENGTH = 8;
|
||||
const MAX_PASSWORD_LENGTH = 200;
|
||||
@@ -112,7 +113,10 @@ const signedIn = req => {
|
||||
};
|
||||
|
||||
const cookieHeader = (token, maxAgeSeconds) =>
|
||||
`${COOKIE}=${token}; Path=/admin; HttpOnly; SameSite=Strict; Max-Age=${maxAgeSeconds}${config.secureCookies ? "; Secure" : ""}`;
|
||||
`${COOKIE}=${token}; Path=/; HttpOnly; SameSite=Strict; Max-Age=${maxAgeSeconds}${config.secureCookies ? "; Secure" : ""}`;
|
||||
|
||||
const discordCookie = (value, maxAgeSeconds) =>
|
||||
`${DISCORD_COOKIE}=${value}; Path=/; HttpOnly; SameSite=Lax; Max-Age=${maxAgeSeconds}${config.secureCookies ? "; Secure" : ""}`;
|
||||
|
||||
const signIn = res => {
|
||||
for (const [hash, expiry] of sessions) {
|
||||
@@ -144,6 +148,7 @@ const accountRow = (account, playing) => {
|
||||
locked: account.locked,
|
||||
twoStep: totp.isOn(account),
|
||||
discord: account.discord,
|
||||
adminDashboardAccess: Boolean(account.adminDashboardAccess),
|
||||
bio: account.bio,
|
||||
badges: account.badges,
|
||||
featuresOff: accounts.FEATURES.filter(name => !accounts.featureOn(account, name)),
|
||||
@@ -384,32 +389,12 @@ const act = (account, body) => {
|
||||
const {actions, PERMANENT} = world.moderation;
|
||||
const name = account.username;
|
||||
switch (body.action) {
|
||||
case "password": {
|
||||
const passwordProblem = accounts.setPassword(account, body.password);
|
||||
if (passwordProblem) {
|
||||
return {error: passwordProblem === "weak_password" ? "That password is too easy to guess: it is a common one, or has your username in it."
|
||||
: "Passwords need 10 characters or more, with a capital letter, a small letter and a digit."};
|
||||
}
|
||||
actions.kick(account);
|
||||
manage.forget(account); // their account page too, whoever has it open
|
||||
modpanel.forget(account);
|
||||
return {ok: `${name} has a new password and was signed out.`};
|
||||
}
|
||||
// the owner locks and unlocks on the account page; this is for when
|
||||
// they cannot.
|
||||
case "unlock":
|
||||
account.locked = false;
|
||||
accounts.save(account);
|
||||
return {ok: `${name} is unlocked.`};
|
||||
// for a player who lost their phone and their recovery codes: their
|
||||
// password alone signs them in again.
|
||||
case "twoStepOff":
|
||||
if (!totp.isOn(account)) {
|
||||
return {error: `${name} does not use 2FA.`};
|
||||
}
|
||||
totp.turnOff(account);
|
||||
accounts.save(account);
|
||||
return {ok: `2FA is off for ${name}.`};
|
||||
case "rename": {
|
||||
world.disconnect(account);
|
||||
manage.forget(account);
|
||||
@@ -549,6 +534,16 @@ const act = (account, body) => {
|
||||
`+${(account.permGrants || []).join(",") || "none"} -${(account.permDenies || []).join(",") || "none"}`);
|
||||
return {ok: `${name}'s permissions are saved.`};
|
||||
}
|
||||
case "adminDashboardAccess": {
|
||||
const on = body.on === true;
|
||||
if (on && !account.discordId) {
|
||||
return {error: `${name} must link their Discord account before it can be allowed.`};
|
||||
}
|
||||
account.adminDashboardAccess = on;
|
||||
accounts.save(account);
|
||||
modlog.record("admin", on ? "admin dashboard access given" : "admin dashboard access removed", account.username, account.discord || "");
|
||||
return {ok: on ? `${name} may now sign in to this dashboard with their linked Discord account.` : `${name}'s Discord dashboard access was removed.`};
|
||||
}
|
||||
case "rejectName":
|
||||
actions.rejectName(account, BY);
|
||||
manage.forget(account);
|
||||
@@ -627,6 +622,34 @@ const handle = async (req, res, url, from, web) => {
|
||||
web.send(res, 200, await stats.withView(await fs.promises.readFile(path.join(config.publicDir, "admin.html"))), "text/html; charset=utf-8");
|
||||
return;
|
||||
}
|
||||
// Discord redirects do not carry the normal Strict admin cookie, so their
|
||||
// own short-lived nonce cookie is Lax and scoped to this callback alone.
|
||||
if (route === "GET /admin/discord/login") {
|
||||
if (!hasPassword() || !discordLinks.linking()) {
|
||||
res.writeHead(302, {Location: "/dashboard/admin?discord=off"});
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
const {url: to, nonce} = discordLinks.startAdmin();
|
||||
res.setHeader("Set-Cookie", discordCookie(nonce, 600));
|
||||
res.writeHead(302, {Location: to});
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
if (route === "GET /admin/discord/callback") {
|
||||
const nonce = (new RegExp(`(?:^|;\\s*)${DISCORD_COOKIE}=([0-9a-f]{48})(?:;|$)`).exec(req.headers.cookie || "") || [])[1];
|
||||
const result = url.searchParams.get("error") ? {error: "cancelled"}
|
||||
: await discordLinks.finishAdmin(url.searchParams.get("state"), nonce, url.searchParams.get("code"));
|
||||
if (result.ok) {
|
||||
signIn(res);
|
||||
console.log(`[admin] ${logText(result.account.username)} signed in with Discord from ${logText(from.address)}`);
|
||||
}
|
||||
const adminCookie = res.getHeader("Set-Cookie");
|
||||
res.setHeader("Set-Cookie", adminCookie ? [adminCookie, discordCookie("", 0)] : discordCookie("", 0));
|
||||
res.writeHead(302, {Location: `/dashboard/admin?discord=${result.ok ? "ok" : result.error}`});
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
if (req.method === "POST" && !sameOrigin(req)) {
|
||||
reply(403, {error: "forbidden"});
|
||||
return;
|
||||
@@ -635,7 +658,7 @@ const handle = async (req, res, url, from, web) => {
|
||||
|
||||
switch (route) {
|
||||
case "GET /admin/api/state":
|
||||
reply(200, {needsSetup: !hasPassword(), needsSetupCode: !from.local, signedIn: signedIn(req)});
|
||||
reply(200, {needsSetup: !hasPassword(), needsSetupCode: !from.local, signedIn: signedIn(req), discordLogin: discordLinks.linking()});
|
||||
return;
|
||||
|
||||
// choosing the first password. whoever is at the server's own machine may;
|
||||
@@ -963,7 +986,7 @@ const handle = async (req, res, url, from, web) => {
|
||||
: "New masterpieces are approved as they are made. Ones already waiting still wait."};
|
||||
}
|
||||
else if (body.action === "approve") {
|
||||
result = world.approveMasterpiece(id) ? {ok: "Approved. Its maker has been told and can hang it now."} : {error: "No such painting waiting."};
|
||||
result = world.approveMasterpiece(id, "admin") ? {ok: "Approved. Its maker has been told and can hang it now."} : {error: "No such painting waiting."};
|
||||
}
|
||||
else if (body.action === "reject" || body.action === "remove") {
|
||||
result = world.removeMasterpiece(id)
|
||||
|
||||
Reference in new issue
Block a user