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
+99
-9
@@ -35,27 +35,57 @@ const BOOSTER = "booster";
|
||||
const linking = () => Boolean(config.discord.clientId && config.discord.clientSecret);
|
||||
const boostChecks = () => Boolean(config.discord.botToken && config.discord.guildId);
|
||||
const redirectUri = () => `${config.siteUrl}account/discord/callback`;
|
||||
const adminRedirectUri = () => `${config.siteUrl}dashboard/admin/discord/callback`;
|
||||
const staffRedirectUri = () => `${config.siteUrl}dashboard/staff/discord/callback`;
|
||||
|
||||
// a link under way: state (in Discord's address) -> {accountId, nonce (in a
|
||||
// cookie of the browser that asked), expires}. both must come back.
|
||||
const states = new Map();
|
||||
const adminStates = new Map();
|
||||
const staffStates = new Map();
|
||||
|
||||
const start = account => {
|
||||
for (const [state, entry] of states) {
|
||||
const newState = statesMap => {
|
||||
for (const [state, entry] of statesMap) {
|
||||
if (entry.expires < Date.now()) {
|
||||
states.delete(state);
|
||||
statesMap.delete(state);
|
||||
}
|
||||
}
|
||||
if (states.size >= MAX_STATES) {
|
||||
states.delete(states.keys().next().value);
|
||||
if (statesMap.size >= MAX_STATES) {
|
||||
statesMap.delete(statesMap.keys().next().value);
|
||||
}
|
||||
const state = crypto.randomBytes(24).toString("hex");
|
||||
const nonce = crypto.randomBytes(24).toString("hex");
|
||||
states.set(state, {accountId: account.id, nonce, expires: Date.now() + STATE_MINUTES * 60000});
|
||||
statesMap.set(state, {nonce, expires: Date.now() + STATE_MINUTES * 60000});
|
||||
return {state, nonce};
|
||||
};
|
||||
|
||||
const authorize = (redirect, state) => {
|
||||
const url = new URL(AUTHORIZE);
|
||||
url.search = new URLSearchParams({client_id: config.discord.clientId, response_type: "code", scope: "identify",
|
||||
redirect_uri: redirectUri(), state, prompt: "consent"}).toString();
|
||||
return {url: url.href, nonce};
|
||||
redirect_uri: redirect, state, prompt: "consent"}).toString();
|
||||
return url.href;
|
||||
};
|
||||
|
||||
const start = account => {
|
||||
const {state, nonce} = newState(states);
|
||||
states.get(state).accountId = account.id;
|
||||
return {url: authorize(redirectUri(), state), nonce};
|
||||
};
|
||||
|
||||
// A separate OAuth state and redirect for the owner dashboard. The Discord
|
||||
// account must already be linked to a Burrow account explicitly granted
|
||||
// adminDashboardAccess; knowing a Discord name is never enough.
|
||||
const startAdmin = () => {
|
||||
const {state, nonce} = newState(adminStates);
|
||||
return {url: authorize(adminRedirectUri(), state), nonce};
|
||||
};
|
||||
|
||||
// Staff sign in through a separately scoped state and callback. A Discord
|
||||
// identity is only useful here after its owner has linked it to their own
|
||||
// Burrow account; modpanel.js also verifies that account remains active staff.
|
||||
const startStaff = () => {
|
||||
const {state, nonce} = newState(staffStates);
|
||||
return {url: authorize(staffRedirectUri(), state), nonce};
|
||||
};
|
||||
|
||||
const call = async (path, options = {}) => {
|
||||
@@ -154,6 +184,66 @@ const finish = async (state, nonce, code) => {
|
||||
}
|
||||
};
|
||||
|
||||
const finishAdmin = async (state, nonce, code) => {
|
||||
const entry = typeof state === "string" ? adminStates.get(state) : null;
|
||||
adminStates.delete(state);
|
||||
const digest = text => crypto.createHash("sha256").update(String(text)).digest();
|
||||
if (!entry || entry.expires < Date.now() || typeof nonce !== "string" || !crypto.timingSafeEqual(digest(entry.nonce), digest(nonce)) || !code) {
|
||||
return {error: "expired"};
|
||||
}
|
||||
try {
|
||||
const token = await call("/oauth2/token", {
|
||||
method: "POST",
|
||||
headers: {"Content-Type": "application/x-www-form-urlencoded"},
|
||||
body: new URLSearchParams({client_id: config.discord.clientId, client_secret: config.discord.clientSecret,
|
||||
grant_type: "authorization_code", code, redirect_uri: adminRedirectUri()}).toString(),
|
||||
});
|
||||
if (token.status !== 200 || !token.body.access_token) {
|
||||
return {error: "discord"};
|
||||
}
|
||||
const me = await call("/users/@me", {headers: {Authorization: `Bearer ${token.body.access_token}`}});
|
||||
if (me.status !== 200 || !/^\d{5,25}$/.test(String(me.body.id))) {
|
||||
return {error: "discord"};
|
||||
}
|
||||
const account = accounts.all().find(candidate => candidate.discordId === String(me.body.id) && candidate.adminDashboardAccess);
|
||||
return account ? {ok: true, account} : {error: "denied"};
|
||||
}
|
||||
catch (err) {
|
||||
console.log(`[discord] admin sign-in failed: ${logText(err.message)}`);
|
||||
return {error: "discord"};
|
||||
}
|
||||
};
|
||||
|
||||
const finishStaff = async (state, nonce, code) => {
|
||||
const entry = typeof state === "string" ? staffStates.get(state) : null;
|
||||
staffStates.delete(state);
|
||||
const digest = text => crypto.createHash("sha256").update(String(text)).digest();
|
||||
if (!entry || entry.expires < Date.now() || typeof nonce !== "string" || !crypto.timingSafeEqual(digest(entry.nonce), digest(nonce)) || !code) {
|
||||
return {error: "expired"};
|
||||
}
|
||||
try {
|
||||
const token = await call("/oauth2/token", {
|
||||
method: "POST",
|
||||
headers: {"Content-Type": "application/x-www-form-urlencoded"},
|
||||
body: new URLSearchParams({client_id: config.discord.clientId, client_secret: config.discord.clientSecret,
|
||||
grant_type: "authorization_code", code, redirect_uri: staffRedirectUri()}).toString(),
|
||||
});
|
||||
if (token.status !== 200 || !token.body.access_token) {
|
||||
return {error: "discord"};
|
||||
}
|
||||
const me = await call("/users/@me", {headers: {Authorization: `Bearer ${token.body.access_token}`}});
|
||||
if (me.status !== 200 || !/^\d{5,25}$/.test(String(me.body.id))) {
|
||||
return {error: "discord"};
|
||||
}
|
||||
const account = accounts.all().find(candidate => candidate.discordId === String(me.body.id));
|
||||
return account ? {ok: true, account} : {error: "denied"};
|
||||
}
|
||||
catch (err) {
|
||||
console.log(`[discord] staff sign-in failed: ${logText(err.message)}`);
|
||||
return {error: "discord"};
|
||||
}
|
||||
};
|
||||
|
||||
// the link undone, by the player or an admin; the booster badge goes with it.
|
||||
const unlink = account => {
|
||||
const had = Boolean(account.discordId || account.discord);
|
||||
@@ -202,4 +292,4 @@ const startup = () => {
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {linking, boostChecks, start, finish, unlink, startup};
|
||||
module.exports = {linking, boostChecks, start, finish, startAdmin, finishAdmin, startStaff, finishStaff, unlink, startup};
|
||||
Reference in new issue
Block a user