Skip to content

Commit 34fe9fe

Browse files
committed
NODE-571 added code 59 to legacy server errors when SCRAM-SHA-1 mechanism fails
1 parent cf548ea commit 34fe9fe

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

lib/admin.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,18 @@ define.classMethod('ping', {callback: true, promise:true});
243243
* @param {Admin~resultCallback} [callback] The command result callback
244244
* @return {Promise} returns Promise if no callback passed
245245
*/
246-
Admin.prototype.authenticate = function(username, password, callback) {
246+
Admin.prototype.authenticate = function(username, password, options, callback) {
247247
var self = this;
248+
if(typeof options == 'function') callback = options, options = {};
249+
options = shallowClone(options);
250+
options.authdb = 'admin';
251+
248252
// Execute using callback
249-
if(typeof callback == 'function') return this.s.db.authenticate(username, password, {authdb: 'admin'}, callback);
253+
if(typeof callback == 'function') return this.s.db.authenticate(username, password, options, callback);
250254

251255
// Return a Promise
252256
return new this.s.promiseLibrary(function(resolve, reject) {
253-
self.s.db.authenticate(username, password, {authdb: 'admin'}, function(err, r) {
257+
self.s.db.authenticate(username, password, options, function(err, r) {
254258
if(err) return reject(err);
255259
resolve(r);
256260
});

lib/db.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1409,11 +1409,20 @@ Db.prototype.authenticate = function(username, password, options, callback) {
14091409
}
14101410

14111411
// If we have a callback fallback
1412-
if(typeof callback == 'function') return authenticate(self, username, password, options, callback);
1412+
if(typeof callback == 'function') return authenticate(self, username, password, options, function(err, r) {
1413+
// Support failed auth method
1414+
if(err.message.indexOf('saslStart') != -1) err.code = 59;
1415+
// Reject error
1416+
if(err) return callback(err);
1417+
callback(null, r);
1418+
});
14131419

14141420
// Return a promise
14151421
return new this.s.promiseLibrary(function(resolve, reject) {
14161422
authenticate(self, username, password, options, function(err, r) {
1423+
// Support failed auth method
1424+
if(err.message.indexOf('saslStart') != -1) err.code = 59;
1425+
// Reject error
14171426
if(err) return reject(err);
14181427
resolve(r);
14191428
});

test/functional/authentication_tests.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,46 @@
22

33
var f = require('util').format;
44

5+
/**
6+
* Fail due to illegal authentication mechanism
7+
*
8+
* @ignore
9+
*/
10+
exports['should fail due to illegal authentication mechanism'] = {
11+
metadata: { requires: { topology: ['auth'], mongodb: "<=2.6.x" } },
12+
13+
// The actual test we wish to run
14+
test: function(configuration, test) {
15+
var Db = configuration.require.Db
16+
, MongoClient = configuration.require.MongoClient
17+
, Server = configuration.require.Server;
18+
19+
// restart server
20+
configuration.restart(function() {
21+
var db1 = new Db('mongo-ruby-test-auth1', new Server(configuration.host, configuration.port, {auto_reconnect: true}), {w:1});
22+
db1.open(function(err, db) {
23+
test.equal(null, err);
24+
25+
db.admin().addUser('admin', 'admin', function(err, result) {
26+
test.equal(null, err);
27+
28+
// Login the user
29+
db.admin().authenticate("admin", "admin", {
30+
authMechanism: 'SCRAM-SHA-1'
31+
}, function(err, result) {
32+
test.equal(59, err.code);
33+
34+
// restart server
35+
configuration.restart(function() {
36+
test.done();
37+
});
38+
});
39+
});
40+
});
41+
});
42+
}
43+
}
44+
545
/**
646
* Retrieve the current replicaset status if the server is running as part of a replicaset using a Promise.
747
*

0 commit comments

Comments
 (0)