Skip to content

Commit 092c03e

Browse files
committed
refactor: password logic in http w/ isCookieValid
1 parent 0974e06 commit 092c03e

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/node/http.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { normalize, Options } from "../common/util"
88
import { AuthType, DefaultedArgs } from "./cli"
99
import { commit, rootPath } from "./constants"
1010
import { Heart } from "./heart"
11-
import { isHashMatch } from "./util"
11+
import { getPasswordMethod, handlePasswordValidation, IsCookieValidArgs, isCookieValid, isHashMatch } from "./util"
1212

1313
declare global {
1414
// eslint-disable-next-line @typescript-eslint/no-namespace
@@ -68,14 +68,16 @@ export const authenticated = async (req: express.Request): Promise<boolean> => {
6868
return true
6969
case AuthType.Password:
7070
// The password is stored in the cookie after being hashed.
71-
// TODO@jsjoeio this also needs to be refactored to check if they're using the legacy password
72-
// or the new one. we can't assume hashed-password means legacy
73-
return !!(
74-
req.cookies.key &&
75-
(req.args["hashed-password"]
76-
? safeCompare(req.cookies.key, req.args["hashed-password"])
77-
: req.args.password && (await isHashMatch(req.args.password, req.cookies.key)))
78-
)
71+
const hashedPasswordFromArgs = req.args["hashed-password"]
72+
const passwordMethod = getPasswordMethod(hashedPasswordFromArgs)
73+
const isCookieValidArgs: IsCookieValidArgs = {
74+
passwordMethod,
75+
cookieKey: req.cookies.key as string,
76+
passwordFromArgs: req.args.password || "",
77+
hashedPasswordFromArgs: req.args["hashed-password"],
78+
}
79+
80+
return await isCookieValid(isCookieValidArgs)
7981
default:
8082
throw new Error(`Unsupported auth type ${req.args.auth}`)
8183
}

0 commit comments

Comments
 (0)