Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add authentication to MongoDBBackend #59

Open
swill opened this issue Jan 4, 2014 · 4 comments
Open

Add authentication to MongoDBBackend #59

swill opened this issue Jan 4, 2014 · 4 comments

Comments

@swill
Copy link

swill commented Jan 4, 2014

MongoDBBackend is currently not usable by me because I use mongolab.com and they require that the 'pymongo.Connection' be authenticated.

Would love to use this functionality.

@swill swill mentioned this issue Jan 4, 2014
@swill
Copy link
Author

swill commented Jan 4, 2014

I was able to get this to work as needed by hacking the mongodb_backend.py file and changing it to this:

# added optional 'username' and 'password' in constructor
class MongoDBBackend(Backend):
    def __init__(self, db_name='cork', hostname='localhost', port=27017, initialize=False, username=None, password=None):
        """Initialize MongoDB Backend"""
        connection = MongoClient(host=hostname, port=port)
        db = connection[db_name]
        # if username and password are present than authenticate...
        if username and password:
            db.authenticate(username, password)
        self.users = MongoMultiValueTable('users', 'login', db.users)
        self.pending_registrations = MongoMultiValueTable(
            'pending_registrations',
            'pending_registration',
            db.pending_registrations
        )
        self.roles = MongoSingleValueTable('roles', 'role', db.roles)

        if initialize:
            self._initialize_storage()

@FedericoCeratto
Copy link
Owner

MongoDB supports URIs in the "host" parameter as in:
http://api.mongodb.org/python/current/examples/authentication.html

Given that the "hostname" parameter in MongoDBBackend is simply passed to MongoClient as "host", you should be already able to perform authentication.

Example:
uri = "mongodb://admin:admin@localhost/"
mb = MongoDBBackend(hostname=uri)

Does that work for you? Certainly "hostname" is a misleading name for an URI, I'll update it.

@swill
Copy link
Author

swill commented Jan 6, 2014

That does not work. That results in the following:

Traceback (most recent call last):
File "server.py", line 94, in
port=auth_db_port
File "build/bdist.macosx-10.9-intel/egg/cork/mongodb_backend.py", line 141, in init
File "/Library/Python/2.7/site-packages/pymongo/mongo_client.py", line 369, in init
raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: command SON([('authenticate', 1), ('user', u'user_admin'), ('nonce', u'2cbbb43173e169de'), ('key', u'4b8df11693f429caa4e3cd506e241470')]) failed: auth fails

@swill
Copy link
Author

swill commented Jan 7, 2014

I will just stick with my two line modification for now because it works perfectly and has no other impact on the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants