Example #1
0
 def __init__(self):
     self.sso_client = Http()
     self.oauth_client = OAuthClient()
Example #2
0
class Application(object):
    PING_URL = 'https://%s/oauth/sso-finished-so-get-tokens/'
    AUTH_URL = 'https://%s/api/1.0/authentications'

    def __init__(self):
        self.sso_client = Http()
        self.oauth_client = OAuthClient()

    def main(self, options):

        hostname = options.hostname
        ping_url = Application.PING_URL % (options.u1_host, )
        auth_url = Application.AUTH_URL % (options.sso_host, )

        if options.verbose:
            print "Creating new entry for %s" % (hostname)

        username = raw_input('Ubuntu SSO Login: '******'Password: '******'ws.op': 'authenticate',
                'token_name': '"Ubuntu One @ %s"' % (hostname, )}

        qs = urlencode(body)

        url = auth_url + '?' + qs

        if options.verbose:
            print "Using SSO URL: " + url

        resp, content = self.sso_client.request(url)
        if resp['status'] != '200':
            print "ERROR: Login failed"
            print " Server headers:\n" + str(resp)
            print " Server response content:\n" + content
            return

        auth_info = loads(content)
        self.oauth_client.set_consumer(auth_info['consumer_key'],
                                       auth_info['consumer_secret'])

        self.oauth_client.set_token(auth_info['token'],
                                    auth_info['token_secret'])

        if options.format == 'config':
            format_string = "oauth=%s:%s:%s:%s"
        elif options.format == "shell":
            format_string = "export OAUTH_CONSUMER_KEY=%s\n"\
                            "export OAUTH_CONSUMER_SECRET=%s\n" \
                            "export OAUTH_TOKEN=%s\n" \
                            "export OAUTH_TOKEN_SECRET=%s"

        print format_string % (
                    auth_info['consumer_key'], auth_info['consumer_secret'],
                    auth_info['token'], auth_info['token_secret'])

        resp, content = self.oauth_client.request(ping_url)
        if resp['status'] != '200':
            print "ERROR pinging Ubuntu One:"
            print " Server headers:\n" + str(resp)
            print " Server response content:\n" + content
            return

        if options.verbose:
            print 'Token copy result: ' + content