ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

onedrivetokenrefresher.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #define FORBIDDEN_SYMBOL_ALLOW_ALL
00024 
00025 #include <curl/curl.h>
00026 #include "backends/cloud/onedrive/onedrivetokenrefresher.h"
00027 #include "backends/cloud/onedrive/onedrivestorage.h"
00028 #include "backends/networking/curl/networkreadstream.h"
00029 #include "common/debug.h"
00030 #include "common/json.h"
00031 
00032 namespace Cloud {
00033 namespace OneDrive {
00034 
00035 OneDriveTokenRefresher::OneDriveTokenRefresher(OneDriveStorage *parent, Networking::JsonCallback callback, Networking::ErrorCallback ecb, const char *url):
00036     CurlJsonRequest(callback, ecb, url), _parentStorage(parent) {}
00037 
00038 OneDriveTokenRefresher::~OneDriveTokenRefresher() {}
00039 
00040 void OneDriveTokenRefresher::tokenRefreshed(Storage::BoolResponse response) {
00041     if (!response.value) {
00042         //failed to refresh token, notify user with NULL in original callback
00043         warning("OneDriveTokenRefresher: failed to refresh token");
00044         finishError(Networking::ErrorResponse(this, false, true, "OneDriveTokenRefresher::tokenRefreshed: failed to refresh token", -1));
00045         return;
00046     }
00047 
00048     //update headers: first change header with token, then pass those to request
00049     for (uint32 i = 0; i < _headers.size(); ++i) {
00050         if (_headers[i].contains("Authorization")) {
00051             _headers[i] = "Authorization: bearer " + _parentStorage->accessToken();
00052         }
00053     }
00054     setHeaders(_headers);
00055 
00056     //successfully received refreshed token, can restart the original request now
00057     retry(0);
00058 }
00059 
00060 void OneDriveTokenRefresher::finishJson(Common::JSONValue *json) {
00061     if (!json) {
00062         //that's probably not an error (200 OK)
00063         CurlJsonRequest::finishJson(nullptr);
00064         return;
00065     }
00066 
00067     if (jsonIsObject(json, "OneDriveTokenRefresher")) {
00068         Common::JSONObject result = json->asObject();
00069         long httpResponseCode = -1;
00070         if (result.contains("error") && jsonIsObject(result.getVal("error"), "OneDriveTokenRefresher")) {
00071             //new token needed => request token & then retry original request
00072             if (_stream) {
00073                 httpResponseCode = _stream->httpResponseCode();
00074                 debug(9, "OneDriveTokenRefresher: code = %ld", httpResponseCode);
00075             }
00076 
00077             Common::JSONObject error = result.getVal("error")->asObject();
00078             bool irrecoverable = true;
00079 
00080             Common::String code, message;
00081             if (jsonContainsString(error, "code", "OneDriveTokenRefresher")) {
00082                 code = error.getVal("code")->asString();
00083                 debug(9, "OneDriveTokenRefresher: code = %s", code.c_str());
00084             }
00085 
00086             if (jsonContainsString(error, "message", "OneDriveTokenRefresher")) {
00087                 message = error.getVal("message")->asString();
00088                 debug(9, "OneDriveTokenRefresher: message = %s", message.c_str());
00089             }
00090 
00091             //determine whether token refreshing would help in this situation
00092             if (code == "itemNotFound") {
00093                 if (message.contains("application ID"))
00094                     irrecoverable = false;
00095             }
00096 
00097             if (code == "unauthenticated" || code == "InvalidAuthenticationToken")
00098                 irrecoverable = false;
00099 
00100             if (irrecoverable) {
00101                 Common::String errorContents = "<irrecoverable> " + json->stringify(true);
00102                 finishError(Networking::ErrorResponse(this, false, true, errorContents, httpResponseCode));
00103                 delete json;
00104                 return;
00105             }
00106 
00107             pause();
00108             delete json;
00109             _parentStorage->refreshAccessToken(new Common::Callback<OneDriveTokenRefresher, Storage::BoolResponse>(this, &OneDriveTokenRefresher::tokenRefreshed));
00110             return;
00111         }
00112     }
00113 
00114     //notify user of success
00115     CurlJsonRequest::finishJson(json);
00116 }
00117 
00118 void OneDriveTokenRefresher::finishError(Networking::ErrorResponse error) {
00119     if (error.failed) {
00120         Common::JSONValue *value = Common::JSON::parse(error.response.c_str());
00121 
00122         //somehow OneDrive returns JSON with '.' in unexpected places, try fixing it
00123         if (!value) {
00124             Common::String fixedResponse = error.response;
00125             for (uint32 i = 0; i < fixedResponse.size(); ++i) {
00126                 if (fixedResponse[i] == '.')
00127                     fixedResponse.replace(i, 1, " ");
00128             }
00129             value = Common::JSON::parse(fixedResponse.c_str());
00130         }
00131 
00132         if (value) {
00133             finishJson(value);
00134             return;
00135         }
00136     }
00137 
00138     Request::finishError(error); //call closest base class's method
00139 }
00140 
00141 void OneDriveTokenRefresher::setHeaders(Common::Array<Common::String> &headers) {
00142     _headers = headers;
00143     curl_slist_free_all(_headersList);
00144     _headersList = 0;
00145     for (uint32 i = 0; i < headers.size(); ++i)
00146         CurlJsonRequest::addHeader(headers[i]);
00147 }
00148 
00149 void OneDriveTokenRefresher::addHeader(Common::String header) {
00150     _headers.push_back(header);
00151     CurlJsonRequest::addHeader(header);
00152 }
00153 
00154 } // End of namespace OneDrive
00155 } // End of namespace Cloud


Generated on Sat Jul 11 2020 05:01:03 for ResidualVM by doxygen 1.7.1
curved edge   curved edge