@claude-flow/security 3.0.0-alpha.1 → 3.0.0-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/README.md +292 -234
  2. package/__tests__/acceptance/security-compliance.test.ts +674 -674
  3. package/__tests__/authorization-propagator.test.ts +155 -0
  4. package/__tests__/credential-generator.test.ts +310 -310
  5. package/__tests__/fixtures/configurations.ts +425 -419
  6. package/__tests__/fixtures/index.ts +21 -21
  7. package/__tests__/helpers/create-mock.ts +469 -469
  8. package/__tests__/helpers/index.ts +32 -32
  9. package/__tests__/input-validator.test.ts +382 -381
  10. package/__tests__/integration/security-flow.test.ts +606 -606
  11. package/__tests__/keychain-adapter.test.ts +60 -0
  12. package/__tests__/oauth-browser.test.ts +13 -0
  13. package/__tests__/oauth-export-surface.test.ts +21 -0
  14. package/__tests__/oauth-pkce.test.ts +37 -0
  15. package/__tests__/password-hasher.test.ts +239 -239
  16. package/__tests__/path-validator.test.ts +302 -302
  17. package/__tests__/plugin-integrity-verifier.test.ts +199 -0
  18. package/__tests__/safe-executor.test.ts +292 -292
  19. package/__tests__/token-generator.test.ts +371 -371
  20. package/__tests__/tool-output-guardrail.test.ts +256 -0
  21. package/__tests__/unit/credential-generator.test.ts +182 -182
  22. package/__tests__/unit/password-hasher.test.ts +359 -359
  23. package/__tests__/unit/path-validator.test.ts +509 -509
  24. package/__tests__/unit/safe-executor.test.ts +667 -667
  25. package/__tests__/unit/token-generator.test.ts +310 -310
  26. package/dist/CVE-REMEDIATION.d.ts +17 -6
  27. package/dist/CVE-REMEDIATION.d.ts.map +1 -1
  28. package/dist/CVE-REMEDIATION.js +328 -12
  29. package/dist/CVE-REMEDIATION.js.map +1 -1
  30. package/dist/authorization/propagator.d.ts +148 -0
  31. package/dist/authorization/propagator.d.ts.map +1 -0
  32. package/dist/authorization/propagator.js +182 -0
  33. package/dist/authorization/propagator.js.map +1 -0
  34. package/dist/credential-generator.js +9 -9
  35. package/dist/domain/services/security-domain-service.js +1 -1
  36. package/dist/domain/services/security-domain-service.js.map +1 -1
  37. package/dist/index.d.ts +9 -1
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +21 -1
  40. package/dist/index.js.map +1 -1
  41. package/dist/keychain-adapter.d.ts +48 -0
  42. package/dist/keychain-adapter.d.ts.map +1 -0
  43. package/dist/keychain-adapter.js +0 -0
  44. package/dist/keychain-adapter.js.map +1 -0
  45. package/dist/oauth/browser.d.ts +31 -0
  46. package/dist/oauth/browser.d.ts.map +1 -0
  47. package/dist/oauth/browser.js +55 -0
  48. package/dist/oauth/browser.js.map +1 -0
  49. package/dist/oauth/callback-server.d.ts +37 -0
  50. package/dist/oauth/callback-server.d.ts.map +1 -0
  51. package/dist/oauth/callback-server.js +86 -0
  52. package/dist/oauth/callback-server.js.map +1 -0
  53. package/dist/oauth/client.d.ts +57 -0
  54. package/dist/oauth/client.d.ts.map +1 -0
  55. package/dist/oauth/client.js +120 -0
  56. package/dist/oauth/client.js.map +1 -0
  57. package/dist/oauth/pkce.d.ts +25 -0
  58. package/dist/oauth/pkce.d.ts.map +1 -0
  59. package/dist/oauth/pkce.js +36 -0
  60. package/dist/oauth/pkce.js.map +1 -0
  61. package/dist/password-hasher.d.ts.map +1 -1
  62. package/dist/password-hasher.js +8 -1
  63. package/dist/password-hasher.js.map +1 -1
  64. package/dist/plugins/integrity-verifier.d.ts +125 -0
  65. package/dist/plugins/integrity-verifier.d.ts.map +1 -0
  66. package/dist/plugins/integrity-verifier.js +186 -0
  67. package/dist/plugins/integrity-verifier.js.map +1 -0
  68. package/dist/safe-executor.d.ts +7 -0
  69. package/dist/safe-executor.d.ts.map +1 -1
  70. package/dist/safe-executor.js +21 -1
  71. package/dist/safe-executor.js.map +1 -1
  72. package/dist/tool-output-guardrail.d.ts +126 -0
  73. package/dist/tool-output-guardrail.d.ts.map +1 -0
  74. package/dist/tool-output-guardrail.js +257 -0
  75. package/dist/tool-output-guardrail.js.map +1 -0
  76. package/node_modules-incomplete-release-test/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  77. package/package.json +33 -28
  78. package/scripts/verify-oauth-exports.mjs +29 -0
  79. package/src/CVE-REMEDIATION.ts +591 -251
  80. package/src/application/index.ts +10 -10
  81. package/src/application/services/security-application-service.ts +193 -193
  82. package/src/authorization/propagator.ts +279 -0
  83. package/src/credential-generator.ts +368 -368
  84. package/src/domain/entities/security-context.ts +173 -173
  85. package/src/domain/index.ts +17 -17
  86. package/src/domain/services/security-domain-service.ts +296 -296
  87. package/src/index.ts +340 -271
  88. package/src/input-validator.ts +466 -466
  89. package/src/keychain-adapter.ts +0 -0
  90. package/src/oauth/browser.ts +58 -0
  91. package/src/oauth/callback-server.ts +97 -0
  92. package/src/oauth/client.ts +153 -0
  93. package/src/oauth/pkce.ts +45 -0
  94. package/src/password-hasher.ts +277 -270
  95. package/src/path-validator.ts +525 -525
  96. package/src/plugins/integrity-verifier.ts +265 -0
  97. package/src/safe-executor.ts +546 -525
  98. package/src/token-generator.ts +463 -463
  99. package/src/tool-output-guardrail.ts +360 -0
  100. package/tmp.json +0 -0
  101. package/tsconfig.json +9 -9
  102. package/tsconfig.tsbuildinfo +1 -1
  103. package/vitest.config.ts +20 -0
  104. package/.agentic-flow/intelligence.json +0 -16
  105. package/__tests__/coverage/base.css +0 -224
  106. package/__tests__/coverage/block-navigation.js +0 -87
  107. package/__tests__/coverage/coverage-final.json +0 -11
  108. package/__tests__/coverage/favicon.png +0 -0
  109. package/__tests__/coverage/index.html +0 -161
  110. package/__tests__/coverage/lcov-report/base.css +0 -224
  111. package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
  112. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  113. package/__tests__/coverage/lcov-report/index.html +0 -161
  114. package/__tests__/coverage/lcov-report/prettify.css +0 -1
  115. package/__tests__/coverage/lcov-report/prettify.js +0 -2
  116. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  117. package/__tests__/coverage/lcov-report/sorter.js +0 -210
  118. package/__tests__/coverage/lcov-report/src/CVE-REMEDIATION.ts.html +0 -838
  119. package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
  120. package/__tests__/coverage/lcov-report/src/application/services/security-application-service.ts.html +0 -664
  121. package/__tests__/coverage/lcov-report/src/credential-generator.ts.html +0 -1189
  122. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
  123. package/__tests__/coverage/lcov-report/src/domain/entities/security-context.ts.html +0 -604
  124. package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
  125. package/__tests__/coverage/lcov-report/src/domain/services/security-domain-service.ts.html +0 -973
  126. package/__tests__/coverage/lcov-report/src/index.html +0 -206
  127. package/__tests__/coverage/lcov-report/src/input-validator.ts.html +0 -1483
  128. package/__tests__/coverage/lcov-report/src/password-hasher.ts.html +0 -895
  129. package/__tests__/coverage/lcov-report/src/path-validator.ts.html +0 -1660
  130. package/__tests__/coverage/lcov-report/src/safe-executor.ts.html +0 -1660
  131. package/__tests__/coverage/lcov-report/src/token-generator.ts.html +0 -1474
  132. package/__tests__/coverage/lcov.info +0 -3650
  133. package/__tests__/coverage/prettify.css +0 -1
  134. package/__tests__/coverage/prettify.js +0 -2
  135. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  136. package/__tests__/coverage/sorter.js +0 -210
  137. package/__tests__/coverage/src/CVE-REMEDIATION.ts.html +0 -838
  138. package/__tests__/coverage/src/application/services/index.html +0 -116
  139. package/__tests__/coverage/src/application/services/security-application-service.ts.html +0 -664
  140. package/__tests__/coverage/src/credential-generator.ts.html +0 -1189
  141. package/__tests__/coverage/src/domain/entities/index.html +0 -116
  142. package/__tests__/coverage/src/domain/entities/security-context.ts.html +0 -604
  143. package/__tests__/coverage/src/domain/services/index.html +0 -116
  144. package/__tests__/coverage/src/domain/services/security-domain-service.ts.html +0 -973
  145. package/__tests__/coverage/src/index.html +0 -206
  146. package/__tests__/coverage/src/input-validator.ts.html +0 -1483
  147. package/__tests__/coverage/src/password-hasher.ts.html +0 -895
  148. package/__tests__/coverage/src/path-validator.ts.html +0 -1660
  149. package/__tests__/coverage/src/safe-executor.ts.html +0 -1660
  150. package/__tests__/coverage/src/token-generator.ts.html +0 -1474
