From 166d1121e5973868b24aa3db6156028edb1d6a72 Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Tue, 26 Jul 2016 13:45:24 +0600 Subject: CLOUD: Update TokenRefreshers Box's, Google Drive's and OneDrive's token refreshing requests have more JSON checks now. --- backends/cloud/box/boxtokenrefresher.cpp | 67 ++++++++++---------- .../googledrive/googledrivetokenrefresher.cpp | 70 ++++++++++---------- backends/cloud/onedrive/onedrivetokenrefresher.cpp | 74 +++++++++++----------- 3 files changed, 109 insertions(+), 102 deletions(-) (limited to 'backends/cloud') diff --git a/backends/cloud/box/boxtokenrefresher.cpp b/backends/cloud/box/boxtokenrefresher.cpp index c798b97343..ca05eef838 100644 --- a/backends/cloud/box/boxtokenrefresher.cpp +++ b/backends/cloud/box/boxtokenrefresher.cpp @@ -63,41 +63,44 @@ void BoxTokenRefresher::finishJson(Common::JSONValue *json) { return; } - Common::JSONObject result = json->asObject(); - if (result.contains("type") && result.getVal("type")->isString() && result.getVal("type")->asString() == "error") { - //new token needed => request token & then retry original request - long httpCode = -1; - if (_stream) { - httpCode = _stream->httpResponseCode(); - debug(9, "BoxTokenRefresher: code %ld", httpCode); - } - - bool irrecoverable = true; - - Common::String code, message; - if (result.contains("code")) { - code = result.getVal("code")->asString(); - debug(9, "BoxTokenRefresher: code = %s", code.c_str()); - } - - if (result.contains("message")) { - message = result.getVal("message")->asString(); - debug(9, "BoxTokenRefresher: message = %s", message.c_str()); - } - - //TODO: decide when token refreshment will help - //if (code == "unauthenticated") irrecoverable = false; - - if (irrecoverable) { - finishError(Networking::ErrorResponse(this, false, true, json->stringify(true), httpCode)); + if (jsonIsObject(json, "BoxTokenRefresher")) { + Common::JSONObject result = json->asObject(); + if (result.contains("type") && result.getVal("type")->isString() && result.getVal("type")->asString() == "error") { + //new token needed => request token & then retry original request + long httpCode = -1; + if (_stream) { + httpCode = _stream->httpResponseCode(); + debug(9, "BoxTokenRefresher: code %ld", httpCode); + } + + bool irrecoverable = true; + + Common::String code, message; + if (jsonContainsString(result, "code", "BoxTokenRefresher")) { + code = result.getVal("code")->asString(); + debug(9, "BoxTokenRefresher: code = %s", code.c_str()); + } + + if (jsonContainsString(result, "message", "BoxTokenRefresher")) { + message = result.getVal("message")->asString(); + debug(9, "BoxTokenRefresher: message = %s", message.c_str()); + } + + //TODO: decide when token refreshment will help + //for now refreshment is used only when HTTP 401 is passed in finishError() + //if (code == "unauthenticated") irrecoverable = false; + + if (irrecoverable) { + finishError(Networking::ErrorResponse(this, false, true, json->stringify(true), httpCode)); + delete json; + return; + } + + pause(); delete json; + _parentStorage->getAccessToken(new Common::Callback(this, &BoxTokenRefresher::tokenRefreshed)); return; } - - pause(); - delete json; - _parentStorage->getAccessToken(new Common::Callback(this, &BoxTokenRefresher::tokenRefreshed)); - return; } //notify user of success diff --git a/backends/cloud/googledrive/googledrivetokenrefresher.cpp b/backends/cloud/googledrive/googledrivetokenrefresher.cpp index 99661c262e..7d68510655 100644 --- a/backends/cloud/googledrive/googledrivetokenrefresher.cpp +++ b/backends/cloud/googledrive/googledrivetokenrefresher.cpp @@ -63,43 +63,45 @@ void GoogleDriveTokenRefresher::finishJson(Common::JSONValue *json) { return; } - Common::JSONObject result = json->asObject(); - long httpResponseCode = -1; - if (result.contains("error")) { - //new token needed => request token & then retry original request - if (_stream) { - httpResponseCode = _stream->httpResponseCode(); - debug(9, "GoogleDriveTokenRefresher: code = %ld", httpResponseCode); - } - - Common::JSONObject error = result.getVal("error")->asObject(); - bool irrecoverable = true; - - uint32 code = -1; - Common::String message; - if (error.contains("code") && error.getVal("code")->isIntegerNumber()) { - code = error.getVal("code")->asIntegerNumber(); - debug(9, "GoogleDriveTokenRefresher: code = %u", code); - } - - if (error.contains("message")) { - message = error.getVal("message")->asString(); - debug(9, "GoogleDriveTokenRefresher: message = %s", message.c_str()); - } - - if (code == 401 || message == "Invalid Credentials") - irrecoverable = false; - - if (irrecoverable) { - finishError(Networking::ErrorResponse(this, false, true, json->stringify(true), httpResponseCode)); + if (jsonIsObject(json, "GoogleDriveTokenRefresher")) { + Common::JSONObject result = json->asObject(); + long httpResponseCode = -1; + if (jsonContainsAttribute(result, "error", "GoogleDriveTokenRefresher") && jsonIsObject(result.getVal("error"), "GoogleDriveTokenRefresher")) { + //new token needed => request token & then retry original request + if (_stream) { + httpResponseCode = _stream->httpResponseCode(); + debug(9, "GoogleDriveTokenRefresher: code = %ld", httpResponseCode); + } + + Common::JSONObject error = result.getVal("error")->asObject(); + bool irrecoverable = true; + + uint32 code = -1; + Common::String message; + if (jsonContainsIntegerNumber(error, "code", "GoogleDriveTokenRefresher")) { + code = error.getVal("code")->asIntegerNumber(); + debug(9, "GoogleDriveTokenRefresher: code = %u", code); + } + + if (jsonContainsString(error, "message", "GoogleDriveTokenRefresher")) { + message = error.getVal("message")->asString(); + debug(9, "GoogleDriveTokenRefresher: message = %s", message.c_str()); + } + + if (code == 401 || message == "Invalid Credentials") + irrecoverable = false; + + if (irrecoverable) { + finishError(Networking::ErrorResponse(this, false, true, json->stringify(true), httpResponseCode)); + delete json; + return; + } + + pause(); delete json; + _parentStorage->getAccessToken(new Common::Callback(this, &GoogleDriveTokenRefresher::tokenRefreshed)); return; } - - pause(); - delete json; - _parentStorage->getAccessToken(new Common::Callback(this, &GoogleDriveTokenRefresher::tokenRefreshed)); - return; } //notify user of success diff --git a/backends/cloud/onedrive/onedrivetokenrefresher.cpp b/backends/cloud/onedrive/onedrivetokenrefresher.cpp index 5e3bce96af..5ee2772ba7 100644 --- a/backends/cloud/onedrive/onedrivetokenrefresher.cpp +++ b/backends/cloud/onedrive/onedrivetokenrefresher.cpp @@ -63,48 +63,50 @@ void OneDriveTokenRefresher::finishJson(Common::JSONValue *json) { return; } - Common::JSONObject result = json->asObject(); - long httpResponseCode = -1; - if (result.contains("error")) { - //new token needed => request token & then retry original request - if (_stream) { - httpResponseCode = _stream->httpResponseCode(); - debug(9, "OneDriveTokenRefresher: code = %ld", httpResponseCode); - } - - Common::JSONObject error = result.getVal("error")->asObject(); - bool irrecoverable = true; - - Common::String code, message; - if (error.contains("code")) { - code = error.getVal("code")->asString(); - debug(9, "OneDriveTokenRefresher: code = %s", code.c_str()); - } - - if (error.contains("message")) { - message = error.getVal("message")->asString(); - debug(9, "OneDriveTokenRefresher: message = %s", message.c_str()); - } - - //determine whether token refreshing would help in this situation - if (code == "itemNotFound") { - if (message.contains("application ID")) + if (jsonIsObject(json, "OneDriveTokenRefresher")) { + Common::JSONObject result = json->asObject(); + long httpResponseCode = -1; + if (jsonContainsAttribute(result, "error", "OneDriveTokenRefresher") && jsonIsObject(result.getVal("error"), "OneDriveTokenRefresher")) { + //new token needed => request token & then retry original request + if (_stream) { + httpResponseCode = _stream->httpResponseCode(); + debug(9, "OneDriveTokenRefresher: code = %ld", httpResponseCode); + } + + Common::JSONObject error = result.getVal("error")->asObject(); + bool irrecoverable = true; + + Common::String code, message; + if (jsonContainsString(error, "code", "OneDriveTokenRefresher")) { + code = error.getVal("code")->asString(); + debug(9, "OneDriveTokenRefresher: code = %s", code.c_str()); + } + + if (jsonContainsString(error, "message", "OneDriveTokenRefresher")) { + message = error.getVal("message")->asString(); + debug(9, "OneDriveTokenRefresher: message = %s", message.c_str()); + } + + //determine whether token refreshing would help in this situation + if (code == "itemNotFound") { + if (message.contains("application ID")) + irrecoverable = false; + } + + if (code == "unauthenticated") irrecoverable = false; - } - if (code == "unauthenticated") - irrecoverable = false; + if (irrecoverable) { + finishError(Networking::ErrorResponse(this, false, true, json->stringify(true), httpResponseCode)); + delete json; + return; + } - if (irrecoverable) { - finishError(Networking::ErrorResponse(this, false, true, json->stringify(true), httpResponseCode)); + pause(); delete json; + _parentStorage->getAccessToken(new Common::Callback(this, &OneDriveTokenRefresher::tokenRefreshed)); return; } - - pause(); - delete json; - _parentStorage->getAccessToken(new Common::Callback(this, &OneDriveTokenRefresher::tokenRefreshed)); - return; } //notify user of success -- cgit v1.2.3