如何在 Modulus 上为 Meteor 应用 运行 设置用户文档的密码?

How can I set a password of a user doc for a Meteor app running on Modulus?

我在 Modulus 上有一个 Meteor 应用 运行ning,我需要 设置(不是 重置)用户密码. 有 Accounts.setPassword() 方法,但只能从服务器代码执行。

但是,我不知道模数 meteor shell 之类的东西。

如何运行服务器代码?

我最后添加了一个 Meteor method 来满足我的需要。

不过我不太喜欢这个想法,认为应该有更好的方法。

source code of Meteor 告诉我们密码是如何被哈希的:

// Use bcrypt to hash the password for storage in the database.
// `password` can be a string (in which case it will be run through
// SHA256 before bcrypt) or an object with properties `digest` and
// `algorithm` (in which case we bcrypt `password.digest`).
//
var hashPassword = function (password) {
  password = getPasswordString(password);
  return bcryptHash(password, Accounts._bcryptRounds);
};

还有:

Accounts._bcryptRounds = 10;

因此,您可以通过首先应用 SHA256 然后对原始密码字符串进行 10 轮 bcrypt 来简单地创建一个新的哈希值。

如果您只有一两个密码需要修改,并且希望手动更新它们,可以使用以下在线工具:

Online SHA-256 calculator

Online bcrypt calculator

创建新哈希后,只需使用 Mongo 控制台更新它:

db.users.update({"emails.address": "my@email.com"}, {$set: {"services.password.bcrypt": "a$u/4JV2MrAKb8Jk9yRHDIL.yGn5SQOInFunvUwEnDv5uJgMkWNe08K"}});