-
-
Notifications
You must be signed in to change notification settings - Fork 10
refactor(users,auth): extract password hashing to lib/helpers to break auth↔users cycle #3864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PierreBrisorgueil
merged 6 commits into
master
from
fix/3862-password-helper-break-cycle
Jun 14, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f403fe5
test(auth): unit-test extracted bcrypt password helper
PierreBrisorgueil 9699b88
refactor(users): hash via lib/helpers/password — drop auth.service im…
PierreBrisorgueil 1cac757
refactor(auth): source hashPassword/comparePassword from lib helper, …
PierreBrisorgueil 2f38e18
test(users): drop dead auth.service mock from remove suites (#3862)
PierreBrisorgueil 9e5a64f
test(auth): restore comparePassword false-match assertion (#3862)
PierreBrisorgueil b00ccd3
docs(password): add per-function JSDoc to hashPassword and comparePas…
PierreBrisorgueil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /** | ||
| * Password hashing helpers (bcrypt). Dependency-free leaf — breaks the auth↔users service cycle. | ||
| * Source of truth for hashPassword / comparePassword; both auth.service and users.service import from here. | ||
| */ | ||
| import bcrypt from 'bcrypt'; | ||
|
|
||
| const saltRounds = 10; | ||
|
|
||
| /** | ||
| * @desc Hash a plaintext password using bcrypt. | ||
| * @param {string|number} password - The plaintext password to hash. | ||
| * @returns {Promise<string>} The bcrypt hash. | ||
| */ | ||
| const hashPassword = (password) => bcrypt.hash(String(password), saltRounds); | ||
|
|
||
| /** | ||
| * @desc Compare a plaintext password against a stored bcrypt hash. | ||
| * @param {string|number} userPassword - The plaintext candidate password. | ||
| * @param {string} storedPassword - The stored bcrypt hash. | ||
| * @returns {Promise<boolean>} True if the password matches the hash. | ||
| */ | ||
| const comparePassword = async (userPassword, storedPassword) => bcrypt.compare(String(userPassword), String(storedPassword)); | ||
|
|
||
| export { hashPassword, comparePassword }; | ||
| export default { hashPassword, comparePassword }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * Unit tests for the password helper (bcrypt wrappers). | ||
| * Dependency-free leaf — mocks only bcrypt. | ||
| */ | ||
| import { jest, describe, test, expect, beforeEach } from '@jest/globals'; | ||
|
|
||
| const mockBcryptHash = jest.fn(); | ||
| const mockBcryptCompare = jest.fn(); | ||
|
|
||
| jest.unstable_mockModule('bcrypt', () => ({ | ||
| default: { | ||
| hash: (...args) => mockBcryptHash(...args), | ||
| compare: (...args) => mockBcryptCompare(...args), | ||
| }, | ||
| })); | ||
|
|
||
| const { default: passwordHelper, hashPassword, comparePassword } = await import('../password.js'); | ||
|
|
||
| describe('password helper', () => { | ||
| beforeEach(() => { | ||
| mockBcryptHash.mockReset(); | ||
| mockBcryptCompare.mockReset(); | ||
| }); | ||
|
|
||
| describe('hashPassword()', () => { | ||
| test('hashes the stringified password with saltRounds = 10', async () => { | ||
| mockBcryptHash.mockResolvedValueOnce('$2b$hashed'); | ||
| const result = await hashPassword('mypassword'); | ||
| expect(result).toBe('$2b$hashed'); | ||
| expect(mockBcryptHash).toHaveBeenCalledWith('mypassword', 10); | ||
| }); | ||
|
|
||
| test('coerces a non-string password to a string before hashing', async () => { | ||
| mockBcryptHash.mockResolvedValueOnce('$2b$num'); | ||
| await hashPassword(12345); | ||
| expect(mockBcryptHash).toHaveBeenCalledWith('12345', 10); | ||
| }); | ||
| }); | ||
|
|
||
| describe('comparePassword()', () => { | ||
| test('compares the stringified candidate against the stored hash', async () => { | ||
| mockBcryptCompare.mockResolvedValueOnce(true); | ||
| const result = await comparePassword('plain', 'hashed'); | ||
| expect(result).toBe(true); | ||
| expect(mockBcryptCompare).toHaveBeenCalledWith('plain', 'hashed'); | ||
| }); | ||
|
|
||
| test('coerces non-string args to strings before comparing', async () => { | ||
| mockBcryptCompare.mockResolvedValueOnce(false); | ||
| await comparePassword(12345, 67890); | ||
| expect(mockBcryptCompare).toHaveBeenCalledWith('12345', '67890'); | ||
| }); | ||
|
|
||
| test('returns false when the candidate does not match the stored hash', async () => { | ||
| mockBcryptCompare.mockResolvedValueOnce(false); | ||
| const result = await comparePassword('wrong', 'hashed'); | ||
| expect(result).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('default export', () => { | ||
| test('exposes hashPassword and comparePassword', () => { | ||
| expect(typeof passwordHelper.hashPassword).toBe('function'); | ||
| expect(typeof passwordHelper.comparePassword).toBe('function'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.