x509.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";
  2. var f = require('util').format
  3. , Query = require('../connection/commands').Query
  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 X509 authentication mechanism
  17. * @class
  18. * @return {X509} A cursor instance
  19. */
  20. var X509 = function(bson) {
  21. this.bson = bson;
  22. this.authStore = [];
  23. }
  24. /**
  25. * Authenticate
  26. * @method
  27. * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
  28. * @param {[]Connections} connections Connections to authenticate using this authenticator
  29. * @param {string} db Name of the database
  30. * @param {string} username Username
  31. * @param {string} password Password
  32. * @param {authResultCallback} callback The callback to return the result from the authentication
  33. * @return {object}
  34. */
  35. X509.prototype.auth = function(server, connections, db, username, password, callback) {
  36. var self = this;
  37. // Total connections
  38. var count = connections.length;
  39. if(count == 0) return callback(null, null);
  40. // Valid connections
  41. var numberOfValidConnections = 0;
  42. var errorObject = null;
  43. // For each connection we need to authenticate
  44. while(connections.length > 0) {
  45. // Execute MongoCR
  46. var execute = function(connection) {
  47. // Let's start the sasl process
  48. var command = {
  49. authenticate: 1
  50. , mechanism: 'MONGODB-X509'
  51. };
  52. // Add username if specified
  53. if(username) {
  54. command.user = username;
  55. }
  56. // Let's start the process
  57. server(connection, new Query(self.bson, "$external.$cmd", command, {
  58. numberToSkip: 0, numberToReturn: 1
  59. }), function(err, r) {
  60. // Adjust count
  61. count = count - 1;
  62. // If we have an error
  63. if(err) {
  64. errorObject = err;
  65. } else if(r.result['$err']) {
  66. errorObject = r.result;
  67. } else if(r.result['errmsg']) {
  68. errorObject = r.result;
  69. } else {
  70. numberOfValidConnections = numberOfValidConnections + 1;
  71. }
  72. // We have authenticated all connections
  73. if(count == 0 && numberOfValidConnections > 0) {
  74. // Store the auth details
  75. addAuthSession(self.authStore, new AuthSession(db, username, password));
  76. // Return correct authentication
  77. callback(null, true);
  78. } else if(count == 0) {
  79. if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using mongocr"));
  80. callback(errorObject, false);
  81. }
  82. });
  83. }
  84. var _execute = function(_connection) {
  85. process.nextTick(function() {
  86. execute(_connection);
  87. });
  88. }
  89. _execute(connections.shift());
  90. }
  91. }
  92. // Add to store only if it does not exist
  93. var addAuthSession = function(authStore, session) {
  94. var found = false;
  95. for(var i = 0; i < authStore.length; i++) {
  96. if(authStore[i].equal(session)) {
  97. found = true;
  98. break;
  99. }
  100. }
  101. if(!found) authStore.push(session);
  102. }
  103. /**
  104. * Remove authStore credentials
  105. * @method
  106. * @param {string} db Name of database we are removing authStore details about
  107. * @return {object}
  108. */
  109. X509.prototype.logout = function(dbName) {
  110. this.authStore = this.authStore.filter(function(x) {
  111. return x.db != dbName;
  112. });
  113. }
  114. /**
  115. * Re authenticate pool
  116. * @method
  117. * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
  118. * @param {[]Connections} connections Connections to authenticate using this authenticator
  119. * @param {authResultCallback} callback The callback to return the result from the authentication
  120. * @return {object}
  121. */
  122. X509.prototype.reauthenticate = function(server, connections, callback) {
  123. var authStore = this.authStore.slice(0);
  124. var count = authStore.length;
  125. if(count == 0) return callback(null, null);
  126. // Iterate over all the auth details stored
  127. for(var i = 0; i < authStore.length; i++) {
  128. this.auth(server, connections, authStore[i].db, authStore[i].username, authStore[i].password, function(err) {
  129. count = count - 1;
  130. // Done re-authenticating
  131. if(count == 0) {
  132. callback(err, null);
  133. }
  134. });
  135. }
  136. }
  137. /**
  138. * This is a result from a authentication strategy
  139. *
  140. * @callback authResultCallback
  141. * @param {error} error An error object. Set to null if no error present
  142. * @param {boolean} result The result of the authentication process
  143. */
  144. module.exports = X509;