2
# Copyright (C) 2012 Canonical
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.
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
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
20
from gi.repository import GObject
21
from gettext import gettext as _
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,
28
from softwarecenter.utils import clear_token_from_ubuntu_sso
32
class SSOLoginHelper(object):
33
def __init__(self, xid=0):
36
self.loop = GObject.MainLoop(GObject.main_context_default())
38
def _login_successful(self, sso_backend, oauth_result):
39
self.oauth = oauth_result
40
# FIXME: actually verify the token against ubuntu SSO
43
def verify_token(self, token):
44
def _whoami_done(sso, me):
47
def _whoami_error(sso, err):
51
sso = UbuntuSSOAPI(token)
52
sso.connect("whoami", _whoami_done)
53
sso.connect("error", _whoami_error)
56
# check if the token is valid
57
if self._whoami is None:
62
def clear_token(self):
63
clear_token_from_ubuntu_sso(SOFTWARE_CENTER_NAME_KEYRING)
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):
71
token = self.get_oauth_token_sync()
74
def get_oauth_token_sync(self):
76
sso = get_sso_backend(
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()