Re: [mongodb-user] Authentication and C-Driver

164 views
Skip to first unread message

Jared Cottrell

unread,
Sep 29, 2012, 10:22:48 PM9/29/12
to mongod...@googlegroups.com
Hi Gary,

Below is a recent Stack Overflow thread that might help. Of course, feel free to contact us at any time at sup...@mongolab.com.

Cheers!


Jared

---


---

Here's the function to call.

MONGO_EXPORT int mongo_cmd_authenticate     (
        mongo
*         conn,
       
const char *    db,
       
const char *    user,
       
const char *    pass
   
)

Authenticate a user.

Parameters:
        conn    a mongo object
.
        db      the database to authenticate against
.
        user    the user name to authenticate
.
        pass    the user
's password.

Returns:
    MONGO_OK on sucess and MONGO_ERROR on failure
.

Here's an excerpt from the test code as an example - mongo-c-driver/test/auth_test.c

if ( mongo_connect( conn , TEST_SERVER, 27017 ) ) {
    printf
( "failed to connect\n" );
    exit
( 1 );
}

ASSERT
( mongo_cmd_authenticate( conn, db, "user", "password" ) == MONGO_OK );

...

Hope that this helps. Here are some links for futher information.

References:

MongoDB C Language Center - http://www.mongodb.org/display/DOCS/C+Language+Center

MongoDB C Driver Documentation - http://api.mongodb.org/c/current/

MongoDB C Driver API Docs - http://api.mongodb.org/c/current/api/index.html

mongo.h File Referenc - http://api.mongodb.org/c/current/api/mongo_8h.html

mongo_cmd_authenticate -http://api.mongodb.org/c/current/api/mongo_8h.html#a715aaa6b82e23486e6caad2b544f2ebf

MongoDB C Driver source code - https://github.com/mongodb/mongo-c-driver

test/auth_test.c - https://github.com/mongodb/mongo-c-driver/blob/master/test/auth_test.c


On Sat, Sep 29, 2012 at 10:01 AM, Gary Pickrell <lifeo...@gmail.com> wrote:
I'm using the C Driver to write an app to use with mongolab.com  I'm able to communicate with my local server which doesn't require authentication, but not with MongoLab which does.  I figure I need to use mongo_cmd_authenticate() but don't have any context for it.  Any recommendations?

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Gary Pickrell

unread,
Sep 30, 2012, 1:03:22 PM9/30/12
to mongod...@googlegroups.com

Hi Jared,

Thanks for the information from Stack Overflow.  It is useful.  It shows how to use the authentication function but it doesn't put it in context, ie when to use it.

     -Gary

Jared D. Cottrell

unread,
Oct 1, 2012, 3:09:39 PM10/1/12
to mongod...@googlegroups.com
Hi Gary,

You usually want to call authenticate right after you establish the basic connection. I'm not very familiar with the C driver myself, but it looks like the basic connection is established with a call to mongo_connect(). So, as in the example, you'll likely want to call mongo_cmd_authenticate() right after that so that anything later using the connection already has the privileges of your user.

if ( mongo_connect( conn , TEST_SERVER, 27017 ) ) {
    printf( "failed to connect\n" );
    exit( 1 );
}
ASSERT( mongo_cmd_authenticate( conn, db, "user", "password" ) == MONGO_OK );

Cheers!


Jared

Gary Pickrell

unread,
Oct 2, 2012, 8:30:25 AM10/2/12
to mongod...@googlegroups.com

Hello Jared,

Thank you!  This is very helpful.  If I have more problems I will email mongolabs for help.

Bruce Zhao

unread,
Oct 5, 2012, 11:38:27 PM10/5/12
to mongod...@googlegroups.com
This is a function in my project.

int CMongoClient::connect(const string &host, int port, const string &db, const string &user, const string &password)
{
int iStatus;
stringstream ss;

  //Check parameter
if (host.empty()) {
  m_lastError = "host is empty.";
return -1;
}
if (port == 0)
port = MONGO_DEFAULT_PORT; //27017
if (user.empty()) {
  m_lastError = "user is empty.";
return -1;
}
if (password.empty()) {
  m_lastError = "password is empty";
return -1;
}

  //connect
iStatus = mongo_connect(m_conn, host.c_str(), port);
if (iStatus == MONGO_ERROR) {
  //failure
iStatus = m_conn->err;

   ss << "Error code:" << iStatus << ", error message:" << MONGO_ERRORS[iStatus];

m_lastError = ss.str();
return -1;
}

//authenticate
iStatus = mongo_cmd_authenticate(m_conn, db.c_str(), user.c_str(), password.c_str());
if (iStatus == MONGO_ERROR) {
  ss << "User " << user << " authenticate fail,please check the username and password.";
m_lastError = ss.str();
return -1;
}

return MONGO_OK;
}





2012/10/2 Gary Pickrell <lifeo...@gmail.com>



--
山贼


Reply all
Reply to author
Forward
0 new messages