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, "", -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")
00098                 irrecoverable = false;
00099 
00100             if (irrecoverable) {
00101                 finishError(Networking::ErrorResponse(this, false, true, json->stringify(true), httpResponseCode));
00102                 delete json;
00103                 return;
00104             }
00105 
00106             pause();
00107             delete json;
00108             _parentStorage->getAccessToken(new Common::Callback<OneDriveTokenRefresher, Storage::BoolResponse>(this, &OneDriveTokenRefresher::tokenRefreshed));
00109             return;
00110         }
00111     }
00112 
00113     //notify user of success
00114     CurlJsonRequest::finishJson(json);
00115 }
00116 
00117 void OneDriveTokenRefresher::setHeaders(Common::Array<Common::String> &headers) {
00118     _headers = headers;
00119     curl_slist_free_all(_headersList);
00120     _headersList = 0;
00121     for (uint32 i = 0; i < headers.size(); ++i)
00122         CurlJsonRequest::addHeader(headers[i]);
00123 }
00124 
00125 void OneDriveTokenRefresher::addHeader(Common::String header) {
00126     _headers.push_back(header);
00127     CurlJsonRequest::addHeader(header);
00128 }
00129 
00130 } // End of namespace OneDrive
00131 } // End of namespace Cloud


Generated on Sat May 18 2019 05:01:11 for ResidualVM by doxygen 1.7.1
curved edge   curved edge