Skip to content

Commit fd686d7

Browse files
vkarpov15mbroadst
authored andcommitted
feat(auth): allow auth option in MongoClient.connect
* allow auth option * copy auth from user-provided options * dont fail if no options * try once more * and once more * add back in options.auth * try options as separate test
1 parent 84433e7 commit fd686d7

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

lib/mongo_client.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var validOptionNames = ['poolSize', 'ssl', 'sslValidate', 'sslCA', 'sslCert',
3838
'serializeFunctions', 'ignoreUndefined', 'raw', 'promoteLongs', 'bufferMaxEntries',
3939
'readPreference', 'pkFactory', 'promiseLibrary', 'readConcern', 'maxStalenessSeconds',
4040
'loggerLevel', 'logger', 'promoteValues', 'promoteBuffers', 'promoteLongs',
41-
'domainsEnabled', 'keepAliveInitialDelay', 'checkServerIdentity', 'validateOptions', 'appname'];
41+
'domainsEnabled', 'keepAliveInitialDelay', 'checkServerIdentity', 'validateOptions', 'appname', 'auth'];
4242
var ignoreOptionNames = ['native_parser'];
4343
var legacyOptionNames = ['server', 'replset', 'replSet', 'mongos', 'db'];
4444

@@ -114,6 +114,8 @@ function MongoClient() {
114114
* @param {number} [options.acceptableLatencyMS=15] Cutoff latency point in MS for Mongos proxies selection.
115115
* @param {boolean} [options.connectWithNoPrimary=false] Sets if the driver should connect even if no primary is available
116116
* @param {string} [options.authSource=undefined] Define the database to authenticate against
117+
* @param {string} [options.auth.user=undefined] The username for auth
118+
* @param {string} [options.auth.password=undefined] The username for auth
117119
* @param {(number|string)} [options.w=null] The write concern.
118120
* @param {number} [options.wtimeout=null] The write concern timeout.
119121
* @param {boolean} [options.j=false] Specify a journal write concern.
@@ -183,6 +185,8 @@ var define = MongoClient.define = new Define('MongoClient', MongoClient, false);
183185
* @param {number} [options.acceptableLatencyMS=15] Cutoff latency point in MS for Mongos proxies selection.
184186
* @param {boolean} [options.connectWithNoPrimary=false] Sets if the driver should connect even if no primary is available
185187
* @param {string} [options.authSource=undefined] Define the database to authenticate against
188+
* @param {string} [options.auth.user=undefined] The username for auth
189+
* @param {string} [options.auth.password=undefined] The username for auth
186190
* @param {(number|string)} [options.w=null] The write concern.
187191
* @param {number} [options.wtimeout=null] The write concern timeout.
188192
* @param {boolean} [options.j=false] Specify a journal write concern.
@@ -488,6 +492,10 @@ var connect = function(self, url, options, callback) {
488492
if(_finalOptions.socketTimeoutMS == null) _finalOptions.socketTimeoutMS = 360000;
489493
if(_finalOptions.connectTimeoutMS == null) _finalOptions.connectTimeoutMS = 30000;
490494

495+
if (_finalOptions.db_options && _finalOptions.db_options.auth) {
496+
delete _finalOptions.db_options.auth;
497+
}
498+
491499
// Failure modes
492500
if(object.servers.length == 0) {
493501
throw new Error("connection string must contain at least one seed host");

lib/url_parser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ module.exports = function(url, options) {
145145

146146
// Add auth to final object if we have 2 elements
147147
if(auth.length == 2) object.auth = {user: auth[0], password: auth[1]};
148+
// if user provided auth options, use that
149+
if(options && options.auth != null) object.auth = options.auth;
148150

149151
// Variables used for temporary storage
150152
var hostPart;

test/functional/connection_tests.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,37 @@ exports.testConnectGoodAuth = {
292292
}
293293
}
294294

295+
/**
296+
* @ignore
297+
*/
298+
exports.testConnectGoodAuthAsOption = {
299+
metadata: { requires: { topology: 'single' } },
300+
301+
// The actual test we wish to run
302+
test: function(configuration, test) {
303+
var connect = configuration.require;
304+
var user = 'testConnectGoodAuthAsOption', password = 'password';
305+
// First add a user.
306+
connect(configuration.url(), function(err, db) {
307+
test.equal(err, null);
308+
309+
db.addUser(user, password, function(err, result) {
310+
test.equal(err, null);
311+
db.close();
312+
restOfTest();
313+
});
314+
});
315+
316+
function restOfTest() {
317+
var opts = { auth: { user: user, password: password } };
318+
connect(configuration.url('baduser', 'badpassword'), opts, connectionTester(test, 'testConnectGoodAuthAsOption', function(db) {
319+
db.close();
320+
test.done();
321+
}));
322+
}
323+
}
324+
}
325+
295326
/**
296327
* @ignore
297328
*/

0 commit comments

Comments
 (0)