/
onedrivedatatypesyncadaptor.cpp
274 lines (235 loc) · 10.3 KB
/
onedrivedatatypesyncadaptor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/****************************************************************************
**
** Copyright (c) 2015 - 2019 Jolla Ltd.
** Copyright (c) 2020 Open Mobile Platform LLC.
**
** This program/library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public License
** version 2.1 as published by the Free Software Foundation.
**
** This program/library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this program/library; if not, write to the Free
** Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
** 02110-1301 USA
**
****************************************************************************/
#include "onedrivedatatypesyncadaptor.h"
#include "trace.h"
#include <QtCore/QVariantMap>
#include <QtCore/QObject>
#include <QtCore/QList>
#include <QtCore/QString>
#include <QtCore/QByteArray>
//libsailfishkeyprovider
#include <sailfishkeyprovider.h>
// libaccounts-qt5
#include <Accounts/Manager>
#include <Accounts/Account>
#include <Accounts/Service>
#include <Accounts/AccountService>
//libsignon-qt: SignOn::NoUserInteractionPolicy
#include <SignOn/Identity>
#include <SignOn/AuthSession>
#include <SignOn/SessionData>
OneDriveDataTypeSyncAdaptor::OneDriveDataTypeSyncAdaptor(SocialNetworkSyncAdaptor::DataType dataType, QObject *parent)
: SocialNetworkSyncAdaptor("onedrive", dataType, 0, parent), m_triedLoading(false)
{
}
OneDriveDataTypeSyncAdaptor::~OneDriveDataTypeSyncAdaptor()
{
}
void OneDriveDataTypeSyncAdaptor::sync(const QString &dataTypeString, int accountId)
{
if (dataTypeString != SocialNetworkSyncAdaptor::dataTypeName(m_dataType)) {
SOCIALD_LOG_ERROR("OneDrive" << SocialNetworkSyncAdaptor::dataTypeName(m_dataType) <<
"sync adaptor was asked to sync" << dataTypeString);
setStatus(SocialNetworkSyncAdaptor::Error);
return;
}
if (clientId().isEmpty()) {
SOCIALD_LOG_ERROR("client id couldn't be retrieved for OneDrive account" << accountId);
setStatus(SocialNetworkSyncAdaptor::Error);
return;
}
setStatus(SocialNetworkSyncAdaptor::Busy);
updateDataForAccount(accountId);
SOCIALD_LOG_DEBUG("successfully triggered sync with profile:" << m_accountSyncProfile->name());
}
void OneDriveDataTypeSyncAdaptor::updateDataForAccount(int accountId)
{
Accounts::Account *account = Accounts::Account::fromId(m_accountManager, accountId, this);
if (!account) {
SOCIALD_LOG_ERROR("existing account with id" << accountId << "couldn't be retrieved");
setStatus(SocialNetworkSyncAdaptor::Error);
return;
}
// will be decremented by either signOnError or signOnResponse.
incrementSemaphore(accountId);
signIn(account);
}
void OneDriveDataTypeSyncAdaptor::finalCleanup()
{
}
void OneDriveDataTypeSyncAdaptor::errorHandler(QNetworkReply::NetworkError err)
{
// OneDrive sends error code 204 (HTTP code 401) for Unauthorized Error
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
const int httpCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (err == QNetworkReply::AuthenticationRequiredError) {
int httpCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
SOCIALD_LOG_INFO("sociald:OneDrive: received:" << httpCode <<
"would normally set CredentialsNeedUpdate for account" <<
reply->property("accountId").toInt() <<
"but could be spurious");
}
SOCIALD_LOG_ERROR(SocialNetworkSyncAdaptor::dataTypeName(m_dataType) <<
"request with account" << sender()->property("accountId").toInt() <<
"experienced error:" << err << "HTTP code:" << httpCode
<< "data:" << reply->readAll());
// set "isError" on the reply so that adapters know to ignore the result in the finished() handler
reply->setProperty("isError", QVariant::fromValue<bool>(true));
// Note: not all errors are "unrecoverable" errors, so we don't change the status here.
}
void OneDriveDataTypeSyncAdaptor::sslErrorsHandler(const QList<QSslError> &errs)
{
QString sslerrs;
foreach (const QSslError &e, errs) {
sslerrs += e.errorString() + "; ";
}
if (errs.size() > 0) {
sslerrs.chop(2);
}
SOCIALD_LOG_ERROR(SocialNetworkSyncAdaptor::dataTypeName(m_dataType) <<
"request with account" << sender()->property("accountId").toInt() <<
"experienced ssl errors:" << sslerrs);
// set "isError" on the reply so that adapters know to ignore the result in the finished() handler
sender()->setProperty("isError", QVariant::fromValue<bool>(true));
// Note: not all errors are "unrecoverable" errors, so we don't change the status here.
}
QString OneDriveDataTypeSyncAdaptor::api() const
{
return m_api;
}
QString OneDriveDataTypeSyncAdaptor::clientId()
{
if (!m_triedLoading) {
loadClientId();
}
return m_clientId;
}
void OneDriveDataTypeSyncAdaptor::loadClientId()
{
m_triedLoading = true;
char *cClientId = NULL;
int cSuccess = SailfishKeyProvider_storedKey("onedrive", "onedrive-sync", "client_id", &cClientId);
if (cClientId == NULL) {
return;
} else if (cSuccess != 0) {
free(cClientId);
return;
}
m_clientId = QLatin1String(cClientId);
free(cClientId);
}
void OneDriveDataTypeSyncAdaptor::setCredentialsNeedUpdate(Accounts::Account *account)
{
qWarning() << "sociald:OneDrive: setting CredentialsNeedUpdate to true for account:" << account->id();
Accounts::Service srv(m_accountManager->service(syncServiceName()));
account->selectService(srv);
account->setValue(QStringLiteral("CredentialsNeedUpdate"), QVariant::fromValue<bool>(true));
account->setValue(QStringLiteral("CredentialsNeedUpdateFrom"), QVariant::fromValue<QString>(QString::fromLatin1("sociald-onedrive")));
account->selectService(Accounts::Service());
account->syncAndBlock();
}
void OneDriveDataTypeSyncAdaptor::signIn(Accounts::Account *account)
{
// Fetch consumer key from keyprovider
int accountId = account->id();
if (!checkAccount(account) || clientId().isEmpty()) {
decrementSemaphore(accountId);
return;
}
// grab out a valid identity for the sync service.
Accounts::Service srv(m_accountManager->service(syncServiceName()));
account->selectService(srv);
SignOn::Identity *identity = account->credentialsId() > 0 ? SignOn::Identity::existingIdentity(account->credentialsId()) : 0;
if (!identity) {
SOCIALD_LOG_ERROR("account" << accountId << "has no valid credentials; cannot sign in");
decrementSemaphore(accountId);
return;
}
Accounts::AccountService accSrv(account, srv);
QString method = accSrv.authData().method();
QString mechanism = accSrv.authData().mechanism();
SignOn::AuthSession *session = identity->createSession(method);
if (!session) {
SOCIALD_LOG_ERROR("could not create signon session for account" << accountId);
identity->deleteLater();
decrementSemaphore(accountId);
return;
}
QVariantMap signonSessionData = accSrv.authData().parameters();
signonSessionData.insert("ClientId", clientId());
signonSessionData.insert("UiPolicy", SignOn::NoUserInteractionPolicy);
connect(session, SIGNAL(response(SignOn::SessionData)),
this, SLOT(signOnResponse(SignOn::SessionData)),
Qt::UniqueConnection);
connect(session, SIGNAL(error(SignOn::Error)),
this, SLOT(signOnError(SignOn::Error)),
Qt::UniqueConnection);
session->setProperty("account", QVariant::fromValue<Accounts::Account*>(account));
session->setProperty("identity", QVariant::fromValue<SignOn::Identity*>(identity));
session->process(SignOn::SessionData(signonSessionData), mechanism);
}
void OneDriveDataTypeSyncAdaptor::signOnError(const SignOn::Error &error)
{
SignOn::AuthSession *session = qobject_cast<SignOn::AuthSession*>(sender());
Accounts::Account *account = session->property("account").value<Accounts::Account*>();
SignOn::Identity *identity = session->property("identity").value<SignOn::Identity*>();
int accountId = account->id();
SOCIALD_LOG_ERROR("credentials for account with id" << accountId <<
"couldn't be retrieved:" << error.type() << error.message());
// if the error is because credentials have expired, we
// set the CredentialsNeedUpdate key.
if (error.type() == SignOn::Error::UserInteraction) {
setCredentialsNeedUpdate(account);
}
session->disconnect(this);
identity->destroySession(session);
identity->deleteLater();
account->deleteLater();
// if we couldn't sign in, we can't sync with this account.
setStatus(SocialNetworkSyncAdaptor::Error);
decrementSemaphore(accountId);
}
void OneDriveDataTypeSyncAdaptor::signOnResponse(const SignOn::SessionData &responseData)
{
QVariantMap data;
foreach (const QString &key, responseData.propertyNames()) {
data.insert(key, responseData.getProperty(key));
}
QString accessToken;
SignOn::AuthSession *session = qobject_cast<SignOn::AuthSession*>(sender());
Accounts::Account *account = session->property("account").value<Accounts::Account*>();
SignOn::Identity *identity = session->property("identity").value<SignOn::Identity*>();
int accountId = account->id();
if (data.contains(QLatin1String("AccessToken"))) {
accessToken = data.value(QLatin1String("AccessToken")).toString();
} else {
SOCIALD_LOG_INFO("signon response for account with id" << accountId << "contained no access token");
}
m_api = account->value(QStringLiteral("api/Host")).toString();
session->disconnect(this);
identity->destroySession(session);
identity->deleteLater();
account->deleteLater();
if (!accessToken.isEmpty()) {
beginSync(accountId, accessToken); // call the derived-class sync entrypoint.
}
decrementSemaphore(accountId);
}