1
- import {
2
- hash ,
3
- isHashMatch ,
4
- handlePasswordValidation ,
5
- PasswordMethod ,
6
- getPasswordMethod ,
7
- hashLegacy ,
8
- isHashLegacyMatch ,
9
- isCookieValid ,
10
- sanitizeString ,
11
- } from "../../../src/node/util"
1
+ import * as util from "../../../src/node/util"
12
2
13
3
describe ( "getEnvPaths" , ( ) => {
14
4
describe ( "on darwin" , ( ) => {
@@ -161,176 +151,176 @@ describe("getEnvPaths", () => {
161
151
describe ( "hash" , ( ) => {
162
152
it ( "should return a hash of the string passed in" , async ( ) => {
163
153
const plainTextPassword = "mySecretPassword123"
164
- const hashed = await hash ( plainTextPassword )
154
+ const hashed = await util . hash ( plainTextPassword )
165
155
expect ( hashed ) . not . toBe ( plainTextPassword )
166
156
} )
167
157
} )
168
158
169
159
describe ( "isHashMatch" , ( ) => {
170
160
it ( "should return true if the password matches the hash" , async ( ) => {
171
161
const password = "codeserver1234"
172
- const _hash = await hash ( password )
173
- const actual = await isHashMatch ( password , _hash )
162
+ const _hash = await util . hash ( password )
163
+ const actual = await util . isHashMatch ( password , _hash )
174
164
expect ( actual ) . toBe ( true )
175
165
} )
176
166
it ( "should return false if the password does not match the hash" , async ( ) => {
177
167
const password = "password123"
178
- const _hash = await hash ( password )
179
- const actual = await isHashMatch ( "otherPassword123" , _hash )
168
+ const _hash = await util . hash ( password )
169
+ const actual = await util . isHashMatch ( "otherPassword123" , _hash )
180
170
expect ( actual ) . toBe ( false )
181
171
} )
182
172
it ( "should return true with actual hash" , async ( ) => {
183
173
const password = "password123"
184
174
const _hash = "$argon2i$v=19$m=4096,t=3,p=1$EAoczTxVki21JDfIZpTUxg$rkXgyrW4RDGoDYrxBFD4H2DlSMEhP4h+Api1hXnGnFY"
185
- const actual = await isHashMatch ( password , _hash )
175
+ const actual = await util . isHashMatch ( password , _hash )
186
176
expect ( actual ) . toBe ( true )
187
177
} )
188
178
it ( "should return false if the password is empty" , async ( ) => {
189
179
const password = ""
190
180
const _hash = "$argon2i$v=19$m=4096,t=3,p=1$EAoczTxVki21JDfIZpTUxg$rkXgyrW4RDGoDYrxBFD4H2DlSMEhP4h+Api1hXnGnFY"
191
- const actual = await isHashMatch ( password , _hash )
181
+ const actual = await util . isHashMatch ( password , _hash )
192
182
expect ( actual ) . toBe ( false )
193
183
} )
194
184
it ( "should return false if the hash is empty" , async ( ) => {
195
185
const password = "hellowpasssword"
196
186
const _hash = ""
197
- const actual = await isHashMatch ( password , _hash )
187
+ const actual = await util . isHashMatch ( password , _hash )
198
188
expect ( actual ) . toBe ( false )
199
189
} )
200
190
} )
201
191
202
192
describe ( "hashLegacy" , ( ) => {
203
193
it ( "should return a hash of the string passed in" , ( ) => {
204
194
const plainTextPassword = "mySecretPassword123"
205
- const hashed = hashLegacy ( plainTextPassword )
195
+ const hashed = util . hashLegacy ( plainTextPassword )
206
196
expect ( hashed ) . not . toBe ( plainTextPassword )
207
197
} )
208
198
} )
209
199
210
200
describe ( "isHashLegacyMatch" , ( ) => {
211
201
it ( "should return true if is match" , ( ) => {
212
202
const password = "password123"
213
- const _hash = hashLegacy ( password )
214
- expect ( isHashLegacyMatch ( password , _hash ) ) . toBe ( true )
203
+ const _hash = util . hashLegacy ( password )
204
+ expect ( util . isHashLegacyMatch ( password , _hash ) ) . toBe ( true )
215
205
} )
216
206
it ( "should return false if is match" , ( ) => {
217
207
const password = "password123"
218
- const _hash = hashLegacy ( password )
219
- expect ( isHashLegacyMatch ( "otherPassword123" , _hash ) ) . toBe ( false )
208
+ const _hash = util . hashLegacy ( password )
209
+ expect ( util . isHashLegacyMatch ( "otherPassword123" , _hash ) ) . toBe ( false )
220
210
} )
221
211
it ( "should return true if hashed from command line" , ( ) => {
222
212
const password = "password123"
223
213
// Hashed using printf "password123" | sha256sum | cut -d' ' -f1
224
214
const _hash = "ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f"
225
- expect ( isHashLegacyMatch ( password , _hash ) ) . toBe ( true )
215
+ expect ( util . isHashLegacyMatch ( password , _hash ) ) . toBe ( true )
226
216
} )
227
217
} )
228
218
229
219
describe ( "getPasswordMethod" , ( ) => {
230
220
it ( "should return PLAIN_TEXT for no hashed password" , ( ) => {
231
221
const hashedPassword = undefined
232
- const passwordMethod = getPasswordMethod ( hashedPassword )
233
- const expected : PasswordMethod = "PLAIN_TEXT"
222
+ const passwordMethod = util . getPasswordMethod ( hashedPassword )
223
+ const expected : util . PasswordMethod = "PLAIN_TEXT"
234
224
expect ( passwordMethod ) . toEqual ( expected )
235
225
} )
236
226
it ( "should return ARGON2 for password with 'argon2'" , ( ) => {
237
227
const hashedPassword =
238
228
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY"
239
- const passwordMethod = getPasswordMethod ( hashedPassword )
240
- const expected : PasswordMethod = "ARGON2"
229
+ const passwordMethod = util . getPasswordMethod ( hashedPassword )
230
+ const expected : util . PasswordMethod = "ARGON2"
241
231
expect ( passwordMethod ) . toEqual ( expected )
242
232
} )
243
233
it ( "should return SHA256 for password with legacy hash" , ( ) => {
244
234
const hashedPassword = "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af"
245
- const passwordMethod = getPasswordMethod ( hashedPassword )
246
- const expected : PasswordMethod = "SHA256"
235
+ const passwordMethod = util . getPasswordMethod ( hashedPassword )
236
+ const expected : util . PasswordMethod = "SHA256"
247
237
expect ( passwordMethod ) . toEqual ( expected )
248
238
} )
249
239
} )
250
240
251
241
describe ( "handlePasswordValidation" , ( ) => {
252
242
it ( "should return true with a hashedPassword for a PLAIN_TEXT password" , async ( ) => {
253
243
const p = "password"
254
- const passwordValidation = await handlePasswordValidation ( {
244
+ const passwordValidation = await util . handlePasswordValidation ( {
255
245
passwordMethod : "PLAIN_TEXT" ,
256
246
passwordFromRequestBody : p ,
257
247
passwordFromArgs : p ,
258
248
hashedPasswordFromArgs : undefined ,
259
249
} )
260
250
261
- const matchesHash = await isHashMatch ( p , passwordValidation . hashedPassword )
251
+ const matchesHash = await util . isHashMatch ( p , passwordValidation . hashedPassword )
262
252
263
253
expect ( passwordValidation . isPasswordValid ) . toBe ( true )
264
254
expect ( matchesHash ) . toBe ( true )
265
255
} )
266
256
it ( "should return false when PLAIN_TEXT password doesn't match args" , async ( ) => {
267
257
const p = "password"
268
- const passwordValidation = await handlePasswordValidation ( {
258
+ const passwordValidation = await util . handlePasswordValidation ( {
269
259
passwordMethod : "PLAIN_TEXT" ,
270
260
passwordFromRequestBody : "password1" ,
271
261
passwordFromArgs : p ,
272
262
hashedPasswordFromArgs : undefined ,
273
263
} )
274
264
275
- const matchesHash = await isHashMatch ( p , passwordValidation . hashedPassword )
265
+ const matchesHash = await util . isHashMatch ( p , passwordValidation . hashedPassword )
276
266
277
267
expect ( passwordValidation . isPasswordValid ) . toBe ( false )
278
268
expect ( matchesHash ) . toBe ( false )
279
269
} )
280
270
it ( "should return true with a hashedPassword for a SHA256 password" , async ( ) => {
281
271
const p = "helloworld"
282
- const passwordValidation = await handlePasswordValidation ( {
272
+ const passwordValidation = await util . handlePasswordValidation ( {
283
273
passwordMethod : "SHA256" ,
284
274
passwordFromRequestBody : p ,
285
275
passwordFromArgs : undefined ,
286
276
hashedPasswordFromArgs : "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af" ,
287
277
} )
288
278
289
- const matchesHash = isHashLegacyMatch ( p , passwordValidation . hashedPassword )
279
+ const matchesHash = util . isHashLegacyMatch ( p , passwordValidation . hashedPassword )
290
280
291
281
expect ( passwordValidation . isPasswordValid ) . toBe ( true )
292
282
expect ( matchesHash ) . toBe ( true )
293
283
} )
294
284
it ( "should return false when SHA256 password doesn't match hash" , async ( ) => {
295
285
const p = "helloworld1"
296
- const passwordValidation = await handlePasswordValidation ( {
286
+ const passwordValidation = await util . handlePasswordValidation ( {
297
287
passwordMethod : "SHA256" ,
298
288
passwordFromRequestBody : p ,
299
289
passwordFromArgs : undefined ,
300
290
hashedPasswordFromArgs : "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af" ,
301
291
} )
302
292
303
- const matchesHash = isHashLegacyMatch ( p , passwordValidation . hashedPassword )
293
+ const matchesHash = util . isHashLegacyMatch ( p , passwordValidation . hashedPassword )
304
294
305
295
expect ( passwordValidation . isPasswordValid ) . toBe ( false )
306
296
expect ( matchesHash ) . toBe ( false )
307
297
} )
308
298
it ( "should return true with a hashedPassword for a ARGON2 password" , async ( ) => {
309
299
const p = "password"
310
- const passwordValidation = await handlePasswordValidation ( {
300
+ const passwordValidation = await util . handlePasswordValidation ( {
311
301
passwordMethod : "ARGON2" ,
312
302
passwordFromRequestBody : p ,
313
303
passwordFromArgs : undefined ,
314
304
hashedPasswordFromArgs :
315
305
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY" ,
316
306
} )
317
307
318
- const matchesHash = await isHashMatch ( p , passwordValidation . hashedPassword )
308
+ const matchesHash = await util . isHashMatch ( p , passwordValidation . hashedPassword )
319
309
320
310
expect ( passwordValidation . isPasswordValid ) . toBe ( true )
321
311
expect ( matchesHash ) . toBe ( true )
322
312
} )
323
313
it ( "should return false when ARGON2 password doesn't match hash" , async ( ) => {
324
314
const p = "password1"
325
- const passwordValidation = await handlePasswordValidation ( {
315
+ const passwordValidation = await util . handlePasswordValidation ( {
326
316
passwordMethod : "ARGON2" ,
327
317
passwordFromRequestBody : p ,
328
318
passwordFromArgs : undefined ,
329
319
hashedPasswordFromArgs :
330
320
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY" ,
331
321
} )
332
322
333
- const matchesHash = await isHashMatch ( p , passwordValidation . hashedPassword )
323
+ const matchesHash = await util . isHashMatch ( p , passwordValidation . hashedPassword )
334
324
335
325
expect ( passwordValidation . isPasswordValid ) . toBe ( false )
336
326
expect ( matchesHash ) . toBe ( false )
@@ -339,7 +329,7 @@ describe("handlePasswordValidation", () => {
339
329
340
330
describe ( "isCookieValid" , ( ) => {
341
331
it ( "should be valid if hashed-password for SHA256 matches cookie.key" , async ( ) => {
342
- const isValid = await isCookieValid ( {
332
+ const isValid = await util . isCookieValid ( {
343
333
passwordMethod : "SHA256" ,
344
334
cookieKey : "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af" ,
345
335
hashedPasswordFromArgs : "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af" ,
@@ -348,7 +338,7 @@ describe("isCookieValid", () => {
348
338
expect ( isValid ) . toBe ( true )
349
339
} )
350
340
it ( "should be invalid if hashed-password for SHA256 does not match cookie.key" , async ( ) => {
351
- const isValid = await isCookieValid ( {
341
+ const isValid = await util . isCookieValid ( {
352
342
passwordMethod : "SHA256" ,
353
343
cookieKey : "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb9442bb6f8f8f07af" ,
354
344
hashedPasswordFromArgs : "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af" ,
@@ -357,7 +347,7 @@ describe("isCookieValid", () => {
357
347
expect ( isValid ) . toBe ( false )
358
348
} )
359
349
it ( "should be valid if hashed-password for ARGON2 matches cookie.key" , async ( ) => {
360
- const isValid = await isCookieValid ( {
350
+ const isValid = await util . isCookieValid ( {
361
351
passwordMethod : "ARGON2" ,
362
352
cookieKey : "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY" ,
363
353
hashedPasswordFromArgs :
@@ -367,7 +357,7 @@ describe("isCookieValid", () => {
367
357
expect ( isValid ) . toBe ( true )
368
358
} )
369
359
it ( "should be invalid if hashed-password for ARGON2 does not match cookie.key" , async ( ) => {
370
- const isValid = await isCookieValid ( {
360
+ const isValid = await util . isCookieValid ( {
371
361
passwordMethod : "ARGON2" ,
372
362
cookieKey : "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9H" ,
373
363
hashedPasswordFromArgs :
@@ -377,7 +367,7 @@ describe("isCookieValid", () => {
377
367
expect ( isValid ) . toBe ( false )
378
368
} )
379
369
it ( "should be valid if password for PLAIN_TEXT matches cookie.key" , async ( ) => {
380
- const isValid = await isCookieValid ( {
370
+ const isValid = await util . isCookieValid ( {
381
371
passwordMethod : "PLAIN_TEXT" ,
382
372
cookieKey : "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY" ,
383
373
passwordFromArgs : "password" ,
@@ -386,7 +376,7 @@ describe("isCookieValid", () => {
386
376
expect ( isValid ) . toBe ( true )
387
377
} )
388
378
it ( "should be invalid if hashed-password for PLAIN_TEXT does not match cookie.key" , async ( ) => {
389
- const isValid = await isCookieValid ( {
379
+ const isValid = await util . isCookieValid ( {
390
380
passwordMethod : "PLAIN_TEXT" ,
391
381
cookieKey : "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9H" ,
392
382
passwordFromArgs : "password1234" ,
@@ -398,12 +388,12 @@ describe("isCookieValid", () => {
398
388
399
389
describe ( "sanitizeString" , ( ) => {
400
390
it ( "should return an empty string if passed a type other than a string" , ( ) => {
401
- expect ( sanitizeString ( { } as string ) ) . toBe ( "" )
391
+ expect ( util . sanitizeString ( { } as string ) ) . toBe ( "" )
402
392
} )
403
393
it ( "should trim whitespace" , ( ) => {
404
- expect ( sanitizeString ( " hello " ) ) . toBe ( "hello" )
394
+ expect ( util . sanitizeString ( " hello " ) ) . toBe ( "hello" )
405
395
} )
406
396
it ( "should always return an empty string" , ( ) => {
407
- expect ( sanitizeString ( " " ) ) . toBe ( "" )
397
+ expect ( util . sanitizeString ( " " ) ) . toBe ( "" )
408
398
} )
409
399
} )
0 commit comments