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
|
/* This file is part of KDevelop
*
* Copyright (C) 2012-2013 Miquel Sabaté <mikisabate@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <ghresource.h>
#include <debug.h>
#include <ghprovidermodel.h>
#include <KIO/Scheduler>
#include <KIO/TransferJob>
#include <KIO/StoredTransferJob>
#include <QUrl>
#include <QJsonDocument>
#include <QHostInfo>
#include <QDateTime>
namespace gh
{
/// Base url for the Github API v3.
const static QUrl baseUrl(QStringLiteral("https://api.github.com"));
KIO::StoredTransferJob* createHttpAuthJob(const QString &httpHeader)
{
QUrl url = baseUrl;
url = url.adjusted(QUrl::StripTrailingSlash);
url.setPath(url.path() + QLatin1String("/authorizations"));
// generate a unique token, see bug 372144
const QString tokenName = QLatin1String("KDevelop Github Provider : ")
+ QHostInfo::localHostName() + QLatin1String(" - ")
+ QDateTime::currentDateTimeUtc().toString();
const QByteArray data = QByteArrayLiteral("{ \"scopes\": [\"repo\"], \"note\": \"") + tokenName.toUtf8() + QByteArrayLiteral("\" }");
KIO::StoredTransferJob *job = KIO::storedHttpPost(data, url, KIO::HideProgressInfo);
job->setProperty("requestedTokenName", tokenName);
job->addMetaData(QStringLiteral("customHTTPHeader"), httpHeader);
return job;
}
Resource::Resource(QObject *parent, ProviderModel *model)
: QObject(parent), m_model(model)
{
/* There's nothing to do here */
}
void Resource::searchRepos(const QString &uri, const QString &token)
{
KIO::TransferJob *job = getTransferJob(uri, token);
connect(job, &KIO::TransferJob::data,
this, &Resource::slotRepos);
}
void Resource::getOrgs(const QString &token)
{
KIO::TransferJob *job = getTransferJob(QStringLiteral("/user/orgs"), token);
connect(job, &KIO::TransferJob::data,
this, &Resource::slotOrgs);
}
void Resource::authenticate(const QString &name, const QString &password)
{
auto job = createHttpAuthJob(QLatin1String("Authorization: Basic ") + QString::fromUtf8(QByteArray(name.toUtf8() + ':' + password.toUtf8()).toBase64()));
job->addMetaData(QStringLiteral("PropagateHttpHeader"), QStringLiteral("true"));
connect(job, &KIO::StoredTransferJob::result,
this, &Resource::slotAuthenticate);
job->start();
}
void Resource::twoFactorAuthenticate(const QString &transferHeader, const QString &code)
{
auto job = createHttpAuthJob(transferHeader + QLatin1String("\nX-GitHub-OTP: ") + code);
connect(job, &KIO::StoredTransferJob::result,
this, &Resource::slotAuthenticate);
job->start();
}
void Resource::revokeAccess(const QString &id, const QString &name, const QString &password)
{
QUrl url = baseUrl;
url.setPath(url.path() + QLatin1String("/authorizations/") + id);
KIO::TransferJob *job = KIO::http_delete(url, KIO::HideProgressInfo);
const auto passwordBase64 = QString::fromLatin1(QString(name + QLatin1Char(':') + password).toUtf8().toBase64());
job->addMetaData(QStringLiteral("customHTTPHeader"), QLatin1String("Authorization: Basic ") + passwordBase64);
/* And we don't care if it's successful ;) */
job->start();
}
KIO::TransferJob * Resource::getTransferJob(const QString &path, const QString &token) const
{
QUrl url = baseUrl;
url = url.adjusted(QUrl::StripTrailingSlash);
url.setPath(url.path() + path);
KIO::TransferJob *job = KIO::get(url, KIO::Reload, KIO::HideProgressInfo);
if (!token.isEmpty())
job->addMetaData(QStringLiteral("customHTTPHeader"), QLatin1String("Authorization: token ") + token);
return job;
}
void Resource::retrieveRepos(const QByteArray &data)
{
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
if (error.error == 0) {
const QVariantList list = doc.toVariant().toList();
m_model->clear();
for (const QVariant& it : list) {
const QVariantMap &map = it.toMap();
Response res;
res.name = map.value(QStringLiteral("name")).toString();
res.url = map.value(QStringLiteral("clone_url")).toUrl();
if (map.value(QStringLiteral("fork")).toBool())
res.kind = Fork;
else if (map.value(QStringLiteral("private")).toBool())
res.kind = Private;
else
res.kind = Public;
auto *item = new ProviderItem(res);
m_model->appendRow(item);
}
}
emit reposUpdated();
}
void Resource::retrieveOrgs(const QByteArray &data)
{
QStringList res;
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
if (error.error == 0) {
const QVariantList json = doc.toVariant().toList();
res.reserve(json.size());
for (const QVariant& it : json) {
QVariantMap map = it.toMap();
res << map.value(QStringLiteral("login")).toString();
}
}
emit orgsUpdated(res);
}
void Resource::slotAuthenticate(KJob *job)
{
const QString tokenName = job->property("requestedTokenName").toString();
Q_ASSERT(!tokenName.isEmpty());
if (job->error()) {
emit authenticated("", "", tokenName);
return;
}
const auto metaData = qobject_cast<KIO::StoredTransferJob*>(job)->metaData();
if (metaData[QStringLiteral("responsecode")] == QLatin1String("401")) {
const auto& header = metaData[QStringLiteral("HTTP-Headers")];
if (header.contains(QLatin1String("X-GitHub-OTP: required;"), Qt::CaseInsensitive)) {
emit twoFactorAuthRequested(qobject_cast<KIO::StoredTransferJob*>(job)->outgoingMetaData()[QStringLiteral("customHTTPHeader")]);
return;
}
}
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(qobject_cast<KIO::StoredTransferJob *>(job)->data(), &error);
qCDebug(GHPROVIDER) << "Response:" << doc;
if (error.error == 0) {
QVariantMap map = doc.toVariant().toMap();
emit authenticated(map.value(QStringLiteral("id")).toByteArray(),
map.value(QStringLiteral("token")).toByteArray(), tokenName);
} else
emit authenticated("", "", tokenName);
}
void Resource::slotRepos(KIO::Job *job, const QByteArray &data)
{
if (!job) {
qCWarning(GHPROVIDER) << "NULL job returned!";
return;
}
if (job->error()) {
qCWarning(GHPROVIDER) << "Job error: " << job->errorString();
return;
}
m_temp.append(data);
if (data.isEmpty()) {
retrieveRepos(m_temp);
m_temp = "";
}
}
void Resource::slotOrgs(KIO::Job *job, const QByteArray &data)
{
QList<QString> res;
if (!job) {
qCWarning(GHPROVIDER) << "NULL job returned!";
emit orgsUpdated(res);
return;
}
if (job->error()) {
qCWarning(GHPROVIDER) << "Job error: " << job->errorString();
emit orgsUpdated(res);
return;
}
m_orgTemp.append(data);
if (data.isEmpty()) {
retrieveOrgs(m_orgTemp);
m_orgTemp = "";
}
}
} // End of namespace gh
|