x509.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 X509 authentication mechanism
  17. * @class
  18. * @return {X509} A cursor instance
  19. */
  20. var X509 = function() {
  21. this.authStore = [];
  22. }
  23. /**
  24. * Authenticate
  25. * @method
  26. * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
  27. * @param {Pool} pool Connection pool for this topology
  28. * @param {string} db Name of the database
  29. * @param {string} username Username
  30. * @param {string} password Password
  31. * @param {authResultCallback} callback The callback to return the result from the authentication
  32. * @return {object}
  33. */
  34. X509.prototype.auth = function(server, pool, db, username, password, callback) {
  35. var self = this;
  36. // Get all the connections
  37. var connections = pool.getAll();
  38. // Total connections
  39. var count = connections.length;
  40. if(count == 0) return callback(null, null);
  41. // Valid connections
  42. var numberOfValidConnections = 0;
  43. var credentialsValid = false;
  44. var errorObject = null;
  45. // For each connection we need to authenticate
  46. while(connections.length > 0) {
  47. // Execute MongoCR
  48. var execute = function(connection) {
  49. // Let's start the sasl process
  50. var command = {
  51. authenticate: 1
  52. , mechanism: 'MONGODB-X509'
  53. , user: username
  54. };
  55. // Let's start the process
  56. server.command("$external.$cmd"
  57. , command
  58. , { connection: connection }, function(err, r) {
  59. // Adjust count
  60. count = count - 1;
  61. // If we have an error
  62. if(err) {
  63. errorObject = err;
  64. } else if(r.result['$err']) {
  65. errorObject = r.result;
  66. } else if(r.result['errmsg']) {
  67. errorObject = r.result;
  68. } else {
  69. credentialsValid = true;
  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. // Get the connection
  85. execute(connections.shift());
  86. }
  87. }
  88. // Add to store only if it does not exist
  89. var addAuthSession = function(authStore, session) {
  90. var found = false;
  91. for(var i = 0; i < authStore.length; i++) {
  92. if(authStore[i].equal(session)) {
  93. found = true;
  94. break;
  95. }
  96. }
  97. if(!found) authStore.push(session);
  98. }
  99. /**
  100. * Re authenticate pool
  101. * @method
  102. * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
  103. * @param {Pool} pool Connection pool for this topology
  104. * @param {authResultCallback} callback The callback to return the result from the authentication
  105. * @return {object}
  106. */
  107. X509.prototype.reauthenticate = function(server, pool, callback) {
  108. var count = this.authStore.length;
  109. if(count == 0) return callback(null, null);
  110. // Iterate over all the auth details stored
  111. for(var i = 0; i < this.authStore.length; i++) {
  112. this.auth(server, pool, this.authStore[i].db, this.authStore[i].username, this.authStore[i].password, function(err, r) {
  113. count = count - 1;
  114. // Done re-authenticating
  115. if(count == 0) {
  116. callback(null, null);
  117. }
  118. });
  119. }
  120. }
  121. /**
  122. * This is a result from a authentication strategy
  123. *
  124. * @callback authResultCallback
  125. * @param {error} error An error object. Set to null if no error present
  126. * @param {boolean} result The result of the authentication process
  127. */
  128. module.exports = X509;