Skip to content

Commit efd8918

Browse files
leesperdjc
authored andcommitted
add verify method to Server class
1 parent 0acaf2c commit efd8918

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

couchdb/client.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,40 @@ def logout_user(self, token):
280280
status, _, _ = self.resource.delete_json('_session', headers=header)
281281
return status == 200
282282

283+
def verify_user(self, token_or_name, password=None):
284+
"""Verify user by token or username/password pairs
285+
:param token_or_name: token or username of login user
286+
:param password: password of login user if given
287+
:return: True if authenticated ok
288+
:rtype: bool
289+
"""
290+
def generate_headers(token=None):
291+
if token is None:
292+
headers = {
293+
'Accept': 'application/json',
294+
'Content-Type': 'application/json',
295+
}
296+
else:
297+
headers = {
298+
'Accept': 'application/json',
299+
'Cookie': 'AuthSession=' + token,
300+
}
301+
return headers
302+
303+
try:
304+
if password is None:
305+
header = generate_headers(token_or_name)
306+
status, _, _ = self.resource.get_json('_session', header)
307+
else:
308+
header = generate_headers()
309+
body = {
310+
'name': token_or_name,
311+
'password': password,
312+
}
313+
status, _, _ = self.resource.post_json('_session', body, header)
314+
except http.Unauthorized:
315+
return False
316+
return status == 200
283317

284318

285319
class Database(object):

0 commit comments

Comments
 (0)