[英]Import external user to firebase
I want to import users from an external database to firebase.我想将用户从外部数据库导入到 firebase。
The password were hashed with a sha256
function with the password prepended by a salt (which is a UUID).密码使用
sha256
function 进行哈希处理,密码前面加上盐(这是一个 UUID)。
For example:例如:
password = "123qwerty!"
salt = "cb60eb29-95a2-418e-be2a-c1c107fb1add"
hash = sha256(salt+password)
# 54ccb21d42c6961aa1b666b7cb0485f85aab2f2323399fb2959ea5e4e9f6f595
Now to import this to firebase I would do the following:现在要将其导入 firebase,我将执行以下操作:
users = []*auth.UserToImport
users = append(users, (&auth.UserToImport{}).
UID("some-uid").
Email("jon.foo@example.com").
PasswordHash([]byte("54ccb21d42c6961aa1b666b7cb0485f85aab2f2323399fb2959ea5e4e9f6f595")).
PasswordSalt([]byte("cb60eb29-95a2-418e-be2a-c1c107fb1add")).
DisplayName("Jon FOO"))
h := hash.SHA256{
Rounds: 1,
InputOrder: hash.InputOrderSaltFirst,
}
res, err := cl.ImportUsers(ctx, users, auth.WithHash(h))
if err != nil {
log.Fatal(err)
}
The user is well imported in firebase (I can see it in the console), but when I try to login, I have this error The password is invalid or the user does not have a password.
用户在 firebase 中输入的很好(我可以在控制台看到),但是当我尝试登录时,我有这个错误
The password is invalid or the user does not have a password.
I cannot see what is wrong with my way, maybe the Rounds
parameter should be updated, but to what value?我看不出我的方式有什么问题,也许
Rounds
参数应该更新,但是更新到什么值?
Thanks!谢谢!
I finally found my issue.我终于找到了我的问题。 In my case I was giving as the
PasswordHash
the hex representation of the password:在我的例子中,我将密码的十六进制表示形式作为
PasswordHash
给出:
PasswordHash([]byte("54ccb21d42c6961aa1b666b7cb0485f85aab2f2323399fb2959ea5e4e9f6f595")).
It turns out I have to decode first the password, like the following:结果我必须先解码密码,如下所示:
decoded, err := hex.DecodeString("54ccb21d42c6961aa1b666b7cb0485f85aab2f2323399fb2959ea5e4e9f6f595")
if err != nil {
return err
}
user := (&auth.UserToImport{}).
PasswordHash(decoded).
PasswordSalt([]byte("cb60eb29-95a2-418e-be2a-c1c107fb1add")). // the salt stays the same
...
// call ImportUsers with the same hash configuration (Rounds: 1, InputOrder: SaltFirst)
After updating this I ran the code and I could now authenticate with my imported user.更新后,我运行了代码,现在我可以对导入的用户进行身份验证了。
Quick note: as mentionned in the comment, the node SDK does not have the option to specify the input order (salt or password first), this seems to be an important missing feature.快速说明:如评论中所述,节点 SDK 没有指定输入顺序的选项(盐或密码优先),这似乎是一个重要的缺失功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.