diff options
Diffstat (limited to 'buteo-plugins/buteo-sync-plugin-mastodon-posts/mastodondatatypesyncadaptor.cpp')
| -rw-r--r-- | buteo-plugins/buteo-sync-plugin-mastodon-posts/mastodondatatypesyncadaptor.cpp | 91 |
1 files changed, 5 insertions, 86 deletions
diff --git a/buteo-plugins/buteo-sync-plugin-mastodon-posts/mastodondatatypesyncadaptor.cpp b/buteo-plugins/buteo-sync-plugin-mastodon-posts/mastodondatatypesyncadaptor.cpp index 83a5249..7b47fe8 100644 --- a/buteo-plugins/buteo-sync-plugin-mastodon-posts/mastodondatatypesyncadaptor.cpp +++ b/buteo-plugins/buteo-sync-plugin-mastodon-posts/mastodondatatypesyncadaptor.cpp @@ -19,10 +19,10 @@ ****************************************************************************/ #include "mastodondatatypesyncadaptor.h" +#include "mastodonauthutils.h" #include "trace.h" #include <QtCore/QVariantMap> -#include <QtCore/QUrl> #include <QtNetwork/QNetworkRequest> // libaccounts-qt5 @@ -131,29 +131,6 @@ void MastodonDataTypeSyncAdaptor::setCredentialsNeedUpdate(Accounts::Account *ac account->syncAndBlock(); } -QString MastodonDataTypeSyncAdaptor::normalizeApiHost(const QString &rawHost) -{ - QString host = rawHost.trimmed(); - if (host.isEmpty()) { - host = QStringLiteral("https://mastodon.social"); - } - if (!host.startsWith(QLatin1String("https://")) - && !host.startsWith(QLatin1String("http://"))) { - host.prepend(QStringLiteral("https://")); - } - - QUrl url(host); - if (!url.isValid() || url.host().isEmpty()) { - return QStringLiteral("https://mastodon.social"); - } - - QString normalized = QString::fromLatin1(url.toEncoded(QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment)); - if (normalized.endsWith(QLatin1Char('/'))) { - normalized.chop(1); - } - return normalized; -} - void MastodonDataTypeSyncAdaptor::signIn(Accounts::Account *account) { const int accountId = account->id(); @@ -185,59 +162,7 @@ void MastodonDataTypeSyncAdaptor::signIn(Accounts::Account *account) } QVariantMap signonSessionData = accSrv.authData().parameters(); - QString configuredHost = account->value(QStringLiteral("auth/oauth2/web_server/Host")).toString().trimmed(); - if (configuredHost.isEmpty()) { - configuredHost = normalizeApiHost(account->value(QStringLiteral("api/Host")).toString()); - } - if (configuredHost.startsWith(QLatin1String("https://"))) { - configuredHost.remove(0, 8); - } else if (configuredHost.startsWith(QLatin1String("http://"))) { - configuredHost.remove(0, 7); - } - while (configuredHost.endsWith(QLatin1Char('/'))) { - configuredHost.chop(1); - } - if (configuredHost.isEmpty()) { - configuredHost = QStringLiteral("mastodon.social"); - } - signonSessionData.insert(QStringLiteral("Host"), configuredHost); - - const QString authPath = account->value(QStringLiteral("auth/oauth2/web_server/AuthPath")).toString().trimmed(); - if (!authPath.isEmpty()) { - signonSessionData.insert(QStringLiteral("AuthPath"), authPath); - } - - const QString tokenPath = account->value(QStringLiteral("auth/oauth2/web_server/TokenPath")).toString().trimmed(); - if (!tokenPath.isEmpty()) { - signonSessionData.insert(QStringLiteral("TokenPath"), tokenPath); - } - - const QString responseType = account->value(QStringLiteral("auth/oauth2/web_server/ResponseType")).toString().trimmed(); - if (!responseType.isEmpty()) { - signonSessionData.insert(QStringLiteral("ResponseType"), responseType); - } - - const QString redirectUri = account->value(QStringLiteral("auth/oauth2/web_server/RedirectUri")).toString().trimmed(); - if (!redirectUri.isEmpty()) { - signonSessionData.insert(QStringLiteral("RedirectUri"), redirectUri); - } - - const QVariant scopeValue = account->value(QStringLiteral("auth/oauth2/web_server/Scope")); - if (scopeValue.isValid()) { - signonSessionData.insert(QStringLiteral("Scope"), scopeValue); - } - - const QString clientId = account->value(QStringLiteral("auth/oauth2/web_server/ClientId")).toString().trimmed(); - if (!clientId.isEmpty()) { - signonSessionData.insert(QStringLiteral("ClientId"), clientId); - } - - const QString clientSecret = account->value(QStringLiteral("auth/oauth2/web_server/ClientSecret")).toString().trimmed(); - if (!clientSecret.isEmpty()) { - signonSessionData.insert(QStringLiteral("ClientSecret"), clientSecret); - } - - signonSessionData.insert(QStringLiteral("UiPolicy"), SignOn::NoUserInteractionPolicy); + MastodonAuthUtils::addSignOnSessionParameters(account, &signonSessionData); connect(session, SIGNAL(response(SignOn::SessionData)), this, SLOT(signOnResponse(SignOn::SessionData)), @@ -276,10 +201,7 @@ void MastodonDataTypeSyncAdaptor::signOnError(const SignOn::Error &error) void MastodonDataTypeSyncAdaptor::signOnResponse(const SignOn::SessionData &responseData) { - QVariantMap data; - foreach (const QString &key, responseData.propertyNames()) { - data.insert(key, responseData.getProperty(key)); - } + const QVariantMap data = MastodonAuthUtils::responseDataToMap(responseData); QString accessToken; SignOn::AuthSession *session = qobject_cast<SignOn::AuthSession*>(sender()); @@ -287,16 +209,13 @@ void MastodonDataTypeSyncAdaptor::signOnResponse(const SignOn::SessionData &resp SignOn::Identity *identity = session->property("identity").value<SignOn::Identity*>(); const int accountId = account->id(); - accessToken = data.value(QLatin1String("AccessToken")).toString().trimmed(); - if (accessToken.isEmpty()) { - accessToken = data.value(QLatin1String("access_token")).toString().trimmed(); - } + accessToken = MastodonAuthUtils::accessToken(data); if (accessToken.isEmpty()) { qCWarning(lcSocialPlugin) << "signon response for account with id" << accountId << "contained no access token; keys:" << data.keys(); } - m_apiHosts.insert(accountId, normalizeApiHost(account->value(QStringLiteral("api/Host")).toString())); + m_apiHosts.insert(accountId, MastodonAuthUtils::normalizeApiHost(account->value(QStringLiteral("api/Host")).toString())); session->disconnect(this); identity->destroySession(session); |
