docs: record auth hash alignment and validation results
This commit is contained in:
1 parent
22887fef03
commit
f7819904b9
51 files changed
+1792
-542
No files matched your search
@@ -12,7 +12,7 @@ import {
|
||||
verifyPassword,
|
||||
verifyPasswordAsync,
|
||||
} from '../src/auth.js';
|
||||
import { totpProvisioningUri, verifyTotp } from '../src/totp.js';
|
||||
import { totpProvisioningUri, verifyTotp, verifyTotpStep } from '../src/totp.js';
|
||||
|
||||
test('reset codes are printed, case-insensitive, expiring, and one-time', () => {
|
||||
const messages = [];
|
||||
@@ -38,9 +38,9 @@ test('password hashes verify and reject a different password', () => {
|
||||
assert.equal(verifyPassword('wrong password', hash), false);
|
||||
});
|
||||
|
||||
test('new password hashes record their iteration count', () => {
|
||||
test('new password hashes use the shared bcrypt scheme and cost', () => {
|
||||
const hash = hashPassword('correct horse battery staple');
|
||||
assert.match(hash, /^pbkdf2-sha256\$600000\$[0-9a-f]{32}\$[0-9a-f]{64}$/);
|
||||
assert.match(hash, /^bcrypt\$\$2b\$12\$/);
|
||||
});
|
||||
|
||||
test('legacy salt:hex hashes still verify at the original iteration count', () => {
|
||||
@@ -55,16 +55,16 @@ test('a hash with a malformed iteration count never verifies', () => {
|
||||
assert.equal(verifyPassword('correct horse battery staple', 'pbkdf2-sha256$x$aa$bb'), false);
|
||||
});
|
||||
|
||||
test('async password helpers produce and verify the same format', async () => {
|
||||
const hash = await hashPasswordAsync('correct horse battery staple', { iterations: 1000 });
|
||||
assert.match(hash, /^pbkdf2-sha256\$1000\$/);
|
||||
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\$/);
|
||||
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', () => {
|
||||
for (const stored of ['', '$2b$12$abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQ', 'bcrypt$$2a$12$abc', 'pbkdf2-sha256$600000$00$00$extra']) {
|
||||
for (const stored of ['', '$2b$12$abc', 'bcrypt$$2a$12$abc', 'bcrypt-sha256$$2b$12$abc', 'pbkdf2-sha256$600000$00$00$extra']) {
|
||||
assert.equal(verifyPassword('correct horse battery staple', stored), false);
|
||||
}
|
||||
assert.equal(passwordHashNeedsUpgrade('$2b$12$abc'), true);
|
||||
@@ -77,15 +77,27 @@ test('a legacy salt:hex hash is rewritten in the current scheme after a successf
|
||||
|
||||
const result = await verifyAndUpgradePasswordAsync('legacy password', legacy);
|
||||
assert.equal(result.valid, true);
|
||||
assert.match(result.upgradedHash, /^pbkdf2-sha256\$600000\$/);
|
||||
assert.match(result.upgradedHash, /^bcrypt\$\$2b\$12\$/);
|
||||
assert.equal(passwordHashNeedsUpgrade(result.upgradedHash), false);
|
||||
assert.equal(verifyPassword('legacy password', result.upgradedHash), true);
|
||||
});
|
||||
|
||||
test('a hash below the current iteration count is rewritten, and a short legacy password still upgrades', async () => {
|
||||
const low = await hashPasswordAsync('correct horse battery staple', { iterations: 1000 });
|
||||
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)}`;
|
||||
const lowResult = await verifyAndUpgradePasswordAsync('correct horse battery staple', low);
|
||||
assert.match(lowResult.upgradedHash, /^pbkdf2-sha256\$600000\$/);
|
||||
assert.match(lowResult.upgradedHash, /^bcrypt\$\$2b\$12\$/);
|
||||
|
||||
const salt = crypto.randomBytes(16).toString('hex');
|
||||
const shortLegacy = `${salt}:${crypto.pbkdf2Sync('short', salt, 100000, 32, 'sha256').toString('hex')}`;
|
||||
@@ -95,7 +107,7 @@ test('a hash below the current iteration count is rewritten, and a short legacy
|
||||
});
|
||||
|
||||
test('a wrong password neither verifies nor upgrades, and a current hash is not rewritten', async () => {
|
||||
const hash = await hashPasswordAsync('correct horse battery staple', { iterations: 1000 });
|
||||
const hash = await hashPasswordAsync('correct horse battery staple');
|
||||
assert.deepEqual(await verifyAndUpgradePasswordAsync('wrong password', hash), { valid: false, upgradedHash: null });
|
||||
|
||||
const current = await hashPasswordAsync('correct horse battery staple');
|
||||
@@ -117,6 +129,7 @@ test('TOTP verifies RFC 6238 SHA-1 vectors', () => {
|
||||
];
|
||||
for (const [seconds, code] of vectors) {
|
||||
assert.equal(verifyTotp(secret, code, seconds * 1000), true);
|
||||
assert.equal(verifyTotpStep(secret, code, seconds * 1000), Math.floor(seconds / 30));
|
||||
}
|
||||
assert.match(totpProvisioningUri('Example App', 'admin@example.com', secret), /^otpauth:\/\/totp\//);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user