@@ -5,7 +5,15 @@ import * as path from "path"
5
5
import safeCompare from "safe-compare"
6
6
import { rootPath } from "../constants"
7
7
import { authenticated , getCookieDomain , redirect , replaceTemplates } from "../http"
8
- import { hash , hashLegacy , humanPath , isHashLegacyMatch , isHashMatch } from "../util"
8
+ import {
9
+ getPasswordMethod ,
10
+ handlePasswordValidation ,
11
+ hash ,
12
+ hashLegacy ,
13
+ humanPath ,
14
+ isHashLegacyMatch ,
15
+ isHashMatch ,
16
+ } from "../util"
9
17
10
18
export enum Cookie {
11
19
Key = "key" ,
@@ -62,36 +70,28 @@ router.get("/", async (req, res) => {
62
70
} )
63
71
64
72
router . post ( "/" , async ( req , res ) => {
73
+ const password = req . body . password
74
+ const hashedPasswordFromArgs = req . args [ "hashed-password" ]
75
+
65
76
try {
66
77
// Check to see if they exceeded their login attempts
67
78
if ( ! limiter . canTry ( ) ) {
68
79
throw new Error ( "Login rate limited!" )
69
80
}
70
81
71
- if ( ! req . body . password ) {
82
+ if ( ! password ) {
72
83
throw new Error ( "Missing password" )
73
84
}
74
85
75
- // this logic below is flawed
76
- const theHash = await hash ( req . body . password )
77
- const hashedPassword = req . args [ "hashed-password" ] || ""
78
- const match = await isHashMatch ( req . body . password , hashedPassword )
79
- // console.log(`The actual hash: ${theHash}`)
80
- // console.log(`hashed-password from config: ${hashedPassword}`)
81
- // console.log(theHash, hashedPassword)
82
- console . log ( `is it a match??? ${ match } ` )
83
- if (
84
- req . args [ "hashed-password" ]
85
- ? isHashLegacyMatch ( req . body . password , req . args [ "hashed-password" ] )
86
- : req . args . password && safeCompare ( req . body . password , req . args . password )
87
- ) {
88
- // NOTE@jsjoeio :
89
- // We store the hashed password as a cookie. In order to be backwards-comptabile for the folks
90
- // using sha256 (the original hashing algorithm), we need to check the hashed-password in the req.args
91
- // TODO all of this logic should be cleaned up honestly. The current implementation only checks for a hashed-password
92
- // but doesn't check which algorithm they are using.
93
- console . log ( `What is this? ${ req . args [ "hashed-password" ] } ` , Boolean ( req . args [ "hashed-password" ] ) )
94
- const hashedPassword = req . args [ "hashed-password" ] ? hashLegacy ( req . body . password ) : await hash ( req . body . password )
86
+ const passwordMethod = getPasswordMethod ( hashedPasswordFromArgs )
87
+ const { isPasswordValid, hashedPassword } = await handlePasswordValidation ( {
88
+ passwordMethod,
89
+ hashedPasswordFromArgs,
90
+ passwordFromRequestBody : password ,
91
+ passwordFromArgs : req . args . password ,
92
+ } )
93
+
94
+ if ( isPasswordValid ) {
95
95
// The hash does not add any actual security but we do it for
96
96
// obfuscation purposes (and as a side effect it handles escaping).
97
97
res . cookie ( Cookie . Key , hashedPassword , {
0 commit comments