You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In some cases, CouchDB can return HTTP 403 errors (for instance with usage of _security and/or validation functions). Here is a fully working example* to demonstrate 2 types of errors 403:
importasynciofromaiocouchimportCouchDBfromaiocouch.documentimportDocumentURL='http://localhost:5984'asyncdefprepare():
asyncwithCouchDB(URL, user='root', password='password') ascouch:
users=awaitcouch['_users']
stranger=Document(users, 'org.couchdb.user:stranger')
stranger.update({'name': 'stranger', 'password': 'pass', 'type': 'user', 'roles': []})
awaitstranger.save()
reader=Document(users, 'org.couchdb.user:reader')
reader.update({'name': 'reader', 'password': 'pass', 'type': 'user', 'roles': []})
awaitreader.save()
writer=Document(users, 'org.couchdb.user:writer')
writer.update({'name': 'writer', 'password': 'pass', 'type': 'user', 'roles': []})
awaitwriter.save()
db=awaitcouch.create('db')
# Only 'reader' and 'writer' will be able to access this base:awaitdb.set_security({'members': {'names': ['reader', 'writer']}})
ddoc=awaitdb.design_doc('_design/write_access')
# Only 'writer' will be able to create or update documents:ddoc.update({
'validate_doc_update': ''' function(newDoc, oldDoc, userCtx, secObj) { if (userCtx.name == 'writer') return; throw {forbidden: 'You are not allowed to write to this database'}; }''',
})
awaitddoc.save()
asyncdeftest():
asyncwithCouchDB(URL, user='stranger', password='pass') ascouch:
db=awaitcouch['db'] # HTTP 403asyncwithCouchDB(URL, user='writer', password='pass') ascouch:
db=awaitcouch['db'] # OKawait(awaitdb.create('doc')).save() # OKprint(awaitdb['doc']) # OKasyncwithCouchDB(URL, user='reader', password='pass') ascouch:
db=awaitcouch['db'] # OKdoc=awaitdb['doc'] # OKdoc["changed"] =Trueawaitdoc.save() # HTTP 403asyncio.get_event_loop().run_until_complete(prepare())
asyncio.get_event_loop().run_until_complete(test())
I'll work on a commit to encapsulate aiohttp.client_exceptions.ClientResponseError(403) (currently raised) in the same manner as UnauthorizedError, BadRequestError, etc.
The text was updated successfully, but these errors were encountered:
In some cases, CouchDB can return HTTP 403 errors (for instance with usage of
_security
and/or validation functions). Here is a fully working example* to demonstrate 2 types of errors 403:(* needs #13)
I'll work on a commit to encapsulate
aiohttp.client_exceptions.ClientResponseError(403)
(currently raised) in the same manner asUnauthorizedError
,BadRequestError
, etc.The text was updated successfully, but these errors were encountered: