Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
feat(auth): add authentication to handshake process
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Feb 22, 2019
1 parent 8889a53 commit aacac68
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/connection/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ const Connection = require('./connection');
const Query = require('./commands').Query;
const createClientInfo = require('../topologies/shared').createClientInfo;
const MongoError = require('../error').MongoError;
const defaultAuthProviders = require('../auth/defaultAuthProviders').defaultAuthProviders;
let AUTH_PROVIDERS;

function connect(options, callback) {
if (AUTH_PROVIDERS == null) {
AUTH_PROVIDERS = defaultAuthProviders(options.bson);
}

if (options.family !== void 0) {
makeConnection(options.family, options, (err, socket) => {
if (err) {
Expand Down Expand Up @@ -132,6 +138,14 @@ function performInitialHandshake(conn, options, callback) {
// relocated, or at very least restructured.
conn.ismaster = ismaster;
conn.lastIsMasterMS = new Date().getTime() - start;

const credentials = options.credentials;
if (credentials) {
credentials.resolveAuthMechanism(ismaster);
authenticate(conn, credentials, callback);
return;
}

callback(null, conn);
});
}
Expand Down Expand Up @@ -260,6 +274,7 @@ function makeConnection(family, options, callback) {

const CONNECTION_ERROR_EVENTS = ['error', 'close', 'timeout', 'parseError'];
function runCommand(conn, ns, command, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
const socketTimeout = typeof options.socketTimeout === 'number' ? options.socketTimeout : 360000;
const bson = conn.options.bson;
const query = new Query(bson, ns, command, {
Expand Down Expand Up @@ -293,4 +308,18 @@ function runCommand(conn, ns, command, options, callback) {
conn.write(query.toBin());
}

function authenticate(conn, credentials, callback) {
const mechanism = credentials.mechanism;
if (!AUTH_PROVIDERS[mechanism]) {
callback(new MongoError(`authMechanism '${mechanism}' not supported`));
return;
}

const provider = AUTH_PROVIDERS[mechanism];
provider.auth(runCommand, [conn], credentials, () => {
console.log('authed through provider!');
callback(null, conn);
});
}

module.exports = connect;

0 comments on commit aacac68

Please sign in to comment.