#! /usr/bin/env python2 ################################################################################ # u1login.py # # Ubuntu one command line login # # Copyright (C) 2012 Paul Barker # # u1login.py is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 2 of the License, or (at your # option) any later version. # # u1login.py is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with duplicity. If not, see . # ################################################################################ import urlparse, httplib, oauth2 import urllib, base64, json, sys import argparse, socket # Check for bad HTTP response def is_bad(r): return r < 200 or r >= 300 def http_request(url, method, body='', headers={}): urlinfo = urlparse.urlparse(url) host = urlinfo.netloc path = urlinfo.path if urlinfo.query: path += '?' + urlinfo.query conn = httplib.HTTPSConnection(host) conn.request(method, path, body, headers) response = conn.getresponse() # Check for error if is_bad(response.status): return response.status, None return response.status, response def oauth_request(data, url, method, body=''): consumer = oauth2.Consumer(data['consumer_key'], data['consumer_secret']) token = oauth2.Token(data['token'], data['token_secret']) oauth_request = oauth2.Request.from_consumer_and_token( consumer, token, method, url) oauth_request.sign_request( oauth2.SignatureMethod_PLAINTEXT(), consumer, token) headers = oauth_request.to_header() return http_request(url, method, body, headers) def login(username=None, password=None, hostname=None, application=None): if not username: username = raw_input('Ubuntu One Username: ') if not password: password = raw_input('Password: ') if not hostname: hostname = socket.gethostname() if not application: application = "u1login.py" url_base = 'https://login.ubuntu.com/api/1.0/authentications?' token_name = 'Ubuntu One @ ' + hostname + ' [' + application + ']' url = url_base + urllib.urlencode({'ws.op':'authenticate', 'token_name':token_name}) headers = {'Accept': 'application/json', 'Authorization': 'Basic %s' % base64.b64encode('%s:%s' % (username, password))} r, content = http_request(url, 'GET', headers=headers) if is_bad(r): print >> sys.stderr, 'Login failed!' return -1 data = json.load(content) # Tell Ubuntu One we now have a token url = 'https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/' r, content = oauth_request(data, url, 'GET') if is_bad(r): print >> sys.stderr, 'Login failed!' return -1 else: print 'U1_CONSUMER_KEY=' + data['consumer_key'] print 'U1_CONSUMER_SECRET=' + data['consumer_secret'] print 'U1_TOKEN=' + data['token'] print 'U1_TOKEN_SECRET=' + data['token_secret'] return 0 parser = argparse.ArgumentParser(prog='u1login') parser.add_argument('user', nargs='?', default='') parser.add_argument('-p', '--password', default='') parser.add_argument('-n', '--hostname', default='') parser.add_argument('-a', '--application', default='') args = parser.parse_args() login(args.user, args.password, args.hostname, args.application)