37
37
if oauth_data is not None:
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})
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))
52
"consumer_key": oauth_consumer_key,
53
"consumer_secret": oauth_consumer_secret,
56
except ImportError, e:
57
logging.info("Can't replicate to Ubuntu One cloud without credentials."
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. '
46
oauth_data = proxy.find_credentials(name)
47
logging.info('Got credentials from Ubuntu SSO. Non emtpy credentials? %s',
69
def get_oauth_token(consumer):
70
"""Get the token from the keyring"""
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})
83
return oauth.OAuthToken.from_string(items[0].secret)
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"""
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)
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")
118
logging.info("Could not get access token from keyring")
119
raise ValueError("No keyring access")
120
self.oauth_header = get_oauth_request_header(
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'])
84
logging.exception("Could not get access token from sso.")
85
raise ValueError("No access token.")
87
self.oauth_header = get_oauth_request_header(
121
88
consumer, access_token, url)
123
90
client = httplib2.Http()