/node_modules/mongoose/node_modules/mongodb-core/lib/auth/sspi.js

https://bitbucket.org/coleman333/smartsite · JavaScript · 304 lines · 208 code · 33 blank · 63 comment · 35 complexity · 5ce8d76ae1125cd587a483ac1d613d42 MD5 · raw file

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