我使用此代码登录用户。密码是用加密的bcrypt
,SALT_ROUNDS
每个用户的密码都是一样的
const user = await User.findOne({email: args.email});
if (!user || !await user.comparePassword(args.password)) throw new Error("User or Password is not correct");
(comparePassword 是一个猫鼬函数)
UserSchema.methods.comparePassword = async function (candidatePassword) {
return await bcrypt.compare(candidatePassword, this.password);
};
如您所见,由于短路,if-check 可能需要不同的时间来执行 - 这对于客户端是可衡量的。
这意味着,客户可以查明某封电子邮件是否使用了我们的服务——这是一次轻微的数据泄露。
我需要这样的脚本:
const err = new Error("User or Password is not correct.")
if (!user) {
await "wait as long as a password comparison would usually take"
throw err
} else if (!await user.comparePassword(args.password)) {
throw err
}
但不知道如何实现它。
一个想法是创建一个虚拟用户来创建comparePassword
on if !user
,但我不确定它的优点/缺点,或者是否有更好的解决方案。
编辑:我如何将所有这些包装在一个setTimeout
函数中?无论如何,这将需要 1 秒(或 500 毫秒或其他时间)。