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

googledrivetokenrefresher.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/googledrive/googledrivetokenrefresher.h"
00027 #include "backends/cloud/googledrive/googledrivestorage.h"
00028 #include "backends/networking/curl/networkreadstream.h"
00029 #include "common/debug.h"
00030 #include "common/json.h"
00031 
00032 namespace Cloud {
00033 namespace GoogleDrive {
00034 
00035 GoogleDriveTokenRefresher::GoogleDriveTokenRefresher(GoogleDriveStorage *parent, Networking::JsonCallback callback, Networking::ErrorCallback ecb, const char *url):
00036     CurlJsonRequest(callback, ecb, url), _parentStorage(parent) {}
00037 
00038 GoogleDriveTokenRefresher::~GoogleDriveTokenRefresher() {}
00039 
00040 void GoogleDriveTokenRefresher::tokenRefreshed(Storage::BoolResponse response) {
00041     if (!response.value) {
00042         //failed to refresh token, notify user with NULL in original callback
00043         warning("GoogleDriveTokenRefresher: 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 GoogleDriveTokenRefresher::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, "GoogleDriveTokenRefresher")) {
00068         Common::JSONObject result = json->asObject();
00069         long httpResponseCode = -1;
00070         if (result.contains("error") && jsonIsObject(result.getVal("error"), "GoogleDriveTokenRefresher")) {
00071             //new token needed => request token & then retry original request
00072             if (_stream) {
00073                 httpResponseCode = _stream->httpResponseCode();
00074                 debug(9, "GoogleDriveTokenRefresher: code = %ld", httpResponseCode);
00075             }
00076 
00077             Common::JSONObject error = result.getVal("error")->asObject();
00078             bool irrecoverable = true;
00079 
00080             uint32 code = -1;
00081             Common::String message;
00082             if (jsonContainsIntegerNumber(error, "code", "GoogleDriveTokenRefresher")) {
00083                 code = error.getVal("code")->asIntegerNumber();
00084                 debug(9, "GoogleDriveTokenRefresher: code = %u", code);
00085             }
00086 
00087             if (jsonContainsString(error, "message", "GoogleDriveTokenRefresher")) {
00088                 message = error.getVal("message")->asString();
00089                 debug(9, "GoogleDriveTokenRefresher: message = %s", message.c_str());
00090             }
00091 
00092             if (code == 401 || message == "Invalid Credentials")
00093                 irrecoverable = false;
00094 
00095             if (irrecoverable) {
00096                 finishError(Networking::ErrorResponse(this, false, true, json->stringify(true), httpResponseCode));
00097                 delete json;
00098                 return;
00099             }
00100 
00101             pause();
00102             delete json;
00103             _parentStorage->getAccessToken(new Common::Callback<GoogleDriveTokenRefresher, Storage::BoolResponse>(this, &GoogleDriveTokenRefresher::tokenRefreshed));
00104             return;
00105         }
00106     }
00107 
00108     //notify user of success
00109     CurlJsonRequest::finishJson(json);
00110 }
00111 
00112 void GoogleDriveTokenRefresher::setHeaders(Common::Array<Common::String> &headers) {
00113     _headers = headers;
00114     curl_slist_free_all(_headersList);
00115     _headersList = 0;
00116     for (uint32 i = 0; i < headers.size(); ++i)
00117         CurlJsonRequest::addHeader(headers[i]);
00118 }
00119 
00120 void GoogleDriveTokenRefresher::addHeader(Common::String header) {
00121     _headers.push_back(header);
00122     CurlJsonRequest::addHeader(header);
00123 }
00124 
00125 } // End of namespace GoogleDrive
00126 } // End of namespace Cloud


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