2
# Copyright (C) 2016 Canonical Ltd
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU Lesser General Public License version 3 as
6
# published by the Free Software Foundation.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU Lesser General Public License for more details.
13
# You should have received a copy of the GNU Lesser General Public License
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
# Authors: James Henstridge <james.henstridge@canonical.com>
18
"""A fake version of the OnlineAccounts D-Bus service."""
24
import dbus.mainloop.glib
25
from gi.repository import GLib
27
BUS_NAME = "com.ubuntu.OnlineAccounts.Manager"
28
OBJECT_PATH = "/com/ubuntu/OnlineAccounts/Manager"
29
OA_IFACE = "com.ubuntu.OnlineAccounts.Manager"
31
INTERFACE_XML = os.path.join(os.path.dirname(__file__),
32
"com.ubuntu.OnlineAccounts.Manager.xml")
41
def __init__(self, consumer_key, consumer_secret, token, token_secret, signature_method="HMAC-SHA1"):
42
self.consumer_key = consumer_key
43
self.consumer_secret = consumer_secret
45
self.token_secret = token_secret
46
self.signature_method = signature_method
49
return dbus.Dictionary({
50
"ConsumerKey": dbus.String(self.consumer_key),
51
"ConsumerSecret": dbus.String(self.consumer_secret),
52
"Token": dbus.String(self.token),
53
"TokenSecret": dbus.String(self.token_secret),
54
"SignatureMethod": dbus.String(self.signature_method),
59
def __init__(self, access_token, expires_in=0, granted_scopes=[]):
60
self.access_token = access_token
61
self.expires_in = expires_in
62
self.granted_scopes = granted_scopes
65
return dbus.Dictionary({
66
"AccessToken": dbus.String(self.access_token),
67
"ExpiresIn": dbus.Int32(self.expires_in),
68
"GrantedScopes": dbus.Array(self.granted_scopes, signature="s"),
72
method = AUTH_PASSWORD
73
def __init__(self, username, password):
74
self.username = username
75
self.password = password
78
return dbus.Dictionary({
79
"Username": dbus.String(self.username),
80
"Password": dbus.String(self.password),
84
def __init__(self, account_id, display_name, service_id, credentials):
85
self.account_id = account_id
86
self.display_name = display_name
87
self.service_id = service_id
88
self.credentials = credentials
91
return (dbus.UInt32(self.account_id), dbus.Dictionary({
92
"displayName": dbus.String(self.display_name),
93
"serviceId": dbus.String(self.service_id),
94
"authMethod": dbus.Int32(self.credentials.method),
97
class Manager(dbus.service.Object):
98
def __init__(self, connection, object_path, accounts):
99
super(Manager, self).__init__(connection, object_path)
100
self.accounts = accounts
102
@dbus.service.method(dbus_interface=OA_IFACE,
103
in_signature="a{sv}", out_signature="a(ua{sv})")
104
def GetAccounts(self, filters):
105
print("GetAccounts %r" % filters)
107
return dbus.Array([a.serialise() for a in self.accounts],
108
signature="a(ua{sv})")
110
@dbus.service.method(dbus_interface=OA_IFACE,
111
in_signature="usbba{sv}", out_signature="a{sv}")
112
def Authenticate(self, account_id, service_id, interactive, invalidate, parameters):
113
print("Authenticate %r %r %r %r %r" % (account_id, service_id, interactive, invalidate, parameters))
115
for account in self.accounts:
116
if account.account_id == account_id and account.service_id == service_id:
117
return account.credentials.serialise()
119
raise KeyError(repr((account_id, service_id)))
121
@dbus.service.method(dbus_interface=OA_IFACE,
122
in_signature="sa{sv}", out_signature="(ua{sv})a{sv}")
123
def RequestAccess(self, service_id, parameters):
124
print("RequestAccess %r %r" % (service_id, parameters))
126
for account in self.accounts:
127
if account.service_id == service_id:
128
return (account.serialise(),
129
account.credentials.serialise())
131
raise KeyError(service_id)
134
def __init__(self, accounts):
135
self.accounts = accounts
136
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
137
self.main_loop = GLib.MainLoop()
138
self.connection = dbus.SessionBus()
139
# Quit when the bus disconnectes
140
self.connection.add_signal_receiver(
141
self.main_loop.quit, signal_name="Disconnected",
142
path="/org/freedesktop/DBus/Local",
143
dbus_interface="org.freedesktop.DBus.Local")
144
self.manager = Manager(self.connection, OBJECT_PATH, self.accounts)
145
self.bus_name = dbus.service.BusName(BUS_NAME, self.connection,
146
allow_replacement=True,
147
replace_existing=True,
153
except KeyboardInterrupt:
156
if __name__ == "__main__":
158
Account(1, "OAuth1 account", "oauth1-service",
159
OAuth1("consumer_key", "consumer_secret", "token", "token_secret")),
160
Account(2, "OAuth2 account", "oauth2-service",
161
OAuth2("access_token", 0, ["scope1", "scope2"])),
162
Account(3, "Password account", "password-service",
163
Password("user", "pass")),
164
Account(42, "Fake google account", "google-drive-scope",
165
OAuth2("fake-google-access-token", 0, [])),
166
Account(99, "Fake mcloud account", "com.canonical.scopes.mcloud_mcloud_mcloud",
167
OAuth2("fake-mcloud-access-token", 0, [])),
169
server = Server(accounts)