~mvo/software-center/lp969050

« back to all changes in this revision

Viewing changes to utils/piston-helpers/piston_generic_helper.py

  • Committer: Gary Lasker
  • Date: 2012-03-29 00:02:04 UTC
  • mfrom: (2926.2.3 fix-clear-token)
  • Revision ID: gary.lasker@canonical.com-20120329000204-eq4bwvg9uk6b26ns
* lp:~mvo/software-center/lp966514:
  - properly handle network disconnect conditions with the Ubuntu
    SSO dialog (LP: #966514)

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
    httplib2.debuglevel = 1
33
33
 
34
34
import piston_mini_client.auth
 
35
import piston_mini_client.failhandlers
35
36
from piston_mini_client.failhandlers import APIError
36
37
 
37
38
try:
93
94
        self.loop.quit()
94
95
 
95
96
    def verify_token_sync(self, token):
 
97
        """ Verify that the token is valid
 
98
        
 
99
            Note that this may raise httplib2 exceptions if the server
 
100
            is not reachable
 
101
        """
96
102
        LOG.debug("verify_token")
97
103
        auth = piston_mini_client.auth.OAuthAuthorizer(
98
104
            token["token"], token["token_secret"],
100
106
        api = UbuntuSsoAPI(auth=auth)
101
107
        try:
102
108
            res = api.whoami()
103
 
        except:
104
 
            LOG.exception("api.whoami failed")
105
 
            return None
106
 
        return res
 
109
        except piston_mini_client.failhandlers.APIError as e:
 
110
            LOG.exception("api.whoami failed with APIError: '%s'" % e)
 
111
            return False
 
112
        return len(res) > 0
107
113
 
108
114
    def clear_token(self):
109
115
        clear_token_from_ubuntu_sso(SOFTWARE_CENTER_NAME_KEYRING)
124
130
    def get_oauth_token_and_verify_sync(self):
125
131
        token = self.get_oauth_token_sync()
126
132
        # check if the token is valid and reset it if it is not
127
 
        if token and not self.verify_token_sync(token):
128
 
            self.clear_token()
129
 
            # re-trigger login
130
 
            token = self.get_oauth_token_sync()
 
133
        if token:
 
134
            # verify token will return false if there is a API error, 
 
135
            # but there maybe httplib2 errors if there is no network,
 
136
            # so ignore them
 
137
            try:
 
138
                if not self.verify_token_sync(token):
 
139
                    self.clear_token()
 
140
                    # re-trigger login once
 
141
                    token = self.get_oauth_token_sync()
 
142
            except Exception as e:
 
143
                LOG.warn(
 
144
                    "token could not be verified (network problem?): %s" % e)
131
145
        return token
132
146
 
133
147