Skip to content
This repository was archived by the owner on Jul 22, 2019. It is now read-only.

Commit 9e8bc50

Browse files
committed
add login logout and verify powered by couchdb
1 parent 6ed56f2 commit 9e8bc50

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

couchdb/client.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,53 @@ def register(self, name, password):
231231
"""Register regular user in authentication database.
232232
:param name: name of regular user, normally user id
233233
:param password: password of regular user
234+
:return: (id, rev) tuple of the registered user
235+
:rtype: `tuple`
234236
"""
235237
user_db = self['_users']
236238
doc = {'_id': 'org.couchdb.user:'+name, 'name': name, 'password': password, 'roles': [], 'type': 'user'}
237239
return user_db.save(doc)
238240

241+
def login(self, name, password):
242+
"""Login regular user in couch db
243+
:param name: name of regular user, normally user id
244+
:param password: password of regular user
245+
:return: (status, token) tuple of the login user
246+
:rtype: `tuple`
247+
"""
248+
data = {'name': name, 'password': password}
249+
status, headers, data = self.resource.post_json('_session', data)
250+
if status != 200:
251+
return status, None
252+
cookie = headers.headers[0].split(';')[0]
253+
pos = cookie.find('=')
254+
token = cookie[pos+1:]
255+
return status, token
256+
257+
def logout(self, token):
258+
"""Logout regular user in couch db
259+
:param token: token of login user
260+
:return: True if successfully logout
261+
:rtype: bool
262+
"""
263+
header = {'Accept': 'application/json', 'Cookie': 'AuthSession='+token}
264+
status, headers, data = self.resource.delete_json('_session', headers=header)
265+
if status != 200:
266+
return False
267+
return True
268+
269+
def verify(self, token):
270+
"""Verify whether token is ok
271+
:param token: token of login user
272+
:return: True if token is ok
273+
:rtype: bool
274+
"""
275+
header = {'Accept': 'application/json', 'Cookie': 'AuthSession='+token}
276+
status, headers, data = self.resource.get_json('_session', headers=header)
277+
if status != 200:
278+
return False
279+
return True
280+
239281

240282
class Database(object):
241283
"""Representation of a database on a CouchDB server.

0 commit comments

Comments
 (0)