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.

sspi.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. 'use strict';
  2. const f = require('util').format;
  3. const Query = require('../connection/commands').Query;
  4. const MongoError = require('../error').MongoError;
  5. const retrieveKerberos = require('../utils').retrieveKerberos;
  6. var AuthSession = function(db, username, password, options) {
  7. this.db = db;
  8. this.username = username;
  9. this.password = password;
  10. this.options = options;
  11. };
  12. AuthSession.prototype.equal = function(session) {
  13. return (
  14. session.db === this.db &&
  15. session.username === this.username &&
  16. session.password === this.password
  17. );
  18. };
  19. /**
  20. * Creates a new SSPI authentication mechanism
  21. * @class
  22. * @return {SSPI} A cursor instance
  23. */
  24. var SSPI = function(bson) {
  25. this.bson = bson;
  26. this.authStore = [];
  27. };
  28. /**
  29. * Authenticate
  30. * @method
  31. * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
  32. * @param {[]Connections} connections Connections to authenticate using this authenticator
  33. * @param {string} db Name of the database
  34. * @param {string} username Username
  35. * @param {string} password Password
  36. * @param {authResultCallback} callback The callback to return the result from the authentication
  37. * @return {object}
  38. */
  39. SSPI.prototype.auth = function(server, connections, db, username, password, options, callback) {
  40. var self = this;
  41. let kerberos;
  42. try {
  43. kerberos = retrieveKerberos();
  44. } catch (e) {
  45. return callback(e, null);
  46. }
  47. var gssapiServiceName = options['gssapiServiceName'] || 'mongodb';
  48. // Total connections
  49. var count = connections.length;
  50. if (count === 0) return callback(null, null);
  51. // Valid connections
  52. var numberOfValidConnections = 0;
  53. var errorObject = null;
  54. // For each connection we need to authenticate
  55. while (connections.length > 0) {
  56. // Execute MongoCR
  57. var execute = function(connection) {
  58. // Start Auth process for a connection
  59. SSIPAuthenticate(
  60. self,
  61. kerberos.processes.MongoAuthProcess,
  62. username,
  63. password,
  64. gssapiServiceName,
  65. server,
  66. connection,
  67. options,
  68. function(err, r) {
  69. // Adjust count
  70. count = count - 1;
  71. // If we have an error
  72. if (err) {
  73. errorObject = err;
  74. } else if (r && typeof r === 'object' && r.result['$err']) {
  75. errorObject = r.result;
  76. } else if (r && typeof r === 'object' && r.result['errmsg']) {
  77. errorObject = r.result;
  78. } else {
  79. numberOfValidConnections = numberOfValidConnections + 1;
  80. }
  81. // We have authenticated all connections
  82. if (count === 0 && numberOfValidConnections > 0) {
  83. // Store the auth details
  84. addAuthSession(self.authStore, new AuthSession(db, username, password, options));
  85. // Return correct authentication
  86. callback(null, true);
  87. } else if (count === 0) {
  88. if (errorObject == null)
  89. errorObject = new MongoError(f('failed to authenticate using mongocr'));
  90. callback(errorObject, false);
  91. }
  92. }
  93. );
  94. };
  95. var _execute = function(_connection) {
  96. process.nextTick(function() {
  97. execute(_connection);
  98. });
  99. };
  100. _execute(connections.shift());
  101. }
  102. };
  103. function SSIPAuthenticate(
  104. self,
  105. MongoAuthProcess,
  106. username,
  107. password,
  108. gssapiServiceName,
  109. server,
  110. connection,
  111. options,
  112. callback
  113. ) {
  114. const authProcess = new MongoAuthProcess(
  115. connection.host,
  116. connection.port,
  117. gssapiServiceName,
  118. options
  119. );
  120. function authCommand(command, authCb) {
  121. const query = new Query(self.bson, '$external.$cmd', command, {
  122. numberToSkip: 0,
  123. numberToReturn: 1
  124. });
  125. server(connection, query, authCb);
  126. }
  127. authProcess.init(username, password, err => {
  128. if (err) return callback(err, false);
  129. authProcess.transition('', (err, payload) => {
  130. if (err) return callback(err, false);
  131. const command = {
  132. saslStart: 1,
  133. mechanism: 'GSSAPI',
  134. payload,
  135. autoAuthorize: 1
  136. };
  137. authCommand(command, (err, result) => {
  138. if (err) return callback(err, false);
  139. const doc = result.result;
  140. authProcess.transition(doc.payload, (err, payload) => {
  141. if (err) return callback(err, false);
  142. const command = {
  143. saslContinue: 1,
  144. conversationId: doc.conversationId,
  145. payload
  146. };
  147. authCommand(command, (err, result) => {
  148. if (err) return callback(err, false);
  149. const doc = result.result;
  150. authProcess.transition(doc.payload, (err, payload) => {
  151. if (err) return callback(err, false);
  152. const command = {
  153. saslContinue: 1,
  154. conversationId: doc.conversationId,
  155. payload
  156. };
  157. authCommand(command, (err, response) => {
  158. if (err) return callback(err, false);
  159. authProcess.transition(null, err => {
  160. if (err) return callback(err, null);
  161. callback(null, response);
  162. });
  163. });
  164. });
  165. });
  166. });
  167. });
  168. });
  169. });
  170. }
  171. // Add to store only if it does not exist
  172. var addAuthSession = function(authStore, session) {
  173. var found = false;
  174. for (var i = 0; i < authStore.length; i++) {
  175. if (authStore[i].equal(session)) {
  176. found = true;
  177. break;
  178. }
  179. }
  180. if (!found) authStore.push(session);
  181. };
  182. /**
  183. * Remove authStore credentials
  184. * @method
  185. * @param {string} db Name of database we are removing authStore details about
  186. * @return {object}
  187. */
  188. SSPI.prototype.logout = function(dbName) {
  189. this.authStore = this.authStore.filter(function(x) {
  190. return x.db !== dbName;
  191. });
  192. };
  193. /**
  194. * Re authenticate pool
  195. * @method
  196. * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
  197. * @param {[]Connections} connections Connections to authenticate using this authenticator
  198. * @param {authResultCallback} callback The callback to return the result from the authentication
  199. * @return {object}
  200. */
  201. SSPI.prototype.reauthenticate = function(server, connections, callback) {
  202. var authStore = this.authStore.slice(0);
  203. var count = authStore.length;
  204. if (count === 0) return callback(null, null);
  205. // Iterate over all the auth details stored
  206. for (var i = 0; i < authStore.length; i++) {
  207. this.auth(
  208. server,
  209. connections,
  210. authStore[i].db,
  211. authStore[i].username,
  212. authStore[i].password,
  213. authStore[i].options,
  214. function(err) {
  215. count = count - 1;
  216. // Done re-authenticating
  217. if (count === 0) {
  218. callback(err, null);
  219. }
  220. }
  221. );
  222. }
  223. };
  224. /**
  225. * This is a result from a authentication strategy
  226. *
  227. * @callback authResultCallback
  228. * @param {error} error An error object. Set to null if no error present
  229. * @param {boolean} result The result of the authentication process
  230. */
  231. module.exports = SSPI;