diff --git a/aiozk/client.py b/aiozk/client.py index 728b3ab..dcec02b 100644 --- a/aiozk/client.py +++ b/aiozk/client.py @@ -105,6 +105,18 @@ async def exists(self, path, watch=False): return False return True + async def add_auth(self, scheme, auth): + """ + @scheme: world auth digest ip + @auth: according by @scheme + @return: True/False + """ + try: + await self.send(protocol.AuthRequest(type=0, scheme=scheme, auth=auth)) + except Exception: + return False + return True + async def create( self, path, diff --git a/aiozk/protocol/acl.py b/aiozk/protocol/acl.py index f3a751a..abc2f42 100644 --- a/aiozk/protocol/acl.py +++ b/aiozk/protocol/acl.py @@ -1,3 +1,5 @@ +import hashlib +from base64 import b64encode from .request import Request from .response import Response from .part import Part @@ -5,6 +7,13 @@ from .primitives import UString, Int, Vector +def make_digest_acl_credential(username, password): + """Create a SHA1 digest credential""" + credential = username.encode('utf-8') + b":" + password.encode('utf-8') + cred_hash = b64encode(hashlib.sha1(credential).digest()).strip() + return username + ":" + cred_hash.decode('utf-8') + + class ID(Part): """ """ @@ -69,6 +78,10 @@ def set_perms(self, read, write, create, delete, admin): read=True, write=True, create=True, delete=True, admin=True ) +DIGEST_ACCESS = lambda username, password: ACL.make( + scheme="digest", id=make_digest_acl_credential(username, password), + read=True, write=True, create=True, delete=True, admin=True +) class GetACLRequest(Request): """