3 var f = require('util').format
4 , crypto = require('crypto')
5 , Query = require('../connection/commands').Query
6 , MongoError = require('../error');
8 var AuthSession = function(db, username, password) {
10 this.username = username;
11 this.password = password;
14 AuthSession.prototype.equal = function(session) {
15 return session.db == this.db
16 && session.username == this.username
17 && session.password == this.password;
21 * Creates a new MongoCR authentication mechanism
23 * @return {MongoCR} A cursor instance
25 var MongoCR = function(bson) {
30 // Add to store only if it does not exist
31 var addAuthSession = function(authStore, session) {
34 for(var i = 0; i < authStore.length; i++) {
35 if(authStore[i].equal(session)) {
41 if(!found) authStore.push(session);
47 * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
48 * @param {[]Connections} connections Connections to authenticate using this authenticator
49 * @param {string} db Name of the database
50 * @param {string} username Username
51 * @param {string} password Password
52 * @param {authResultCallback} callback The callback to return the result from the authentication
55 MongoCR.prototype.auth = function(server, connections, db, username, password, callback) {
58 var count = connections.length;
59 if(count == 0) return callback(null, null);
62 var numberOfValidConnections = 0;
63 var credentialsValid = false;
64 var errorObject = null;
66 // For each connection we need to authenticate
67 while(connections.length > 0) {
69 var executeMongoCR = function(connection) {
70 // Write the commmand on the connection
71 server(connection, new Query(self.bson, f("%s.$cmd", db), {
74 numberToSkip: 0, numberToReturn: 1
75 }), function(err, r) {
79 // Adjust the number of connections left
82 nonce = r.result.nonce;
83 // Use node md5 generator
84 var md5 = crypto.createHash('md5');
85 // Generate keys used for authentication
86 md5.update(username + ":mongo:" + password, 'utf8');
87 var hash_password = md5.digest('hex');
89 md5 = crypto.createHash('md5');
90 md5.update(nonce + username + hash_password, 'utf8');
91 key = md5.digest('hex');
95 // Write the commmand on the connection
96 server(connection, new Query(self.bson, f("%s.$cmd", db), {
97 authenticate: 1, user: username, nonce: nonce, key:key
99 numberToSkip: 0, numberToReturn: 1
100 }), function(err, r) {
103 // If we have an error
106 } else if(r.result['$err']) {
107 errorObject = r.result;
108 } else if(r.result['errmsg']) {
109 errorObject = r.result;
111 credentialsValid = true;
112 numberOfValidConnections = numberOfValidConnections + 1;
115 // We have authenticated all connections
116 if(count == 0 && numberOfValidConnections > 0) {
117 // Store the auth details
118 addAuthSession(self.authStore, new AuthSession(db, username, password));
119 // Return correct authentication
120 callback(null, true);
121 } else if(count == 0) {
122 if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using mongocr"));
123 callback(errorObject, false);
129 var _execute = function(_connection) {
130 process.nextTick(function() {
131 executeMongoCR(_connection);
135 _execute(connections.shift());
140 * Remove authStore credentials
142 * @param {string} db Name of database we are removing authStore details about
145 MongoCR.prototype.logout = function(dbName) {
146 this.authStore = this.authStore.filter(function(x) {
147 return x.db != dbName;
152 * Re authenticate pool
154 * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
155 * @param {[]Connections} connections Connections to authenticate using this authenticator
156 * @param {authResultCallback} callback The callback to return the result from the authentication
159 MongoCR.prototype.reauthenticate = function(server, connections, callback) {
160 var authStore = this.authStore.slice(0);
162 var count = authStore.length;
163 if(count == 0) return callback(null, null);
164 // Iterate over all the auth details stored
165 for(var i = 0; i < authStore.length; i++) {
166 this.auth(server, connections, authStore[i].db, authStore[i].username, authStore[i].password, function(err, r) {
169 // Done re-authenticating
178 * This is a result from a authentication strategy
180 * @callback authResultCallback
181 * @param {error} error An error object. Set to null if no error present
182 * @param {boolean} result The result of the authentication process
185 module.exports = MongoCR;