Skip to content

Commit a69c810

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

File tree

1 file changed

+4
-30
lines changed

1 file changed

+4
-30
lines changed

src/gui/creds/flow2auth.cpp

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -89,41 +89,16 @@ void Flow2Auth::fetchNewToken(const TokenAction action)
8989
job->setTimeout(qMin(30 * 1000ll, job->timeoutMsec()));
9090

9191
QObject::connect(job, &SimpleNetworkJob::finishedSignal, this, [this, action](QNetworkReply *reply) {
92-
const auto jsonData = reply->readAll();
93-
QJsonParseError jsonParseError{};
94-
const auto json = QJsonDocument::fromJson(jsonData, &jsonParseError).object();
92+
const auto json = handleRequest(reply);
9593
QString pollToken, pollEndpoint, loginUrl;
9694

97-
if (reply->error() == QNetworkReply::NoError && jsonParseError.error == QJsonParseError::NoError
98-
&& !json.isEmpty()) {
95+
if (!json.isEmpty()) {
9996
pollToken = json.value("poll").toObject().value("token").toString();
10097
pollEndpoint = json.value("poll").toObject().value("endpoint").toString();
101-
if (_enforceHttps && QUrl(pollEndpoint).scheme() != QStringLiteral("https")) {
102-
qCWarning(lcFlow2auth) << "Can not poll endpoint because the returned url" << pollEndpoint << "does not start with https";
103-
emit result(Error, tr("The polling 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."));
104-
return;
105-
}
10698
loginUrl = json["login"].toString();
10799
}
108100

109-
if (reply->error() != QNetworkReply::NoError || jsonParseError.error != QJsonParseError::NoError
110-
|| json.isEmpty() || pollToken.isEmpty() || pollEndpoint.isEmpty() || loginUrl.isEmpty()) {
111-
QString errorReason;
112-
if (const auto errorFromJson = json["error"].toString();
113-
!errorFromJson.isEmpty()) {
114-
errorReason = tr("Error returned from the server: <em>%1</em>")
115-
.arg(errorFromJson.toHtmlEscaped());
116-
} else if (reply->error() != QNetworkReply::NoError) {
117-
errorReason = tr("There was an error accessing the \"token\" endpoint: <br><em>%1</em>")
118-
.arg(reply->errorString().toHtmlEscaped());
119-
} else if (jsonParseError.error != QJsonParseError::NoError) {
120-
errorReason = tr("Could not parse the JSON returned from the server: <br><em>%1</em>")
121-
.arg(jsonParseError.errorString());
122-
} else {
123-
errorReason = tr("The reply from the server did not contain all expected fields");
124-
}
125-
qCWarning(lcFlow2auth) << "Error when getting the loginUrl" << json << errorReason;
126-
emit result(Error, errorReason);
101+
if (json.isEmpty() || pollToken.isEmpty() || pollEndpoint.isEmpty() || loginUrl.isEmpty()) {
127102
_pollTimer.stop();
128103
_isBusy = false;
129104
return;
@@ -154,8 +129,7 @@ void Flow2Auth::fetchNewToken(const TokenAction action)
154129
_pollTimer.start();
155130
}
156131

157-
158-
switch(action) {
132+
switch (action) {
159133
case actionOpenBrowser:
160134
// Try to open Browser
161135
if (!Utility::openBrowser(authorisationLink())) {

0 commit comments

Comments
 (0)