11

I have installed couchDB v 0.10.0, and am attempting to talk to it via python from Couch class downloaded from couchDB wiki. Problem is:

Create database 'mydb': {'error': 'unauthorized', 'reason': 'You are not a server admin.'}

I hand edited the local.ini file to include my standard osx login and password. I now have full access via futon but no joy WRT python. Is this an http header issue?

At a a loss - thanks!

idiotype
  • 201
  • 3
  • 10
  • Which python library are you using? There are several listed on the python wiki page (http://wiki.apache.org/couchdb/Getting_started_with_Python) – andyuk Nov 21 '09 at 19:26
  • thanks andyuk - I am using EXAMPLE code that is on wiki page since it is simplest. I am also trying to find couchDB doc on disabling auth (locally) – idiotype Nov 21 '09 at 19:30

6 Answers6

23

To concour David's reply, (i.e. "This is how I do it using module CouchDB 0.8 in python 2.6 with couchdb 1.0.2")

couch = couchdb.Server(couch_server)

couch.resource.credentials = (USERNAME, PASSWORD)
lysdexia
  • 1,786
  • 18
  • 29
  • worked for me in a similar situation...once I remembered to put my username and password in quotes! It's kind of implied in lysdexia's answer here but I mention it here just in case someone else is missing that at the end of a longish day. Unless of course you have actually defined them elsewhere and are just using variable names here. – rossdavidh Apr 26 '11 at 02:31
  • Just wanted to confirm that I was able to use this on the library version 1.0, running on debian wheezy against CouchDB 1.6.1, where the admin password was set in local.ini under [admins]. I assume it will work with normal users, too. – Fran K. Mar 31 '15 at 21:08
6

You can also do:

db = couchdb.Database("http://your.url/yourdb")
db.resource.http.add_credentials(username, password)

after which all your requests should work.

thisfred
  • 211
  • 1
  • 4
  • 4
    Doesn't appear to be a .http property in resource, but there is couch.resource.credentials. So I did couch.resource.credentials = (SERVER_USER, SERVER_PASSWD) which works for me. – David Dec 19 '10 at 08:41
5

The Couch class in the example does not pass any authentication information to the database, so it is not a miracle that it does not allow privileged operations. So your only options are:

  • disable authentication completely (as you mentioned)
  • pass the user name and password as part of the URI
  • pass the user name and password as an Authorization HTTP request header

If you want to pass a user name and a password, then you will need to change the Couch class. Sending an Authorization HTTP request header is easier, since the Couch class uses the httplib.HTTPConnection class. You can add such a header next to the Accept one this way:

headers = {
    "Accept": "application/json",
    "Authorization": "Basic " + 'username:password'.encode('base64')[:-1]}

Same for the other HTTP request methods.

The documentation on the basic authentication is here:

http://books.couchdb.org/relax/reference/security

fviktor
  • 2,861
  • 20
  • 24
  • Documentation on the security features of CouchDB: http://wiki.apache.org/couchdb/Security_Features_Overview – fviktor Nov 22 '09 at 02:57
3

Just pass it as part of the URI...python-couchdb will parse the user/pass out and use them:

http://user:pass@localhost:5984

jasonjwwilliams
  • 2,541
  • 3
  • 19
  • 14
1

Above are all nice; but I've found that for oauth validation methods versus basic auth, this works really well:

from couchdb import Server, Session
auth = Session()
auth.name = USERNAME
auth.password = PASSWORD
s = Server('http://localhost:5984/', session=auth)
db = s['dbname']

Note: This will not work with basic authentication; in such a case, fviktor has what I consider to be the best answer. You might also look into the security reference material he linked to if you're interested in persistent auth sessions.

0

There are several patches for python-couchdb that enable authentication. The code probably will be included in Version 0.7 but until then you can usr teh fork at http://github.com/mdornseif/couchdb-python - it allows you to use http://user:pass@127.0.0.1:5984/ type URLs.

http://blogs.23.nu/c0re/2009/12/running-a-couchdb-cluster-on-amazon-ec2/ (at the bottom) shows how to use CouchDB passwords.

max
  • 29,122
  • 12
  • 52
  • 79