gpt4 book ai didi

node.js - invalid_client 在 node-restify-oauth2-mongodb 的初始设置

转载 作者:IT王子 更新时间:2023-10-29 06:11:45 26 4
gpt4 key购买 nike

我的问题和这个问题类似,没有接受答案: Problems using node-restify-oauth2-mongodb

但是,我的方法略有不同,因为 clientKeys 集合的命名是正确的,至少我是这么认为的。

当我进入我的 mongo 实例并查看我的集合时:

clientkeys
users

当我查看这些集合中的内容时,我看到:

> db.clientkeys.find().pretty()
{
"_id" : ObjectId("51c6e846ede91c0b8600005e"),
"clientName" : "Test Client",
"client" : "test",
"secret" : "password"
}

> db.users.find().pretty()
{
"_id" : ObjectId("543d7b974f1870f131c6d0aa"),
"name" : "Test",
"email" : "test@test.com",
"username" : "tester1",
"hashed_password" : "$2a$10$gpMXyCuILBbMF2K6Aroc7.lKoIpuGhvL98ZGGoE0tEOtYSQaCpMde",
"role" : "Admin",
"__v" : 0
}

我遵循 repo 协议(protocol)中的指示: https://github.com/rgallagher27/node-restify-oauth2-mongodb

所以我运行这个:

curl --data "name=Test&email=test@test.com&username=tester1&password=testing27&vPassword=testing27&role=Admin" http://localhost:8090/register

它会回应他说的话,像这样:

{
"__v":0,
"name":"Test",
"email":"test@test.com",
"username":"tester1",
"hashed_password":"$2a$10$3uwD9IiKVlkQJvdQXqm07uQnfcXae3AGjDh.zil8.8CgtlQ2MuACK",
"_id":"520774753472d74e2c000001",
"role":"Admin"
}

然后我运行以下命令:

curl --user test:password --data grant_type=password --data username=tester1 --data password=testing27 http://localhost:8090/token

然而这会返回:

{
"error":"invalid_client",
"error_description":"Client ID and secret did not validate."
}

我不太明白我错过了什么。除非我为我的 clientkeys 使用了错误的集合名称,但我不认为我是。

感谢您提供的任何帮助!

最佳答案

所以在完成整个过程后,我发现在 hooks.js 中,他们收到了错误的 validateClient line 35 和 grantUserToken 参数第 52 行。

要使此应用正常运行,您需要进行以下更改。

第 35 行

exports.validateClient = function (clientId, clientSecret, cb)

exports.validateClient = function (clientCredentials, req, cb)

第 39 行

Client.findOne({ client: clientId, secret: clientSecret }, function (err, client) {

Client.findOne({ client: clientCredentials.clientId, secret: clientCredentials.clientSecret }, function (err, client) {

第 52 行

exports.grantUserToken = function (username, password, cb)

exports.grantUserToken = function (allCredentials, req, cb)

第 54 行

var query = User.where( 'username', new RegExp('^' + username + '$', 'i') );

var query = User.where( 'username', new RegExp('^' + allCredentials.username + '$', 'i') );

第 61 行

} else if (user.authenticate(password)) {

} else if (user.authenticate(allCredentials.password)) {

第 65 和 66 行

var token       = generateToken(username + ":" + password);
var newToken = new Token({ username: username, token: token });

var token       = generateToken(allCredentials.username + ":" + allCredentials.password);
var newToken = new Token({ username: allCredentials.username, token: token });

第 70 行

redisClient.set(token, username);

redisClient.set(token, allCredentials.username);

希望这对某人有帮助,如果其他人有更好的解决方案,请告诉我。这对我有用。

现在当我运行这个

curl --user test:password --data grant_type=password --data username=tester1 --data password=testing27 http://localhost:8090/token

这是我的回答

{
"access_token":"S7QEwABRiv6HCSaXy70mUlxY7i/Un/EcgWpvbrBhXlw=",
"token_type":"Bearer"
}

现在对于仍在 hooks.js 中的 secret 路线更改第 80 行

exports.authenticateToken = function (token, cb)

exports.authenticateToken = function (token, req, cb)

如果你运行这个命令

curl -H "Authorization:Bearer Your-Bearer-Token" http://localhost:8090/secret

你的回应是

{
"message":"Success"
}

编码愉快!

关于node.js - invalid_client 在 node-restify-oauth2-mongodb 的初始设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26369527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com