Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

x509.js 4.8KB

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