Initial upload to Gitea

This commit is contained in:
antonn committed 2026-11-30 16:32:37 +00:00
1 parent 78193254ad
commit 8478998cae
1485 files changed
+461025 -42348

No files matched your search

+101
View File
@@ -0,0 +1,101 @@
"use strict";
const { newSessionEpoch, newSessionEpochDifferentFrom } = require("../utils/session-epoch");
const { withAccountActionTransaction } = require("./account-actions");
// adpanel:credential-generation-cas - every self-service credential commit
// pins the generation its proof authorized, not only the field it changes.
// Credential and generation move atomically; missing or stale proofs write
// nothing. MFA proofs use the generation captured when setup was authorized.
const CREDENTIAL_COMMIT_SQL = Object.freeze({
password: "UPDATE users SET password = ?, session_epoch = ? WHERE id = ? AND password = ? AND session_epoch = ?",
email: "UPDATE users SET email = ?, session_epoch = ? WHERE id = ? AND password = ? AND LOWER(email) = LOWER(?) AND session_epoch = ?",
// adpanel:totp-confirm-atomic-burn - a new seed must never be visible without its consumed step.
twoFactor: "UPDATE users SET secret = ?, recovery_codes = ?, session_epoch = ?, totp_last_counter = GREATEST(COALESCE(totp_last_counter, 0), ?) WHERE id = ? AND secret <=> ? AND session_epoch = ?",
});
// adpanel:recovery-proof-commit - a reset or code-set replacement during hashing
// invalidates the proof; a successful inventory replacement keeps sessions live.
const RECOVERY_CODES_COMMIT_SQL = "UPDATE users SET recovery_codes = ? WHERE id = ? AND password = ? AND session_epoch = ? AND recovery_codes <=> ?";
function createCredentialCommitService({ db } = {}) {
if (typeof db?.query !== "function") throw new TypeError("db.query dependency is required");
async function commit(kind, user, newEpoch, params, provedEpoch) {
if (!user?.id || !Number.isSafeInteger(provedEpoch) || provedEpoch < 0 ||
provedEpoch !== user.session_epoch || !Number.isSafeInteger(newEpoch) ||
newEpoch <= 0 || newEpoch === provedEpoch) {
return { affectedRows: 0 };
}
const result = await db.query(CREDENTIAL_COMMIT_SQL[kind], [...params, provedEpoch]);
return result?.affectedRows === 1 ? result : { affectedRows: 0 };
}
return {
commitPassword: (user, password, newEpoch) =>
commit("password", user, newEpoch, [password, newEpoch, user?.id, user?.password], user?.session_epoch),
commitEmail: (user, email, oldEmail, newEpoch) =>
commit("email", user, newEpoch, [email, newEpoch, user?.id, user?.password, oldEmail], user?.session_epoch),
async commitTwoFactor(user, secret, recoveryCodes, priorSecret, newEpoch, provedEpoch, verifiedStep) {
if (!Number.isSafeInteger(verifiedStep) || verifiedStep < 0) return { affectedRows: 0 };
return commit("twoFactor", user, newEpoch, [secret, recoveryCodes, newEpoch, verifiedStep, user?.id, priorSecret], provedEpoch);
},
async commitRecoveryCodes(user, recoveryCodes, priorRecoveryCodes) {
if (!user?.id || !user.password || !Number.isSafeInteger(user.session_epoch) || user.session_epoch < 0 ||
(priorRecoveryCodes !== null && typeof priorRecoveryCodes !== "string")) {
return { affectedRows: 0 };
}
const result = await db.query(RECOVERY_CODES_COMMIT_SQL, [recoveryCodes, user.id, user.password, user.session_epoch, priorRecoveryCodes]);
return result?.affectedRows === 1 ? result : { affectedRows: 0 };
},
};
}
// adpanel:account-actor-commit - provisioning and credential overrides lock
// the acting administrator's proved generation and the selected target row.
async function createProvisionedAccount(db, actor, { email, passwordHash, admin, avatarUrl }) {
return withAccountActionTransaction(db, actor, null, async (connection) => {
const [result] = await connection.execute(
"INSERT INTO users (email, password, secret, admin, recovery_codes, avatar_url, session_epoch) VALUES (?, ?, NULL, ?, NULL, ?, ?)",
[email, passwordHash, admin ? 1 : 0, avatarUrl, newSessionEpoch()]
);
return { ok: result?.affectedRows === 1 };
});
}
async function resetAccountPassword(db, targetUser, passwordHash, actor) {
const outcome = await withAccountActionTransaction(db, actor, targetUser, async (connection, live) => {
const [result] = await connection.execute(
"UPDATE users SET password = ?, session_epoch = ? WHERE id = ?",
[passwordHash, newSessionEpochDifferentFrom(Number(live.session_epoch || 0)), live.id]
);
return { ok: result?.affectedRows === 1 };
}, { allowAdminTarget: true });
return outcome.ok;
}
async function resetAccountTwoFactor(db, targetUser, actor) {
const outcome = await withAccountActionTransaction(db, actor, targetUser, async (connection, live) => {
// adpanel:admin-2fa-recovery-revocation - backup codes must not outlive
// the authenticator reset; revoke both factors with the session generation.
const [result] = await connection.execute(
"UPDATE users SET secret = NULL, recovery_codes = NULL, session_epoch = ? WHERE id = ?",
[newSessionEpochDifferentFrom(Number(live.session_epoch || 0)), live.id]
);
return { ok: result?.affectedRows === 1 };
}, { allowAdminTarget: true });
return outcome.ok;
}
async function resetAccountRecoveryCodes(db, targetUser, recoveryCodes, actor) {
const outcome = await withAccountActionTransaction(db, actor, targetUser, async (connection, live) => {
const [result] = await connection.execute(
"UPDATE users SET recovery_codes = ? WHERE id = ? AND admin = 0",
[recoveryCodes, live.id]
);
return { ok: result?.affectedRows === 1 };
});
return outcome.ok;
}
module.exports = { createProvisionedAccount, resetAccountPassword, resetAccountTwoFactor, resetAccountRecoveryCodes, createCredentialCommitService, CREDENTIAL_COMMIT_SQL, RECOVERY_CODES_COMMIT_SQL };