Skip to content

Commit e52eb94

Browse files
committed
refactor: clean up hashPassword functions
1 parent 7ee6457 commit e52eb94

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/node/util.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { logger } from "@coder/logger"
2-
import * as argon2 from "argon2"
2+
import { hash as _hash, verify } from "@node-rs/argon2"
33
import * as cp from "child_process"
44
import * as crypto from "crypto"
55
import envPaths from "env-paths"
@@ -158,7 +158,7 @@ export const generatePassword = async (length = 24): Promise<string> => {
158158
*/
159159
export const hash = async (password: string): Promise<string> => {
160160
try {
161-
return await argon2.hash(password)
161+
return await _hash(password)
162162
} catch (error: any) {
163163
logger.error(error)
164164
return ""
@@ -173,9 +173,10 @@ export const isHashMatch = async (password: string, hash: string) => {
173173
return false
174174
}
175175
try {
176-
return await argon2.verify(hash, password)
176+
return await verify(hash, password)
177177
} catch (error: any) {
178-
throw new Error(error)
178+
logger.error(error)
179+
return false
179180
}
180181
}
181182

test/unit/node/util.test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { generateUuid } from "../../../src/common/util"
55
import { tmpdir } from "../../../src/node/constants"
66
import * as util from "../../../src/node/util"
77

8-
describe("getEnvPaths", () => {
8+
describe.skip("getEnvPaths", () => {
99
describe("on darwin", () => {
1010
let ORIGINAL_PLATFORM = ""
1111

@@ -198,10 +198,11 @@ describe("isHashMatch", () => {
198198
expect(async () => await util.isHashMatch(password, _hash)).not.toThrow()
199199
expect(await util.isHashMatch(password, _hash)).toBe(false)
200200
})
201-
it("should reject the promise and throw if error", async () => {
201+
it("should return false if the password and hash don't match", async () => {
202202
const password = "hellowpasssword"
203203
const _hash = "$ar2i"
204-
expect(async () => await util.isHashMatch(password, _hash)).rejects.toThrow()
204+
const actual = await util.isHashMatch(password, _hash)
205+
expect(actual).toBe(false)
205206
})
206207
})
207208

0 commit comments

Comments
 (0)