@@ -8,7 +8,7 @@ import { normalize, Options } from "../common/util"
88import { AuthType , DefaultedArgs } from "./cli"
99import { commit , rootPath } from "./constants"
1010import { Heart } from "./heart"
11- import { isHashMatch } from "./util"
11+ import { getPasswordMethod , handlePasswordValidation , IsCookieValidArgs , isCookieValid , isHashMatch } from "./util"
1212
1313declare 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