mongodb 2.2.36 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,109 +1,129 @@
1
- var shallowClone = require('./utils').shallowClone
2
- , handleCallback = require('./utils').handleCallback
3
- , MongoError = require('mongodb-core').MongoError
4
- , f = require('util').format;
1
+ 'use strict';
5
2
 
6
- var authenticate = function(self, username, password, options, callback) {
3
+ var shallowClone = require('./utils').shallowClone,
4
+ handleCallback = require('./utils').handleCallback,
5
+ MongoError = require('mongodb-core').MongoError,
6
+ f = require('util').format;
7
+
8
+ var authenticate = function(client, username, password, options, callback) {
7
9
  // Did the user destroy the topology
8
- if(self.serverConfig && self.serverConfig.isDestroyed()) return callback(new MongoError('topology was destroyed'));
10
+ if (client.topology && client.topology.isDestroyed())
11
+ return callback(new MongoError('topology was destroyed'));
9
12
 
10
13
  // the default db to authenticate against is 'self'
11
- // if authenticate is called from a retry context, it may be another one, like admin
12
- var authdb = options.dbName ? options.dbName : self.databaseName;
13
- authdb = self.authSource ? self.authSource : authdb;
14
+ // if authententicate is called from a retry context, it may be another one, like admin
15
+ var authdb = options.dbName;
14
16
  authdb = options.authdb ? options.authdb : authdb;
15
17
  authdb = options.authSource ? options.authSource : authdb;
16
18
 
17
19
  // Callback
18
20
  var _callback = function(err, result) {
19
- if(self.listeners('authenticated').length > 0) {
20
- self.emit('authenticated', err, result);
21
+ if (client.listeners('authenticated').length > 0) {
22
+ client.emit('authenticated', err, result);
21
23
  }
22
24
 
23
25
  // Return to caller
24
26
  handleCallback(callback, err, result);
25
- }
27
+ };
26
28
 
27
29
  // authMechanism
28
30
  var authMechanism = options.authMechanism || '';
29
31
  authMechanism = authMechanism.toUpperCase();
30
32
 
31
33
  // If classic auth delegate to auth command
32
- if(authMechanism == 'MONGODB-CR') {
33
- self.s.topology.auth('mongocr', authdb, username, password, function(err) {
34
- if(err) return handleCallback(callback, err, false);
34
+ if (authMechanism === 'MONGODB-CR') {
35
+ client.topology.auth('mongocr', authdb, username, password, function(err) {
36
+ if (err) return handleCallback(callback, err, false);
35
37
  _callback(null, true);
36
38
  });
37
- } else if(authMechanism == 'PLAIN') {
38
- self.s.topology.auth('plain', authdb, username, password, function(err) {
39
- if(err) return handleCallback(callback, err, false);
39
+ } else if (authMechanism === 'PLAIN') {
40
+ client.topology.auth('plain', authdb, username, password, function(err) {
41
+ if (err) return handleCallback(callback, err, false);
40
42
  _callback(null, true);
41
43
  });
42
- } else if(authMechanism == 'MONGODB-X509') {
43
- self.s.topology.auth('x509', authdb, username, password, function(err) {
44
- if(err) return handleCallback(callback, err, false);
44
+ } else if (authMechanism === 'MONGODB-X509') {
45
+ client.topology.auth('x509', authdb, username, password, function(err) {
46
+ if (err) return handleCallback(callback, err, false);
45
47
  _callback(null, true);
46
48
  });
47
- } else if(authMechanism == 'SCRAM-SHA-1') {
48
- self.s.topology.auth('scram-sha-1', authdb, username, password, function(err) {
49
- if(err) return handleCallback(callback, err, false);
49
+ } else if (authMechanism === 'SCRAM-SHA-1') {
50
+ client.topology.auth('scram-sha-1', authdb, username, password, function(err) {
51
+ if (err) return handleCallback(callback, err, false);
50
52
  _callback(null, true);
51
53
  });
52
- } else if(authMechanism == 'GSSAPI') {
53
- if(process.platform == 'win32') {
54
- self.s.topology.auth('sspi', authdb, username, password, options, function(err) {
55
- if(err) return handleCallback(callback, err, false);
54
+ } else if (authMechanism === 'GSSAPI') {
55
+ if (process.platform === 'win32') {
56
+ client.topology.auth('sspi', authdb, username, password, options, function(err) {
57
+ if (err) return handleCallback(callback, err, false);
56
58
  _callback(null, true);
57
59
  });
58
60
  } else {
59
- self.s.topology.auth('gssapi', authdb, username, password, options, function(err) {
60
- if(err) return handleCallback(callback, err, false);
61
+ client.topology.auth('gssapi', authdb, username, password, options, function(err) {
62
+ if (err) return handleCallback(callback, err, false);
61
63
  _callback(null, true);
62
64
  });
63
65
  }
64
- } else if(authMechanism == 'DEFAULT') {
65
- self.s.topology.auth('default', authdb, username, password, function(err) {
66
- if(err) return handleCallback(callback, err, false);
66
+ } else if (authMechanism === 'DEFAULT') {
67
+ client.topology.auth('default', authdb, username, password, function(err) {
68
+ if (err) return handleCallback(callback, err, false);
67
69
  _callback(null, true);
68
70
  });
69
71
  } else {
70
- handleCallback(callback, MongoError.create({message: f("authentication mechanism %s not supported", options.authMechanism), driver:true}));
72
+ handleCallback(
73
+ callback,
74
+ MongoError.create({
75
+ message: f('authentication mechanism %s not supported', options.authMechanism),
76
+ driver: true
77
+ })
78
+ );
71
79
  }
72
- }
80
+ };
73
81
 
74
82
  module.exports = function(self, username, password, options, callback) {
75
- if(typeof options == 'function') callback = options, options = {};
83
+ if (typeof options === 'function') (callback = options), (options = {});
84
+ options = options || {};
85
+
76
86
  // Shallow copy the options
77
87
  options = shallowClone(options);
78
88
 
79
89
  // Set default mechanism
80
- if(!options.authMechanism) {
90
+ if (!options.authMechanism) {
81
91
  options.authMechanism = 'DEFAULT';
82
- } else if(options.authMechanism != 'GSSAPI'
83
- && options.authMechanism != 'DEFAULT'
84
- && options.authMechanism != 'MONGODB-CR'
85
- && options.authMechanism != 'MONGODB-X509'
86
- && options.authMechanism != 'SCRAM-SHA-1'
87
- && options.authMechanism != 'PLAIN') {
88
- return handleCallback(callback, MongoError.create({message: "only DEFAULT, GSSAPI, PLAIN, MONGODB-X509, SCRAM-SHA-1 or MONGODB-CR is supported by authMechanism", driver:true}));
92
+ } else if (
93
+ options.authMechanism !== 'GSSAPI' &&
94
+ options.authMechanism !== 'DEFAULT' &&
95
+ options.authMechanism !== 'MONGODB-CR' &&
96
+ options.authMechanism !== 'MONGODB-X509' &&
97
+ options.authMechanism !== 'SCRAM-SHA-1' &&
98
+ options.authMechanism !== 'PLAIN'
99
+ ) {
100
+ return handleCallback(
101
+ callback,
102
+ MongoError.create({
103
+ message:
104
+ 'only DEFAULT, GSSAPI, PLAIN, MONGODB-X509, SCRAM-SHA-1 or MONGODB-CR is supported by authMechanism',
105
+ driver: true
106
+ })
107
+ );
89
108
  }
90
109
 
91
110
  // If we have a callback fallback
92
- if(typeof callback == 'function') return authenticate(self, username, password, options, function(err, r) {
93
- // Support failed auth method
94
- if(err && err.message && err.message.indexOf('saslStart') != -1) err.code = 59;
95
- // Reject error
96
- if(err) return callback(err, r);
97
- callback(null, r);
98
- });
111
+ if (typeof callback === 'function')
112
+ return authenticate(self, username, password, options, function(err, r) {
113
+ // Support failed auth method
114
+ if (err && err.message && err.message.indexOf('saslStart') !== -1) err.code = 59;
115
+ // Reject error
116
+ if (err) return callback(err, r);
117
+ callback(null, r);
118
+ });
99
119
 
100
120
  // Return a promise
101
121
  return new self.s.promiseLibrary(function(resolve, reject) {
102
122
  authenticate(self, username, password, options, function(err, r) {
103
123
  // Support failed auth method
104
- if(err && err.message && err.message.indexOf('saslStart') != -1) err.code = 59;
124
+ if (err && err.message && err.message.indexOf('saslStart') !== -1) err.code = 59;
105
125
  // Reject error
106
- if(err) return reject(err);
126
+ if (err) return reject(err);
107
127
  resolve(r);
108
128
  });
109
129
  });