Files
Cato d528260d49 feat(auth): CJS ./auth/password and one new-password field on reset (0.9.1) (#37)
* feat(auth): CommonJS build of ./auth/password; reset form asks for the new password once, in the same field as sign-up

* feat(auth): PasswordField as its own entry for products with their own pages

* fix(styles): the eye's room applies to any input in the box

* feat(auth): ./auth/hash, bcrypt over SHA-256 with verify-and-rehash of old hashes

* feat(auth): PasswordField accepts name and placeholder
2026-10-28 03:54:02 +01:00

57 lines
1.9 KiB
JavaScript

import assert from "node:assert/strict";
import { createRequire } from "node:module";
import test from "node:test";
import bcrypt from "bcryptjs";
import {
createPasswordHasher,
PASSWORD_HASH_PREFIX,
} from "../dist/auth/hash.js";
// A cheap cost keeps the test fast; the algorithm is the same.
const hasher = createPasswordHasher(bcrypt, 4);
test("every character of a long password counts", async () => {
const stored = await hasher.hash("a".repeat(100) + "X");
assert.ok(stored.startsWith(PASSWORD_HASH_PREFIX));
assert.equal(
(await hasher.verify("a".repeat(100) + "X", stored)).valid,
true,
);
assert.equal(
(await hasher.verify("a".repeat(100) + "Y", stored)).valid,
false,
);
assert.equal((await hasher.verify("a".repeat(72), stored)).valid, false);
});
test("an old plain bcrypt hash still verifies and asks to be replaced", async () => {
const legacy = await bcrypt.hash("old password", 4);
const ok = await hasher.verify("old password", legacy);
assert.deepEqual(ok, { valid: true, rehash: true });
assert.deepEqual(await hasher.verify("wrong", legacy), {
valid: false,
rehash: false,
});
const upgraded = await hasher.hash("old password");
assert.deepEqual(await hasher.verify("old password", upgraded), {
valid: true,
rehash: false,
});
});
test("a missing hash or password never verifies", async () => {
assert.equal((await hasher.verify("x", null)).valid, false);
assert.equal((await hasher.verify("x", "")).valid, false);
assert.equal((await hasher.verify(undefined, "$2b$04$abc")).valid, false);
});
test("the CommonJS build is the same hasher", async () => {
const require = createRequire(import.meta.url);
const cjs = require("@bitbaum/accountkit/auth/hash");
const stored = await createPasswordHasher(bcrypt, 4).hash("same");
assert.equal(
(await cjs.createPasswordHasher(bcrypt, 4).verify("same", stored)).valid,
true,
);
});