From 64fd11609205bb7f1082975d2e2452a6f6bc7194 Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Mon, 15 Jul 2019 19:42:25 +0700 Subject: CLOUD: Add BaseStorage, which does auth via cloud.scummvm.org --- backends/cloud/basestorage.cpp | 104 ++++++++++++++++++++++++++++++ backends/cloud/basestorage.h | 75 +++++++++++++++++++++ backends/cloud/dropbox/dropboxstorage.cpp | 50 ++------------ backends/cloud/dropbox/dropboxstorage.h | 19 ++++-- backends/module.mk | 1 + 5 files changed, 196 insertions(+), 53 deletions(-) create mode 100644 backends/cloud/basestorage.cpp create mode 100644 backends/cloud/basestorage.h diff --git a/backends/cloud/basestorage.cpp b/backends/cloud/basestorage.cpp new file mode 100644 index 0000000000..9b31b4cad3 --- /dev/null +++ b/backends/cloud/basestorage.cpp @@ -0,0 +1,104 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "backends/cloud/basestorage.h" +#include "backends/cloud/cloudmanager.h" +#include "backends/networking/curl/connectionmanager.h" +#include "backends/networking/curl/curljsonrequest.h" +#include "common/config-manager.h" +#include "common/debug.h" +#include "common/json.h" + +namespace Cloud { + +BaseStorage::BaseStorage() {} + +BaseStorage::BaseStorage(Common::String token, Common::String refreshToken): + _token(token), _refreshToken(refreshToken) {} + +BaseStorage::~BaseStorage() {} + +void BaseStorage::getAccessToken(Common::String code) { + Networking::JsonCallback callback = new Common::Callback(this, &BaseStorage::codeFlowComplete); + Networking::ErrorCallback errorCallback = new Common::Callback(this, &BaseStorage::codeFlowFailed); + + Common::String url = Common::String::format("https://cloud.scummvm.org/%s/%s", cloudProvider().c_str(), code.c_str()); + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, errorCallback, url); + + addRequest(request); +} + +void BaseStorage::codeFlowComplete(Networking::JsonResponse response) { + Common::JSONValue *json = (Common::JSONValue *)response.value; + if (json == nullptr) { + debug(9, "BaseStorage::codeFlowComplete: got NULL instead of JSON!"); + CloudMan.removeStorage(this); + return; + } + + if (!json->isObject()) { + debug(9, "BaseStorage::codeFlowComplete: passed JSON is not an object!"); + CloudMan.removeStorage(this); + delete json; + return; + } + + Common::JSONObject result = json->asObject(); + if (!Networking::CurlJsonRequest::jsonContainsAttribute(result, "error", "BaseStorage::codeFlowComplete")) { + warning("BaseStorage: bad response, no 'error' attribute passed"); + debug(9, "%s", json->stringify(true).c_str()); + CloudMan.removeStorage(this); + delete json; + return; + } + + if (result.getVal("error")->asBool()) { + Common::String errorMessage = "{error: true}, message is missing"; + if (Networking::CurlJsonRequest::jsonContainsString(result, "message", "BaseStorage::codeFlowComplete")) { + errorMessage = result.getVal("message")->asString(); + } + warning("BaseStorage: response says error occurred: %s", errorMessage.c_str()); + CloudMan.removeStorage(this); + delete json; + return; + } + + if (!Networking::CurlJsonRequest::jsonContainsString(result, "access_token", "BaseStorage::codeFlowComplete")) { + warning("BaseStorage: bad response, no 'access_token' attribute passed"); + debug(9, "%s", json->stringify(true).c_str()); + CloudMan.removeStorage(this); + } else { + _token = result.getVal("access_token")->asString(); + CloudMan.replaceStorage(this, storageIndex()); + ConfMan.flushToDisk(); + } + + delete json; +} + +void BaseStorage::codeFlowFailed(Networking::ErrorResponse error) { + debug(9, "BaseStorage: code flow failed (%s, %ld):", (error.failed ? "failed" : "interrupted"), error.httpResponseCode); + debug(9, "%s", error.response.c_str()); + CloudMan.removeStorage(this); +} + +} // End of namespace Cloud diff --git a/backends/cloud/basestorage.h b/backends/cloud/basestorage.h new file mode 100644 index 0000000000..cca6e1bb48 --- /dev/null +++ b/backends/cloud/basestorage.h @@ -0,0 +1,75 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef BACKENDS_CLOUD_BASE_STORAGE_H +#define BACKENDS_CLOUD_BASE_STORAGE_H + +#include "backends/cloud/storage.h" +#include "backends/networking/curl/curljsonrequest.h" + +namespace Cloud { + +class BaseStorage: public Cloud::Storage { +protected: + /** Storage's access and refresh tokens. */ + Common::String _token, _refreshToken; + + /** + * Gets token from cloud.scummvm.org using given code. + * Base implementation for storages with common auth procedure. + */ + virtual void getAccessToken(Common::String code); + + /** + * Handles JSON response which should contain access token requested + * with getAccessToken(). + */ + virtual void codeFlowComplete(Networking::JsonResponse response); + + /** + * Handles network errors occurred while getting access token requested + * with getAccessToken(). + */ + virtual void codeFlowFailed(Networking::ErrorResponse error); + + /** + * Return cloud provider name, used in cloud.scummvm.org endpoints. + * @return cloud provider (for example, "dropbox"). + */ + virtual Common::String cloudProvider() = 0; + + /** + * Return CloudManager's StorageID for this storage. + * @return StorageID corresponding to this storage (for example, + * kStorageDropboxId). + */ + virtual uint32 storageIndex() = 0; + +public: + BaseStorage(); + BaseStorage(Common::String token, Common::String refreshToken); + virtual ~BaseStorage(); +}; + +} // End of namespace Cloud + +#endif diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index 63c21fb5db..53ed704993 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -42,61 +42,19 @@ namespace Cloud { namespace Dropbox { -#define DROPBOX_OAUTH2_TOKEN "https://cloud.scummvm.org/dropbox/token/" #define DROPBOX_API_FILES_DOWNLOAD "https://content.dropboxapi.com/2/files/download" -DropboxStorage::DropboxStorage(Common::String accessToken, bool unused): _token(accessToken) {} +DropboxStorage::DropboxStorage(Common::String accessToken, bool unused): BaseStorage(accessToken, "") {} -DropboxStorage::DropboxStorage(Common::String code) { +DropboxStorage::DropboxStorage(Common::String code): BaseStorage() { getAccessToken(code); } DropboxStorage::~DropboxStorage() {} -void DropboxStorage::getAccessToken(Common::String code) { - Networking::JsonCallback callback = new Common::Callback(this, &DropboxStorage::codeFlowComplete); - Networking::ErrorCallback errorCallback = new Common::Callback(this, &DropboxStorage::codeFlowFailed); +Common::String DropboxStorage::cloudProvider() { return "dropbox"; } - Common::String url = Common::String(DROPBOX_OAUTH2_TOKEN) + code; - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, errorCallback, url); - - addRequest(request); -} - -void DropboxStorage::codeFlowComplete(Networking::JsonResponse response) { - Common::JSONValue *json = (Common::JSONValue *)response.value; - if (json == nullptr) { - debug(9, "DropboxStorage::codeFlowComplete: got NULL instead of JSON!"); - CloudMan.removeStorage(this); - return; - } - - if (!json->isObject()) { - debug(9, "DropboxStorage::codeFlowComplete: Passed JSON is not an object!"); - CloudMan.removeStorage(this); - delete json; - return; - } - - Common::JSONObject result = json->asObject(); - if (!Networking::CurlJsonRequest::jsonContainsString(result, "access_token", "DropboxStorage::codeFlowComplete")) { - warning("DropboxStorage: bad response, no token passed"); - debug(9, "%s", json->stringify(true).c_str()); - CloudMan.removeStorage(this); - } else { - _token = result.getVal("access_token")->asString(); - CloudMan.replaceStorage(this, kStorageDropboxId); - ConfMan.flushToDisk(); - } - - delete json; -} - -void DropboxStorage::codeFlowFailed(Networking::ErrorResponse error) { - debug(9, "DropboxStorage: code flow failed (%s, %ld):", (error.failed ? "failed" : "interrupted"), error.httpResponseCode); - debug(9, "%s", error.response.c_str()); - CloudMan.removeStorage(this); -} +uint32 DropboxStorage::storageIndex() { return kStorageDropboxId; } void DropboxStorage::saveConfig(Common::String keyPrefix) { ConfMan.set(keyPrefix + "access_token", _token, ConfMan.kCloudDomain); diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index 7505edd461..b82e38e554 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -23,22 +23,27 @@ #ifndef BACKENDS_CLOUD_DROPBOX_STORAGE_H #define BACKENDS_CLOUD_DROPBOX_STORAGE_H -#include "backends/cloud/storage.h" +#include "backends/cloud/basestorage.h" #include "common/callback.h" #include "backends/networking/curl/curljsonrequest.h" namespace Cloud { namespace Dropbox { -class DropboxStorage: public Cloud::Storage { - Common::String _token; - +class DropboxStorage: public Cloud::BaseStorage { /** This private constructor is called from loadFromConfig(). */ DropboxStorage(Common::String token, bool unused); - void getAccessToken(Common::String code); - void codeFlowComplete(Networking::JsonResponse response); - void codeFlowFailed(Networking::ErrorResponse error); +protected: + /** + * @return "dropbox" + */ + virtual Common::String cloudProvider(); + + /** + * @return kStorageDropboxId + */ + virtual uint32 storageIndex(); public: /** This constructor uses OAuth code flow to get tokens. */ diff --git a/backends/module.mk b/backends/module.mk index 22b4a63371..ee123434d8 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -23,6 +23,7 @@ ifdef USE_CLOUD ifdef USE_LIBCURL MODULE_OBJS += \ + cloud/basestorage.o \ cloud/cloudicon.o \ cloud/cloudmanager.o \ cloud/iso8601.o \ -- cgit v1.2.3