anonymous login

This commit is contained in:
richard 2013-05-18 01:58:41 +01:00
parent 6e2dc604e9
commit fe7a515241
5 changed files with 31 additions and 9 deletions

View file

@ -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

View file

@ -27,8 +27,11 @@ class Mega(object):
options = {}
self.options = options
def login(self, 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)

View file

@ -1,3 +1,3 @@
requests>=0.10
pycrypto
mega.py>=0.9.16
mega.py

View file

@ -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',

View file

@ -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