Changeset View
Standalone View
providers/ghprovider/ghresource.cpp
Show All 29 Lines | |||||
30 | #include <ghprovidermodel.h> | 30 | #include <ghprovidermodel.h> | ||
31 | 31 | | |||
32 | 32 | | |||
33 | namespace gh | 33 | namespace gh | ||
34 | { | 34 | { | ||
35 | /// Base url for the Github API v3. | 35 | /// Base url for the Github API v3. | ||
36 | const static QUrl baseUrl("https://api.github.com"); | 36 | const static QUrl baseUrl("https://api.github.com"); | ||
37 | 37 | | |||
38 | KIO::StoredTransferJob* getHttpAuthJob(const QString &httpHeader) | ||||
mwolff: get -> create | |||||
39 | { | ||||
40 | QUrl url = baseUrl; | ||||
41 | url = url.adjusted(QUrl::StripTrailingSlash); | ||||
42 | url.setPath(url.path() + '/' + "/authorizations"); | ||||
mwolff: + QLatin1String("//authorizations")
is the double-slash really needed? | |||||
43 | QByteArray data = "{ \"scopes\": [\"repo\"], \"note\": \"KDevelop Github Provider\" }"; | ||||
mwolff: QByteArrayLiteral | |||||
44 | KIO::StoredTransferJob *job = KIO::storedHttpPost(data, url, KIO::HideProgressInfo); | ||||
45 | job->addMetaData("customHTTPHeader", httpHeader); | ||||
mwolff: QStringLiteral | |||||
46 | return job; | ||||
47 | } | ||||
48 | | ||||
38 | Resource::Resource(QObject *parent, ProviderModel *model) | 49 | Resource::Resource(QObject *parent, ProviderModel *model) | ||
39 | : QObject(parent), m_model(model) | 50 | : QObject(parent), m_model(model) | ||
40 | { | 51 | { | ||
41 | /* There's nothing to do here */ | 52 | /* There's nothing to do here */ | ||
42 | } | 53 | } | ||
43 | 54 | | |||
44 | void Resource::searchRepos(const QString &uri, const QString &token) | 55 | void Resource::searchRepos(const QString &uri, const QString &token) | ||
45 | { | 56 | { | ||
46 | KIO::TransferJob *job = getTransferJob(uri, token); | 57 | KIO::TransferJob *job = getTransferJob(uri, token); | ||
47 | connect(job, &KIO::TransferJob::data, | 58 | connect(job, &KIO::TransferJob::data, | ||
48 | this, &Resource::slotRepos); | 59 | this, &Resource::slotRepos); | ||
49 | } | 60 | } | ||
50 | 61 | | |||
51 | void Resource::getOrgs(const QString &token) | 62 | void Resource::getOrgs(const QString &token) | ||
52 | { | 63 | { | ||
53 | KIO::TransferJob *job = getTransferJob("/user/orgs", token); | 64 | KIO::TransferJob *job = getTransferJob("/user/orgs", token); | ||
54 | connect(job, &KIO::TransferJob::data, | 65 | connect(job, &KIO::TransferJob::data, | ||
55 | this, &Resource::slotOrgs); | 66 | this, &Resource::slotOrgs); | ||
56 | } | 67 | } | ||
57 | 68 | | |||
58 | void Resource::authenticate(const QString &name, const QString &password) | 69 | void Resource::authenticate(const QString &name, const QString &password) | ||
59 | { | 70 | { | ||
60 | QUrl url = baseUrl; | 71 | m_tfHttpHeader.clear(); | ||
61 | url = url.adjusted(QUrl::StripTrailingSlash); | 72 | auto job = getHttpAuthJob("Authorization: Basic " + QString (name + ':' + password).toUtf8().toBase64()); | ||
QLatin1String("Authorization: Basic ") + QString::fromUtf8((name.toUtf8() + ':' + password.toUtf8()).toBase64()) mwolff: QLatin1String("Authorization: Basic ") + QString::fromUtf8((name.toUtf8() + ':' + password. | |||||
62 | url.setPath(url.path() + '/' + "/authorizations"); | 73 | job->addMetaData("PropagateHttpHeader","true"); | ||
63 | QByteArray data = "{ \"scopes\": [\"repo\"], \"note\": \"KDevelop Github Provider\" }"; | 74 | connect(job, &KIO::StoredTransferJob::result, this, &Resource::slotAuthenticate); | ||
64 | KIO::StoredTransferJob *job = KIO::storedHttpPost(data, url, KIO::HideProgressInfo); | 75 | job->start(); | ||
65 | job->addMetaData("customHTTPHeader", "Authorization: Basic " + QString (name + ':' + password).toUtf8().toBase64()); | 76 | } | ||
77 | | ||||
78 | void Resource::twoFactorAuthenticate(const QString &code) { | ||||
kfunk: Newline before `{` | |||||
79 | auto job = getHttpAuthJob(m_tfHttpHeader + "\nX-GitHub-OTP: " + code); | ||||
mwolff: QLatin1String for the string literal in the middle | |||||
80 | m_tfHttpHeader.clear(); | ||||
66 | connect(job, &KIO::StoredTransferJob::result, this, &Resource::slotAuthenticate); | 81 | connect(job, &KIO::StoredTransferJob::result, this, &Resource::slotAuthenticate); | ||
67 | job->start(); | 82 | job->start(); | ||
68 | } | 83 | } | ||
69 | 84 | | |||
70 | void Resource::revokeAccess(const QString &id, const QString &name, const QString &password) | 85 | void Resource::revokeAccess(const QString &id, const QString &name, const QString &password) | ||
71 | { | 86 | { | ||
72 | QUrl url = baseUrl; | 87 | QUrl url = baseUrl; | ||
73 | url.setPath(url.path() + "/authorizations/" + id); | 88 | url.setPath(url.path() + "/authorizations/" + id); | ||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Line(s) | |||||
132 | 147 | | |||
133 | void Resource::slotAuthenticate(KJob *job) | 148 | void Resource::slotAuthenticate(KJob *job) | ||
134 | { | 149 | { | ||
135 | if (job->error()) { | 150 | if (job->error()) { | ||
136 | emit authenticated("", ""); | 151 | emit authenticated("", ""); | ||
137 | return; | 152 | return; | ||
138 | } | 153 | } | ||
139 | 154 | | |||
155 | const auto metaData = qobject_cast<KIO::StoredTransferJob*>(job)->metaData(); | ||||
kfunk: `const ...` | |||||
156 | if (metaData["responsecode"] == QStringLiteral("401")) { | ||||
mwolff: QStringLiteral for the responsecode | |||||
157 | const auto& header = metaData["HTTP-Headers"]; | ||||
kfunk: `const auto&` | |||||
mwolff: QSL | |||||
158 | if (header.contains("x-github-otp: required;", Qt::CaseSensitive)) { | ||||
kfunk: Isn't the casing fixed? Can it really vary? | |||||
mwolff: QSL | |||||
"x-github-otp: required: Still incorrect casing. Does this work? https://developer.github.com/v3/auth/ says it's X-GitHub-OTP: required; kfunk: `"x-github-otp: required`: Still incorrect casing. Does this work?
https://developer.github. | |||||
Than it lies :) zhigalin: > https://developer.github.com/v3/auth/ says it's X-GitHub-OTP: required;
Than it lies :)
It's… | |||||
kfunk: Ugh.
Please add a note then :) | |||||
159 | m_tfHttpHeader = qobject_cast<KIO::StoredTransferJob*>(job)->outgoingMetaData()["customHTTPHeader"]; | ||||
mwolff: QSL | |||||
Please pass m_tfHttpHeader via twoFactorRequested() instead I.e. add a parameter to that function. That way you can save the extra member in this class for m_tfHttpHeader. And in general: Please refrain from adding less known abbreviations to symbol names; it makes reading code harder. kfunk: Please pass `m_tfHttpHeader` via `twoFactorRequested()` instead I.e. add a parameter to that… | |||||
m_tfHttpHeader was created by the previous author, not me zhigalin: > And in general: Please refrain from adding less known abbreviations to symbol names; it makes… | |||||
160 | emit twoFactorRequested(); | ||||
161 | return; | ||||
162 | } | ||||
163 | } | ||||
164 | | ||||
140 | QJsonParseError error; | 165 | QJsonParseError error; | ||
141 | QJsonDocument doc = QJsonDocument::fromJson(qobject_cast<KIO::StoredTransferJob *>(job)->data(), &error); | 166 | QJsonDocument doc = QJsonDocument::fromJson(qobject_cast<KIO::StoredTransferJob *>(job)->data(), &error); | ||
142 | 167 | | |||
143 | qCDebug(GHPROVIDER) << "Response:" << doc; | 168 | qCDebug(GHPROVIDER) << "Response:" << doc; | ||
144 | 169 | | |||
145 | if (error.error == 0) { | 170 | if (error.error == 0) { | ||
146 | QVariantMap map = doc.toVariant().toMap(); | 171 | QVariantMap map = doc.toVariant().toMap(); | ||
147 | emit authenticated(map.value("id").toByteArray(), | 172 | emit authenticated(map.value("id").toByteArray(), | ||
▲ Show 20 Lines • Show All 46 Lines • Show Last 20 Lines |
get -> create