Permalink
Browse files

Optionally authenticate the connection

  • Loading branch information...
1 parent e5a8920 commit f3101c16235bcfeb173f2dcbd69e7a43ffe26d31 @scttnlsn committed Feb 7, 2012
Showing with 18 additions and 7 deletions.
  1. +16 −2 lib/connection.js
  2. +2 −5 test/store_test.js
View
@@ -15,6 +15,12 @@ function Connection(options) {
this.options.host = uri.hostname;
this.options.port = parseInt(uri.port, 10);
this.options.db = uri.pathname && uri.pathname.replace(/\//g, '');
+
+ if (uri.auth) {
+ var auth = uri.auth.split(':');
+ this.options.user = auth[0];
+ this.options.pass = auth[1];
+ }
} else {
this.options = options || {};
this.options.host || (this.options.host = 'localhost');
@@ -54,7 +60,15 @@ Connection.prototype.open = function(callback) {
var db = new mongo.Db(this.options.db, server);
db.open(function(err, db) {
- self.db.resolve(err, db);
- if (callback) callback(err, db);
+ var opened = function() {
+ self.db.resolve(err, db);
+ if (callback) callback(err, db);
+ };
+
+ if (self.options.user && self.options.pass) {
+ db.authenticate(self.options.user, self.options.pass, opened)
+ } else {
+ opened();
+ }
});
};
View
@@ -3,11 +3,8 @@ var async = require('async');
var mongo = require('mongodb');
var nettle = require('../lib/index');
-var store = nettle.store({
- host: process.env.NETTLE_TEST_HOST || 'localhost',
- port: process.env.NETTLE_TEST_PORT || 27017,
- db: process.env.NETTLE_TEST_DB || 'nettle_tests'
-});
+var db = process.env.NETTLE_TEST_DB || 'mongodb://test:test@localhost:27017/nettle_tests';
+var store = nettle.store(db);
describe('Store', function() {
var id;

0 comments on commit f3101c1

Please sign in to comment.