| 1 |
'''signond mock template |
| 2 |
|
| 3 |
This creates the expected methods and properties of the |
| 4 |
com.google.code.AccountsSSO.SingleSignOn service. |
| 5 |
''' |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
__author__ = 'Alberto Mardegan' |
| 14 |
__email__ = '[email protected]' |
| 15 |
__copyright__ = '(c) 2015 Canonical Ltd.' |
| 16 |
__license__ = 'LGPL 3+' |
| 17 |
|
| 18 |
import dbus |
| 19 |
import time |
| 20 |
|
| 21 |
from dbusmock import MOCK_IFACE |
| 22 |
import dbusmock |
| 23 |
|
| 24 |
BUS_NAME = 'com.google.code.AccountsSSO.SingleSignOn' |
| 25 |
MAIN_OBJ = '/com/google/code/AccountsSSO/SingleSignOn' |
| 26 |
AUTH_SERVICE_IFACE = 'com.google.code.AccountsSSO.SingleSignOn.AuthService' |
| 27 |
MAIN_IFACE = AUTH_SERVICE_IFACE |
| 28 |
IDENTITY_IFACE = 'com.google.code.AccountsSSO.SingleSignOn.Identity' |
| 29 |
AUTH_SESSION_IFACE = 'com.google.code.AccountsSSO.SingleSignOn.AuthSession' |
| 30 |
SYSTEM_BUS = False |
| 31 |
|
| 32 |
ERROR_PREFIX = 'com.google.code.AccountsSSO.SingleSignOn.Error.' |
| 33 |
ERROR_IDENTITY_NOT_FOUND = ERROR_PREFIX + 'IdentityNotFound' |
| 34 |
ERROR_PERMISSION_DENIED = ERROR_PREFIX + 'PermissionDenied' |
| 35 |
ERROR_USER_INTERACTION= ERROR_PREFIX + 'UserInteraction' |
| 36 |
|
| 37 |
|
| 38 |
def identity_store(self, new_info): |
| 39 |
self.info.update(new_info) |
| 40 |
return self.info['Id'] |
| 41 |
|
| 42 |
def get_identity(self, identity, application_context): |
| 43 |
if identity not in self.identities: |
| 44 |
raise dbus.exceptions.DBusException('Identity not found', |
| 45 |
name=ERROR_IDENTITY_NOT_FOUND) |
| 46 |
path = '/Identity%s' % identity |
| 47 |
if not path in dbusmock.get_objects(): |
| 48 |
self.AddObject(path, IDENTITY_IFACE, {}, [ |
| 49 |
('getInfo', '', 'a{sv}', 'ret = self.parent.identities[%s]' % identity), |
| 50 |
('store', 'a{sv}', 'u', 'ret = self.store(self, args[0])'), |
| 51 |
]) |
| 52 |
identity_obj = dbusmock.get_object(path) |
| 53 |
identity_obj.parent = self |
| 54 |
identity_obj.store = identity_store |
| 55 |
identity_obj.info = self.identities[identity] |
| 56 |
return (path, self.identities[identity]) |
| 57 |
|
| 58 |
|
| 59 |
def auth_session_process(identity, params, method): |
| 60 |
if 'errorName' in params: |
| 61 |
raise dbus.exceptions.DBusException('Authentication error', |
| 62 |
name=params['errorName']) |
| 63 |
if 'delay' in params: |
| 64 |
time.sleep(params['delay']) |
| 65 |
return params |
| 66 |
|
| 67 |
def get_auth_session_object_path(self, identity, application_context, method): |
| 68 |
if identity != 0 and (identity not in self.identities): |
| 69 |
raise dbus.exceptions.DBusException('Identity not found', |
| 70 |
name=ERROR_IDENTITY_NOT_FOUND) |
| 71 |
path = '/AuthSession%s' % self.sessions_counter |
| 72 |
self.sessions_counter += 1 |
| 73 |
self.AddObject(path, AUTH_SESSION_IFACE, {}, [ |
| 74 |
('process', 'a{sv}s', 'a{sv}', 'ret = self.auth_session_process(self.identity, args[0], args[1])'), |
| 75 |
]) |
| 76 |
|
| 77 |
auth_session = dbusmock.get_object(path) |
| 78 |
auth_session.auth_session_process = auth_session_process |
| 79 |
auth_session.identity = identity |
| 80 |
auth_session.method = method |
| 81 |
|
| 82 |
return path |
| 83 |
|
| 84 |
|
| 85 |
def load(mock, parameters): |
| 86 |
mock.get_identity = get_identity |
| 87 |
mock.get_auth_session_object_path = get_auth_session_object_path |
| 88 |
mock.AddMethods(AUTH_SERVICE_IFACE, [ |
| 89 |
('getIdentity', 'us', 'oa{sv}', 'ret = self.get_identity(self, args[0], args[1])'), |
| 90 |
('getAuthSessionObjectPath', 'uss', 's', 'ret = self.get_auth_session_object_path(self, args[0], args[1], args[2])'), |
| 91 |
]) |
| 92 |
|
| 93 |
mock.sessions_counter = 1 |
| 94 |
mock.identities = {} |
| 95 |
mock.auth_sessions = {} |
| 96 |
mock.auth_replies = {} |
| 97 |
|
| 98 |
@dbus.service.method(MOCK_IFACE, in_signature='ua{sv}', out_signature='') |
| 99 |
def AddIdentity(self, identity, data): |
| 100 |
self.identities[identity] = data |