|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import argparse, base64, getpass, oauth2, platform, urllib, urllib2 |
| 4 | + |
| 5 | +class AuthenticationFailure(Exception): |
| 6 | + """The provided email address and password were incorrect.""" |
| 7 | + def __init__(self, what): |
| 8 | + self.what = "Authentication failure: " + what |
| 9 | + |
| 10 | +class U1Driver: |
| 11 | + BASE_API_PATH = "/api/file_storage/v1" |
| 12 | + CREDENTIALS = '.credentials' |
| 13 | + |
| 14 | + |
| 15 | + def __init__(self, resource_host="https://edge.one.ubuntu.com", content_host="https://files.one.ubuntu.com"): |
| 16 | + self.email = '' |
| 17 | + self.passwd = '' |
| 18 | + self.resource_host = resource_host |
| 19 | + self.content_host = content_host |
| 20 | + |
| 21 | + def sign_request(url): |
| 22 | + oauth_request = oauth2.Request.from_consumer_and_token(self.consumer, self.token, 'GET', url) |
| 23 | + oauth_request.sign_request(oauth2.SignatureMethod_PLAINTEXT(), self.consumer, self.token) |
| 24 | + request = urllib2.Request(url) |
| 25 | + for header, value in oauth_request.to_header().items(): |
| 26 | + request.add_header(header, value) |
| 27 | + return request |
| 28 | + |
| 29 | + def oauth_credentials(self, data): |
| 30 | + consumer = oauth2.Consumer(data['consumer_key'], data['consumer_secret']) |
| 31 | + token = oauth2.Token(data['token'], data['token_secret']) |
| 32 | + return consumer, token |
| 33 | + |
| 34 | + def authenticate(self): |
| 35 | + #get hostname |
| 36 | + description = 'Ubuntu One @ %s' % platform.node() |
| 37 | + """Aquire an OAuth access token for the given user.""" |
| 38 | + # Issue a new access token for the user. |
| 39 | + request = urllib2.Request('https://login.ubuntu.com/api/1.0/authentications?' + |
| 40 | + urllib.urlencode({'ws.op': 'authenticate', 'token_name': description})) |
| 41 | + request.add_header('Accept', 'application/json') |
| 42 | + request.add_header('Authorization', 'Basic %s' % base64.b64encode('%s:%s' % (self.email, self.passwd))) |
| 43 | + try: |
| 44 | + response = urllib2.urlopen(request) |
| 45 | + except urllib2.HTTPError, exc: |
| 46 | + if exc.code == 401: # Unauthorized |
| 47 | + raise AuthenticationFailure("Bad email address or password") |
| 48 | + else: |
| 49 | + raise |
| 50 | + data = json.load(response) |
| 51 | + self.consumer, self.token = oauth_credentials(data) |
| 52 | + # Tell Ubuntu One about the new token. |
| 53 | + get_tokens_url = ('https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/') |
| 54 | + request = sign_request(self.consumer, self.token, get_tokens_url) |
| 55 | + response = urllib2.urlopen(request) |
| 56 | + print response.headers |
| 57 | + print response.read() |
| 58 | + print 'Success' |
| 59 | + return data |
| 60 | + |
| 61 | + def login(self): |
| 62 | + pass |
| 63 | + |
| 64 | + def logout(self): |
| 65 | + pass |
| 66 | + |
| 67 | + def get(self, file_path): |
| 68 | + pass |
| 69 | + |
| 70 | + def put(self, file_path): |
| 71 | + pass |
| 72 | + |
| 73 | + def delete(self, file_path): |
| 74 | + pass |
| 75 | + |
| 76 | + def list_files(self): |
| 77 | + pass |
| 78 | + |
| 79 | +def main(): |
| 80 | + driver = U1Driver() |
| 81 | + |
| 82 | + parser = argparse.ArgumentParser(description="Test Onitu Driver for Ubuntu One", epilog="2013 Onitu") |
| 83 | + parser.add_argument('-p', action='store_true', default=False, dest='prompt', help='Prompts user identification') |
| 84 | + args = parser.parse_args() |
| 85 | + if args.prompt: |
| 86 | + driver.email = raw_input("Ubuntu One email: ") |
| 87 | + driver.passwd = getpass.getpass("Ubuntu One Password for " + driver.email + ": ") |
| 88 | + print driver.email, driver.passwd |
| 89 | + try: |
| 90 | + driver.authenticate() |
| 91 | + except AuthenticationFailure as af: |
| 92 | + print af.what |
| 93 | + else: |
| 94 | + print "Successfully authenticated" |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + main() |
0 commit comments