Commit 796b2b95 authored by Shashwat Jolly's avatar Shashwat Jolly
Browse files

Handle token refresh on getting "Unathorized" error

parent 99d31082
......@@ -68,6 +68,7 @@ bool BaseHandler::createEteSyncEntry(const EteSyncSyncEntry *syncEntry, const Et
const auto result = etesync_entry_manager_create(entryManager.get(), entries, collection.remoteRevision());
if (result) {
handleConflictError(collection);
mResource->handleTokenError();
return false;
}
updateCollectionRevision(entry.get(), collection);
......@@ -131,6 +132,7 @@ void BaseHandler::slotItemsRetrieved(KJob *job)
bool BaseHandler::handleConflictError(const Collection &collection)
{
if (etesync_get_error_code() == EteSyncErrorCode::ETESYNC_ERROR_CODE_CONFLICT) {
qCDebug(ETESYNC_LOG) << "Conflict error";
mResource->deferTask();
mResource->scheduleCustomTask(this, "syncCollection", QVariant::fromValue(collection), ResourceBase::Prepend);
return false;
......
......@@ -207,7 +207,11 @@ void CalendarTaskBaseHandler::collectionAdded(const Akonadi::Collection &collect
etesync_journal_set_info(journal.get(), cryptoManager.get(), info.get());
etesync_journal_manager_create(mClientState->journalManager(), journal.get());
const auto result = etesync_journal_manager_create(mClientState->journalManager(), journal.get());
if (result) {
mResource->handleTokenError();
return;
}
Collection newCollection(collection);
mResource->setupCollection(newCollection, journal.get());
......@@ -233,7 +237,11 @@ void CalendarTaskBaseHandler::collectionChanged(const Akonadi::Collection &colle
etesync_journal_set_info(journal.get(), cryptoManager.get(), info.get());
etesync_journal_manager_update(mClientState->journalManager(), journal.get());
const auto result = etesync_journal_manager_update(mClientState->journalManager(), journal.get());
if (result) {
mResource->handleTokenError();
return;
}
mResource->changeCommitted(collection);
}
......@@ -243,5 +251,9 @@ void CalendarTaskBaseHandler::collectionRemoved(const Akonadi::Collection &colle
QString journalUid = collection.remoteId();
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), journalUid));
etesync_journal_manager_delete(mClientState->journalManager(), journal.get());
const auto result = etesync_journal_manager_delete(mClientState->journalManager(), journal.get());
if (result) {
mResource->handleTokenError();
return;
}
}
......@@ -101,6 +101,7 @@ void EteSyncClientState::refreshToken()
}
qCDebug(ETESYNC_LOG) << "Received token" << mToken;
etesync_set_auth_token(mClient.get(), mToken);
tokenRefreshed();
}
bool EteSyncClientState::initUserInfo()
......
......@@ -34,7 +34,6 @@ public:
bool initKeypair(const QString &encryptionPassword);
void initAccount(const QString &encryptionPassword);
void saveSettings();
void refreshToken();
void invalidateToken();
EteSync *client()
......@@ -62,8 +61,12 @@ public:
return mUsername;
}
public Q_SLOTS:
void refreshToken();
Q_SIGNALS:
void clientInitialised(bool successful);
void tokenRefreshed();
private:
EteSyncPtr mClient = nullptr;
......
......@@ -113,6 +113,7 @@ void EteSyncResource::configure(WId windowId)
void EteSyncResource::retrieveCollections()
{
setCollectionStreamingEnabled(true);
// Set up root collection for resource
mResourceCollection = Collection();
mResourceCollection.setContentMimeTypes({Collection::mimeType()});
......@@ -132,37 +133,52 @@ void EteSyncResource::retrieveCollections()
attr->setDisplayName(mClientState->username());
attr->setIconName(QStringLiteral("akonadi-etesync"));
collectionsRetrieved({mResourceCollection});
auto job = new JournalsFetchJob(mClientState->client(), this);
connect(job, &JournalsFetchJob::finished, this, [this](KJob *job) {
if (job->error()) {
qCWarning(ETESYNC_LOG) << "Error in fetching journals";
return;
}
qCDebug(ETESYNC_LOG) << "Retrieving collections";
EteSyncJournal **journals = qobject_cast<JournalsFetchJob *>(job)->journals();
Collection::List list;
list << mResourceCollection;
for (EteSyncJournal **iter = journals; *iter; iter++) {
EteSyncJournalPtr journal(*iter);
Collection collection;
collection.setParentCollection(mResourceCollection);
setupCollection(collection, journal.get());
list << collection;
}
free(journals);
collectionsRetrieved(list);
});
connect(job, &JournalsFetchJob::finished, this, &EteSyncResource::slotCollectionsRetrieved);
job->start();
// checkTokenRefresh();
}
bool EteSyncResource::handleTokenError()
void EteSyncResource::slotCollectionsRetrieved(KJob *job)
{
if (job->error()) {
qCWarning(ETESYNC_LOG) << "Error in fetching journals";
qCDebug(ETESYNC_LOG) << "Incorrect token in retrieveCollections()";
handleTokenError(true);
return;
}
qCDebug(ETESYNC_LOG) << "Retrieving collections";
EteSyncJournal **journals = qobject_cast<JournalsFetchJob *>(job)->journals();
Collection::List list;
for (EteSyncJournal **iter = journals; *iter; iter++) {
EteSyncJournalPtr journal(*iter);
Collection collection;
collection.setParentCollection(mResourceCollection);
setupCollection(collection, journal.get());
list << collection;
}
free(journals);
collectionsRetrieved(list);
collectionsRetrievalDone();
}
bool EteSyncResource::handleTokenError(bool forRetrieveCollections)
{
if (etesync_get_error_code() == EteSyncErrorCode::ETESYNC_ERROR_CODE_UNAUTHORIZED) {
deferTask();
scheduleCustomTask(mClientState, "refreshToken", QVariant(), ResourceBase::Prepend);
qCDebug(ETESYNC_LOG) << "Invalid token";
if (forRetrieveCollections) {
mClientState->refreshToken();
auto job = new JournalsFetchJob(mClientState->client(), this);
connect(job, &JournalsFetchJob::finished, this, &EteSyncResource::slotCollectionsRetrieved);
job->start();
} else {
deferTask();
connect(mClientState, &EteSyncClientState::tokenRefreshed, this, &EteSyncResource::taskDone);
scheduleCustomTask(mClientState, "refreshToken", QVariant(), ResourceBase::Prepend);
}
return false;
}
return true;
......@@ -242,13 +258,14 @@ void EteSyncResource::retrieveItems(const Akonadi::Collection &collection)
connect(job, &EntriesFetchJob::finished, this, &EteSyncResource::slotItemsRetrieved);
job->start();
checkTokenRefresh();
}
void EteSyncResource::slotItemsRetrieved(KJob *job)
{
if (job->error()) {
qCWarning(ETESYNC_LOG) << job->errorText();
qCDebug(ETESYNC_LOG) << "Incorrect token in retrieveItems()";
handleTokenError();
return;
}
......@@ -286,14 +303,6 @@ void EteSyncResource::initialiseDone(bool successful)
}
}
void EteSyncResource::checkTokenRefresh()
{
if (etesync_get_error_code() == EteSyncErrorCode::ETESYNC_ERROR_CODE_UNAUTHORIZED) {
deferTask();
mClientState->refreshToken();
}
}
QString EteSyncResource::baseDirectoryPath() const
{
return Settings::self()->basePath();
......@@ -330,8 +339,6 @@ void EteSyncResource::itemAdded(const Akonadi::Item &item,
qCWarning(ETESYNC_LOG) << "Could not add item" << item.mimeType();
cancelTask(i18n("Invalid payload type"));
}
// checkTokenRefresh();
}
void EteSyncResource::itemChanged(const Akonadi::Item &item,
......@@ -346,8 +353,6 @@ void EteSyncResource::itemChanged(const Akonadi::Item &item,
qCWarning(ETESYNC_LOG) << "Could not change item" << item.mimeType();
cancelTask(i18n("Invalid payload type"));
}
// checkTokenRefresh();
}
void EteSyncResource::itemRemoved(const Akonadi::Item &item)
......@@ -361,8 +366,6 @@ void EteSyncResource::itemRemoved(const Akonadi::Item &item)
qCWarning(ETESYNC_LOG) << "Could not remove item" << item.mimeType();
cancelTask(i18n("Invalid payload type"));
}
// checkTokenRefresh();
}
void EteSyncResource::collectionAdded(const Akonadi::Collection &collection, const Akonadi::Collection &parent)
......@@ -376,8 +379,6 @@ void EteSyncResource::collectionAdded(const Akonadi::Collection &collection, con
qCWarning(ETESYNC_LOG) << "Could not add collection" << collection.displayName() << "mimetypes:" << collection.contentMimeTypes();
cancelTask(i18n("Unknown collection mimetype"));
}
// checkTokenRefresh();
}
void EteSyncResource::collectionChanged(const Akonadi::Collection &collection)
......@@ -391,8 +392,6 @@ void EteSyncResource::collectionChanged(const Akonadi::Collection &collection)
qCWarning(ETESYNC_LOG) << "Could not change collection" << collection.displayName() << "mimetypes:" << collection.contentMimeTypes();
cancelTask(i18n("Unknown collection mimetype"));
}
// checkTokenRefresh();
}
void EteSyncResource::collectionRemoved(const Akonadi::Collection &collection)
......@@ -407,8 +406,6 @@ void EteSyncResource::collectionRemoved(const Akonadi::Collection &collection)
qCWarning(ETESYNC_LOG) << "Could not remove collection" << collection.displayName() << "mimetypes:" << collection.contentMimeTypes();
cancelTask(i18n("Unknown collection mimetype"));
}
// checkTokenRefresh();
}
AKONADI_RESOURCE_MAIN(EteSyncResource)
......@@ -74,14 +74,13 @@ protected:
QString baseDirectoryPath() const;
void checkTokenRefresh();
bool handleTokenError();
bool handleTokenError(bool forRetrieveCollections = false);
private Q_SLOTS:
void onReloadConfiguration();
void initialiseDone(bool successful);
void slotItemsRetrieved(KJob *job);
void slotCollectionsRetrieved(KJob *job);
private:
Akonadi::Collection mResourceCollection;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment