docs: record auth hash alignment and validation results

This commit is contained in:
Gabriel20xx committed 2026-10-20 15:26:58 +00:00
1 parent 22887fef03
commit f7819904b9
51 files changed
+1792 -542

No files matched your search

+35 -1
View File
@@ -10,7 +10,7 @@ function fakeDatabase({ rowCount = 1, totpEnabled = false, rowsByCondition = {}
async query(text, params = []) {
queries.push({ text, params });
if (text.includes('totp_enabled, false) AS enabled')) {
return { rows: [{ secret: 'S', enabled: totpEnabled }], rowCount: 1 };
return { rows: [{ secret: 'S', enabled: totpEnabled, last_used_step: -1 }], rowCount: 1 };
}
for (const [condition, row] of Object.entries(rowsByCondition)) {
if (text.includes(condition)) return { rows: [row], rowCount: 1 };
@@ -27,6 +27,7 @@ test('ensureSchema requests the case-insensitive username index, only while no c
assert.match(statements, /auth_users_username_lower_idx/);
assert.match(statements, /lower\(username\)/);
assert.match(statements, /HAVING count\(\*\) > 1/);
assert.match(statements, /ADD COLUMN IF NOT EXISTS last_used_step BIGINT NOT NULL DEFAULT -1/);
});
test('setTotpSecret refuses to replace an enabled second factor', async () => {
@@ -41,6 +42,39 @@ test('setTotpSecret stores a pending secret while TOTP is off, and ignores an un
await new AuthStore(fakeDatabase({ rowCount: 0 })).setTotpSecret(2, 'NEWSECRET');
});
test('TOTP enrollment records its code step and a replay is rejected atomically', async () => {
const state = { secret: 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ', enabled: false, last_used_step: -1 };
const database = {
async query(text, params = []) {
if (text.startsWith('SELECT COALESCE(totp_secret')) {
return { rows: [{ ...state }], rowCount: 1 };
}
const [step, , secret] = params;
if (text.includes('SET totp_enabled = true')) {
if (!state.enabled && secret === state.secret && state.last_used_step < step) {
state.enabled = true;
state.last_used_step = step;
return { rows: [], rowCount: 1 };
}
return { rows: [], rowCount: 0 };
}
if (text.includes('SET last_used_step = $1')) {
if (state.enabled && secret === state.secret && state.last_used_step < step) {
state.last_used_step = step;
return { rows: [], rowCount: 1 };
}
return { rows: [], rowCount: 0 };
}
throw new Error(`unexpected query: ${text}`);
},
};
const store = new AuthStore(database);
assert.equal(await store.enableTotp(1, '287082', 59000), true);
assert.equal(state.last_used_step, 1);
assert.equal(await store.consumeTotpCode(1, '287082', 59000), false);
assert.equal(state.last_used_step, 1);
});
test('credential writes refuse an empty hash and a username containing @', async () => {
const database = fakeDatabase();
const store = new AuthStore(database);