Debian Codegraph @

online-accounts-service/src/signonui-service.cpp

raw symbols SCIP
1 /*
2 * This file is part of lomiri-online-accounts-service
3 *
4 * Copyright (C) 2011-2014 Canonical Ltd.
5 *
6 * Contact: Alberto Mardegan <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 3, as published
10 * by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranties of
14 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <online-accounts-common/debug.h>
22 #include "request.h"
23 #include "request-manager.h"
24 #include "signonui-service.h"
25
26 #include <QDBusArgument>
27 #include <QDateTime>
28 #include <QDir>
29 #include <QFile>
30 #include <QFileInfo>
31 #include <QJsonArray>
32 #include <QJsonDocument>
33 #include <QList>
34 #include <QNetworkCookie>
35 #include <QStandardPaths>
36 #include <QVariant>
37 #include <SignOn/uisessiondata_priv.h>
38
39 using namespace SignOnUi;
40
41 namespace SignOnUi {
42
43 static QList<QByteArray> cookiesFromVariant(const QVariantList &cl)
44 {
45 QList<QByteArray> cookies;
46 Q_FOREACH(QVariant cookie, cl) {
47 if (!cookie.canConvert(QVariant::Map)) {
48 continue;
49 }
50
51 QNetworkCookie nc;
52 QVariantMap vm = cookie.toMap();
53 if (!vm.contains("name") || !vm.contains("value")) {
54 continue;
55 }
56
57 nc.setName(vm.value("name").toByteArray());
58 nc.setValue(vm.value("value").toByteArray());
59 nc.setDomain(vm.value("domain").toString());
60 nc.setPath(vm.value("path").toString());
61 if (vm.contains("httponly") &&
62 vm.value("httponly").canConvert(QVariant::Bool)) {
63 nc.setHttpOnly(vm.value("httponly").toBool());
64 }
65
66 if (vm.contains("issecure") &&
67 vm.value("issecure").canConvert(QVariant::Bool)) {
68 nc.setSecure(vm.value("issecure").toBool());
69 }
70
71 if (vm.contains("expirationdate") &&
72 vm.value("expirationdate").canConvert(QMetaType::QDateTime)) {
73 nc.setExpirationDate(vm.value("expirationdate").toDateTime());
74 }
75
76 cookies.append(nc.toRawForm());
77 }
78 return cookies;
79 }
80
81 static QVariant dbusValueToVariant(const QDBusArgument &argument)
82 {
83 QVariant ret;
84
85 /* Note: this function should operate recursively, but it doesn't. */
86 if (argument.currentType() == QDBusArgument::MapType) {
87 /* Assume that all maps are a{sv} */
88 ret = qdbus_cast<QVariantMap>(argument);
89 } else {
90 /* We don't know how to handle other types */
91 ret = argument.asVariant();
92 }
93 return ret;
94 }
95
96 static QVariantMap expandDBusArguments(const QVariantMap &dbusMap)
97 {
98 QVariantMap map;
99 QMapIterator<QString, QVariant> it(dbusMap);
100 while (it.hasNext()) {
101 it.next();
102 if (qstrcmp(it.value().typeName(), "QDBusArgument") == 0) {
103 QDBusArgument dbusValue = it.value().value<QDBusArgument>();
104 map.insert(it.key(), dbusValueToVariant(dbusValue));
105 } else {
106 map.insert(it.key(), it.value());
107 }
108 }
109 return map;
110 }
111
112 class ServicePrivate: public QObject
113 {
114 Q_OBJECT
115 Q_DECLARE_PUBLIC(Service)
116
117 public:
118 ServicePrivate(Service *service);
119 ~ServicePrivate();
120
121 void cancelUiRequest(const QString &requestId);
122 void removeIdentityData(quint32 id);
123 RawCookies cookiesForIdentity(quint32 id, qint64 &timestamp) const;
124
125 static QString rootDirForIdentity(quint32 id);
126
127 private:
128 mutable Service *q_ptr;
129 };
130
131 } // namespace
132
133 ServicePrivate::ServicePrivate(Service *service):
134 QObject(service),
135 q_ptr(service)
136 {
137 qRegisterMetaType<RawCookies>("RawCookies");
138 }
139
140 ServicePrivate::~ServicePrivate()
141 {
142 }
143
144 void ServicePrivate::cancelUiRequest(const QString &requestId)
145 {
146 QVariantMap match;
147 match.insert(SSOUI_KEY_REQUESTID, requestId);
148 OnlineAccountsUi::Request *request =
149 OnlineAccountsUi::Request::find(match);
150
151 DEBUG() << "Cancelling request" << request;
152 if (request != 0) {
153 request->cancel();
154 }
155 }
156
157 QString ServicePrivate::rootDirForIdentity(quint32 id)
158 {
159 /* the BrowserRequest class instructs the webview to store its cookies and
160 * local data into
161 * ~/.cache/lomiri-online-accounts-ui/id-<signon-id>-<provider-id>/; while we
162 * don't normally expect to find more than one entry for a given signon-id,
163 * it's a possibility we cannot completely discard, since in older versions
164 * we were not appending the "-<provider-id>" suffix. Besides, unless we
165 * look up into the accounts DB, we don't know the value for the provider
166 * ID.
167 * Because of both of these reasons, we list all the directories whose name
168 * starts with "id-<signon-id>" and pick the most recent one.
169 */
170 QString cachePath =
171 QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation);
172 QDir cacheDir(cachePath + QStringLiteral("/lomiri-online-accounts-ui"));
173 QString nameFilter = QString("id-%1*").arg(id);
174 QStringList rootDirs = cacheDir.entryList(QStringList() << nameFilter,
175 QDir::Dirs, QDir::Time);
176 return rootDirs.count() > 0 ? cacheDir.filePath(rootDirs.at(0)) : QString();
177 }
178
179 void ServicePrivate::removeIdentityData(quint32 id)
180 {
181 /* Remove any data associated with the given identity. */
182 QString rootDirName = rootDirForIdentity(id);
183 if (rootDirName.isEmpty()) return;
184 QDir rootDir(rootDirName);
185 rootDir.removeRecursively();
186 }
187
188 RawCookies ServicePrivate::cookiesForIdentity(quint32 id,
189 qint64 &timestamp) const
190 {
191 RawCookies cookies;
192
193 QFileInfo fileInfo(rootDirForIdentity(id) + "/cookies.json");
194 if (!fileInfo.exists()) {
195 DEBUG() << "File does not exist:" << fileInfo.filePath();
196 return cookies;
197 }
198 timestamp = fileInfo.lastModified().toMSecsSinceEpoch() / 1000;
199
200 QFile file(fileInfo.filePath());
201 if (Q_UNLIKELY(!file.open(QIODevice::ReadOnly | QIODevice::Text))) {
202 qWarning() << "Cannot open file" << fileInfo.filePath();
203 return cookies;
204 }
205
206 QByteArray contents = file.readAll();
207 QJsonDocument doc = QJsonDocument::fromJson(contents);
208 if (doc.isEmpty() || !doc.isArray()) return cookies;
209
210 QVariantList cookieVariants = doc.array().toVariantList();
211 return cookiesFromVariant(cookieVariants);
212 }
213
214 Service::Service(QObject *parent):
215 QObject(parent),
216 d_ptr(new ServicePrivate(this))
217 {
218 }
219
220 Service::~Service()
221 {
222 }
223
224 QVariantMap Service::queryDialog(const QVariantMap &parameters)
225 {
226 QVariantMap cleanParameters = expandDBusArguments(parameters);
227 DEBUG() << "Got request:" << cleanParameters;
228
229 /* The following line tells QtDBus not to generate a reply now */
230 setDelayedReply(true);
231
232 OnlineAccountsUi::Request *request =
233 new OnlineAccountsUi::Request(connection(),
234 message(),
235 cleanParameters,
236 this);
237
238 OnlineAccountsUi::RequestManager::instance()->enqueue(request);
239
240 return QVariantMap();
241 }
242
243 QVariantMap Service::refreshDialog(const QVariantMap &newParameters)
244 {
245 QVariantMap cleanParameters = expandDBusArguments(newParameters);
246 // TODO find the request and update it
247
248 /* The following line tells QtDBus not to generate a reply now */
249 setDelayedReply(true);
250 return QVariantMap();
251 }
252
253 void Service::cancelUiRequest(const QString &requestId)
254 {
255 Q_D(Service);
256 d->cancelUiRequest(requestId);
257 }
258
259 void Service::removeIdentityData(quint32 id)
260 {
261 Q_D(Service);
262 d->removeIdentityData(id);
263 }
264
265 void Service::cookiesForIdentity(quint32 id,
266 RawCookies &cookies, qint64 &timestamp)
267 {
268 Q_D(Service);
269 cookies = d->cookiesForIdentity(id, timestamp);
270 }
271
272 #include "signonui-service.moc"