aboutsummaryrefslogtreecommitdiff
path: root/node_modules/mongodb-core/lib/auth/scram.js
blob: cdb6507cd56c99d7bb2ab1eee3aa57848ba3d878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
"use strict";

var f = require('util').format
  , crypto = require('crypto')
  , Query = require('../connection/commands').Query
  , Binary = require('bson').Binary
  , MongoError = require('../error');

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;

/**
 * Creates a new ScramSHA1 authentication mechanism
 * @class
 * @return {ScramSHA1} A cursor instance
 */
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");
  // Use node md5 generator
  var md5 = crypto.createHash('md5');
  // Generate keys used for authentication
  md5.update(username + ":mongo:" + password, 'utf8');
  return md5.digest('hex');
}

// XOR two buffers
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 (var i = 0; i < a.length; i++) {
      res.push(a[i] ^ b[i])
    }
  }
  return new Buffer(res);
}

// Create a final digest
var hi = function(data, salt, iterations) {
  // Create digest
  var digest = function(msg) {
    var hmac = crypto.createHmac('sha1', data);
    hmac.update(msg);
    return new Buffer(hmac.digest('base64'), 'base64');
  }

  // Create variables
  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;
}

/**
 * Authenticate
 * @method
 * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
 * @param {[]Connections} connections Connections to authenticate using this authenticator
 * @param {string} db Name of the database
 * @param {string} username Username
 * @param {string} password Password
 * @param {authResultCallback} callback The callback to return the result from the authentication
 * @return {object}
 */
ScramSHA1.prototype.auth = function(server, connections, db, username, password, callback) {
  var self = this;
  // Total connections
  var count = connections.length;
  if(count == 0) return callback(null, null);

  // Valid connections
  var numberOfValidConnections = 0;
  var credentialsValid = false;
  var errorObject = null;

  // Execute MongoCR
  var executeScram = function(connection) {
    // Clean up the user
    username = username.replace('=', "=3D").replace(',', '=2C');

    // Create a random nonce
    var nonce = crypto.randomBytes(24).toString('base64');
    // var nonce = 'MsQUY9iw0T9fx2MUEz6LZPwGuhVvWAhc'
    var firstBare = f("n=%s,r=%s", username, nonce);

    // Build command structure
    var cmd = {
        saslStart: 1
      , mechanism: 'SCRAM-SHA-1'
      , payload: new Binary(f("n,,%s", firstBare))
      , autoAuthorize: 1
    }

    // Handle the error
    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 {
        credentialsValid = true;
        numberOfValidConnections = numberOfValidConnections + 1;
      }

      return true
    }

    // Finish up
    var finish = function(_count, _numberOfValidConnections) {
      if(_count == 0 && _numberOfValidConnections > 0) {
        // Store the auth details
        addAuthSession(self.authStore, new AuthSession(db, username, password));
        // Return correct authentication
        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) {
      // Handle any error
      handleError(_err, _r)
      // Adjust the number of connections
      count = count - 1;
      // Execute the finish
      finish(count, numberOfValidConnections);
    }

    // Write the commmand on the connection
    server(connection, new Query(self.bson, f("%s.$cmd", db), cmd, {
      numberToSkip: 0, numberToReturn: 1
    }).toBin(), function(err, r) {
      // Do we have an error, handle it
      if(handleError(err, r) == false) {
        count = count - 1;

        if(count == 0 && numberOfValidConnections > 0) {
          // Store the auth details
          addAuthSession(self.authStore, new AuthSession(db, username, password));
          // Return correct authentication
          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;
      }

      // Get the dictionary
      var dict = parsePayload(r.result.payload.value())

      // Unpack dictionary
      var iterations = parseInt(dict.i, 10);
      var salt = dict.s;
      var rnonce = dict.r;

      // Set up start of proof
      var withoutProof = f("c=biws,r=%s", rnonce);
      var passwordDig = passwordDigest(username, password);
      var saltedPassword = hi(passwordDig
          , new Buffer(salt, 'base64')
          , iterations);

      // Create the client key
      var hmac = crypto.createHmac('sha1', saltedPassword);
      hmac.update(new Buffer("Client Key"));
      var clientKey = new Buffer(hmac.digest('base64'), 'base64');

      // Create the stored key
      var hash = crypto.createHash('sha1');
      hash.update(clientKey);
      var storedKey = new Buffer(hash.digest('base64'), 'base64');

      // Create the authentication message
      var authMsg = [firstBare, r.result.payload.value().toString('base64'), withoutProof].join(',');

      // Create client signature
      var hmac = crypto.createHmac('sha1', storedKey);
      hmac.update(new Buffer(authMsg));
      var clientSig = new Buffer(hmac.digest('base64'), 'base64');

      // Create client proof
      var clientProof = f("p=%s", new Buffer(xor(clientKey, clientSig)).toString('base64'));

      // Create client final
      var clientFinal = [withoutProof, clientProof].join(',');

      // Generate server key
      var hmac = crypto.createHmac('sha1', saltedPassword);
      hmac.update(new Buffer('Server Key'))
      var serverKey = new Buffer(hmac.digest('base64'), 'base64');

      // Generate server signature
      var hmac = crypto.createHmac('sha1', serverKey);
      hmac.update(new Buffer(authMsg))
      var serverSig = new Buffer(hmac.digest('base64'), 'base64');

      //
      // Create continue message
      var cmd = {
          saslContinue: 1
        , conversationId: r.result.conversationId
        , payload: new Binary(new Buffer(clientFinal))
      }

      //
      // Execute sasl continue
      // Write the commmand on the connection
      server(connection, new Query(self.bson, f("%s.$cmd", db), cmd, {
        numberToSkip: 0, numberToReturn: 1
      }).toBin(), function(err, r) {
        if(r && r.result.done == false) {
          var cmd = {
              saslContinue: 1
            , conversationId: r.result.conversationId
            , payload: new Buffer(0)
          }

          // Write the commmand on the connection
          server(connection, new Query(self.bson, f("%s.$cmd", db), cmd, {
            numberToSkip: 0, numberToReturn: 1
          }).toBin(), function(err, r) {
            handleEnd(err, r);
          });
        } else {
          handleEnd(err, r);
        }
      });
    });
  }

  var _execute = function(_connection) {
    process.nextTick(function() {
      executeScram(_connection);
    });
  }

  // For each connection we need to authenticate
  while(connections.length > 0) {
    _execute(connections.shift());
  }
}

// Add to store only if it does not exist
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);
}

/**
 * Remove authStore credentials
 * @method
 * @param {string} db Name of database we are removing authStore details about
 * @return {object}
 */
ScramSHA1.prototype.logout = function(dbName) {
  this.authStore = this.authStore.filter(function(x) {
    return x.db != dbName;
  });
}

/**
 * Re authenticate pool
 * @method
 * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
 * @param {[]Connections} connections Connections to authenticate using this authenticator
 * @param {authResultCallback} callback The callback to return the result from the authentication
 * @return {object}
 */
ScramSHA1.prototype.reauthenticate = function(server, connections, callback) {
  var authStore = this.authStore.slice(0);
  var count = authStore.length;
  var err = null;
  // No connections
  if(count == 0) return callback(null, null);
  // Iterate over all the auth details stored
  for(var i = 0; i < authStore.length; i++) {
    this.auth(server, connections, authStore[i].db, authStore[i].username, authStore[i].password, function(err, r) {
      if(err) err = err;
      count = count - 1;
      // Done re-authenticating
      if(count == 0) {
        callback(err, null);
      }
    });
  }
}


module.exports = ScramSHA1;