Skip to content

Commit be21a0a

Browse files
committed
chore(Flow2Auth): modernize slotPollTimerTimeout.
Move part of the server repsonse logic to another function. Signed-off-by: Camila Ayres <hello@camilasan.com>
1 parent f1ff72f commit be21a0a

2 files changed

Lines changed: 55 additions & 36 deletions

File tree

src/gui/creds/flow2auth.cpp

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void Flow2Auth::copyLinkToClipboard()
6767

6868
void Flow2Auth::fetchNewToken(const TokenAction action)
6969
{
70-
if(_isBusy) {
70+
if (_isBusy) {
7171
return;
7272
}
7373

@@ -177,7 +177,7 @@ void Flow2Auth::fetchNewToken(const TokenAction action)
177177

178178
void Flow2Auth::slotPollTimerTimeout()
179179
{
180-
if(_isBusy || !_hasToken) {
180+
if (_isBusy || !_hasToken) {
181181
return;
182182
}
183183

@@ -203,47 +203,17 @@ void Flow2Auth::slotPollTimerTimeout()
203203
job->setTimeout(qMin(30 * 1000ll, job->timeoutMsec()));
204204

205205
QObject::connect(job, &SimpleNetworkJob::finishedSignal, this, [this](QNetworkReply *reply) {
206-
const auto jsonData = reply->readAll();
207-
QJsonParseError jsonParseError{};
208-
const QJsonObject json = QJsonDocument::fromJson(jsonData, &jsonParseError).object();
206+
const QJsonObject json = handleRequest(reply);
209207
QUrl serverUrl;
210208
QString loginName, appPassword;
211209

212-
if (reply->error() == QNetworkReply::NoError && jsonParseError.error == QJsonParseError::NoError
213-
&& !json.isEmpty()) {
210+
if (!json.isEmpty()) {
214211
serverUrl = json["server"].toString();
215-
if (_enforceHttps && serverUrl.scheme() != QStringLiteral("https")) {
216-
qCWarning(lcFlow2auth) << "Returned server url" << serverUrl << "does not start with https";
217-
emit result(Error, tr("The returned server URL does not start with HTTPS despite the login URL started with HTTPS. Login will not be possible because this might be a security issue. Please contact your administrator."));
218-
return;
219-
}
220212
loginName = json["loginName"].toString();
221213
appPassword = json["appPassword"].toString();
222214
}
223215

224-
if (reply->error() != QNetworkReply::NoError || jsonParseError.error != QJsonParseError::NoError
225-
|| json.isEmpty() || serverUrl.isEmpty() || loginName.isEmpty() || appPassword.isEmpty()) {
226-
QString errorReason;
227-
if (const QString errorFromJson = json["error"].toString();
228-
!errorFromJson.isEmpty()) {
229-
errorReason = tr("Error returned from the server: <em>%1</em>")
230-
.arg(errorFromJson.toHtmlEscaped());
231-
} else if (reply->error() != QNetworkReply::NoError) {
232-
errorReason = tr("There was an error accessing the \"token\" endpoint: <br><em>%1</em>")
233-
.arg(reply->errorString().toHtmlEscaped());
234-
} else if (jsonParseError.error != QJsonParseError::NoError) {
235-
errorReason = tr("Could not parse the JSON returned from the server: <br><em>%1</em>")
236-
.arg(jsonParseError.errorString());
237-
} else {
238-
errorReason = tr("The reply from the server did not contain all expected fields");
239-
}
240-
qCDebug(lcFlow2auth) << "Error when polling for the appPassword" << json << errorReason;
241-
242-
// We get a 404 until authentication is done, so don't show this error in the GUI.
243-
if (reply->error() != QNetworkReply::ContentNotFoundError) {
244-
emit result(Error, errorReason);
245-
}
246-
216+
if (json.isEmpty() || serverUrl.isEmpty() || loginName.isEmpty() || appPassword.isEmpty()) {
247217
// Forget sensitive data
248218
appPassword.clear();
249219
loginName.clear();
@@ -276,10 +246,56 @@ void Flow2Auth::slotPollTimerTimeout()
276246
});
277247
}
278248

249+
QJsonObject Flow2Auth::handleRequest(QNetworkReply *reply)
250+
{
251+
const auto jsonData = reply->readAll();
252+
QJsonParseError jsonParseError{};
253+
const auto json = QJsonDocument::fromJson(jsonData, &jsonParseError).object();
254+
255+
if (reply->error() == QNetworkReply::NoError && jsonParseError.error == QJsonParseError::NoError
256+
&& !json.isEmpty()) {
257+
const QUrl serverUrl = json["server"].toString();
258+
if (_enforceHttps && serverUrl.scheme() != QStringLiteral("https")) {
259+
qCWarning(lcFlow2auth) << "Returned server url" << serverUrl << "does not start with https";
260+
emit result(Error, tr("The returned server URL does not start with HTTPS despite the login URL started with HTTPS. Login will not be possible because this might be a security issue. Please contact your administrator."));
261+
return {};
262+
}
263+
}
264+
265+
if (reply->error() != QNetworkReply::NoError || jsonParseError.error != QJsonParseError::NoError) {
266+
QString errorReason;
267+
if (const QString errorFromJson = json["error"].toString();
268+
!errorFromJson.isEmpty()) {
269+
errorReason = tr("Error returned from the server: <em>%1</em>")
270+
.arg(errorFromJson.toHtmlEscaped());
271+
} else if (reply->error() != QNetworkReply::NoError) {
272+
errorReason = tr("There was an error accessing the \"token\" endpoint: <br><em>%1</em>")
273+
.arg(reply->errorString().toHtmlEscaped());
274+
} else if (jsonParseError.error != QJsonParseError::NoError) {
275+
errorReason = tr("Could not parse the JSON returned from the server: <br><em>%1</em>")
276+
.arg(jsonParseError.errorString());
277+
} else if (json.isEmpty()) {
278+
errorReason = tr("The reply from the server did not contain all expected fields")
279+
.arg(jsonParseError.errorString());
280+
}
281+
282+
qCDebug(lcFlow2auth) << "Error when requesting:" << reply->url() << "- json returned:" << json << "- error:" << errorReason;
283+
284+
// We get a 404 until authentication is done, so don't show this error in the GUI.
285+
if (reply->error() != QNetworkReply::ContentNotFoundError) {
286+
emit result(Error, errorReason);
287+
}
288+
289+
return {};
290+
}
291+
292+
return json;
293+
}
294+
279295
void Flow2Auth::slotPollNow()
280296
{
281297
// poll now if we're not already doing so
282-
if(_isBusy || !_hasToken) {
298+
if (_isBusy || !_hasToken) {
283299
return;
284300
}
285301

src/gui/creds/flow2auth.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <QTimer>
2020
#include "accountfwd.h"
2121

22+
class QNetworkReply;
23+
2224
namespace OCC {
2325

2426
/**
@@ -71,6 +73,7 @@ private slots:
7173

7274
private:
7375
void fetchNewToken(const TokenAction action);
76+
[[nodiscard]] QJsonObject handleRequest(QNetworkReply *reply);
7477

7578
Account *_account;
7679
QUrl _loginUrl;

0 commit comments

Comments
 (0)