@@ -1,359 +1,359 @@
1
- /**
2
- * V3 Claude-Flow Password Hasher Unit Tests
3
- *
4
- * London School TDD - Behavior Verification
5
- * Tests password hashing and verification behavior
6
- */
7
- import { describe, it, expect, vi, beforeEach } from 'vitest';
8
- import { createMock, createMockWithBehavior, type MockedInterface } from '../../helpers/create-mock';
9
- import { securityConfigs } from '../../fixtures/configurations';
10
-
11
- /**
12
- * Password hasher interface (to be implemented)
13
- */
14
- interface IPasswordHasher {
15
- hash(password: string): Promise<string>;
16
- verify(password: string, hash: string): Promise<boolean>;
17
- needsRehash(hash: string): boolean;
18
- }
19
-
20
- /**
21
- * Crypto provider interface (collaborator)
22
- */
23
- interface ICryptoProvider {
24
- argon2Hash(password: string, options: Argon2Options): Promise<string>;
25
- argon2Verify(hash: string, password: string): Promise<boolean>;
26
- generateSalt(length: number): Promise<string>;
27
- }
28
-
29
- interface Argon2Options {
30
- memoryCost: number;
31
- timeCost: number;
32
- parallelism: number;
33
- salt?: string;
34
- }
35
-
36
- /**
37
- * Password hasher implementation for testing
38
- */
39
- class PasswordHasher implements IPasswordHasher {
40
- constructor(
41
- private readonly cryptoProvider: ICryptoProvider,
42
- private readonly config: typeof securityConfigs.strict.hashing
43
- ) {}
44
-
45
- async hash(password: string): Promise<string> {
46
- if (!password || password.length < 8) {
47
- throw new Error('Password must be at least 8 characters');
48
- }
49
-
50
- const salt = await this.cryptoProvider.generateSalt(16);
51
-
52
- return this.cryptoProvider.argon2Hash(password, {
53
- memoryCost: this.config.memoryCost ?? 65536,
54
- timeCost: this.config.timeCost ?? 3,
55
- parallelism: this.config.parallelism ?? 4,
56
- salt,
57
- });
58
- }
59
-
60
- async verify(password: string, hash: string): Promise<boolean> {
61
- if (!password || !hash) {
62
- return false;
63
- }
64
-
65
- return this.cryptoProvider.argon2Verify(hash, password);
66
- }
67
-
68
- needsRehash(hash: string): boolean {
69
- // Check if hash uses current algorithm and parameters
70
- const currentVersion = `$argon2id$v=19$m=${this.config.memoryCost},t=${this.config.timeCost},p=${this.config.parallelism}`;
71
- return !hash.startsWith(currentVersion);
72
- }
73
- }
74
-
75
- describe('PasswordHasher', () => {
76
- let mockCryptoProvider: MockedInterface<ICryptoProvider>;
77
- let passwordHasher: PasswordHasher;
78
- const hashingConfig = securityConfigs.strict.hashing;
79
-
80
- beforeEach(() => {
81
- mockCryptoProvider = createMock<ICryptoProvider>();
82
-
83
- // Configure default mock behavior
84
- mockCryptoProvider.generateSalt.mockResolvedValue('randomsalt16byte');
85
- mockCryptoProvider.argon2Hash.mockResolvedValue(
86
- '$argon2id$v=19$m=65536,t=3,p=4$cmFuZG9tc2FsdA$hashedpassword'
87
- );
88
- mockCryptoProvider.argon2Verify.mockResolvedValue(true);
89
-
90
- passwordHasher = new PasswordHasher(mockCryptoProvider, hashingConfig);
91
- });
92
-
93
- describe('hash', () => {
94
- it('should generate salt before hashing', async () => {
95
- // Given
96
- const password = 'securePassword123!';
97
-
98
- // When
99
- await passwordHasher.hash(password);
100
-
101
- // Then - verify interaction
102
- expect(mockCryptoProvider.generateSalt).toHaveBeenCalledWith(16);
103
- expect(mockCryptoProvider.generateSalt).toHaveBeenCalledBefore(
104
- mockCryptoProvider.argon2Hash
105
- );
106
- });
107
-
108
- it('should hash password with configured parameters', async () => {
109
- // Given
110
- const password = 'securePassword123!';
111
-
112
- // When
113
- await passwordHasher.hash(password);
114
-
115
- // Then - verify interaction with correct parameters
116
- expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledWith(
117
- password,
118
- expect.objectContaining({
119
- memoryCost: 65536,
120
- timeCost: 3,
121
- parallelism: 4,
122
- })
123
- );
124
- });
125
-
126
- it('should return hash from crypto provider', async () => {
127
- // Given
128
- const password = 'securePassword123!';
129
- const expectedHash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
130
- mockCryptoProvider.argon2Hash.mockResolvedValue(expectedHash);
131
-
132
- // When
133
- const result = await passwordHasher.hash(password);
134
-
135
- // Then
136
- expect(result).toBe(expectedHash);
137
- });
138
-
139
- it('should reject passwords shorter than 8 characters', async () => {
140
- // Given
141
- const shortPassword = 'short';
142
-
143
- // When/Then
144
- await expect(passwordHasher.hash(shortPassword)).rejects.toThrow(
145
- 'Password must be at least 8 characters'
146
- );
147
-
148
- // Verify no interaction with crypto provider
149
- expect(mockCryptoProvider.generateSalt).not.toHaveBeenCalled();
150
- expect(mockCryptoProvider.argon2Hash).not.toHaveBeenCalled();
151
- });
152
-
153
- it('should reject empty passwords', async () => {
154
- // Given
155
- const emptyPassword = '';
156
-
157
- // When/Then
158
- await expect(passwordHasher.hash(emptyPassword)).rejects.toThrow(
159
- 'Password must be at least 8 characters'
160
- );
161
- });
162
-
163
- it('should use generated salt in hash options', async () => {
164
- // Given
165
- const password = 'securePassword123!';
166
- const generatedSalt = 'unique-salt-value';
167
- mockCryptoProvider.generateSalt.mockResolvedValue(generatedSalt);
168
-
169
- // When
170
- await passwordHasher.hash(password);
171
-
172
- // Then
173
- expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledWith(
174
- password,
175
- expect.objectContaining({
176
- salt: generatedSalt,
177
- })
178
- );
179
- });
180
- });
181
-
182
- describe('verify', () => {
183
- it('should delegate verification to crypto provider', async () => {
184
- // Given
185
- const password = 'securePassword123!';
186
- const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
187
-
188
- // When
189
- await passwordHasher.verify(password, hash);
190
-
191
- // Then - verify interaction
192
- expect(mockCryptoProvider.argon2Verify).toHaveBeenCalledWith(hash, password);
193
- });
194
-
195
- it('should return true for valid password', async () => {
196
- // Given
197
- const password = 'securePassword123!';
198
- const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
199
- mockCryptoProvider.argon2Verify.mockResolvedValue(true);
200
-
201
- // When
202
- const result = await passwordHasher.verify(password, hash);
203
-
204
- // Then
205
- expect(result).toBe(true);
206
- });
207
-
208
- it('should return false for invalid password', async () => {
209
- // Given
210
- const password = 'wrongPassword123!';
211
- const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
212
- mockCryptoProvider.argon2Verify.mockResolvedValue(false);
213
-
214
- // When
215
- const result = await passwordHasher.verify(password, hash);
216
-
217
- // Then
218
- expect(result).toBe(false);
219
- });
220
-
221
- it('should return false for empty password', async () => {
222
- // Given
223
- const emptyPassword = '';
224
- const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
225
-
226
- // When
227
- const result = await passwordHasher.verify(emptyPassword, hash);
228
-
229
- // Then
230
- expect(result).toBe(false);
231
- expect(mockCryptoProvider.argon2Verify).not.toHaveBeenCalled();
232
- });
233
-
234
- it('should return false for empty hash', async () => {
235
- // Given
236
- const password = 'securePassword123!';
237
- const emptyHash = '';
238
-
239
- // When
240
- const result = await passwordHasher.verify(password, emptyHash);
241
-
242
- // Then
243
- expect(result).toBe(false);
244
- expect(mockCryptoProvider.argon2Verify).not.toHaveBeenCalled();
245
- });
246
- });
247
-
248
- describe('needsRehash', () => {
249
- it('should return false for hash with current parameters', () => {
250
- // Given
251
- const currentHash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
252
-
253
- // When
254
- const result = passwordHasher.needsRehash(currentHash);
255
-
256
- // Then
257
- expect(result).toBe(false);
258
- });
259
-
260
- it('should return true for hash with different memory cost', () => {
261
- // Given
262
- const oldHash = '$argon2id$v=19$m=32768,t=3,p=4$salt$hash';
263
-
264
- // When
265
- const result = passwordHasher.needsRehash(oldHash);
266
-
267
- // Then
268
- expect(result).toBe(true);
269
- });
270
-
271
- it('should return true for hash with different time cost', () => {
272
- // Given
273
- const oldHash = '$argon2id$v=19$m=65536,t=2,p=4$salt$hash';
274
-
275
- // When
276
- const result = passwordHasher.needsRehash(oldHash);
277
-
278
- // Then
279
- expect(result).toBe(true);
280
- });
281
-
282
- it('should return true for hash with different parallelism', () => {
283
- // Given
284
- const oldHash = '$argon2id$v=19$m=65536,t=3,p=2$salt$hash';
285
-
286
- // When
287
- const result = passwordHasher.needsRehash(oldHash);
288
-
289
- // Then
290
- expect(result).toBe(true);
291
- });
292
-
293
- it('should return true for bcrypt hash', () => {
294
- // Given
295
- const bcryptHash = '$2b$10$salt.hash';
296
-
297
- // When
298
- const result = passwordHasher.needsRehash(bcryptHash);
299
-
300
- // Then
301
- expect(result).toBe(true);
302
- });
303
- });
304
-
305
- describe('error handling', () => {
306
- it('should propagate crypto provider errors on hash', async () => {
307
- // Given
308
- const password = 'securePassword123!';
309
- const cryptoError = new Error('Crypto operation failed');
310
- mockCryptoProvider.argon2Hash.mockRejectedValue(cryptoError);
311
-
312
- // When/Then
313
- await expect(passwordHasher.hash(password)).rejects.toThrow('Crypto operation failed');
314
- });
315
-
316
- it('should propagate crypto provider errors on verify', async () => {
317
- // Given
318
- const password = 'securePassword123!';
319
- const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
320
- const cryptoError = new Error('Verification failed');
321
- mockCryptoProvider.argon2Verify.mockRejectedValue(cryptoError);
322
-
323
- // When/Then
324
- await expect(passwordHasher.verify(password, hash)).rejects.toThrow(
325
- 'Verification failed'
326
- );
327
- });
328
- });
329
-
330
- describe('interaction verification', () => {
331
- it('should not call argon2Hash if salt generation fails', async () => {
332
- // Given
333
- const password = 'securePassword123!';
334
- mockCryptoProvider.generateSalt.mockRejectedValue(new Error('Salt generation failed'));
335
-
336
- // When
337
- try {
338
- await passwordHasher.hash(password);
339
- } catch {
340
- // Expected to fail
341
- }
342
-
343
- // Then
344
- expect(mockCryptoProvider.argon2Hash).not.toHaveBeenCalled();
345
- });
346
-
347
- it('should only call crypto provider once per operation', async () => {
348
- // Given
349
- const password = 'securePassword123!';
350
-
351
- // When
352
- await passwordHasher.hash(password);
353
-
354
- // Then
355
- expect(mockCryptoProvider.generateSalt).toHaveBeenCalledTimes(1);
356
- expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledTimes(1);
357
- });
358
- });
359
- });
1
+ /**
2
+ * V3 Claude-Flow Password Hasher Unit Tests
3
+ *
4
+ * London School TDD - Behavior Verification
5
+ * Tests password hashing and verification behavior
6
+ */
7
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
8
+ import { createMock, createMockWithBehavior, type MockedInterface } from '../helpers/create-mock';
9
+ import { securityConfigs } from '../fixtures/configurations';
10
+
11
+ /**
12
+ * Password hasher interface (to be implemented)
13
+ */
14
+ interface IPasswordHasher {
15
+ hash(password: string): Promise<string>;
16
+ verify(password: string, hash: string): Promise<boolean>;
17
+ needsRehash(hash: string): boolean;
18
+ }
19
+
20
+ /**
21
+ * Crypto provider interface (collaborator)
22
+ */
23
+ interface ICryptoProvider {
24
+ argon2Hash(password: string, options: Argon2Options): Promise<string>;
25
+ argon2Verify(hash: string, password: string): Promise<boolean>;
26
+ generateSalt(length: number): Promise<string>;
27
+ }
28
+
29
+ interface Argon2Options {
30
+ memoryCost: number;
31
+ timeCost: number;
32
+ parallelism: number;
33
+ salt?: string;
34
+ }
35
+
36
+ /**
37
+ * Password hasher implementation for testing
38
+ */
39
+ class PasswordHasher implements IPasswordHasher {
40
+ constructor(
41
+ private readonly cryptoProvider: ICryptoProvider,
42
+ private readonly config: typeof securityConfigs.strict.hashing
43
+ ) {}
44
+
45
+ async hash(password: string): Promise<string> {
46
+ if (!password || password.length < 8) {
47
+ throw new Error('Password must be at least 8 characters');
48
+ }
49
+
50
+ const salt = await this.cryptoProvider.generateSalt(16);
51
+
52
+ return this.cryptoProvider.argon2Hash(password, {
53
+ memoryCost: this.config.memoryCost ?? 65536,
54
+ timeCost: this.config.timeCost ?? 3,
55
+ parallelism: this.config.parallelism ?? 4,
56
+ salt,
57
+ });
58
+ }
59
+
60
+ async verify(password: string, hash: string): Promise<boolean> {
61
+ if (!password || !hash) {
62
+ return false;
63
+ }
64
+
65
+ return this.cryptoProvider.argon2Verify(hash, password);
66
+ }
67
+
68
+ needsRehash(hash: string): boolean {
69
+ // Check if hash uses current algorithm and parameters
70
+ const currentVersion = `$argon2id$v=19$m=${this.config.memoryCost},t=${this.config.timeCost},p=${this.config.parallelism}`;
71
+ return !hash.startsWith(currentVersion);
72
+ }
73
+ }
74
+
75
+ describe('PasswordHasher', () => {
76
+ let mockCryptoProvider: MockedInterface<ICryptoProvider>;
77
+ let passwordHasher: PasswordHasher;
78
+ const hashingConfig = securityConfigs.strict.hashing;
79
+
80
+ beforeEach(() => {
81
+ mockCryptoProvider = createMock<ICryptoProvider>();
82
+
83
+ // Configure default mock behavior
84
+ mockCryptoProvider.generateSalt.mockResolvedValue('randomsalt16byte');
85
+ mockCryptoProvider.argon2Hash.mockResolvedValue(
86
+ '$argon2id$v=19$m=65536,t=3,p=4$cmFuZG9tc2FsdA$hashedpassword'
87
+ );
88
+ mockCryptoProvider.argon2Verify.mockResolvedValue(true);
89
+
90
+ passwordHasher = new PasswordHasher(mockCryptoProvider, hashingConfig);
91
+ });
92
+
93
+ describe('hash', () => {
94
+ it('should generate salt before hashing', async () => {
95
+ // Given
96
+ const password = 'securePassword123!';
97
+
98
+ // When
99
+ await passwordHasher.hash(password);
100
+
101
+ // Then - verify interaction
102
+ expect(mockCryptoProvider.generateSalt).toHaveBeenCalledWith(16);
103
+ expect(mockCryptoProvider.generateSalt).toHaveBeenCalledBefore(
104
+ mockCryptoProvider.argon2Hash
105
+ );
106
+ });
107
+
108
+ it('should hash password with configured parameters', async () => {
109
+ // Given
110
+ const password = 'securePassword123!';
111
+
112
+ // When
113
+ await passwordHasher.hash(password);
114
+
115
+ // Then - verify interaction with correct parameters
116
+ expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledWith(
117
+ password,
118
+ expect.objectContaining({
119
+ memoryCost: 65536,
120
+ timeCost: 3,
121
+ parallelism: 4,
122
+ })
123
+ );
124
+ });
125
+
126
+ it('should return hash from crypto provider', async () => {
127
+ // Given
128
+ const password = 'securePassword123!';
129
+ const expectedHash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
130
+ mockCryptoProvider.argon2Hash.mockResolvedValue(expectedHash);
131
+
132
+ // When
133
+ const result = await passwordHasher.hash(password);
134
+
135
+ // Then
136
+ expect(result).toBe(expectedHash);
137
+ });
138
+
139
+ it('should reject passwords shorter than 8 characters', async () => {
140
+ // Given
141
+ const shortPassword = 'short';
142
+
143
+ // When/Then
144
+ await expect(passwordHasher.hash(shortPassword)).rejects.toThrow(
145
+ 'Password must be at least 8 characters'
146
+ );
147
+
148
+ // Verify no interaction with crypto provider
149
+ expect(mockCryptoProvider.generateSalt).not.toHaveBeenCalled();
150
+ expect(mockCryptoProvider.argon2Hash).not.toHaveBeenCalled();
151
+ });
152
+
153
+ it('should reject empty passwords', async () => {
154
+ // Given
155
+ const emptyPassword = '';
156
+
157
+ // When/Then
158
+ await expect(passwordHasher.hash(emptyPassword)).rejects.toThrow(
159
+ 'Password must be at least 8 characters'
160
+ );
161
+ });
162
+
163
+ it('should use generated salt in hash options', async () => {
164
+ // Given
165
+ const password = 'securePassword123!';
166
+ const generatedSalt = 'unique-salt-value';
167
+ mockCryptoProvider.generateSalt.mockResolvedValue(generatedSalt);
168
+
169
+ // When
170
+ await passwordHasher.hash(password);
171
+
172
+ // Then
173
+ expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledWith(
174
+ password,
175
+ expect.objectContaining({
176
+ salt: generatedSalt,
177
+ })
178
+ );
179
+ });
180
+ });
181
+
182
+ describe('verify', () => {
183
+ it('should delegate verification to crypto provider', async () => {
184
+ // Given
185
+ const password = 'securePassword123!';
186
+ const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
187
+
188
+ // When
189
+ await passwordHasher.verify(password, hash);
190
+
191
+ // Then - verify interaction
192
+ expect(mockCryptoProvider.argon2Verify).toHaveBeenCalledWith(hash, password);
193
+ });
194
+
195
+ it('should return true for valid password', async () => {
196
+ // Given
197
+ const password = 'securePassword123!';
198
+ const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
199
+ mockCryptoProvider.argon2Verify.mockResolvedValue(true);
200
+
201
+ // When
202
+ const result = await passwordHasher.verify(password, hash);
203
+
204
+ // Then
205
+ expect(result).toBe(true);
206
+ });
207
+
208
+ it('should return false for invalid password', async () => {
209
+ // Given
210
+ const password = 'wrongPassword123!';
211
+ const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
212
+ mockCryptoProvider.argon2Verify.mockResolvedValue(false);
213
+
214
+ // When
215
+ const result = await passwordHasher.verify(password, hash);
216
+
217
+ // Then
218
+ expect(result).toBe(false);
219
+ });
220
+
221
+ it('should return false for empty password', async () => {
222
+ // Given
223
+ const emptyPassword = '';
224
+ const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
225
+
226
+ // When
227
+ const result = await passwordHasher.verify(emptyPassword, hash);
228
+
229
+ // Then
230
+ expect(result).toBe(false);
231
+ expect(mockCryptoProvider.argon2Verify).not.toHaveBeenCalled();
232
+ });
233
+
234
+ it('should return false for empty hash', async () => {
235
+ // Given
236
+ const password = 'securePassword123!';
237
+ const emptyHash = '';
238
+
239
+ // When
240
+ const result = await passwordHasher.verify(password, emptyHash);
241
+
242
+ // Then
243
+ expect(result).toBe(false);
244
+ expect(mockCryptoProvider.argon2Verify).not.toHaveBeenCalled();
245
+ });
246
+ });
247
+
248
+ describe('needsRehash', () => {
249
+ it('should return false for hash with current parameters', () => {
250
+ // Given
251
+ const currentHash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
252
+
253
+ // When
254
+ const result = passwordHasher.needsRehash(currentHash);
255
+
256
+ // Then
257
+ expect(result).toBe(false);
258
+ });
259
+
260
+ it('should return true for hash with different memory cost', () => {
261
+ // Given
262
+ const oldHash = '$argon2id$v=19$m=32768,t=3,p=4$salt$hash';
263
+
264
+ // When
265
+ const result = passwordHasher.needsRehash(oldHash);
266
+
267
+ // Then
268
+ expect(result).toBe(true);
269
+ });
270
+
271
+ it('should return true for hash with different time cost', () => {
272
+ // Given
273
+ const oldHash = '$argon2id$v=19$m=65536,t=2,p=4$salt$hash';
274
+
275
+ // When
276
+ const result = passwordHasher.needsRehash(oldHash);
277
+
278
+ // Then
279
+ expect(result).toBe(true);
280
+ });
281
+
282
+ it('should return true for hash with different parallelism', () => {
283
+ // Given
284
+ const oldHash = '$argon2id$v=19$m=65536,t=3,p=2$salt$hash';
285
+
286
+ // When
287
+ const result = passwordHasher.needsRehash(oldHash);
288
+
289
+ // Then
290
+ expect(result).toBe(true);
291
+ });
292
+
293
+ it('should return true for bcrypt hash', () => {
294
+ // Given
295
+ const bcryptHash = '$2b$10$salt.hash';
296
+
297
+ // When
298
+ const result = passwordHasher.needsRehash(bcryptHash);
299
+
300
+ // Then
301
+ expect(result).toBe(true);
302
+ });
303
+ });
304
+
305
+ describe('error handling', () => {
306
+ it('should propagate crypto provider errors on hash', async () => {
307
+ // Given
308
+ const password = 'securePassword123!';
309
+ const cryptoError = new Error('Crypto operation failed');
310
+ mockCryptoProvider.argon2Hash.mockRejectedValue(cryptoError);
311
+
312
+ // When/Then
313
+ await expect(passwordHasher.hash(password)).rejects.toThrow('Crypto operation failed');
314
+ });
315
+
316
+ it('should propagate crypto provider errors on verify', async () => {
317
+ // Given
318
+ const password = 'securePassword123!';
319
+ const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
320
+ const cryptoError = new Error('Verification failed');
321
+ mockCryptoProvider.argon2Verify.mockRejectedValue(cryptoError);
322
+
323
+ // When/Then
324
+ await expect(passwordHasher.verify(password, hash)).rejects.toThrow(
325
+ 'Verification failed'
326
+ );
327
+ });
328
+ });
329
+
330
+ describe('interaction verification', () => {
331
+ it('should not call argon2Hash if salt generation fails', async () => {
332
+ // Given
333
+ const password = 'securePassword123!';
334
+ mockCryptoProvider.generateSalt.mockRejectedValue(new Error('Salt generation failed'));
335
+
336
+ // When
337
+ try {
338
+ await passwordHasher.hash(password);
339
+ } catch {
340
+ // Expected to fail
341
+ }
342
+
343
+ // Then
344
+ expect(mockCryptoProvider.argon2Hash).not.toHaveBeenCalled();
345
+ });
346
+
347
+ it('should only call crypto provider once per operation', async () => {
348
+ // Given
349
+ const password = 'securePassword123!';
350
+
351
+ // When
352
+ await passwordHasher.hash(password);
353
+
354
+ // Then
355
+ expect(mockCryptoProvider.generateSalt).toHaveBeenCalledTimes(1);
356
+ expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledTimes(1);
357
+ });
358
+ });
359
+ });