27
27
#include < QJsonObject>
28
28
#include < QLoggingCategory>
29
29
#include < QMutex>
30
+ #include < QNetworkInformation>
30
31
#include < QNetworkReply>
31
32
32
33
#include < chrono>
@@ -37,6 +38,7 @@ Q_LOGGING_CATEGORY(lcHttpCredentials, "sync.credentials.http", QtInfoMsg)
37
38
38
39
namespace {
39
40
constexpr int TokenRefreshMaxRetries = 3 ;
41
+ constexpr std::chrono::seconds TokenRefreshDefaultTimeout = 30s;
40
42
constexpr int CredentialVersion = 1 ;
41
43
const char authenticationFailedC[] = " owncloud-authentication-failed" ;
42
44
@@ -270,29 +272,47 @@ bool HttpCredentials::refreshAccessTokenInternal(int tokenRefreshRetriesCount)
270
272
_oAuthJob = new AccountBasedOAuth (_account->sharedFromThis (), _account->accessManager ());
271
273
connect (_oAuthJob, &AccountBasedOAuth::refreshError, this , [tokenRefreshRetriesCount, this ](QNetworkReply::NetworkError error, const QString &) {
272
274
_oAuthJob->deleteLater ();
275
+
276
+ auto networkUnavailable = []() {
277
+ if (auto qni = QNetworkInformation::instance ()) {
278
+ if (qni->reachability () == QNetworkInformation::Reachability::Disconnected) {
279
+ return true ;
280
+ }
281
+ }
282
+
283
+ return false ;
284
+ };
285
+
273
286
int nextTry = tokenRefreshRetriesCount + 1 ;
274
287
std::chrono::seconds timeout = {};
275
- switch (error) {
276
- case QNetworkReply::ContentNotFoundError:
277
- // 404: bigip f5?
278
- timeout = 0s;
279
- break ;
280
- case QNetworkReply::HostNotFoundError:
281
- [[fallthrough]];
282
- case QNetworkReply::TimeoutError:
283
- [[fallthrough]];
284
- // Qt reports OperationCanceledError if the request timed out
285
- case QNetworkReply::OperationCanceledError:
286
- [[fallthrough]];
287
- case QNetworkReply::TemporaryNetworkFailureError:
288
- [[fallthrough]];
289
- // VPN not ready?
290
- case QNetworkReply::ConnectionRefusedError:
288
+
289
+ if (networkUnavailable ()) {
291
290
nextTry = 0 ;
292
- [[fallthrough]];
293
- default :
294
- timeout = 30s;
291
+ timeout = TokenRefreshDefaultTimeout;
292
+ } else {
293
+ switch (error) {
294
+ case QNetworkReply::ContentNotFoundError:
295
+ // 404: bigip f5?
296
+ timeout = 0s;
297
+ break ;
298
+ case QNetworkReply::HostNotFoundError:
299
+ [[fallthrough]];
300
+ case QNetworkReply::TimeoutError:
301
+ [[fallthrough]];
302
+ // Qt reports OperationCanceledError if the request timed out
303
+ case QNetworkReply::OperationCanceledError:
304
+ [[fallthrough]];
305
+ case QNetworkReply::TemporaryNetworkFailureError:
306
+ [[fallthrough]];
307
+ // VPN not ready?
308
+ case QNetworkReply::ConnectionRefusedError:
309
+ nextTry = 0 ;
310
+ [[fallthrough]];
311
+ default :
312
+ timeout = TokenRefreshDefaultTimeout;
313
+ }
295
314
}
315
+
296
316
if (nextTry >= TokenRefreshMaxRetries) {
297
317
qCWarning (lcHttpCredentials) << " Too many failed refreshes" << nextTry << " -> log out" ;
298
318
forgetSensitiveData ();
@@ -331,6 +351,8 @@ bool HttpCredentials::refreshAccessTokenInternal(int tokenRefreshRetriesCount)
331
351
332
352
void HttpCredentials::invalidateToken ()
333
353
{
354
+ qCWarning (lcHttpCredentials) << " Invalidating the credentials" ;
355
+
334
356
if (!_password.isEmpty ()) {
335
357
_previousPassword = _password;
336
358
}
0 commit comments