3 var f = require('util').format
4 , crypto = require('crypto')
5 , require_optional = require('require_optional')
6 , Query = require('../connection/commands').Query
7 , MongoError = require('../error');
9 var AuthSession = function(db, username, password, options) {
11 this.username = username;
12 this.password = password;
13 this.options = options;
16 AuthSession.prototype.equal = function(session) {
17 return session.db == this.db
18 && session.username == this.username
19 && session.password == this.password;
24 var MongoAuthProcess = null;
26 // Try to grab the Kerberos class
28 Kerberos = require_optional('kerberos').Kerberos
29 // Authentication process for Mongo
30 MongoAuthProcess = require_optional('kerberos').processes.MongoAuthProcess
34 * Creates a new SSPI authentication mechanism
36 * @return {SSPI} A cursor instance
38 var SSPI = function(bson) {
46 * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
47 * @param {[]Connections} connections Connections to authenticate using this authenticator
48 * @param {string} db Name of the database
49 * @param {string} username Username
50 * @param {string} password Password
51 * @param {authResultCallback} callback The callback to return the result from the authentication
54 SSPI.prototype.auth = function(server, connections, db, username, password, options, callback) {
56 // We don't have the Kerberos library
57 if(Kerberos == null) return callback(new Error("Kerberos library is not installed"));
58 var gssapiServiceName = options['gssapiServiceName'] || 'mongodb';
60 var count = connections.length;
61 if(count == 0) return callback(null, null);
64 var numberOfValidConnections = 0;
65 var credentialsValid = false;
66 var errorObject = null;
68 // For each connection we need to authenticate
69 while(connections.length > 0) {
71 var execute = function(connection) {
72 // Start Auth process for a connection
73 SSIPAuthenticate(self, username, password, gssapiServiceName, server, connection, options, function(err, r) {
77 // If we have an error
80 } else if(r && typeof r == 'object' && r.result['$err']) {
81 errorObject = r.result;
82 } else if(r && typeof r == 'object' && r.result['errmsg']) {
83 errorObject = r.result;
85 credentialsValid = true;
86 numberOfValidConnections = numberOfValidConnections + 1;
89 // We have authenticated all connections
90 if(count == 0 && numberOfValidConnections > 0) {
91 // Store the auth details
92 addAuthSession(self.authStore, new AuthSession(db, username, password, options));
93 // Return correct authentication
95 } else if(count == 0) {
96 if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using mongocr"));
97 callback(errorObject, false);
102 var _execute = function(_connection) {
103 process.nextTick(function() {
104 execute(_connection);
108 _execute(connections.shift());
112 var SSIPAuthenticate = function(self, username, password, gssapiServiceName, server, connection, options, callback) {
113 // Build Authentication command to send to MongoDB
116 , mechanism: 'GSSAPI'
121 // Create authenticator
122 var mongo_auth_process = new MongoAuthProcess(connection.host, connection.port, gssapiServiceName, options);
124 // Execute first sasl step
125 server(connection, new Query(self.bson, "$external.$cmd", command, {
126 numberToSkip: 0, numberToReturn: 1
127 }), function(err, r) {
128 if(err) return callback(err, false);
131 mongo_auth_process.init(username, password, function(err) {
132 if(err) return callback(err);
134 mongo_auth_process.transition(doc.payload, function(err, payload) {
135 if(err) return callback(err);
137 // Perform the next step against mongod
140 , conversationId: doc.conversationId
144 // Execute the command
145 server(connection, new Query(self.bson, "$external.$cmd", command, {
146 numberToSkip: 0, numberToReturn: 1
147 }), function(err, r) {
148 if(err) return callback(err, false);
151 mongo_auth_process.transition(doc.payload, function(err, payload) {
152 if(err) return callback(err);
154 // Perform the next step against mongod
157 , conversationId: doc.conversationId
161 // Execute the command
162 server(connection, new Query(self.bson, "$external.$cmd", command, {
163 numberToSkip: 0, numberToReturn: 1
164 }), function(err, r) {
165 if(err) return callback(err, false);
168 mongo_auth_process.transition(doc.payload, function(err, payload) {
169 // Perform the next step against mongod
172 , conversationId: doc.conversationId
176 // Execute the command
177 server(connection, new Query(self.bson, "$external.$cmd", command, {
178 numberToSkip: 0, numberToReturn: 1
179 }), function(err, r) {
180 if(err) return callback(err, false);
183 if(doc.done) return callback(null, true);
184 callback(new Error("Authentication failed"), false);
195 // Add to store only if it does not exist
196 var addAuthSession = function(authStore, session) {
199 for(var i = 0; i < authStore.length; i++) {
200 if(authStore[i].equal(session)) {
206 if(!found) authStore.push(session);
210 * Remove authStore credentials
212 * @param {string} db Name of database we are removing authStore details about
215 SSPI.prototype.logout = function(dbName) {
216 this.authStore = this.authStore.filter(function(x) {
217 return x.db != dbName;
222 * Re authenticate pool
224 * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
225 * @param {[]Connections} connections Connections to authenticate using this authenticator
226 * @param {authResultCallback} callback The callback to return the result from the authentication
229 SSPI.prototype.reauthenticate = function(server, connections, callback) {
230 var authStore = this.authStore.slice(0);
232 var count = authStore.length;
233 if(count == 0) return callback(null, null);
234 // Iterate over all the auth details stored
235 for(var i = 0; i < authStore.length; i++) {
236 this.auth(server, connections, authStore[i].db, authStore[i].username, authStore[i].password, authStore[i].options, function(err, r) {
239 // Done re-authenticating
248 * This is a result from a authentication strategy
250 * @callback authResultCallback
251 * @param {error} error An error object. Set to null if no error present
252 * @param {boolean} result The result of the authentication process
255 module.exports = SSPI;