mongocr.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. "use strict";
  2. var f = require('util').format
  3. , crypto = require('crypto')
  4. , MongoError = require('../error');
  5. var AuthSession = function(db, username, password) {
  6. this.db = db;
  7. this.username = username;
  8. this.password = password;
  9. }
  10. AuthSession.prototype.equal = function(session) {
  11. return session.db == this.db
  12. && session.username == this.username
  13. && session.password == this.password;
  14. }
  15. /**
  16. * Creates a new MongoCR authentication mechanism
  17. * @class
  18. * @return {MongoCR} A cursor instance
  19. */
  20. var MongoCR = function() {
  21. this.authStore = [];
  22. }
  23. // Add to store only if it does not exist
  24. var addAuthSession = function(authStore, session) {
  25. var found = false;
  26. for(var i = 0; i < authStore.length; i++) {
  27. if(authStore[i].equal(session)) {
  28. found = true;
  29. break;
  30. }
  31. }
  32. if(!found) authStore.push(session);
  33. }
  34. /**
  35. * Authenticate
  36. * @method
  37. * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
  38. * @param {Pool} pool Connection pool for this topology
  39. * @param {string} db Name of the database
  40. * @param {string} username Username
  41. * @param {string} password Password
  42. * @param {authResultCallback} callback The callback to return the result from the authentication
  43. * @return {object}
  44. */
  45. MongoCR.prototype.auth = function(server, pool, db, username, password, callback) {
  46. var self = this;
  47. // Get all the connections
  48. var connections = pool.getAll();
  49. // Total connections
  50. var count = connections.length;
  51. if(count == 0) return callback(null, null);
  52. // Valid connections
  53. var numberOfValidConnections = 0;
  54. var credentialsValid = false;
  55. var errorObject = null;
  56. // For each connection we need to authenticate
  57. while(connections.length > 0) {
  58. // Execute MongoCR
  59. var executeMongoCR = function(connection) {
  60. // Let's start the process
  61. server.command(f("%s.$cmd", db)
  62. , { getnonce: 1 }
  63. , { connection: connection }, function(err, r) {
  64. var nonce = null;
  65. var key = null;
  66. // Adjust the number of connections left
  67. // Get nonce
  68. if(err == null) {
  69. nonce = r.result.nonce;
  70. // Use node md5 generator
  71. var md5 = crypto.createHash('md5');
  72. // Generate keys used for authentication
  73. md5.update(username + ":mongo:" + password);
  74. var hash_password = md5.digest('hex');
  75. // Final key
  76. md5 = crypto.createHash('md5');
  77. md5.update(nonce + username + hash_password);
  78. key = md5.digest('hex');
  79. }
  80. // Execute command
  81. server.command(f("%s.$cmd", db)
  82. , { authenticate: 1, user: username, nonce: nonce, key:key}
  83. , { connection: connection }, function(err, r) {
  84. count = count - 1;
  85. // If we have an error
  86. if(err) {
  87. errorObject = err;
  88. } else if(r.result['$err']) {
  89. errorObject = r.result;
  90. } else if(r.result['errmsg']) {
  91. errorObject = r.result;
  92. } else {
  93. credentialsValid = true;
  94. numberOfValidConnections = numberOfValidConnections + 1;
  95. }
  96. // We have authenticated all connections
  97. if(count == 0 && numberOfValidConnections > 0) {
  98. // Store the auth details
  99. addAuthSession(self.authStore, new AuthSession(db, username, password));
  100. // Return correct authentication
  101. callback(null, true);
  102. } else if(count == 0) {
  103. if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using mongocr"));
  104. callback(errorObject, false);
  105. }
  106. });
  107. });
  108. }
  109. // Get the connection
  110. executeMongoCR(connections.shift());
  111. }
  112. }
  113. /**
  114. * Re authenticate pool
  115. * @method
  116. * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
  117. * @param {Pool} pool Connection pool for this topology
  118. * @param {authResultCallback} callback The callback to return the result from the authentication
  119. * @return {object}
  120. */
  121. MongoCR.prototype.reauthenticate = function(server, pool, callback) {
  122. var count = this.authStore.length;
  123. if(count == 0) return callback(null, null);
  124. // Iterate over all the auth details stored
  125. for(var i = 0; i < this.authStore.length; i++) {
  126. this.auth(server, pool, this.authStore[i].db, this.authStore[i].username, this.authStore[i].password, function(err, r) {
  127. count = count - 1;
  128. // Done re-authenticating
  129. if(count == 0) {
  130. callback(null, null);
  131. }
  132. });
  133. }
  134. }
  135. /**
  136. * This is a result from a authentication strategy
  137. *
  138. * @callback authResultCallback
  139. * @param {error} error An error object. Set to null if no error present
  140. * @param {boolean} result The result of the authentication process
  141. */
  142. module.exports = MongoCR;