Skip to content

Loading…

Mongo authentication and client #131

Merged
merged 2 commits into from

2 participants

@dgreisen

Hello,

I added the ability to connect to a password-protected mongo database.

I also added the ability to pass in a mongodb.Db instance in case people need the flexibility.

dgreisen added some commits
@dgreisen dgreisen Added support for Mongo auth and passing in client
can now pass `user` and `password` in options to authenticate to
mongo database.

can now pass `client` in options, where `client` is a mongodb.Db
instance, and mongo driver will use that client rather than generating
one from the options.
8489cbc
@dgreisen dgreisen authentication now occurs after client properly opened 69ffca2
@wmertens

Looks good, thanks!

@wmertens wmertens merged commit 02564ee into share:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 19, 2012
  1. @dgreisen

    Added support for Mongo auth and passing in client

    dgreisen committed
    can now pass `user` and `password` in options to authenticate to
    mongo database.
    
    can now pass `client` in options, where `client` is a mongodb.Db
    instance, and mongo driver will use that client rather than generating
    one from the options.
  2. @dgreisen
Showing with 10 additions and 2 deletions.
  1. +10 −2 src/server/db/mongo.coffee
View
12 src/server/db/mongo.coffee
@@ -19,7 +19,9 @@ defaultOptions =
hostname: '127.0.0.1'
port: 27017
mongoOptions: {auto_reconnect: true}
-
+ client: null # an instance of mongodb.Db
+ user: null # an optional username for authentication
+ password: null # an optional password for authentication
# Valid options as above.
module.exports = MongoDb = (options) ->
@@ -27,7 +29,13 @@ module.exports = MongoDb = (options) ->
options ?= {}
options[k] ?= v for k, v of defaultOptions
- client = new mongodb.Db(options.db, new mongodb.Server(options.hostname, options.port, options.mongoOptions))
+ client = options.client or new mongodb.Db(options.db, new mongodb.Server(options.hostname, options.port, options.mongoOptions))
+
+ if options.user and options.password
+ client.open (err, db) ->
+ if not err
+ client = db
+ client.authenticate(options.user, options.password)
opsCollectionForDoc = (docName) -> 'ops.' + encodeURIComponent(docName).replace(/\./g, '%2E').replace(/-/g, '%2D')
Something went wrong with that request. Please try again.