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

boxtokenrefresher.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/box/boxtokenrefresher.h"
00027 #include "backends/cloud/box/boxstorage.h"
00028 #include "backends/networking/curl/networkreadstream.h"
00029 #include "common/debug.h"
00030 #include "common/json.h"
00031 
00032 namespace Cloud {
00033 namespace Box {
00034 
00035 BoxTokenRefresher::BoxTokenRefresher(BoxStorage *parent, Networking::JsonCallback callback, Networking::ErrorCallback ecb, const char *url):
00036     CurlJsonRequest(callback, ecb, url), _parentStorage(parent) {}
00037 
00038 BoxTokenRefresher::~BoxTokenRefresher() {}
00039 
00040 void BoxTokenRefresher::tokenRefreshed(Storage::BoolResponse response) {
00041     if (!response.value) {
00042         //failed to refresh token, notify user with NULL in original callback
00043         warning("BoxTokenRefresher: 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 BoxTokenRefresher::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, "BoxTokenRefresher")) {
00068         Common::JSONObject result = json->asObject();
00069         if (result.contains("type") && result.getVal("type")->isString() && result.getVal("type")->asString() == "error") {
00070             //new token needed => request token & then retry original request
00071             long httpCode = -1;
00072             if (_stream) {
00073                 httpCode = _stream->httpResponseCode();
00074                 debug(9, "BoxTokenRefresher: code %ld", httpCode);
00075             }
00076 
00077             bool irrecoverable = true;
00078 
00079             Common::String code, message;
00080             if (jsonContainsString(result, "code", "BoxTokenRefresher")) {
00081                 code = result.getVal("code")->asString();
00082                 debug(9, "BoxTokenRefresher: code = %s", code.c_str());
00083             }
00084 
00085             if (jsonContainsString(result, "message", "BoxTokenRefresher")) {
00086                 message = result.getVal("message")->asString();
00087                 debug(9, "BoxTokenRefresher: message = %s", message.c_str());
00088             }
00089 
00090             //TODO: decide when token refreshment will help
00091             //for now refreshment is used only when HTTP 401 is passed in finishError()
00092             //if (code == "unauthenticated") irrecoverable = false;
00093 
00094             if (irrecoverable) {
00095                 finishError(Networking::ErrorResponse(this, false, true, json->stringify(true), httpCode));
00096                 delete json;
00097                 return;
00098             }
00099 
00100             pause();
00101             delete json;
00102             _parentStorage->getAccessToken(new Common::Callback<BoxTokenRefresher, Storage::BoolResponse>(this, &BoxTokenRefresher::tokenRefreshed));
00103             return;
00104         }
00105     }
00106 
00107     //notify user of success
00108     CurlJsonRequest::finishJson(json);
00109 }
00110 
00111 void BoxTokenRefresher::finishError(Networking::ErrorResponse error) {
00112     if (error.httpResponseCode == 401) { // invalid_token
00113         pause();
00114         _parentStorage->getAccessToken(new Common::Callback<BoxTokenRefresher, Storage::BoolResponse>(this, &BoxTokenRefresher::tokenRefreshed));
00115         return;
00116     }
00117 
00118     // there are also 400 == invalid_request and 403 == insufficient_scope
00119     // but TokenRefresher is there to refresh token when it's invalid only
00120 
00121     Request::finishError(error);
00122 }
00123 
00124 void BoxTokenRefresher::setHeaders(Common::Array<Common::String> &headers) {
00125     _headers = headers;
00126     curl_slist_free_all(_headersList);
00127     _headersList = 0;
00128     for (uint32 i = 0; i < headers.size(); ++i)
00129         CurlJsonRequest::addHeader(headers[i]);
00130 }
00131 
00132 void BoxTokenRefresher::addHeader(Common::String header) {
00133     _headers.push_back(header);
00134     CurlJsonRequest::addHeader(header);
00135 }
00136 
00137 } // End of namespace Box
00138 } // End of namespace Cloud


Generated on Sat May 25 2019 05:00:39 for ResidualVM by doxygen 1.7.1
curved edge   curved edge