Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

couchDB , python and authentication

Tags:

python

couchdb

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!

like image 466
idiotype Avatar asked Nov 21 '09 19:11

idiotype


4 Answers

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)
like image 70
lysdexia Avatar answered Nov 08 '22 23:11

lysdexia


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.

like image 20
thisfred Avatar answered Nov 09 '22 00:11

thisfred


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

like image 30
fviktor Avatar answered Nov 09 '22 00:11

fviktor


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

http://user:pass@localhost:5984

like image 21
jasonjwwilliams Avatar answered Nov 09 '22 01:11

jasonjwwilliams