~elachuni/software-center/test_downloader_fix

« back to all changes in this revision

Viewing changes to softwarecenter/backend/piston/sso_helper.py

  • Committer: Michael Vogt
  • Date: 2012-01-06 09:10:27 UTC
  • mfrom: (2635.2.5 software-center)
  • Revision ID: michael.vogt@ubuntu.com-20120106091027-8b6kq5a8h4yp8evb
lp:~gary-lasker/software-center/recommends-tweaks: merged

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# Copyright (C) 2012 Canonical
 
3
#
 
4
# Authors:
 
5
#  Michael Vogt
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify it under
 
8
# the terms of the GNU General Public License as published by the Free Software
 
9
# Foundation; version 3.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but WITHOUT
 
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
13
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
14
# details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along with
 
17
# this program; if not, write to the Free Software Foundation, Inc.,
 
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
19
 
 
20
from gi.repository import GObject
 
21
from gettext import gettext as _
 
22
 
 
23
from softwarecenter.backend.login_sso import get_sso_backend
 
24
from softwarecenter.backend.restfulclient import UbuntuSSOAPI
 
25
from softwarecenter.enums import (SOFTWARE_CENTER_NAME_KEYRING,
 
26
                                  SOFTWARE_CENTER_SSO_DESCRIPTION,
 
27
                                  )
 
28
from softwarecenter.utils import clear_token_from_ubuntu_sso
 
29
 
 
30
 
 
31
 
 
32
class SSOLoginHelper(object):
 
33
    def __init__(self, xid=0):
 
34
        self.oauth = None
 
35
        self.xid = xid
 
36
        self.loop = GObject.MainLoop(GObject.main_context_default())
 
37
    
 
38
    def _login_successful(self, sso_backend, oauth_result):
 
39
        self.oauth = oauth_result
 
40
        # FIXME: actually verify the token against ubuntu SSO
 
41
        self.loop.quit()
 
42
 
 
43
    def verify_token(self, token):
 
44
        def _whoami_done(sso, me):
 
45
            self._whoami = me
 
46
            self.loop.quit()
 
47
        def _whoami_error(sso, err):
 
48
            #print "ERRR", err
 
49
            self.loop.quit()
 
50
        self._whoami = None
 
51
        sso = UbuntuSSOAPI(token)
 
52
        sso.connect("whoami", _whoami_done)
 
53
        sso.connect("error", _whoami_error)
 
54
        sso.whoami()
 
55
        self.loop.run()
 
56
        # check if the token is valid
 
57
        if self._whoami is None:
 
58
            return False
 
59
        else:
 
60
            return True
 
61
 
 
62
    def clear_token(self):
 
63
        clear_token_from_ubuntu_sso(SOFTWARE_CENTER_NAME_KEYRING)
 
64
 
 
65
    def get_oauth_token_and_verify_sync(self):
 
66
        token = self.get_oauth_token_sync()
 
67
        # check if the token is valid and reset it if it is not
 
68
        if token and not self.verify_token(token):
 
69
            self.clear_token()
 
70
            # re-trigger login
 
71
            token = self.get_oauth_token_sync()
 
72
        return token
 
73
 
 
74
    def get_oauth_token_sync(self):
 
75
        self.oauth = None
 
76
        sso = get_sso_backend(
 
77
            self.xid, 
 
78
            SOFTWARE_CENTER_NAME_KEYRING,
 
79
            _(SOFTWARE_CENTER_SSO_DESCRIPTION))
 
80
        sso.connect("login-successful", self._login_successful)
 
81
        sso.connect("login-failed", lambda s: self.loop.quit())
 
82
        sso.connect("login-canceled", lambda s: self.loop.quit())
 
83
        sso.login_or_register()
 
84
        self.loop.run()
 
85
        return self.oauth