mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-12 18:52:50 +00:00
updated bunch of file paths and changed the way posts are loaded
This commit is contained in:
244
node_modules/mongodb-core/lib/auth/gssapi.js
generated
vendored
Normal file
244
node_modules/mongodb-core/lib/auth/gssapi.js
generated
vendored
Normal file
@@ -0,0 +1,244 @@
|
||||
"use strict";
|
||||
|
||||
var f = require('util').format
|
||||
, crypto = require('crypto')
|
||||
, MongoError = require('../error');
|
||||
|
||||
var AuthSession = function(db, username, password, options) {
|
||||
this.db = db;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
AuthSession.prototype.equal = function(session) {
|
||||
return session.db == this.db
|
||||
&& session.username == this.username
|
||||
&& session.password == this.password;
|
||||
}
|
||||
|
||||
// Kerberos class
|
||||
var Kerberos = null;
|
||||
var MongoAuthProcess = null;
|
||||
|
||||
// Try to grab the Kerberos class
|
||||
try {
|
||||
Kerberos = require('kerberos').Kerberos
|
||||
// Authentication process for Mongo
|
||||
MongoAuthProcess = require('kerberos').processes.MongoAuthProcess
|
||||
} catch(err) {}
|
||||
|
||||
/**
|
||||
* Creates a new GSSAPI authentication mechanism
|
||||
* @class
|
||||
* @return {GSSAPI} A cursor instance
|
||||
*/
|
||||
var GSSAPI = function() {
|
||||
this.authStore = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate
|
||||
* @method
|
||||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
|
||||
* @param {Pool} pool Connection pool for this topology
|
||||
* @param {string} db Name of the database
|
||||
* @param {string} username Username
|
||||
* @param {string} password Password
|
||||
* @param {authResultCallback} callback The callback to return the result from the authentication
|
||||
* @return {object}
|
||||
*/
|
||||
GSSAPI.prototype.auth = function(server, pool, db, username, password, options, callback) {
|
||||
var self = this;
|
||||
// We don't have the Kerberos library
|
||||
if(Kerberos == null) return callback(new Error("Kerberos library is not installed"));
|
||||
var gssapiServiceName = options['gssapiServiceName'] || 'mongodb';
|
||||
// Get all the connections
|
||||
var connections = pool.getAll();
|
||||
// Total connections
|
||||
var count = connections.length;
|
||||
if(count == 0) return callback(null, null);
|
||||
|
||||
// Valid connections
|
||||
var numberOfValidConnections = 0;
|
||||
var credentialsValid = false;
|
||||
var errorObject = null;
|
||||
|
||||
// For each connection we need to authenticate
|
||||
while(connections.length > 0) {
|
||||
// Execute MongoCR
|
||||
var execute = function(connection) {
|
||||
// Start Auth process for a connection
|
||||
GSSAPIInitialize(db, username, password, db, gssapiServiceName, server, connection, function(err, r) {
|
||||
// Adjust count
|
||||
count = count - 1;
|
||||
|
||||
// If we have an error
|
||||
if(err) {
|
||||
errorObject = err;
|
||||
} else if(r.result['$err']) {
|
||||
errorObject = r.result;
|
||||
} else if(r.result['errmsg']) {
|
||||
errorObject = r.result;
|
||||
} else {
|
||||
credentialsValid = true;
|
||||
numberOfValidConnections = numberOfValidConnections + 1;
|
||||
}
|
||||
|
||||
// We have authenticated all connections
|
||||
if(count == 0 && numberOfValidConnections > 0) {
|
||||
// Store the auth details
|
||||
addAuthSession(self.authStore, new AuthSession(db, username, password, options));
|
||||
// Return correct authentication
|
||||
callback(null, true);
|
||||
} else if(count == 0) {
|
||||
if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using mongocr"));
|
||||
callback(errorObject, false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Get the connection
|
||||
execute(connections.shift());
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Initialize step
|
||||
var GSSAPIInitialize = function(db, username, password, authdb, gssapiServiceName, server, connection, callback) {
|
||||
// Create authenticator
|
||||
var mongo_auth_process = new MongoAuthProcess(connection.host, connection.port, gssapiServiceName);
|
||||
|
||||
// Perform initialization
|
||||
mongo_auth_process.init(username, password, function(err, context) {
|
||||
if(err) return callback(err, false);
|
||||
|
||||
// Perform the first step
|
||||
mongo_auth_process.transition('', function(err, payload) {
|
||||
if(err) return callback(err, false);
|
||||
|
||||
// Call the next db step
|
||||
MongoDBGSSAPIFirstStep(mongo_auth_process, payload, db, username, password, authdb, server, connection, callback);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
// Perform first step against mongodb
|
||||
var MongoDBGSSAPIFirstStep = function(mongo_auth_process, payload, db, username, password, authdb, server, connection, callback) {
|
||||
// Build the sasl start command
|
||||
var command = {
|
||||
saslStart: 1
|
||||
, mechanism: 'GSSAPI'
|
||||
, payload: payload
|
||||
, autoAuthorize: 1
|
||||
};
|
||||
|
||||
// Execute first sasl step
|
||||
server.command("$external.$cmd"
|
||||
, command
|
||||
, { connection: connection }, function(err, r) {
|
||||
if(err) return callback(err, false);
|
||||
var doc = r.result;
|
||||
// Execute mongodb transition
|
||||
mongo_auth_process.transition(r.result.payload, function(err, payload) {
|
||||
if(err) return callback(err, false);
|
||||
|
||||
// MongoDB API Second Step
|
||||
MongoDBGSSAPISecondStep(mongo_auth_process, payload, doc, db, username, password, authdb, server, connection, callback);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
// Perform first step against mongodb
|
||||
var MongoDBGSSAPISecondStep = function(mongo_auth_process, payload, doc, db, username, password, authdb, server, connection, callback) {
|
||||
// Build Authentication command to send to MongoDB
|
||||
var command = {
|
||||
saslContinue: 1
|
||||
, conversationId: doc.conversationId
|
||||
, payload: payload
|
||||
};
|
||||
|
||||
// Execute the command
|
||||
server.command("$external.$cmd"
|
||||
, command
|
||||
, { connection: connection }, function(err, r) {
|
||||
if(err) return callback(err, false);
|
||||
var doc = r.result;
|
||||
// Call next transition for kerberos
|
||||
mongo_auth_process.transition(doc.payload, function(err, payload) {
|
||||
if(err) return callback(err, false);
|
||||
|
||||
// Call the last and third step
|
||||
MongoDBGSSAPIThirdStep(mongo_auth_process, payload, doc, db, username, password, authdb, server, connection, callback);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
var MongoDBGSSAPIThirdStep = function(mongo_auth_process, payload, doc, db, username, password, authdb, server, connection, callback) {
|
||||
// Build final command
|
||||
var command = {
|
||||
saslContinue: 1
|
||||
, conversationId: doc.conversationId
|
||||
, payload: payload
|
||||
};
|
||||
|
||||
// Execute the command
|
||||
server.command("$external.$cmd"
|
||||
, command
|
||||
, { connection: connection }, function(err, r) {
|
||||
if(err) return callback(err, false);
|
||||
mongo_auth_process.transition(null, function(err, payload) {
|
||||
if(err) return callback(err, null);
|
||||
callback(null, r);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Add to store only if it does not exist
|
||||
var addAuthSession = function(authStore, session) {
|
||||
var found = false;
|
||||
|
||||
for(var i = 0; i < authStore.length; i++) {
|
||||
if(authStore[i].equal(session)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!found) authStore.push(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* Re authenticate pool
|
||||
* @method
|
||||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
|
||||
* @param {Pool} pool Connection pool for this topology
|
||||
* @param {authResultCallback} callback The callback to return the result from the authentication
|
||||
* @return {object}
|
||||
*/
|
||||
GSSAPI.prototype.reauthenticate = function(server, pool, callback) {
|
||||
var count = this.authStore.length;
|
||||
if(count == 0) return callback(null, null);
|
||||
// Iterate over all the auth details stored
|
||||
for(var i = 0; i < this.authStore.length; i++) {
|
||||
this.auth(server, pool, this.authStore[i].db, this.authStore[i].username, this.authStore[i].password, this.authStore[i].options, function(err, r) {
|
||||
count = count - 1;
|
||||
// Done re-authenticating
|
||||
if(count == 0) {
|
||||
callback(null, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a result from a authentication strategy
|
||||
*
|
||||
* @callback authResultCallback
|
||||
* @param {error} error An error object. Set to null if no error present
|
||||
* @param {boolean} result The result of the authentication process
|
||||
*/
|
||||
|
||||
module.exports = GSSAPI;
|
||||
160
node_modules/mongodb-core/lib/auth/mongocr.js
generated
vendored
Normal file
160
node_modules/mongodb-core/lib/auth/mongocr.js
generated
vendored
Normal file
@@ -0,0 +1,160 @@
|
||||
"use strict";
|
||||
|
||||
var f = require('util').format
|
||||
, crypto = require('crypto')
|
||||
, MongoError = require('../error');
|
||||
|
||||
var AuthSession = function(db, username, password) {
|
||||
this.db = db;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
AuthSession.prototype.equal = function(session) {
|
||||
return session.db == this.db
|
||||
&& session.username == this.username
|
||||
&& session.password == this.password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new MongoCR authentication mechanism
|
||||
* @class
|
||||
* @return {MongoCR} A cursor instance
|
||||
*/
|
||||
var MongoCR = function() {
|
||||
this.authStore = [];
|
||||
}
|
||||
|
||||
// Add to store only if it does not exist
|
||||
var addAuthSession = function(authStore, session) {
|
||||
var found = false;
|
||||
|
||||
for(var i = 0; i < authStore.length; i++) {
|
||||
if(authStore[i].equal(session)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!found) authStore.push(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate
|
||||
* @method
|
||||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
|
||||
* @param {Pool} pool Connection pool for this topology
|
||||
* @param {string} db Name of the database
|
||||
* @param {string} username Username
|
||||
* @param {string} password Password
|
||||
* @param {authResultCallback} callback The callback to return the result from the authentication
|
||||
* @return {object}
|
||||
*/
|
||||
MongoCR.prototype.auth = function(server, pool, db, username, password, callback) {
|
||||
var self = this;
|
||||
// Get all the connections
|
||||
var connections = pool.getAll();
|
||||
// Total connections
|
||||
var count = connections.length;
|
||||
if(count == 0) return callback(null, null);
|
||||
|
||||
// Valid connections
|
||||
var numberOfValidConnections = 0;
|
||||
var credentialsValid = false;
|
||||
var errorObject = null;
|
||||
|
||||
// For each connection we need to authenticate
|
||||
while(connections.length > 0) {
|
||||
// Execute MongoCR
|
||||
var executeMongoCR = function(connection) {
|
||||
// Let's start the process
|
||||
server.command(f("%s.$cmd", db)
|
||||
, { getnonce: 1 }
|
||||
, { connection: connection }, function(err, r) {
|
||||
var nonce = null;
|
||||
var key = null;
|
||||
|
||||
// Adjust the number of connections left
|
||||
// Get nonce
|
||||
if(err == null) {
|
||||
nonce = r.result.nonce;
|
||||
// Use node md5 generator
|
||||
var md5 = crypto.createHash('md5');
|
||||
// Generate keys used for authentication
|
||||
md5.update(username + ":mongo:" + password);
|
||||
var hash_password = md5.digest('hex');
|
||||
// Final key
|
||||
md5 = crypto.createHash('md5');
|
||||
md5.update(nonce + username + hash_password);
|
||||
key = md5.digest('hex');
|
||||
}
|
||||
|
||||
// Execute command
|
||||
server.command(f("%s.$cmd", db)
|
||||
, { authenticate: 1, user: username, nonce: nonce, key:key}
|
||||
, { connection: connection }, function(err, r) {
|
||||
count = count - 1;
|
||||
|
||||
// If we have an error
|
||||
if(err) {
|
||||
errorObject = err;
|
||||
} else if(r.result['$err']) {
|
||||
errorObject = r.result;
|
||||
} else if(r.result['errmsg']) {
|
||||
errorObject = r.result;
|
||||
} else {
|
||||
credentialsValid = true;
|
||||
numberOfValidConnections = numberOfValidConnections + 1;
|
||||
}
|
||||
|
||||
// We have authenticated all connections
|
||||
if(count == 0 && numberOfValidConnections > 0) {
|
||||
// Store the auth details
|
||||
addAuthSession(self.authStore, new AuthSession(db, username, password));
|
||||
// Return correct authentication
|
||||
callback(null, true);
|
||||
} else if(count == 0) {
|
||||
if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using mongocr"));
|
||||
callback(errorObject, false);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Get the connection
|
||||
executeMongoCR(connections.shift());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Re authenticate pool
|
||||
* @method
|
||||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
|
||||
* @param {Pool} pool Connection pool for this topology
|
||||
* @param {authResultCallback} callback The callback to return the result from the authentication
|
||||
* @return {object}
|
||||
*/
|
||||
MongoCR.prototype.reauthenticate = function(server, pool, callback) {
|
||||
var count = this.authStore.length;
|
||||
if(count == 0) return callback(null, null);
|
||||
// Iterate over all the auth details stored
|
||||
for(var i = 0; i < this.authStore.length; i++) {
|
||||
this.auth(server, pool, this.authStore[i].db, this.authStore[i].username, this.authStore[i].password, function(err, r) {
|
||||
count = count - 1;
|
||||
// Done re-authenticating
|
||||
if(count == 0) {
|
||||
callback(null, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a result from a authentication strategy
|
||||
*
|
||||
* @callback authResultCallback
|
||||
* @param {error} error An error object. Set to null if no error present
|
||||
* @param {boolean} result The result of the authentication process
|
||||
*/
|
||||
|
||||
module.exports = MongoCR;
|
||||
150
node_modules/mongodb-core/lib/auth/plain.js
generated
vendored
Normal file
150
node_modules/mongodb-core/lib/auth/plain.js
generated
vendored
Normal file
@@ -0,0 +1,150 @@
|
||||
"use strict";
|
||||
|
||||
var f = require('util').format
|
||||
, crypto = require('crypto')
|
||||
, Binary = require('bson').Binary
|
||||
, MongoError = require('../error');
|
||||
|
||||
var AuthSession = function(db, username, password) {
|
||||
this.db = db;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
AuthSession.prototype.equal = function(session) {
|
||||
return session.db == this.db
|
||||
&& session.username == this.username
|
||||
&& session.password == this.password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Plain authentication mechanism
|
||||
* @class
|
||||
* @return {Plain} A cursor instance
|
||||
*/
|
||||
var Plain = function() {
|
||||
this.authStore = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate
|
||||
* @method
|
||||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
|
||||
* @param {Pool} pool Connection pool for this topology
|
||||
* @param {string} db Name of the database
|
||||
* @param {string} username Username
|
||||
* @param {string} password Password
|
||||
* @param {authResultCallback} callback The callback to return the result from the authentication
|
||||
* @return {object}
|
||||
*/
|
||||
Plain.prototype.auth = function(server, pool, db, username, password, callback) {
|
||||
var self = this;
|
||||
// Get all the connections
|
||||
var connections = pool.getAll();
|
||||
// Total connections
|
||||
var count = connections.length;
|
||||
if(count == 0) return callback(null, null);
|
||||
|
||||
// Valid connections
|
||||
var numberOfValidConnections = 0;
|
||||
var credentialsValid = false;
|
||||
var errorObject = null;
|
||||
|
||||
// For each connection we need to authenticate
|
||||
while(connections.length > 0) {
|
||||
// Execute MongoCR
|
||||
var execute = function(connection) {
|
||||
// Create payload
|
||||
var payload = new Binary(f("\x00%s\x00%s", username, password));
|
||||
|
||||
// Let's start the sasl process
|
||||
var command = {
|
||||
saslStart: 1
|
||||
, mechanism: 'PLAIN'
|
||||
, payload: payload
|
||||
, autoAuthorize: 1
|
||||
};
|
||||
|
||||
// Let's start the process
|
||||
server.command("$external.$cmd"
|
||||
, command
|
||||
, { connection: connection }, function(err, r) {
|
||||
// Adjust count
|
||||
count = count - 1;
|
||||
|
||||
// If we have an error
|
||||
if(err) {
|
||||
errorObject = err;
|
||||
} else if(r.result['$err']) {
|
||||
errorObject = r.result;
|
||||
} else if(r.result['errmsg']) {
|
||||
errorObject = r.result;
|
||||
} else {
|
||||
credentialsValid = true;
|
||||
numberOfValidConnections = numberOfValidConnections + 1;
|
||||
}
|
||||
|
||||
// We have authenticated all connections
|
||||
if(count == 0 && numberOfValidConnections > 0) {
|
||||
// Store the auth details
|
||||
addAuthSession(self.authStore, new AuthSession(db, username, password));
|
||||
// Return correct authentication
|
||||
callback(null, true);
|
||||
} else if(count == 0) {
|
||||
if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using mongocr"));
|
||||
callback(errorObject, false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Get the connection
|
||||
execute(connections.shift());
|
||||
}
|
||||
}
|
||||
|
||||
// Add to store only if it does not exist
|
||||
var addAuthSession = function(authStore, session) {
|
||||
var found = false;
|
||||
|
||||
for(var i = 0; i < authStore.length; i++) {
|
||||
if(authStore[i].equal(session)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!found) authStore.push(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* Re authenticate pool
|
||||
* @method
|
||||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
|
||||
* @param {Pool} pool Connection pool for this topology
|
||||
* @param {authResultCallback} callback The callback to return the result from the authentication
|
||||
* @return {object}
|
||||
*/
|
||||
Plain.prototype.reauthenticate = function(server, pool, callback) {
|
||||
var count = this.authStore.length;
|
||||
if(count == 0) return callback(null, null);
|
||||
// Iterate over all the auth details stored
|
||||
for(var i = 0; i < this.authStore.length; i++) {
|
||||
this.auth(server, pool, this.authStore[i].db, this.authStore[i].username, this.authStore[i].password, function(err, r) {
|
||||
count = count - 1;
|
||||
// Done re-authenticating
|
||||
if(count == 0) {
|
||||
callback(null, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a result from a authentication strategy
|
||||
*
|
||||
* @callback authResultCallback
|
||||
* @param {error} error An error object. Set to null if no error present
|
||||
* @param {boolean} result The result of the authentication process
|
||||
*/
|
||||
|
||||
module.exports = Plain;
|
||||
317
node_modules/mongodb-core/lib/auth/scram.js
generated
vendored
Normal file
317
node_modules/mongodb-core/lib/auth/scram.js
generated
vendored
Normal file
@@ -0,0 +1,317 @@
|
||||
"use strict";
|
||||
|
||||
var f = require('util').format
|
||||
, crypto = require('crypto')
|
||||
, Binary = require('bson').Binary
|
||||
, MongoError = require('../error');
|
||||
|
||||
var AuthSession = function(db, username, password) {
|
||||
this.db = db;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
AuthSession.prototype.equal = function(session) {
|
||||
return session.db == this.db
|
||||
&& session.username == this.username
|
||||
&& session.password == this.password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new ScramSHA1 authentication mechanism
|
||||
* @class
|
||||
* @return {ScramSHA1} A cursor instance
|
||||
*/
|
||||
var ScramSHA1 = function() {
|
||||
this.authStore = [];
|
||||
}
|
||||
|
||||
var parsePayload = function(payload) {
|
||||
var dict = {};
|
||||
var parts = payload.split(',');
|
||||
|
||||
for(var i = 0; i < parts.length; i++) {
|
||||
var valueParts = parts[i].split('=');
|
||||
dict[valueParts[0]] = valueParts[1];
|
||||
}
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
var passwordDigest = function(username, password) {
|
||||
if(typeof username != 'string') throw new MongoError("username must be a string");
|
||||
if(typeof password != 'string') throw new MongoError("password must be a string");
|
||||
if(password.length == 0) throw new MongoError("password cannot be empty");
|
||||
// Use node md5 generator
|
||||
var md5 = crypto.createHash('md5');
|
||||
// Generate keys used for authentication
|
||||
md5.update(username + ":mongo:" + password);
|
||||
return md5.digest('hex');
|
||||
}
|
||||
|
||||
// XOR two buffers
|
||||
var xor = function(a, b) {
|
||||
if (!Buffer.isBuffer(a)) a = new Buffer(a)
|
||||
if (!Buffer.isBuffer(b)) b = new Buffer(b)
|
||||
var res = []
|
||||
if (a.length > b.length) {
|
||||
for (var i = 0; i < b.length; i++) {
|
||||
res.push(a[i] ^ b[i])
|
||||
}
|
||||
} else {
|
||||
for (var i = 0; i < a.length; i++) {
|
||||
res.push(a[i] ^ b[i])
|
||||
}
|
||||
}
|
||||
return new Buffer(res);
|
||||
}
|
||||
|
||||
// Create a final digest
|
||||
var hi = function(data, salt, iterations) {
|
||||
// Create digest
|
||||
var digest = function(msg) {
|
||||
var hmac = crypto.createHmac('sha1', data);
|
||||
hmac.update(msg);
|
||||
return new Buffer(hmac.digest('base64'), 'base64');
|
||||
}
|
||||
|
||||
// Create variables
|
||||
salt = Buffer.concat([salt, new Buffer('\x00\x00\x00\x01')])
|
||||
var ui = digest(salt);
|
||||
var u1 = ui;
|
||||
|
||||
for(var i = 0; i < iterations - 1; i++) {
|
||||
u1 = digest(u1);
|
||||
ui = xor(ui, u1);
|
||||
}
|
||||
|
||||
return ui;
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate
|
||||
* @method
|
||||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
|
||||
* @param {Pool} pool Connection pool for this topology
|
||||
* @param {string} db Name of the database
|
||||
* @param {string} username Username
|
||||
* @param {string} password Password
|
||||
* @param {authResultCallback} callback The callback to return the result from the authentication
|
||||
* @return {object}
|
||||
*/
|
||||
ScramSHA1.prototype.auth = function(server, pool, db, username, password, callback) {
|
||||
var self = this;
|
||||
// Get all the connections
|
||||
var connections = pool.getAll();
|
||||
// Total connections
|
||||
var count = connections.length;
|
||||
if(count == 0) return callback(null, null);
|
||||
|
||||
// Valid connections
|
||||
var numberOfValidConnections = 0;
|
||||
var credentialsValid = false;
|
||||
var errorObject = null;
|
||||
|
||||
// For each connection we need to authenticate
|
||||
while(connections.length > 0) {
|
||||
// Execute MongoCR
|
||||
var executeScram = function(connection) {
|
||||
// Clean up the user
|
||||
username = username.replace('=', "=3D").replace(',', '=2C');
|
||||
|
||||
// Create a random nonce
|
||||
var nonce = crypto.randomBytes(24).toString('base64');
|
||||
// var nonce = 'MsQUY9iw0T9fx2MUEz6LZPwGuhVvWAhc'
|
||||
var firstBare = f("n=%s,r=%s", username, nonce);
|
||||
|
||||
// Build command structure
|
||||
var cmd = {
|
||||
saslStart: 1
|
||||
, mechanism: 'SCRAM-SHA-1'
|
||||
, payload: new Binary(f("n,,%s", firstBare))
|
||||
, autoAuthorize: 1
|
||||
}
|
||||
|
||||
// Handle the error
|
||||
var handleError = function(err, r) {
|
||||
if(err) {
|
||||
numberOfValidConnections = numberOfValidConnections - 1;
|
||||
errorObject = err; return false;
|
||||
} else if(r.result['$err']) {
|
||||
errorObject = r.result; return false;
|
||||
} else if(r.result['errmsg']) {
|
||||
errorObject = r.result; return false;
|
||||
} else {
|
||||
credentialsValid = true;
|
||||
numberOfValidConnections = numberOfValidConnections + 1;
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Finish up
|
||||
var finish = function(_count, _numberOfValidConnections) {
|
||||
if(_count == 0 && _numberOfValidConnections > 0) {
|
||||
// Store the auth details
|
||||
addAuthSession(self.authStore, new AuthSession(db, username, password));
|
||||
// Return correct authentication
|
||||
return callback(null, true);
|
||||
} else if(_count == 0) {
|
||||
if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using scram"));
|
||||
return callback(errorObject, false);
|
||||
}
|
||||
}
|
||||
|
||||
var handleEnd = function(_err, _r) {
|
||||
// Handle any error
|
||||
handleError(_err, _r)
|
||||
// Adjust the number of connections
|
||||
count = count - 1;
|
||||
// Execute the finish
|
||||
finish(count, numberOfValidConnections);
|
||||
}
|
||||
|
||||
// Execute start sasl command
|
||||
server.command(f("%s.$cmd", db)
|
||||
, cmd, { connection: connection }, function(err, r) {
|
||||
|
||||
// Do we have an error, handle it
|
||||
if(handleError(err, r) == false) {
|
||||
count = count - 1;
|
||||
|
||||
if(count == 0 && numberOfValidConnections > 0) {
|
||||
// Store the auth details
|
||||
addAuthSession(self.authStore, new AuthSession(db, username, password));
|
||||
// Return correct authentication
|
||||
return callback(null, true);
|
||||
} else if(count == 0) {
|
||||
if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using scram"));
|
||||
return callback(errorObject, false);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the dictionary
|
||||
var dict = parsePayload(r.result.payload.value())
|
||||
|
||||
// Unpack dictionary
|
||||
var iterations = parseInt(dict.i, 10);
|
||||
var salt = dict.s;
|
||||
var rnonce = dict.r;
|
||||
|
||||
// Set up start of proof
|
||||
var withoutProof = f("c=biws,r=%s", rnonce);
|
||||
var passwordDig = passwordDigest(username, password);
|
||||
var saltedPassword = hi(passwordDig
|
||||
, new Buffer(salt, 'base64')
|
||||
, iterations);
|
||||
|
||||
// Create the client key
|
||||
var hmac = crypto.createHmac('sha1', saltedPassword);
|
||||
hmac.update(new Buffer("Client Key"));
|
||||
var clientKey = new Buffer(hmac.digest('base64'), 'base64');
|
||||
|
||||
// Create the stored key
|
||||
var hash = crypto.createHash('sha1');
|
||||
hash.update(clientKey);
|
||||
var storedKey = new Buffer(hash.digest('base64'), 'base64');
|
||||
|
||||
// Create the authentication message
|
||||
var authMsg = [firstBare, r.result.payload.value().toString('base64'), withoutProof].join(',');
|
||||
|
||||
// Create client signature
|
||||
var hmac = crypto.createHmac('sha1', storedKey);
|
||||
hmac.update(new Buffer(authMsg));
|
||||
var clientSig = new Buffer(hmac.digest('base64'), 'base64');
|
||||
|
||||
// Create client proof
|
||||
var clientProof = f("p=%s", new Buffer(xor(clientKey, clientSig)).toString('base64'));
|
||||
|
||||
// Create client final
|
||||
var clientFinal = [withoutProof, clientProof].join(',');
|
||||
|
||||
// Generate server key
|
||||
var hmac = crypto.createHmac('sha1', saltedPassword);
|
||||
hmac.update(new Buffer('Server Key'))
|
||||
var serverKey = new Buffer(hmac.digest('base64'), 'base64');
|
||||
|
||||
// Generate server signature
|
||||
var hmac = crypto.createHmac('sha1', serverKey);
|
||||
hmac.update(new Buffer(authMsg))
|
||||
var serverSig = new Buffer(hmac.digest('base64'), 'base64');
|
||||
|
||||
//
|
||||
// Create continue message
|
||||
var cmd = {
|
||||
saslContinue: 1
|
||||
, conversationId: r.result.conversationId
|
||||
, payload: new Binary(new Buffer(clientFinal))
|
||||
}
|
||||
|
||||
//
|
||||
// Execute sasl continue
|
||||
server.command(f("%s.$cmd", db)
|
||||
, cmd, { connection: connection }, function(err, r) {
|
||||
if(r && r.result.done == false) {
|
||||
var cmd = {
|
||||
saslContinue: 1
|
||||
, conversationId: r.result.conversationId
|
||||
, payload: new Buffer(0)
|
||||
}
|
||||
|
||||
server.command(f("%s.$cmd", db)
|
||||
, cmd, { connection: connection }, function(err, r) {
|
||||
handleEnd(err, r);
|
||||
});
|
||||
} else {
|
||||
handleEnd(err, r);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Get the connection
|
||||
executeScram(connections.shift());
|
||||
}
|
||||
}
|
||||
|
||||
// Add to store only if it does not exist
|
||||
var addAuthSession = function(authStore, session) {
|
||||
var found = false;
|
||||
|
||||
for(var i = 0; i < authStore.length; i++) {
|
||||
if(authStore[i].equal(session)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!found) authStore.push(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* Re authenticate pool
|
||||
* @method
|
||||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
|
||||
* @param {Pool} pool Connection pool for this topology
|
||||
* @param {authResultCallback} callback The callback to return the result from the authentication
|
||||
* @return {object}
|
||||
*/
|
||||
ScramSHA1.prototype.reauthenticate = function(server, pool, callback) {
|
||||
var count = this.authStore.length;
|
||||
if(count == 0) return callback(null, null);
|
||||
// Iterate over all the auth details stored
|
||||
for(var i = 0; i < this.authStore.length; i++) {
|
||||
this.auth(server, pool, this.authStore[i].db, this.authStore[i].username, this.authStore[i].password, function(err, r) {
|
||||
count = count - 1;
|
||||
// Done re-authenticating
|
||||
if(count == 0) {
|
||||
callback(null, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = ScramSHA1;
|
||||
234
node_modules/mongodb-core/lib/auth/sspi.js
generated
vendored
Normal file
234
node_modules/mongodb-core/lib/auth/sspi.js
generated
vendored
Normal file
@@ -0,0 +1,234 @@
|
||||
"use strict";
|
||||
|
||||
var f = require('util').format
|
||||
, crypto = require('crypto')
|
||||
, MongoError = require('../error');
|
||||
|
||||
var AuthSession = function(db, username, password, options) {
|
||||
this.db = db;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
AuthSession.prototype.equal = function(session) {
|
||||
return session.db == this.db
|
||||
&& session.username == this.username
|
||||
&& session.password == this.password;
|
||||
}
|
||||
|
||||
// Kerberos class
|
||||
var Kerberos = null;
|
||||
var MongoAuthProcess = null;
|
||||
|
||||
// Try to grab the Kerberos class
|
||||
try {
|
||||
Kerberos = require('kerberos').Kerberos
|
||||
// Authentication process for Mongo
|
||||
MongoAuthProcess = require('kerberos').processes.MongoAuthProcess
|
||||
} catch(err) {}
|
||||
|
||||
/**
|
||||
* Creates a new SSPI authentication mechanism
|
||||
* @class
|
||||
* @return {SSPI} A cursor instance
|
||||
*/
|
||||
var SSPI = function() {
|
||||
this.authStore = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate
|
||||
* @method
|
||||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
|
||||
* @param {Pool} pool Connection pool for this topology
|
||||
* @param {string} db Name of the database
|
||||
* @param {string} username Username
|
||||
* @param {string} password Password
|
||||
* @param {authResultCallback} callback The callback to return the result from the authentication
|
||||
* @return {object}
|
||||
*/
|
||||
SSPI.prototype.auth = function(server, pool, db, username, password, options, callback) {
|
||||
var self = this;
|
||||
// We don't have the Kerberos library
|
||||
if(Kerberos == null) return callback(new Error("Kerberos library is not installed"));
|
||||
var gssapiServiceName = options['gssapiServiceName'] || 'mongodb';
|
||||
// Get all the connections
|
||||
var connections = pool.getAll();
|
||||
// Total connections
|
||||
var count = connections.length;
|
||||
if(count == 0) return callback(null, null);
|
||||
|
||||
// Valid connections
|
||||
var numberOfValidConnections = 0;
|
||||
var credentialsValid = false;
|
||||
var errorObject = null;
|
||||
|
||||
// For each connection we need to authenticate
|
||||
while(connections.length > 0) {
|
||||
// Execute MongoCR
|
||||
var execute = function(connection) {
|
||||
// Start Auth process for a connection
|
||||
SSIPAuthenticate(username, password, gssapiServiceName, server, connection, function(err, r) {
|
||||
// Adjust count
|
||||
count = count - 1;
|
||||
|
||||
// If we have an error
|
||||
if(err) {
|
||||
errorObject = err;
|
||||
} else if(r && typeof r == 'object' && r.result['$err']) {
|
||||
errorObject = r.result;
|
||||
} else if(r && typeof r == 'object' && r.result['errmsg']) {
|
||||
errorObject = r.result;
|
||||
} else {
|
||||
credentialsValid = true;
|
||||
numberOfValidConnections = numberOfValidConnections + 1;
|
||||
}
|
||||
|
||||
// We have authenticated all connections
|
||||
if(count == 0 && numberOfValidConnections > 0) {
|
||||
// Store the auth details
|
||||
addAuthSession(self.authStore, new AuthSession(db, username, password, options));
|
||||
// Return correct authentication
|
||||
callback(null, true);
|
||||
} else if(count == 0) {
|
||||
if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using mongocr"));
|
||||
callback(errorObject, false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Get the connection
|
||||
execute(connections.shift());
|
||||
}
|
||||
}
|
||||
|
||||
var SSIPAuthenticate = function(username, password, gssapiServiceName, server, connection, callback) {
|
||||
// Build Authentication command to send to MongoDB
|
||||
var command = {
|
||||
saslStart: 1
|
||||
, mechanism: 'GSSAPI'
|
||||
, payload: ''
|
||||
, autoAuthorize: 1
|
||||
};
|
||||
|
||||
// Create authenticator
|
||||
var mongo_auth_process = new MongoAuthProcess(connection.host, connection.port, gssapiServiceName);
|
||||
|
||||
// Execute first sasl step
|
||||
server.command("$external.$cmd"
|
||||
, command
|
||||
, { connection: connection }, function(err, r) {
|
||||
if(err) return callback(err, false);
|
||||
var doc = r.result;
|
||||
|
||||
mongo_auth_process.init(username, password, function(err) {
|
||||
if(err) return callback(err);
|
||||
|
||||
mongo_auth_process.transition(doc.payload, function(err, payload) {
|
||||
if(err) return callback(err);
|
||||
|
||||
// Perform the next step against mongod
|
||||
var command = {
|
||||
saslContinue: 1
|
||||
, conversationId: doc.conversationId
|
||||
, payload: payload
|
||||
};
|
||||
|
||||
// Execute the command
|
||||
server.command("$external.$cmd"
|
||||
, command
|
||||
, { connection: connection }, function(err, r) {
|
||||
if(err) return callback(err, false);
|
||||
var doc = r.result;
|
||||
|
||||
mongo_auth_process.transition(doc.payload, function(err, payload) {
|
||||
if(err) return callback(err);
|
||||
|
||||
// Perform the next step against mongod
|
||||
var command = {
|
||||
saslContinue: 1
|
||||
, conversationId: doc.conversationId
|
||||
, payload: payload
|
||||
};
|
||||
|
||||
// Execute the command
|
||||
server.command("$external.$cmd"
|
||||
, command
|
||||
, { connection: connection }, function(err, r) {
|
||||
if(err) return callback(err, false);
|
||||
var doc = r.result;
|
||||
|
||||
mongo_auth_process.transition(doc.payload, function(err, payload) {
|
||||
// Perform the next step against mongod
|
||||
var command = {
|
||||
saslContinue: 1
|
||||
, conversationId: doc.conversationId
|
||||
, payload: payload
|
||||
};
|
||||
|
||||
// Execute the command
|
||||
server.command("$external.$cmd"
|
||||
, command
|
||||
, { connection: connection }, function(err, r) {
|
||||
if(err) return callback(err, false);
|
||||
var doc = r.result;
|
||||
|
||||
if(doc.done) return callback(null, true);
|
||||
callback(new Error("Authentication failed"), false);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Add to store only if it does not exist
|
||||
var addAuthSession = function(authStore, session) {
|
||||
var found = false;
|
||||
|
||||
for(var i = 0; i < authStore.length; i++) {
|
||||
if(authStore[i].equal(session)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!found) authStore.push(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* Re authenticate pool
|
||||
* @method
|
||||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
|
||||
* @param {Pool} pool Connection pool for this topology
|
||||
* @param {authResultCallback} callback The callback to return the result from the authentication
|
||||
* @return {object}
|
||||
*/
|
||||
SSPI.prototype.reauthenticate = function(server, pool, callback) {
|
||||
var count = this.authStore.length;
|
||||
if(count == 0) return callback(null, null);
|
||||
// Iterate over all the auth details stored
|
||||
for(var i = 0; i < this.authStore.length; i++) {
|
||||
this.auth(server, pool, this.authStore[i].db, this.authStore[i].username, this.authStore[i].password, this.authStore[i].options, function(err, r) {
|
||||
count = count - 1;
|
||||
// Done re-authenticating
|
||||
if(count == 0) {
|
||||
callback(null, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a result from a authentication strategy
|
||||
*
|
||||
* @callback authResultCallback
|
||||
* @param {error} error An error object. Set to null if no error present
|
||||
* @param {boolean} result The result of the authentication process
|
||||
*/
|
||||
|
||||
module.exports = SSPI;
|
||||
145
node_modules/mongodb-core/lib/auth/x509.js
generated
vendored
Normal file
145
node_modules/mongodb-core/lib/auth/x509.js
generated
vendored
Normal file
@@ -0,0 +1,145 @@
|
||||
"use strict";
|
||||
|
||||
var f = require('util').format
|
||||
, crypto = require('crypto')
|
||||
, MongoError = require('../error');
|
||||
|
||||
var AuthSession = function(db, username, password) {
|
||||
this.db = db;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
AuthSession.prototype.equal = function(session) {
|
||||
return session.db == this.db
|
||||
&& session.username == this.username
|
||||
&& session.password == this.password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new X509 authentication mechanism
|
||||
* @class
|
||||
* @return {X509} A cursor instance
|
||||
*/
|
||||
var X509 = function() {
|
||||
this.authStore = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate
|
||||
* @method
|
||||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
|
||||
* @param {Pool} pool Connection pool for this topology
|
||||
* @param {string} db Name of the database
|
||||
* @param {string} username Username
|
||||
* @param {string} password Password
|
||||
* @param {authResultCallback} callback The callback to return the result from the authentication
|
||||
* @return {object}
|
||||
*/
|
||||
X509.prototype.auth = function(server, pool, db, username, password, callback) {
|
||||
var self = this;
|
||||
// Get all the connections
|
||||
var connections = pool.getAll();
|
||||
// Total connections
|
||||
var count = connections.length;
|
||||
if(count == 0) return callback(null, null);
|
||||
|
||||
// Valid connections
|
||||
var numberOfValidConnections = 0;
|
||||
var credentialsValid = false;
|
||||
var errorObject = null;
|
||||
|
||||
// For each connection we need to authenticate
|
||||
while(connections.length > 0) {
|
||||
// Execute MongoCR
|
||||
var execute = function(connection) {
|
||||
// Let's start the sasl process
|
||||
var command = {
|
||||
authenticate: 1
|
||||
, mechanism: 'MONGODB-X509'
|
||||
, user: username
|
||||
};
|
||||
|
||||
// Let's start the process
|
||||
server.command("$external.$cmd"
|
||||
, command
|
||||
, { connection: connection }, function(err, r) {
|
||||
// Adjust count
|
||||
count = count - 1;
|
||||
|
||||
// If we have an error
|
||||
if(err) {
|
||||
errorObject = err;
|
||||
} else if(r.result['$err']) {
|
||||
errorObject = r.result;
|
||||
} else if(r.result['errmsg']) {
|
||||
errorObject = r.result;
|
||||
} else {
|
||||
credentialsValid = true;
|
||||
numberOfValidConnections = numberOfValidConnections + 1;
|
||||
}
|
||||
|
||||
// We have authenticated all connections
|
||||
if(count == 0 && numberOfValidConnections > 0) {
|
||||
// Store the auth details
|
||||
addAuthSession(self.authStore, new AuthSession(db, username, password));
|
||||
// Return correct authentication
|
||||
callback(null, true);
|
||||
} else if(count == 0) {
|
||||
if(errorObject == null) errorObject = new MongoError(f("failed to authenticate using mongocr"));
|
||||
callback(errorObject, false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Get the connection
|
||||
execute(connections.shift());
|
||||
}
|
||||
}
|
||||
|
||||
// Add to store only if it does not exist
|
||||
var addAuthSession = function(authStore, session) {
|
||||
var found = false;
|
||||
|
||||
for(var i = 0; i < authStore.length; i++) {
|
||||
if(authStore[i].equal(session)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!found) authStore.push(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* Re authenticate pool
|
||||
* @method
|
||||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
|
||||
* @param {Pool} pool Connection pool for this topology
|
||||
* @param {authResultCallback} callback The callback to return the result from the authentication
|
||||
* @return {object}
|
||||
*/
|
||||
X509.prototype.reauthenticate = function(server, pool, callback) {
|
||||
var count = this.authStore.length;
|
||||
if(count == 0) return callback(null, null);
|
||||
// Iterate over all the auth details stored
|
||||
for(var i = 0; i < this.authStore.length; i++) {
|
||||
this.auth(server, pool, this.authStore[i].db, this.authStore[i].username, this.authStore[i].password, function(err, r) {
|
||||
count = count - 1;
|
||||
// Done re-authenticating
|
||||
if(count == 0) {
|
||||
callback(null, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a result from a authentication strategy
|
||||
*
|
||||
* @callback authResultCallback
|
||||
* @param {error} error An error object. Set to null if no error present
|
||||
* @param {boolean} result The result of the authentication process
|
||||
*/
|
||||
|
||||
module.exports = X509;
|
||||
Reference in New Issue
Block a user