Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for authSource (2.4) to MongoClient URL and db.authenti…

…cate method (DRIVER-69/NODE-34)
  • Loading branch information...
commit 08de05c9e41a0eaeaed182bc5d5119de22dd0536 1 parent fbf12f4
@christkv christkv authored
View
1  HISTORY
@@ -8,6 +8,7 @@
- Fixed race condition in Cursor stream (NODE-31)
- Fixed issues related to node 0.10 and process.nextTick now correctly using setImmediate where needed on node 0.10
- Added support for maxMessageSizeBytes if available (DRIVERS-1)
+- Added support for authSource (2.4) to MongoClient URL and db.authenticate method (DRIVER-69/NODE-34)
1.2.13 2013-02-22
-----------------
View
3  Makefile
@@ -18,6 +18,9 @@ test_replicaset:
test_sharded:
node test/runner.js -t sharded
+test_auth:
+ node test/runner.js -t auth
+
generate_docs:
$(NODE) dev/tools/build-docs.js
make --directory=./docs/sphinx-docs --file=Makefile html
View
4 docs/articles/MongoClient.md
@@ -98,6 +98,10 @@ More detailed information about write concerns can be found at [http://www.mongo
* Default value is false
* If conflicting values for fireAndForget, and any write concern are passed the driver should raise an exception about the conflict.
+### Auth options
+* **authSource=string:** Used when the user for authentication is stored in another database using indirect authentication.
+ * Default value is null
+
### Read Preference
* **slaveOk=true|false:** Whether a driver connected to a replica set will send reads to slaves/secondaries.
* Default value is false
View
2  lib/mongodb/connection/url_parser.js
@@ -172,6 +172,8 @@ exports.parse = function(url, options) {
case 'w':
dbOptions.w = parseInt(value, 10);
break;
+ case 'authSource':
+ dbOptions.authSource = value;
case 'wtimeoutMS':
dbOptions.wtimeoutMS = parseInt(value, 10);
break;
View
13 lib/mongodb/db.js
@@ -625,8 +625,9 @@ Db.prototype.logout = function(options, callback) {
* Authenticate a user against the server.
*
* Options
- * - **authdb** {String}, The database that the credentials are for,
+ * - **authSource** {String}, The database that the credentials are for,
* different from the name of the current DB, for example admin
+ *
* @param {String} username username.
* @param {String} password password.
* @param {Object} [options] the options
@@ -644,6 +645,7 @@ Db.prototype.authenticate = function(username, password, options, callback) {
// the default db to authenticate against is 'this'
// if authententicate is called from a retry context, it may be another one, like admin
var authdb = options.authdb ? options.authdb : self.databaseName;
+ authdb = options.authSource ? options.authSource : authdb;
// Push the new auth if we have no previous record
var numberOfConnections = 0;
@@ -2036,7 +2038,14 @@ var _finishConnecting = function(serverConfig, object, options, callback) {
// Open the db
db.open(function(err, db){
if(err == null && object.auth){
- db.authenticate(object.auth.user, object.auth.password, function(err, success){
+ // What db to authenticate against
+ var authentication_db = db;
+ if(object.db_options && object.db_options.authSource) {
+ authentication_db = db.db(object.db_options.authSource);
+ }
+
+ // Authenticate
+ authentication_db.authenticate(object.auth.user, object.auth.password, function(err, success){
if(success){
callback(null, db);
} else {
View
19 test/runner.js
@@ -63,21 +63,4 @@ if(argv.t == 'functional') {
sharded_runners.runner.run('sharded', run_options);
} else if(argv.t == 'replicaset') {
replicaset_runners.runner.run('replica_set', run_options);
-}
-
-// console.log(argv.t)
-
-// Replicaset runners
-// replicaset_runners.runner.run('replica_set')
-// replicaset_runners.runner_auth.run('replica_set_auth')
-
-// Standalone runners
-// standalone_runners.runner.run('single_server')
-// standalone_runners.runner_auth.run('single_server_auth');
-
-// Sharded runners
-// sharded_runners.runner.run('sharded');
-// sharded_runners.runner_auth.run('sharded_auth');
-
-// SSL
-// ssl_runners.runner.run('none');
+}
View
83 test/tests/authentication/authentication_tests.js
@@ -3,6 +3,89 @@ var Step = require('step');
/**
* @ignore
*/
+exports['Should Correctly Authenticate using different user source database and MongoClient'] = function(configuration, test) {
+ var Db = configuration.getMongoPackage().Db
+ , MongoClient = configuration.getMongoPackage().MongoClient
+ , Server = configuration.getMongoPackage().Server;
+
+ var auth_db = new Db('foo', new Server('localhost', 27017), {w:1});
+ var db = new Db('users', new Server('localhost', 27017), {w:1});
+ db.open(function(err, db) {
+
+ // Add admin user
+ db.admin().addUser('admin', 'admin', function(err, result) {
+ test.equal(null, err);
+ test.ok(result != null);
+
+ // Authenticate
+ db.admin().authenticate('admin', 'admin', function(err, result) {
+ test.equal(null, err);
+ test.equal(true, result);
+
+ db.addUser('mallory', 'a', function(err, result) {
+ test.equal(null, err);
+ test.ok(result != null);
+
+ db.db('foo').collection('system.users').insert({user:"mallory", roles: ["readWrite"], userSource: "users"}, function(err, result) {
+ test.equal(null, err);
+
+ // Exit
+ db.close();
+
+ //
+ // Authenticate using MongoClient
+ MongoClient.connect('mongodb://mallory:a@localhost:27017/foo?authSource=users', function(err, db) {
+ test.equal(null, err);
+
+ db.collection('t').insert({a:1}, function(err, result) {
+ test.equal(null, err);
+ db.close();
+
+ //
+ // Authenticate using db.authenticate against alternative source
+ auth_db.open(function(err, db) {
+
+ db.authenticate('mallory', 'a', {authSource:'users'}, function(err, result) {
+ test.equal(null, err);
+ test.equal(true, result);
+
+ db.collection('t').insert({a:1}, function(err, result) {
+ test.equal(null, err);
+
+ db.close();
+ test.done();
+ });
+ });
+ });
+ });
+ });
+
+ // // Attempt to log in
+ // auth_db.open(function(err, db) {
+ // db.db('users').authenticate('mallory', 'a', function(err, result) {
+ // console.log("==================================================== 4");
+ // console.dir(err);
+ // console.dir(result);
+
+ // db.collection('t').insert({a:1}, function(err, result) {
+ // console.log("==================================================== 5");
+ // console.dir(err);
+ // console.dir(result);
+ // db.close();
+ // test.done();
+ // });
+ // });
+ // });
+ });
+ });
+ });
+ });
+ });
+}
+
+/**
+ * @ignore
+ */
exports.shouldCorrectlyAuthenticateWithHorribleBananaCode = function(configuration, test) {
var Db = configuration.getMongoPackage().Db
, Server = configuration.getMongoPackage().Server;
Please sign in to comment.
Something went wrong with that request. Please try again.