From f60a1487a3cdc130bdfd5191ee77485ff244d86f Mon Sep 17 00:00:00 2001 From: Arne Wendt Date: Mon, 22 Oct 2018 18:36:16 +0200 Subject: [PATCH] Allow user creation with MongoDB versions > 4.0 and internal digestion with SCRAM-SHA-256; automatically setting digest parameter and controlling digestion. Taken from [1df6ab0](https://github.com/mongodb/node-mongodb-native/commit/1df6ab0) for minimal changes to the code. --- HISTORY.md | 16 +++++++++++++--- lib/db.js | 18 ++++++++++++------ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index eabfc09053f..a34fbc231df 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,13 @@ + +## [2.2.37](https://github.com/mongodb/node-mongodb-native/compare/v2.2.37...v2.2.37) (2018-10-22) + + +### Features + +* **db, addUser**: Allow user creation with MongoDB versions > 4.0; automatically setting digest parameter and controlling digestion + + + ## [2.2.35](https://github.com/mongodb/node-mongodb-native/compare/v2.2.34...v2.2.35) (2018-02-26) @@ -1616,9 +1626,9 @@ * Reworked socket handling code to emit errors on unparsable messages * Added logger option for Db class, lets you pass in a function in the shape { - log : function(message, object) {}, - error : function(errorMessage, errorObject) {}, - debug : function(debugMessage, object) {}, + ​ log : function(message, object) {}, + ​ error : function(errorMessage, errorObject) {}, + ​ debug : function(debugMessage, object) {}, } Usage is new Db(new Server(..), {logger: loggerInstance}) diff --git a/lib/db.js b/lib/db.js index db878c53891..bb2acc8d387 100644 --- a/lib/db.js +++ b/lib/db.js @@ -1263,22 +1263,28 @@ var _executeAuthCreateUserCommand = function(self, username, password, options, roles = ['dbOwner'] } + const lastIsMaster = self.serverConfig.lastIsMaster() || {}; + const digestPassword = lastIsMaster.maxWireVersion >= 7; + // Build the command to execute var command = { createUser: username , customData: customData , roles: roles - , digestPassword:false + , digestPassword: digestPassword } // Apply write concern to command command = writeConcern(command, self, options); - // Use node md5 generator - var md5 = crypto.createHash('md5'); - // Generate keys used for authentication - md5.update(username + ":mongo:" + password); - var userPassword = md5.digest('hex'); + var userPassword = password; + if(!digestPassword){ + // Use node md5 generator + var md5 = crypto.createHash('md5'); + // Generate keys used for authentication + md5.update(username + ":mongo:" + password); + userPassword = md5.digest('hex'); + } // No password if(typeof password == 'string') {