Problems using node-restify-oauth2-mongodb

2019-09-05 14:11发布

I would like to use Restify/Nodejs + Oauth2 + Mongodb to register and authenticate users... I found a great git hub repo here:

https://github.com/rgallagher27/node-restify-oauth2-mongodb

I have both Redis and Mongo installed and I get the node up and running with the code and I can register users. However, I have some problems with validating a user. There was one step in the instructions that I am not sure I am completing correctly ...

//////////////////////
**Insert a Client Key into the mongoDB wit the format:** 

{
    _id: ObjectId("51c6e846ede91c0b8600005e"),
    clientName: "Test Client",
    client: "test",
    secret: "password"
}
//////////////////

Should this be a mongo db as in

db.ClientKey.insert({_id: ObjectId("51c6e846ede91c0b8600005e"), clientName: "Test Client", client: "test",  secret: "password"});

or is this a collection?

I have tried both to no avail.

I believe my failure to understand how this should be created in mongoDB may be causing my issues with an inability to validate a user

in mongodb my restify_test db has a "users" collections... which appears as

> db.users.find();
{
    "name" : "Test",  
    "email" : "test@test.com",
    "username" : "tester1", 
    "hashed_password" : "$2a$10$67rLfuKQqHIwHc2tOPNu.ejY.L/5Mk6XnuOdn0xc9UXUyzKBs6NQC",
    "_id" : ObjectId("520d5874421b580000000001"),
    "role" : "Admin", 
    "__v" : 0
}
> 

But when i try to curl the login

$ curl --user test:password --data grant_type=password --data username=tester1 --data password=testing27 http://[my-localhost]:8090/token
{"error":"invalid_client","error_description":"Client ID and secret did not validate."}

Once again I thank you for any direction you can give me here.

1条回答
家丑人穷心不美
2楼-- · 2019-09-05 14:43

I had the exact same problem as you when using this insert statement:

db.ClientKey.insert({_id: ObjectId("51c6e846ede91c0b8600005e"), clientName: "Test Client", client: "test",  secret: "password"});

The thing that I noticed was the if I did show collections in mongo, one collections was called users and the other was ClientKey. With some trial and error I worked out that the collection needed to called clientkeys (note the plural and lowercase). You should be able to run these commands to fix the issue (assuming you're working from the command line):

mongo
use restify_test
db.ClientKey.renameCollection("clientkeys")

Once I did this, executing the curl command correctly returned my access_token and token_type.

Hope that helps!

查看更多
登录 后发表回答