~ubuntu-branches/ubuntu/trusty/desktopcouch/trusty

« back to all changes in this revision

Viewing changes to desktopcouch/replication_services/ubuntuone.py

  • Committer: Ken VanDine
  • Date: 2010-09-28 15:53:39 UTC
  • mfrom: (30.1.1 d-0.6.9)
  • Revision ID: ken.vandine@canonical.com-20100928155339-acv35l2m5py9o5r6
Tags: 0.6.9-0ubuntu1
releasing version 0.6.9-0ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from oauth import oauth
2
2
import logging
3
3
import httplib2
4
 
import gnomekeyring
 
4
 
 
5
import dbus
 
6
import ubuntu_sso
5
7
 
6
8
try:
7
9
    # Python 2.5
14
16
name = "Ubuntu One"
15
17
description = "The Ubuntu One cloud service"
16
18
 
17
 
oauth_consumer_key = "ubuntuone"
18
 
oauth_consumer_secret = "hammertime"
19
19
 
20
20
def in_main_thread(function, *args, **kwargs):
21
21
    from twisted.python.threadable import isInIOThread
37
37
    if oauth_data is not None:
38
38
        return oauth_data
39
39
 
40
 
    try:
41
 
        matches = in_main_thread(gnomekeyring.find_items_sync,
42
 
            gnomekeyring.ITEM_GENERIC_SECRET,
43
 
            {'ubuntuone-realm': "https://ubuntuone.com",
44
 
             'oauth-consumer-key': oauth_consumer_key})
45
 
        if matches:
46
 
            # parse "a=b&c=d" to {"a":"b","c":"d"}
47
 
            kv_list = [x.split("=", 1) for x in matches[0].secret.split("&")]
48
 
            keys, values = zip(*kv_list)
49
 
            keys = [k.replace("oauth_", "") for k in keys]
50
 
            oauth_data = dict(zip(keys, values))
51
 
            oauth_data.update({
52
 
                "consumer_key": oauth_consumer_key,
53
 
                "consumer_secret": oauth_consumer_secret,
54
 
            })
55
 
            return oauth_data
56
 
    except ImportError, e:
57
 
        logging.info("Can't replicate to Ubuntu One cloud without credentials."
58
 
                " %s", e)
59
 
    except gnomekeyring.NoMatchError:
60
 
        logging.info("This machine hasn't authorized itself to Ubuntu One; "
61
 
                "replication to the cloud isn't possible until it has.  See "
62
 
                "'ubuntuone-client-applet'.")
63
 
    except gnomekeyring.NoKeyringDaemonError:
64
 
        logging.error("No keyring daemon found in this session, so we have "
65
 
                "no access to Ubuntu One data.")
66
 
    except gnomekeyring.CancelledError:
67
 
        logging.error("User cancelled the keyring dialog. We have no access.")
 
40
    bus = dbus.SessionBus()
 
41
    proxy = bus.get_object(ubuntu_sso.DBUS_BUS_NAME,
 
42
                           ubuntu_sso.DBUS_CRED_PATH,
 
43
                           follow_name_owner_changes=True)
 
44
    logging.info('get_oauth_data: asking for credentials to Ubuntu SSO. '
 
45
                 'App name: %s', name)
 
46
    oauth_data = proxy.find_credentials(name)
 
47
    logging.info('Got credentials from Ubuntu SSO. Non emtpy credentials? %s',
 
48
                 len(oauth_data) > 0)
 
49
    return oauth_data
68
50
 
69
 
def get_oauth_token(consumer):
70
 
    """Get the token from the keyring"""
71
 
    try:
72
 
        items = in_main_thread(gnomekeyring.find_items_sync,
73
 
            gnomekeyring.ITEM_GENERIC_SECRET,
74
 
            {'ubuntuone-realm': "https://one.ubuntu.com",
75
 
             'oauth-consumer-key': consumer.key})
76
 
    except gnomekeyring.NoMatchError:
77
 
        logging.info("No o.u.c key.  Maybe there's uo.c key?")
78
 
        items = in_main_thread(gnomekeyring.find_items_sync,
79
 
            gnomekeyring.ITEM_GENERIC_SECRET,
80
 
            {'ubuntuone-realm': "https://ubuntuone.com",
81
 
             'oauth-consumer-key': consumer.key})
82
 
    if len(items):
83
 
        return oauth.OAuthToken.from_string(items[0].secret)
84
51
 
85
52
def get_oauth_request_header(consumer, access_token, http_url):
86
53
    """Get an oauth request header given the token and the url"""
107
74
 
108
75
        url = "https://one.ubuntu.com/api/account/"
109
76
        if self.oauth_header is None:
110
 
            consumer = oauth.OAuthConsumer(oauth_consumer_key,
111
 
                    oauth_consumer_secret)
112
77
            try:
113
 
                access_token = get_oauth_token(consumer)
114
 
            except gnomekeyring.NoKeyringDaemonError:
115
 
                logging.info("No keyring daemon is running for this session.")
116
 
                raise ValueError("No keyring access")
117
 
            if not access_token:
118
 
                logging.info("Could not get access token from keyring")
119
 
                raise ValueError("No keyring access")
120
 
            self.oauth_header = get_oauth_request_header(
 
78
                get_oauth_data()
 
79
                consumer = oauth.OAuthConsumer(oauth_data['consumer_key'],
 
80
                                               oauth_data['consumer_secret'])
 
81
                access_token = oauth.OAuthToken(oauth_data['token'],
 
82
                                                oauth_data['token_secret'])
 
83
            except:
 
84
                logging.exception("Could not get access token from sso.")
 
85
                raise ValueError("No access token.")
 
86
 
 
87
        self.oauth_header = get_oauth_request_header(
121
88
                consumer, access_token, url)
122
89
 
123
90
        client = httplib2.Http()