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 errorObject = null;
65 // For each connection we need to authenticate
66 while(connections.length > 0) {
68 var executeMongoCR = function(connection) {
69 // Write the commmand on the connection
70 server(connection, new Query(self.bson, f("%s.$cmd", db), {
73 numberToSkip: 0, numberToReturn: 1
74 }), function(err, r) {
78 // Adjust the number of connections left
81 nonce = r.result.nonce;
82 // Use node md5 generator
83 var md5 = crypto.createHash('md5');
84 // Generate keys used for authentication
85 md5.update(username + ":mongo:" + password, 'utf8');
86 var hash_password = md5.digest('hex');
88 md5 = crypto.createHash('md5');
89 md5.update(nonce + username + hash_password, 'utf8');
90 key = md5.digest('hex');
94 // Write the commmand on the connection
95 server(connection, new Query(self.bson, f("%s.$cmd", db), {
96 authenticate: 1, user: username, nonce: nonce, key:key
98 numberToSkip: 0, numberToReturn: 1
99 }), function(err, r) {
102 // If we have an error
105 } else if(r.result['$err']) {
106 errorObject = r.result;
107 } else if(r.result['errmsg']) {
108 errorObject = r.result;
110 numberOfValidConnections = numberOfValidConnections + 1;
113 // We have authenticated all connections
114 if(count == 0 && numberOfValidConnections > 0) {
115 // Store the auth details
116 addAuthSession(self.authStore, new AuthSession(db, username, password));
117 // Return correct authentication
118 callback(null, true);
119 } else if(count == 0) {
120 if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using mongocr"));
121 callback(errorObject, false);
127 var _execute = function(_connection) {
128 process.nextTick(function() {
129 executeMongoCR(_connection);
133 _execute(connections.shift());
138 * Remove authStore credentials
140 * @param {string} db Name of database we are removing authStore details about
143 MongoCR.prototype.logout = function(dbName) {
144 this.authStore = this.authStore.filter(function(x) {
145 return x.db != dbName;
150 * Re authenticate pool
152 * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
153 * @param {[]Connections} connections Connections to authenticate using this authenticator
154 * @param {authResultCallback} callback The callback to return the result from the authentication
157 MongoCR.prototype.reauthenticate = function(server, connections, callback) {
158 var authStore = this.authStore.slice(0);
159 var count = authStore.length;
160 if(count == 0) return callback(null, null);
161 // Iterate over all the auth details stored
162 for(var i = 0; i < authStore.length; i++) {
163 this.auth(server, connections, authStore[i].db, authStore[i].username, authStore[i].password, function(err) {
165 // Done re-authenticating
174 * This is a result from a authentication strategy
176 * @callback authResultCallback
177 * @param {error} error An error object. Set to null if no error present
178 * @param {boolean} result The result of the authentication process
181 module.exports = MongoCR;