Skip to content

Commit d1d2530

Browse files
committed
chore: update http and login
1 parent 2f726e7 commit d1d2530

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/node/http.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const ensureAuthenticated = (req: express.Request, _?: express.Response,
5757
/**
5858
* Return true if authenticated via cookies.
5959
*/
60-
export const authenticated = (req: express.Request): boolean => {
60+
export const authenticated = async (req: express.Request): Promise<boolean> => {
6161
switch (req.args.auth) {
6262
case AuthType.None:
6363
return true
@@ -67,7 +67,7 @@ export const authenticated = (req: express.Request): boolean => {
6767
req.cookies.key &&
6868
(req.args["hashed-password"]
6969
? safeCompare(req.cookies.key, req.args["hashed-password"])
70-
: req.args.password && isHashMatch(req.args.password, req.cookies.key))
70+
: req.args.password && (await isHashMatch(req.args.password, req.cookies.key)))
7171
)
7272
default:
7373
throw new Error(`Unsupported auth type ${req.args.auth}`)

src/node/routes/login.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ router.post("/", async (req, res) => {
7777
? isHashLegacyMatch(req.body.password, req.args["hashed-password"])
7878
: req.args.password && safeCompare(req.body.password, req.args.password)
7979
) {
80-
const hashedPassword = req.args["hashed-password"] ? hashLegacy(req.body.password) : hash(req.body.password)
80+
const hashedPassword = req.args["hashed-password"] ? hashLegacy(req.body.password) : await hash(req.body.password)
8181
// The hash does not add any actual security but we do it for
8282
// obfuscation purposes (and as a side effect it handles escaping).
8383
res.cookie(Cookie.Key, hashedPassword, {

src/node/util.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as cp from "child_process"
22
import * as crypto from "crypto"
3-
import * as bcrypt from "bcrypt"
3+
import * as argon2 from "argon2"
44
import envPaths from "env-paths"
55
import { promises as fs } from "fs"
66
import * as net from "net"
@@ -9,6 +9,7 @@ import * as path from "path"
99
import * as util from "util"
1010
import xdgBasedir from "xdg-basedir"
1111
import safeCompare from "safe-compare"
12+
import { logger } from "@coder/logger"
1213

1314
export interface Paths {
1415
data: string
@@ -120,15 +121,25 @@ export const generatePassword = async (length = 24): Promise<string> => {
120121
/**
121122
* Used to hash the password.
122123
*/
123-
export const hash = (password: string): string => {
124-
return bcrypt.hashSync(password, 10)
124+
export const hash = async (password: string) => {
125+
try {
126+
return await argon2.hash(password)
127+
} catch (error) {
128+
logger.error(error)
129+
return ""
130+
}
125131
}
126132

127133
/**
128134
* Used to verify if the password matches the hash
129135
*/
130-
export const isHashMatch = (password: string, hash: string) => {
131-
return bcrypt.compareSync(password, hash)
136+
export const isHashMatch = async (password: string, hash: string) => {
137+
try {
138+
return await argon2.verify(hash, password)
139+
} catch (error) {
140+
logger.error(error)
141+
return false
142+
}
132143
}
133144

134145
/**

0 commit comments

Comments
 (0)