Permalink
Browse files

addUser, removeUser, auth, tests for authentication

  • Loading branch information...
1 parent e85fa34 commit 9044ed42fac5552d3159bcd01607a66b1aa41426 @seanhess seanhess committed Apr 17, 2012
Showing with 73 additions and 20 deletions.
  1. +41 −10 lib/mongodb-wrapper.js
  2. +32 −10 test.js
View
@@ -526,7 +526,6 @@ exports.AutoCloseTimeout = 100
var Database = exports.Database = function(host, port, name, prefix, username, password) {
var alreadyDefinedCollections, autoCloser, connection, connectionOpened, emitter, open, reset, self, state
- var authenticated = false
self = this
emitter = null
@@ -593,9 +592,9 @@ var Database = exports.Database = function(host, port, name, prefix, username, p
connection.on('error', function(err) {
if (state == DatabaseConnectionIsOpening) {
- connectionOpened.shouldAutoClose = true
+ connectionOpened.shouldAutoClose = true
connectionOpened(err)
- }
+ }
})
}
reset()
@@ -630,8 +629,13 @@ var Database = exports.Database = function(host, port, name, prefix, username, p
function connectionOpened(err, connection) {
// if they want to authenticate. If we've had an error, just pass it through
- if (!err && username && password)
- connection.authenticate(username, password, notifyListeners)
+ if (!err && username && password) {
+ connection.authenticate(username, password, function(err) {
+ // if there is an error, then this connection is used up
+ if (err) connection.done()
+ notifyListeners(err)
+ })
+ }
else
notifyListeners(err)
@@ -649,11 +653,6 @@ var Database = exports.Database = function(host, port, name, prefix, username, p
currentEmitter.removeAllListeners('opened')
}
- function authenticate(cb) {
- connection.authenticate(username, password, function(err) {
- })
- }
-
// recycle =->
// #emitter.emit 'recycle', connection
// reset()
@@ -692,6 +691,38 @@ var Database = exports.Database = function(host, port, name, prefix, username, p
if (name.match(new RegExp("^" + prefix + "\\."))) return name
return (prefix + "." + name)
}
+
+
+ self.auth = function(username, password, cb) {
+ self.connection(function(err, connection) {
+ if (err) return cb(err)
+ connection.authenticate(username, password, function(err) {
+ connection.done()
+ cb(err)
+ })
+ })
+ }
+
+ self.addUser = function(username, password, cb) {
+
+ self.connection(function(err, connection) {
+ if (err) return cb(err)
+ connection.addUser(username, password, {}, function(err, user) {
+ connection.done()
+ cb(err, user)
+ })
+ })
+ }
+
+ self.removeUser = function(username, cb) {
+ self.connection(function(err, connection) {
+ if (err) return cb(err)
+ connection.removeUser(username, function(err) {
+ connection.done()
+ cb(err)
+ })
+ })
+ }
// cb (err, lastError)
// lastError = {err: null, n: 0, ok: 1}
View
42 test.js
@@ -6,6 +6,37 @@ var assert = require('assert')
// we'll reuse it for each
var mongo = require('./lib/mongodb-wrapper')
+exports.authentication = function(assert) {
+ var db = mongo.db('localhost', 27017, 'test', null, 'baduser', 'badpass')
+ db.collection('mongo.auth')
+
+ // fail bad login
+ db.mongo.auth.save({one:"two"}, function(err, doc) {
+ assert.ok(err, "Authentication should fail")
+
+ var db = mongo.db('localhost', 27017, 'test')
+
+ db.addUser('user', 'pass', function(err) {
+ assert.ifError(err)
+
+ db.auth('user', 'pass', function(err) {
+ assert.ifError(err)
+
+ var db = mongo.db('localhost', 27017, 'test', null, 'user', 'pass')
+ db.collection('mongo.auth')
+ db.mongo.auth.save({one:"two"}, function(err, doc) {
+ assert.ifError(err)
+
+ db.removeUser('user', function(err) {
+ assert.ifError(err)
+ assert.finish()
+ })
+ })
+ })
+ })
+ })
+}
+
exports.basics = function(assert) {
var db = mongo.db("localhost", 27017, "test")
db.collection('mongo.basics')
@@ -768,17 +799,8 @@ exports.backgroundIndex = function(assert) {
}
-exports.authentication = function(assert) {
- var db = mongo.db('localhost', 27017, 'test')
- db.collection('mongo.auth')
-
- db.mongo.auth.save({one:"two"}, function(err, doc) {
- assert.ifError(err)
- assert.finish()
- })
-}
-
+//module.exports = {authentication: exports.authentication}
if (module == require.main) {
require('async_testing').run(__filename, [])

0 comments on commit 9044ed4

Please sign in to comment.