3 var f = require('util').format
4 , Query = require('../connection/commands').Query
5 , MongoError = require('../error');
7 var AuthSession = function(db, username, password) {
9 this.username = username;
10 this.password = password;
13 AuthSession.prototype.equal = function(session) {
14 return session.db == this.db
15 && session.username == this.username
16 && session.password == this.password;
20 * Creates a new X509 authentication mechanism
22 * @return {X509} A cursor instance
24 var X509 = function(bson) {
32 * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
33 * @param {[]Connections} connections Connections to authenticate using this authenticator
34 * @param {string} db Name of the database
35 * @param {string} username Username
36 * @param {string} password Password
37 * @param {authResultCallback} callback The callback to return the result from the authentication
40 X509.prototype.auth = function(server, connections, db, username, password, callback) {
43 var count = connections.length;
44 if(count == 0) return callback(null, null);
47 var numberOfValidConnections = 0;
48 var errorObject = null;
50 // For each connection we need to authenticate
51 while(connections.length > 0) {
53 var execute = function(connection) {
54 // Let's start the sasl process
57 , mechanism: 'MONGODB-X509'
60 // Add username if specified
62 command.user = username;
65 // Let's start the process
66 server(connection, new Query(self.bson, "$external.$cmd", command, {
67 numberToSkip: 0, numberToReturn: 1
68 }), function(err, r) {
72 // If we have an error
75 } else if(r.result['$err']) {
76 errorObject = r.result;
77 } else if(r.result['errmsg']) {
78 errorObject = r.result;
80 numberOfValidConnections = numberOfValidConnections + 1;
83 // We have authenticated all connections
84 if(count == 0 && numberOfValidConnections > 0) {
85 // Store the auth details
86 addAuthSession(self.authStore, new AuthSession(db, username, password));
87 // Return correct authentication
89 } else if(count == 0) {
90 if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using mongocr"));
91 callback(errorObject, false);
96 var _execute = function(_connection) {
97 process.nextTick(function() {
102 _execute(connections.shift());
106 // Add to store only if it does not exist
107 var addAuthSession = function(authStore, session) {
110 for(var i = 0; i < authStore.length; i++) {
111 if(authStore[i].equal(session)) {
117 if(!found) authStore.push(session);
121 * Remove authStore credentials
123 * @param {string} db Name of database we are removing authStore details about
126 X509.prototype.logout = function(dbName) {
127 this.authStore = this.authStore.filter(function(x) {
128 return x.db != dbName;
133 * Re authenticate pool
135 * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
136 * @param {[]Connections} connections Connections to authenticate using this authenticator
137 * @param {authResultCallback} callback The callback to return the result from the authentication
140 X509.prototype.reauthenticate = function(server, connections, callback) {
141 var authStore = this.authStore.slice(0);
142 var count = authStore.length;
143 if(count == 0) return callback(null, null);
144 // Iterate over all the auth details stored
145 for(var i = 0; i < authStore.length; i++) {
146 this.auth(server, connections, authStore[i].db, authStore[i].username, authStore[i].password, function(err) {
148 // Done re-authenticating
157 * This is a result from a authentication strategy
159 * @callback authResultCallback
160 * @param {error} error An error object. Set to null if no error present
161 * @param {boolean} result The result of the authentication process
164 module.exports = X509;