123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- "use strict";
- var f = require('util').format
- , crypto = require('crypto')
- , retrieveBSON = require('../connection/utils').retrieveBSON
- , Query = require('../connection/commands').Query
- , MongoError = require('../error');
- var BSON = retrieveBSON(),
- Binary = BSON.Binary;
- var AuthSession = function(db, username, password) {
- this.db = db;
- this.username = username;
- this.password = password;
- }
- AuthSession.prototype.equal = function(session) {
- return session.db == this.db
- && session.username == this.username
- && session.password == this.password;
- }
- var id = 0;
- var ScramSHA1 = function(bson) {
- this.bson = bson;
- this.authStore = [];
- this.id = id++;
- }
- var parsePayload = function(payload) {
- var dict = {};
- var parts = payload.split(',');
- for(var i = 0; i < parts.length; i++) {
- var valueParts = parts[i].split('=');
- dict[valueParts[0]] = valueParts[1];
- }
- return dict;
- }
- var passwordDigest = function(username, password) {
- if(typeof username != 'string') throw new MongoError("username must be a string");
- if(typeof password != 'string') throw new MongoError("password must be a string");
- if(password.length == 0) throw new MongoError("password cannot be empty");
-
- var md5 = crypto.createHash('md5');
-
- md5.update(username + ":mongo:" + password, 'utf8');
- return md5.digest('hex');
- }
- var xor = function(a, b) {
- if (!Buffer.isBuffer(a)) a = new Buffer(a)
- if (!Buffer.isBuffer(b)) b = new Buffer(b)
- var res = []
- if (a.length > b.length) {
- for (var i = 0; i < b.length; i++) {
- res.push(a[i] ^ b[i])
- }
- } else {
- for (i = 0; i < a.length; i++) {
- res.push(a[i] ^ b[i])
- }
- }
- return new Buffer(res);
- }
- var hi = function(data, salt, iterations) {
-
- var digest = function(msg) {
- var hmac = crypto.createHmac('sha1', data);
- hmac.update(msg);
- return new Buffer(hmac.digest('base64'), 'base64');
- }
-
- salt = Buffer.concat([salt, new Buffer('\x00\x00\x00\x01')])
- var ui = digest(salt);
- var u1 = ui;
- for(var i = 0; i < iterations - 1; i++) {
- u1 = digest(u1);
- ui = xor(ui, u1);
- }
- return ui;
- }
- ScramSHA1.prototype.auth = function(server, connections, db, username, password, callback) {
- var self = this;
-
- var count = connections.length;
- if(count == 0) return callback(null, null);
-
- var numberOfValidConnections = 0;
- var errorObject = null;
-
- var executeScram = function(connection) {
-
- username = username.replace('=', "=3D").replace(',', '=2C');
-
- var nonce = crypto.randomBytes(24).toString('base64');
-
- var firstBare = f("n=%s,r=%s", username, nonce);
-
- var cmd = {
- saslStart: 1
- , mechanism: 'SCRAM-SHA-1'
- , payload: new Binary(f("n,,%s", firstBare))
- , autoAuthorize: 1
- }
-
- var handleError = function(err, r) {
- if(err) {
- numberOfValidConnections = numberOfValidConnections - 1;
- errorObject = err; return false;
- } else if(r.result['$err']) {
- errorObject = r.result; return false;
- } else if(r.result['errmsg']) {
- errorObject = r.result; return false;
- } else {
- numberOfValidConnections = numberOfValidConnections + 1;
- }
- return true
- }
-
- var finish = function(_count, _numberOfValidConnections) {
- if(_count == 0 && _numberOfValidConnections > 0) {
-
- addAuthSession(self.authStore, new AuthSession(db, username, password));
-
- return callback(null, true);
- } else if(_count == 0) {
- if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using scram"));
- return callback(errorObject, false);
- }
- }
- var handleEnd = function(_err, _r) {
-
- handleError(_err, _r)
-
- count = count - 1;
-
- finish(count, numberOfValidConnections);
- }
-
- server(connection, new Query(self.bson, f("%s.$cmd", db), cmd, {
- numberToSkip: 0, numberToReturn: 1
- }), function(err, r) {
-
- if(handleError(err, r) == false) {
- count = count - 1;
- if(count == 0 && numberOfValidConnections > 0) {
-
- addAuthSession(self.authStore, new AuthSession(db, username, password));
-
- return callback(null, true);
- } else if(count == 0) {
- if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using scram"));
- return callback(errorObject, false);
- }
- return;
- }
-
- var dict = parsePayload(r.result.payload.value())
-
- var iterations = parseInt(dict.i, 10);
- var salt = dict.s;
- var rnonce = dict.r;
-
- var withoutProof = f("c=biws,r=%s", rnonce);
- var passwordDig = passwordDigest(username, password);
- var saltedPassword = hi(passwordDig
- , new Buffer(salt, 'base64')
- , iterations);
-
- var hmac = crypto.createHmac('sha1', saltedPassword);
- hmac.update(new Buffer("Client Key"));
- var clientKey = new Buffer(hmac.digest('base64'), 'base64');
-
- var hash = crypto.createHash('sha1');
- hash.update(clientKey);
- var storedKey = new Buffer(hash.digest('base64'), 'base64');
-
- var authMsg = [firstBare, r.result.payload.value().toString('base64'), withoutProof].join(',');
-
- hmac = crypto.createHmac('sha1', storedKey);
- hmac.update(new Buffer(authMsg));
- var clientSig = new Buffer(hmac.digest('base64'), 'base64');
-
- var clientProof = f("p=%s", new Buffer(xor(clientKey, clientSig)).toString('base64'));
-
- var clientFinal = [withoutProof, clientProof].join(',');
-
- hmac = crypto.createHmac('sha1', saltedPassword);
- hmac.update(new Buffer('Server Key'))
- var serverKey = new Buffer(hmac.digest('base64'), 'base64');
-
- hmac = crypto.createHmac('sha1', serverKey);
- hmac.update(new Buffer(authMsg))
-
-
- var cmd = {
- saslContinue: 1
- , conversationId: r.result.conversationId
- , payload: new Binary(new Buffer(clientFinal))
- }
-
-
-
- server(connection, new Query(self.bson, f("%s.$cmd", db), cmd, {
- numberToSkip: 0, numberToReturn: 1
- }), function(err, r) {
- if(r && r.result.done == false) {
- var cmd = {
- saslContinue: 1
- , conversationId: r.result.conversationId
- , payload: new Buffer(0)
- }
-
- server(connection, new Query(self.bson, f("%s.$cmd", db), cmd, {
- numberToSkip: 0, numberToReturn: 1
- }), function(err, r) {
- handleEnd(err, r);
- });
- } else {
- handleEnd(err, r);
- }
- });
- });
- }
- var _execute = function(_connection) {
- process.nextTick(function() {
- executeScram(_connection);
- });
- }
-
- while(connections.length > 0) {
- _execute(connections.shift());
- }
- }
- var addAuthSession = function(authStore, session) {
- var found = false;
- for(var i = 0; i < authStore.length; i++) {
- if(authStore[i].equal(session)) {
- found = true;
- break;
- }
- }
- if(!found) authStore.push(session);
- }
- ScramSHA1.prototype.logout = function(dbName) {
- this.authStore = this.authStore.filter(function(x) {
- return x.db != dbName;
- });
- }
- ScramSHA1.prototype.reauthenticate = function(server, connections, callback) {
- var authStore = this.authStore.slice(0);
- var count = authStore.length;
-
- if(count == 0) return callback(null, null);
-
- for(var i = 0; i < authStore.length; i++) {
- this.auth(server, connections, authStore[i].db, authStore[i].username, authStore[i].password, function(err) {
- count = count - 1;
-
- if(count == 0) {
- callback(err, null);
- }
- });
- }
- }
- module.exports = ScramSHA1;
|