Merge lp:~mvo/software-center/lp966514 into lp:software-center

Proposed by Michael Vogt
Status: Merged
Merged at revision: 2928
Proposed branch: lp:~mvo/software-center/lp966514
Merge into: lp:software-center
Diff against target: 149 lines (+22/-93)
2 files modified
softwarecenter/backend/piston/sso_helper.py (+0/-85)
utils/piston-helpers/piston_generic_helper.py (+22/-8)
To merge this branch: bzr merge lp:~mvo/software-center/lp966514
Reviewer Review Type Date Requested Status
Gary Lasker (community) Approve
Review via email: mp+99716@code.launchpad.net

Description of the change

This fixes bug #966514. To verify:
- open software-center
- click on reinstall-previous-purchases to ensure you are logged in
- close software-center
- stop the network connecion (e.g. via network manager or ifconfig down)
- click on reinstall-previous-purchases again
- verify that no "sign-in" dialog appears
(doing the same in trunk will trigger sign-in dialogs)

To post a comment you must log in.
Revision history for this message
Gary Lasker (gary-lasker) wrote :

Nice fix!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== removed file 'softwarecenter/backend/piston/sso_helper.py'
2--- softwarecenter/backend/piston/sso_helper.py 2012-03-16 20:12:57 +0000
3+++ softwarecenter/backend/piston/sso_helper.py 1970-01-01 00:00:00 +0000
4@@ -1,85 +0,0 @@
5-#!/usr/bin/python
6-# Copyright (C) 2012 Canonical
7-#
8-# Authors:
9-# Michael Vogt
10-#
11-# This program is free software; you can redistribute it and/or modify it under
12-# the terms of the GNU General Public License as published by the Free Software
13-# Foundation; version 3.
14-#
15-# This program is distributed in the hope that it will be useful, but WITHOUT
16-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17-# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18-# details.
19-#
20-# You should have received a copy of the GNU General Public License along with
21-# this program; if not, write to the Free Software Foundation, Inc.,
22-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23-
24-from gi.repository import GObject
25-from gettext import gettext as _
26-
27-from softwarecenter.backend.login_sso import get_sso_backend
28-from softwarecenter.backend.ubuntusso import UbuntuSSOAPI
29-from softwarecenter.enums import (SOFTWARE_CENTER_NAME_KEYRING,
30- SOFTWARE_CENTER_SSO_DESCRIPTION,
31- )
32-from softwarecenter.utils import clear_token_from_ubuntu_sso
33-
34-
35-class SSOLoginHelper(object):
36- def __init__(self, xid=0):
37- self.oauth = None
38- self.xid = xid
39- self.loop = GObject.MainLoop(GObject.main_context_default())
40-
41- def _login_successful(self, sso_backend, oauth_result):
42- self.oauth = oauth_result
43- # FIXME: actually verify the token against ubuntu SSO
44- self.loop.quit()
45-
46- def verify_token(self, token):
47- def _whoami_done(sso, me):
48- self._whoami = me
49- self.loop.quit()
50-
51- def _whoami_error(sso, err):
52- #print "ERRR", err
53- self.loop.quit()
54- self._whoami = None
55- sso = UbuntuSSOAPI()
56- sso.connect("whoami", _whoami_done)
57- sso.connect("error", _whoami_error)
58- sso.whoami()
59- self.loop.run()
60- # check if the token is valid
61- if self._whoami is None:
62- return False
63- else:
64- return True
65-
66- def clear_token(self):
67- clear_token_from_ubuntu_sso(SOFTWARE_CENTER_NAME_KEYRING)
68-
69- def get_oauth_token_and_verify_sync(self):
70- token = self.get_oauth_token_sync()
71- # check if the token is valid and reset it if it is not
72- if token and not self.verify_token(token):
73- self.clear_token()
74- # re-trigger login
75- token = self.get_oauth_token_sync()
76- return token
77-
78- def get_oauth_token_sync(self):
79- self.oauth = None
80- sso = get_sso_backend(
81- self.xid,
82- SOFTWARE_CENTER_NAME_KEYRING,
83- _(SOFTWARE_CENTER_SSO_DESCRIPTION))
84- sso.connect("login-successful", self._login_successful)
85- sso.connect("login-failed", lambda s: self.loop.quit())
86- sso.connect("login-canceled", lambda s: self.loop.quit())
87- sso.login_or_register()
88- self.loop.run()
89- return self.oauth
90
91=== modified file 'utils/piston-helpers/piston_generic_helper.py'
92--- utils/piston-helpers/piston_generic_helper.py 2012-03-15 10:43:13 +0000
93+++ utils/piston-helpers/piston_generic_helper.py 2012-03-28 12:45:48 +0000
94@@ -32,6 +32,7 @@
95 httplib2.debuglevel = 1
96
97 import piston_mini_client.auth
98+import piston_mini_client.failhandlers
99 from piston_mini_client.failhandlers import APIError
100
101 try:
102@@ -93,6 +94,11 @@
103 self.loop.quit()
104
105 def verify_token_sync(self, token):
106+ """ Verify that the token is valid
107+
108+ Note that this may raise httplib2 exceptions if the server
109+ is not reachable
110+ """
111 LOG.debug("verify_token")
112 auth = piston_mini_client.auth.OAuthAuthorizer(
113 token["token"], token["token_secret"],
114@@ -100,10 +106,10 @@
115 api = UbuntuSsoAPI(auth=auth)
116 try:
117 res = api.whoami()
118- except:
119- LOG.exception("api.whoami failed")
120- return None
121- return res
122+ except piston_mini_client.failhandlers.APIError as e:
123+ LOG.exception("api.whoami failed with APIError: '%s'" % e)
124+ return False
125+ return len(res) > 0
126
127 def clear_token(self):
128 clear_token_from_ubuntu_sso(SOFTWARE_CENTER_NAME_KEYRING)
129@@ -124,10 +130,18 @@
130 def get_oauth_token_and_verify_sync(self):
131 token = self.get_oauth_token_sync()
132 # check if the token is valid and reset it if it is not
133- if token and not self.verify_token_sync(token):
134- self.clear_token()
135- # re-trigger login
136- token = self.get_oauth_token_sync()
137+ if token:
138+ # verify token will return false if there is a API error,
139+ # but there maybe httplib2 errors if there is no network,
140+ # so ignore them
141+ try:
142+ if not self.verify_token_sync(token):
143+ self.clear_token()
144+ # re-trigger login once
145+ token = self.get_oauth_token_sync()
146+ except Exception as e:
147+ LOG.warn(
148+ "token could not be verified (network problem?): %s" % e)
149 return token
150
151

Subscribers

People subscribed via source and target branches