Skip to content

Commit fd91d7b

Browse files
committed
handle lonely ifs
1 parent 2e6ddf3 commit fd91d7b

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

ep_hash_auth.js

+27-28
Original file line numberDiff line numberDiff line change
@@ -57,42 +57,41 @@ if (settings.ep_hash_auth) {
5757
// This function calls callback(hashType) if authenticated, or callback(null) if not.
5858
const compareHashes = async (password, hash, callback) => {
5959
const cryptoHash = crypto.createHash(hash_typ).update(password).digest(hash_dig);
60+
6061
if (hash === cryptoHash) { // Check whether this is a crypto hash first
6162
return callback('crypto');
62-
} else { // If not, check other hash types
63-
if (hash[0] === '$') { // This is an argon2 or bcrypt hash
64-
if (hash.slice(0, 7) === '$argon2') { // This is argon2
65-
if (argon2) {
66-
if (await argon2.verify(hash, password)) {
67-
return callback('argon2');
68-
} else {
69-
return callback(null);
70-
}
71-
} else {
72-
console.log('Warning: Could not verify argon2 hash due to missing dependency');
73-
}
74-
} else { // This is bcrypt
75-
if (bcrypt) {
76-
if (await bcrypt.compare(password, hash)) {
77-
return callback('bcrypt');
78-
} else {
79-
return callback(null);
80-
}
81-
} else {
82-
console.log('Warning: Could not verify bcrypt hash due to missing dependency');
83-
}
84-
}
85-
} else { // This is a scrypt hash or a failed crypto hash
86-
if (scrypt) {
87-
if (scrypt.verifyKdfSync(Buffer.from(hash, 'hex'), Buffer.from(password))) {
88-
return callback('scrypt');
63+
// If not, check other hash types
64+
} else if (hash[0] === '$') {
65+
// This is an argon2 or bcrypt hash
66+
if (hash.slice(0, 7) === '$argon2') {
67+
// This is argon2
68+
if (argon2) {
69+
if (await argon2.verify(hash, password)) {
70+
return callback('argon2');
8971
} else {
9072
return callback(null);
9173
}
9274
} else {
93-
console.log('Warning: Could not verify scrypt hash due to missing dependency');
75+
console.log('Warning: Could not verify argon2 hash due to missing dependency');
76+
}
77+
} else if (bcrypt) {
78+
if (await bcrypt.compare(password, hash)) {
79+
return callback('bcrypt');
80+
} else {
81+
return callback(null);
9482
}
83+
} else {
84+
console.log('Warning: Could not verify bcrypt hash due to missing dependency');
85+
}
86+
} else if (scrypt) {
87+
// This is a scrypt hash or a failed crypto hash
88+
if (scrypt.verifyKdfSync(Buffer.from(hash, 'hex'), Buffer.from(password))) {
89+
return callback('scrypt');
90+
} else {
91+
return callback(null);
9592
}
93+
} else {
94+
console.log('Warning: Could not verify scrypt hash due to missing dependency');
9695
}
9796
return callback(null);
9897
};

0 commit comments

Comments
 (0)