我在feathersjs文档中注意到
hashPassword此钩子用于在输入之前对纯文本密码进行哈希处理 它们保存到数据库中。它通过使用bcrypt算法 默认值,但可以通过传递自己的选项进行自定义。 功能。
如何在羽毛js钩子,hashPassword钩子中应用此自定义函数?
const { authenticate } = require('@feathersjs/authentication').hooks;
const {
hashPassword, protect
} = require('@feathersjs/authentication-local').hooks;
module.exports = {
before: {
all: [],
find: [ authenticate('jwt') ],
get: [ authenticate('jwt') ],
create: [ hashPassword() ],
update: [ hashPassword(), authenticate('jwt') ],
patch: [ hashPassword(), authenticate('jwt') ],
remove: [ authenticate('jwt') ]
},
after: {
all: [
// Make sure the password field is never sent to the client
// Always must be the last hook
protect('password')
],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};
有人知道答案吗?
谢谢
答案 0 :(得分:0)
这似乎不起作用,至少对于当前版本1.2.9的@ feathersjs / authentication-local无效。
在verifier.js中,您将看到:
// stuff omitted
_comparePassword (entity, password) {
return new Promise((resolve, reject) => {
bcrypt.compare(password, hash, function (error, result) {
// Handle 500 server error.
if (error) {
return reject(error);
}
if (!result) {
debug('Password incorrect');
return reject(false); // eslint-disable-line
}
debug('Password correct');
return resolve(entity);
});
});
}
因此默认验证程序始终使用硬编码的bcrypt.compare,而不使用任何提供的哈希函数。
我发现的唯一解决方案是扩展Verifier并覆盖_comparePassword。
然后:app.configure(local({ Verifier: MyCustomVerifier }));
将起作用。
答案 1 :(得分:0)
您可以将对象称为hashPassword
传递给options
。选项可以有两个字段passwordField
和hash
。
...
create: [ hashPassword({passwordField: 'password', hash: hasherFunc}) ]
...
答案 2 :(得分:0)
使用羽毛库本身。这是目前最简单,最可靠的一种。
const hash = require("@feathersjs/authentication-local/lib/utils/hash");
await hash(req.body.password).then(result => { // Do anything });