From fe7a515241b42127ad6aa8ef10068c8332c6f321 Mon Sep 17 00:00:00 2001 From: richard Date: Sat, 18 May 2013 01:58:41 +0100 Subject: [PATCH] anonymous login --- README.md | 2 ++ mega/mega.py | 25 +++++++++++++++++++++++-- requirements.txt | 2 +- setup.py | 2 +- tests/unit-tests.py | 9 ++++----- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index dc95d66..0243168 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,8 @@ mega = Mega({'verbose': True}) ### Login to Mega ```python m = mega.login(email, password) +# login using a temporary anonymous account +m = mega.login() ``` ### Get user details ```python diff --git a/mega/mega.py b/mega/mega.py index eb808ed..9161dd6 100644 --- a/mega/mega.py +++ b/mega/mega.py @@ -27,8 +27,11 @@ class Mega(object): options = {} self.options = options - def login(self, email, password): - self.login_user(email, password) + def login(self, email=None, password=None): + if email: + self.login_user(email, password) + else: + self.login_anonymous() return self def login_user(self, email, password): @@ -40,6 +43,24 @@ class Mega(object): raise RequestError(resp) self._login_process(resp, password_aes) + def login_anonymous(self): + master_key = [random.randint(0, 0xFFFFFFFF)] * 4 + password_key = [random.randint(0, 0xFFFFFFFF)] * 4 + session_self_challenge = [random.randint(0, 0xFFFFFFFF)] * 4 + + user = self.api_request({ + 'a': 'up', + 'k': a32_to_base64(encrypt_key(master_key, password_key)), + 'ts': base64_url_encode(a32_to_str(session_self_challenge) + + a32_to_str(encrypt_key(session_self_challenge, master_key))) + }) + + resp = self.api_request({'a': 'us', 'user': user}) + #if numeric error code response + if isinstance(resp, int): + raise RequestError(resp) + self._login_process(resp, password_key) + def _login_process(self, resp, password): encrypted_master_key = base64_to_a32(resp['k']) self.master_key = decrypt_key(encrypted_master_key, password) diff --git a/requirements.txt b/requirements.txt index bc2c6d7..26edc79 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ requests>=0.10 pycrypto -mega.py>=0.9.16 +mega.py diff --git a/setup.py b/setup.py index bb43279..205ce0e 100644 --- a/setup.py +++ b/setup.py @@ -30,7 +30,7 @@ def get_package_data(package): setup( name='mega.py', - version='0.9.16', + version='0.9.17', packages=get_packages('mega'), package_data=get_package_data('mega'), description='Python lib for the Mega.co.nz API', diff --git a/tests/unit-tests.py b/tests/unit-tests.py index f101f7b..900cc29 100644 --- a/tests/unit-tests.py +++ b/tests/unit-tests.py @@ -12,7 +12,10 @@ email = 'your@email.com' password = 'password' mega = Mega() -m = mega.login(email, password) +# anonymous login +m = mega.login() +# normal login +#m = mega.login(email, password) FIND_RESP = None TEST_CONTACT = 'test@mega.co.nz' @@ -86,16 +89,12 @@ class TestMega(unittest.TestCase): if file: resp = m.delete(file[0]) self.assertTrue(isinstance(resp, int)) - else: - raise ValueError('file not found') def test_destroy(self): file = m.find(TEST_FILE) if file: resp = m.destroy(file[0]) self.assertTrue(isinstance(resp, int)) - else: - raise ValueError('file not found') def test_empty_trash(self): #resp None if already empty, else int