2026-08-06 16:53:48 +02:00
|
|
|
import test from 'node:test';
|
|
|
|
|
import assert from 'node:assert/strict';
|
2026-10-19 21:46:52 +00:00
|
|
|
import crypto from 'node:crypto';
|
2026-08-06 16:53:48 +02:00
|
|
|
import {
|
|
|
|
|
ResetManager,
|
|
|
|
|
createSessionToken,
|
|
|
|
|
hashPassword,
|
2026-10-19 21:46:52 +00:00
|
|
|
hashPasswordAsync,
|
2026-08-06 16:53:48 +02:00
|
|
|
hashToken,
|
2026-10-19 21:46:52 +00:00
|
|
|
passwordHashNeedsUpgrade,
|
|
|
|
|
verifyAndUpgradePasswordAsync,
|
2026-08-06 16:53:48 +02:00
|
|
|
verifyPassword,
|
2026-10-19 21:46:52 +00:00
|
|
|
verifyPasswordAsync,
|
2026-08-06 16:53:48 +02:00
|
|
|
} from '../src/auth.js';
|
2026-10-20 15:27:53 +00:00
|
|
|
import { totpProvisioningUri, verifyTotp, verifyTotpStep } from '../src/totp.js';
|
2026-08-06 16:53:48 +02:00
|
|
|
|
|
|
|
|
test('reset codes are printed, case-insensitive, expiring, and one-time', () => {
|
|
|
|
|
const messages = [];
|
|
|
|
|
const manager = new ResetManager({ log: (message) => messages.push(message) });
|
|
|
|
|
const code = manager.request();
|
|
|
|
|
|
|
|
|
|
assert.equal(code.length, 8);
|
|
|
|
|
assert.equal(messages.length, 1);
|
|
|
|
|
assert.match(messages[0], new RegExp(`ADMIN RESET CODE: ${code}`));
|
|
|
|
|
assert.equal(manager.confirm(` ${code.toLowerCase()} `), true);
|
|
|
|
|
assert.equal(manager.confirm(code), false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('expired reset codes are rejected', () => {
|
|
|
|
|
const manager = new ResetManager({ ttlMs: -1, log: () => {} });
|
|
|
|
|
const code = manager.request();
|
|
|
|
|
assert.equal(manager.confirm(code), false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('password hashes verify and reject a different password', () => {
|
|
|
|
|
const hash = hashPassword('correct horse battery staple');
|
|
|
|
|
assert.equal(verifyPassword('correct horse battery staple', hash), true);
|
|
|
|
|
assert.equal(verifyPassword('wrong password', hash), false);
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-20 15:27:53 +00:00
|
|
|
test('new password hashes use the shared bcrypt scheme and cost', () => {
|
2026-10-19 21:46:52 +00:00
|
|
|
const hash = hashPassword('correct horse battery staple');
|
2026-10-20 15:27:53 +00:00
|
|
|
assert.match(hash, /^bcrypt\$\$2b\$12\$/);
|
2026-10-19 21:46:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('legacy salt:hex hashes still verify at the original iteration count', () => {
|
|
|
|
|
const salt = crypto.randomBytes(16).toString('hex');
|
|
|
|
|
const hash = crypto.pbkdf2Sync('legacy password', salt, 100000, 32, 'sha256').toString('hex');
|
|
|
|
|
assert.equal(verifyPassword('legacy password', `${salt}:${hash}`), true);
|
|
|
|
|
assert.equal(verifyPassword('wrong password', `${salt}:${hash}`), false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('a hash with a malformed iteration count never verifies', () => {
|
|
|
|
|
assert.equal(verifyPassword('correct horse battery staple', 'pbkdf2-sha256$0$aa$bb'), false);
|
|
|
|
|
assert.equal(verifyPassword('correct horse battery staple', 'pbkdf2-sha256$x$aa$bb'), false);
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-20 15:27:53 +00:00
|
|
|
test('async password helpers produce and verify the same bcrypt format', async () => {
|
|
|
|
|
const hash = await hashPasswordAsync('correct horse battery staple');
|
|
|
|
|
assert.match(hash, /^bcrypt\$\$2b\$12\$/);
|
2026-10-19 21:46:52 +00:00
|
|
|
assert.equal(await verifyPasswordAsync('correct horse battery staple', hash), true);
|
|
|
|
|
assert.equal(await verifyPasswordAsync('wrong password', hash), false);
|
|
|
|
|
assert.equal(verifyPassword('correct horse battery staple', hash), true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('formats this module does not verify fail closed', () => {
|
2026-10-20 15:27:53 +00:00
|
|
|
for (const stored of ['', '$2b$12$abc', 'bcrypt$$2a$12$abc', 'bcrypt-sha256$$2b$12$abc', 'pbkdf2-sha256$600000$00$00$extra']) {
|
2026-10-19 21:46:52 +00:00
|
|
|
assert.equal(verifyPassword('correct horse battery staple', stored), false);
|
|
|
|
|
}
|
|
|
|
|
assert.equal(passwordHashNeedsUpgrade('$2b$12$abc'), true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('a legacy salt:hex hash is rewritten in the current scheme after a successful login', async () => {
|
|
|
|
|
const salt = crypto.randomBytes(16).toString('hex');
|
|
|
|
|
const legacy = `${salt}:${crypto.pbkdf2Sync('legacy password', salt, 100000, 32, 'sha256').toString('hex')}`;
|
|
|
|
|
assert.equal(passwordHashNeedsUpgrade(legacy), true);
|
|
|
|
|
|
|
|
|
|
const result = await verifyAndUpgradePasswordAsync('legacy password', legacy);
|
|
|
|
|
assert.equal(result.valid, true);
|
2026-10-20 15:27:53 +00:00
|
|
|
assert.match(result.upgradedHash, /^bcrypt\$\$2b\$12\$/);
|
2026-10-19 21:46:52 +00:00
|
|
|
assert.equal(passwordHashNeedsUpgrade(result.upgradedHash), false);
|
|
|
|
|
assert.equal(verifyPassword('legacy password', result.upgradedHash), true);
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-20 15:27:53 +00:00
|
|
|
test('a long legacy PBKDF2 password is migrated without truncation', async () => {
|
|
|
|
|
const longPassword = 'a'.repeat(80);
|
|
|
|
|
const salt = crypto.randomBytes(16).toString('hex');
|
|
|
|
|
const legacy = `pbkdf2-sha256$600000$${salt}$${crypto.pbkdf2Sync(longPassword, salt, 600000, 32, 'sha256').toString('hex')}`;
|
|
|
|
|
const result = await verifyAndUpgradePasswordAsync(longPassword, legacy);
|
|
|
|
|
assert.equal(result.valid, true);
|
|
|
|
|
assert.match(result.upgradedHash, /^bcrypt-sha256\$\$2b\$12\$/);
|
|
|
|
|
assert.equal(await verifyPasswordAsync(longPassword, result.upgradedHash), true);
|
|
|
|
|
assert.equal(await verifyPasswordAsync(`${longPassword}x`, result.upgradedHash), false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('a low-cost bcrypt hash is rewritten, and a short legacy password still upgrades', async () => {
|
|
|
|
|
const bcrypt = (await import('bcryptjs')).default;
|
|
|
|
|
const low = `bcrypt$${await bcrypt.hash('correct horse battery staple', 4)}`;
|
2026-10-19 21:46:52 +00:00
|
|
|
const lowResult = await verifyAndUpgradePasswordAsync('correct horse battery staple', low);
|
2026-10-20 15:27:53 +00:00
|
|
|
assert.match(lowResult.upgradedHash, /^bcrypt\$\$2b\$12\$/);
|
2026-10-19 21:46:52 +00:00
|
|
|
|
|
|
|
|
const salt = crypto.randomBytes(16).toString('hex');
|
|
|
|
|
const shortLegacy = `${salt}:${crypto.pbkdf2Sync('short', salt, 100000, 32, 'sha256').toString('hex')}`;
|
|
|
|
|
const shortResult = await verifyAndUpgradePasswordAsync('short', shortLegacy);
|
|
|
|
|
assert.equal(shortResult.valid, true);
|
|
|
|
|
assert.equal(verifyPassword('short', shortResult.upgradedHash), true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('a wrong password neither verifies nor upgrades, and a current hash is not rewritten', async () => {
|
2026-10-20 15:27:53 +00:00
|
|
|
const hash = await hashPasswordAsync('correct horse battery staple');
|
2026-10-19 21:46:52 +00:00
|
|
|
assert.deepEqual(await verifyAndUpgradePasswordAsync('wrong password', hash), { valid: false, upgradedHash: null });
|
|
|
|
|
|
|
|
|
|
const current = await hashPasswordAsync('correct horse battery staple');
|
|
|
|
|
assert.equal(passwordHashNeedsUpgrade(current), false);
|
|
|
|
|
assert.deepEqual(await verifyAndUpgradePasswordAsync('correct horse battery staple', current), {
|
|
|
|
|
valid: true,
|
|
|
|
|
upgradedHash: null,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-06 16:53:48 +02:00
|
|
|
test('TOTP verifies RFC 6238 SHA-1 vectors', () => {
|
|
|
|
|
const secret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ';
|
|
|
|
|
const vectors = [
|
|
|
|
|
[59, '287082'],
|
|
|
|
|
[1111111109, '081804'],
|
|
|
|
|
[1111111111, '050471'],
|
|
|
|
|
[1234567890, '005924'],
|
|
|
|
|
[2000000000, '279037'],
|
|
|
|
|
];
|
|
|
|
|
for (const [seconds, code] of vectors) {
|
|
|
|
|
assert.equal(verifyTotp(secret, code, seconds * 1000), true);
|
2026-10-20 15:27:53 +00:00
|
|
|
assert.equal(verifyTotpStep(secret, code, seconds * 1000), Math.floor(seconds / 30));
|
2026-08-06 16:53:48 +02:00
|
|
|
}
|
|
|
|
|
assert.match(totpProvisioningUri('Example App', 'admin@example.com', secret), /^otpauth:\/\/totp\//);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('session token hashes are not the raw token', () => {
|
|
|
|
|
const { token, tokenHash } = createSessionToken();
|
|
|
|
|
assert.notEqual(token, tokenHash);
|
|
|
|
|
assert.equal(hashToken(token), tokenHash);
|
|
|
|
|
});
|