2026-10-19 21:14:31 +00:00
|
|
|
import test from 'node:test';
|
|
|
|
|
import assert from 'node:assert/strict';
|
|
|
|
|
import { existsSync, readFileSync } from 'node:fs';
|
|
|
|
|
import { hashPassword, passwordHashNeedsUpgrade, verifyAndUpgradePasswordAsync, verifyPassword } from '../src/auth.js';
|
2026-10-20 15:26:58 +00:00
|
|
|
import { verifyTotp, verifyTotpStep } from '../src/totp.js';
|
|
|
|
|
import {
|
|
|
|
|
EXAMPLE_INTERNAL_SECRETS,
|
|
|
|
|
MIN_INTERNAL_SECRET_LENGTH,
|
|
|
|
|
internalSecretMatches,
|
|
|
|
|
} from '../src/coreconfig.js';
|
2026-10-19 21:14:31 +00:00
|
|
|
|
|
|
|
|
// The Go, Node and Python auth helpers load the same fixtures; see "description" in the file.
|
|
|
|
|
const contractUrl = new URL('../../../contract/auth-contract.json', import.meta.url);
|
|
|
|
|
const contract = existsSync(contractUrl) ? JSON.parse(readFileSync(contractUrl, 'utf8')) : null;
|
|
|
|
|
const skip = contract ? false : 'contract/auth-contract.json is not part of this checkout';
|
|
|
|
|
|
|
|
|
|
test('password hashes verify as the contract says for Node', { skip }, async () => {
|
|
|
|
|
const { plaintext, wrongPassword, hashes } = contract.password;
|
|
|
|
|
for (const entry of hashes) {
|
2026-10-20 15:26:58 +00:00
|
|
|
const password = entry.password ?? plaintext;
|
|
|
|
|
const rejectedPassword = entry.wrongPassword ?? wrongPassword;
|
|
|
|
|
assert.equal(verifyPassword(password, entry.hash), entry.verifies.node, entry.name);
|
|
|
|
|
assert.equal(verifyPassword(rejectedPassword, entry.hash), false, `${entry.name}: wrong password`);
|
2026-10-19 21:14:31 +00:00
|
|
|
if ('node' in (entry.needsUpgrade ?? {})) {
|
|
|
|
|
assert.equal(passwordHashNeedsUpgrade(entry.hash), entry.needsUpgrade.node, `${entry.name}: needs upgrade`);
|
2026-10-20 15:26:58 +00:00
|
|
|
const { valid, upgradedHash } = await verifyAndUpgradePasswordAsync(password, entry.hash);
|
2026-10-19 21:14:31 +00:00
|
|
|
assert.equal(valid, true, `${entry.name}: valid on upgrade`);
|
|
|
|
|
assert.equal(upgradedHash !== null, entry.needsUpgrade.node, `${entry.name}: upgraded hash`);
|
2026-10-20 15:26:58 +00:00
|
|
|
if (upgradedHash) assert.equal(verifyPassword(password, upgradedHash), true);
|
2026-10-19 21:14:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('the password length contract holds for Node', { skip }, () => {
|
|
|
|
|
const { bytes, accepted } = contract.password.tooLongToHash;
|
|
|
|
|
let didHash = true;
|
|
|
|
|
try {
|
2026-10-20 15:26:58 +00:00
|
|
|
hashPassword('a'.repeat(bytes));
|
2026-10-19 21:14:31 +00:00
|
|
|
} catch {
|
|
|
|
|
didHash = false;
|
|
|
|
|
}
|
|
|
|
|
assert.equal(didHash, accepted.node);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('TOTP verifies as the contract says', { skip }, () => {
|
|
|
|
|
for (const entry of contract.totp.cases) {
|
|
|
|
|
const secret = entry.secret ?? contract.totp.secret;
|
|
|
|
|
assert.equal(verifyTotp(secret, entry.code, entry.unix * 1000), entry.valid, entry.name);
|
2026-10-20 15:26:58 +00:00
|
|
|
if (entry.valid)
|
|
|
|
|
assert.equal(
|
|
|
|
|
verifyTotpStep(secret, entry.code, entry.unix * 1000),
|
|
|
|
|
entry.matchedStep ?? Math.floor(entry.unix / 30),
|
|
|
|
|
entry.name,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('the internal secret gate matches the contract for Node', { skip }, () => {
|
|
|
|
|
const rule = contract.internalSecret;
|
|
|
|
|
assert.equal(MIN_INTERNAL_SECRET_LENGTH, rule.minLength);
|
|
|
|
|
assert.deepEqual([...EXAMPLE_INTERNAL_SECRETS].sort(), [...rule.placeholders].sort());
|
|
|
|
|
for (const entry of rule.cases) {
|
|
|
|
|
assert.equal(internalSecretMatches(entry.provided, entry.secret), entry.matches, entry.name);
|
2026-10-19 21:14:31 +00:00
|
|
|
}
|
|
|
|
|
});
|