diff options
author | Alexander Tkachev | 2019-07-15 21:06:00 +0700 |
---|---|---|
committer | Matan Bareket | 2019-07-30 14:51:41 -0400 |
commit | 0ee0e2d537e6217278e75ec59c216936896bf6cb (patch) | |
tree | 223ac53d5aefd91766e03d369cfeee32a65dc5e7 /backends/cloud | |
parent | 8333cce498a0c600ff42e58d644f9dad7a10068b (diff) | |
download | scummvm-rg350-0ee0e2d537e6217278e75ec59c216936896bf6cb.tar.gz scummvm-rg350-0ee0e2d537e6217278e75ec59c216936896bf6cb.tar.bz2 scummvm-rg350-0ee0e2d537e6217278e75ec59c216936896bf6cb.zip |
CLOUD: Update GoogleDriveStorage and BoxStorage to auth via cloud.scummvm.org
Diffstat (limited to 'backends/cloud')
-rw-r--r-- | backends/cloud/box/boxstorage.cpp | 77 | ||||
-rw-r--r-- | backends/cloud/box/boxstorage.h | 25 | ||||
-rw-r--r-- | backends/cloud/box/boxtokenrefresher.cpp | 4 | ||||
-rw-r--r-- | backends/cloud/googledrive/googledrivestorage.cpp | 71 | ||||
-rw-r--r-- | backends/cloud/googledrive/googledrivestorage.h | 25 | ||||
-rw-r--r-- | backends/cloud/googledrive/googledrivetokenrefresher.cpp | 2 | ||||
-rw-r--r-- | backends/cloud/id/idstorage.cpp | 5 | ||||
-rw-r--r-- | backends/cloud/id/idstorage.h | 6 |
8 files changed, 59 insertions, 156 deletions
diff --git a/backends/cloud/box/boxstorage.cpp b/backends/cloud/box/boxstorage.cpp index 2671a77a5e..df81773855 100644 --- a/backends/cloud/box/boxstorage.cpp +++ b/backends/cloud/box/boxstorage.cpp @@ -42,50 +42,25 @@ namespace Cloud { namespace Box { -#define BOX_OAUTH2_TOKEN "https://api.box.com/oauth2/token" #define BOX_API_FOLDERS "https://api.box.com/2.0/folders" #define BOX_API_FILES_CONTENT "https://api.box.com/2.0/files/%s/content" #define BOX_API_USERS_ME "https://api.box.com/2.0/users/me" -char *BoxStorage::KEY = nullptr; //can't use CloudConfig there yet, loading it on instance creation/auth -char *BoxStorage::SECRET = nullptr; - -void BoxStorage::loadKeyAndSecret() { -#ifdef ENABLE_RELEASE - KEY = RELEASE_BOX_KEY; - SECRET = RELEASE_BOX_SECRET; -#else - Common::String k = ConfMan.get("BOX_KEY", ConfMan.kCloudDomain); - KEY = new char[k.size() + 1]; - memcpy(KEY, k.c_str(), k.size()); - KEY[k.size()] = 0; - - k = ConfMan.get("BOX_SECRET", ConfMan.kCloudDomain); - SECRET = new char[k.size() + 1]; - memcpy(SECRET, k.c_str(), k.size()); - SECRET[k.size()] = 0; -#endif -} - BoxStorage::BoxStorage(Common::String token, Common::String refreshToken): - _token(token), _refreshToken(refreshToken) {} + IdStorage(token, refreshToken) {} BoxStorage::BoxStorage(Common::String code) { - getAccessToken( - new Common::Callback<BoxStorage, BoolResponse>(this, &BoxStorage::codeFlowComplete), - new Common::Callback<BoxStorage, Networking::ErrorResponse>(this, &BoxStorage::codeFlowFailed), - code - ); + getAccessToken(code); } BoxStorage::~BoxStorage() {} -void BoxStorage::getAccessToken(BoolCallback callback, Networking::ErrorCallback errorCallback, Common::String code) { - if (!KEY || !SECRET) - loadKeyAndSecret(); - bool codeFlow = (code != ""); +Common::String BoxStorage::cloudProvider() { return "box"; } - if (!codeFlow && _refreshToken == "") { +uint32 BoxStorage::storageIndex() { return kStorageBoxId; } + +void BoxStorage::refreshAccessToken(BoolCallback callback, Networking::ErrorCallback errorCallback) { + if (_refreshToken == "") { warning("BoxStorage: no refresh token available to get new access token."); if (callback) (*callback)(BoolResponse(nullptr, false)); return; @@ -95,23 +70,8 @@ void BoxStorage::getAccessToken(BoolCallback callback, Networking::ErrorCallback if (errorCallback == nullptr) errorCallback = getErrorPrintingCallback(); - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, BOX_OAUTH2_TOKEN); - if (codeFlow) { - request->addPostField("grant_type=authorization_code"); - request->addPostField("code=" + code); - } else { - request->addPostField("grant_type=refresh_token"); - request->addPostField("refresh_token=" + _refreshToken); - } - request->addPostField("client_id=" + Common::String(KEY)); - request->addPostField("client_secret=" + Common::String(SECRET)); - /* - if (Cloud::CloudManager::couldUseLocalServer()) { - request->addPostField("&redirect_uri=http%3A%2F%2Flocalhost%3A12345"); - } else { - request->addPostField("&redirect_uri=https%3A%2F%2Fwww.scummvm.org/c/code"); - } - */ + Common::String url = "https://cloud.scummvm.org/box/refresh/" + _refreshToken; // TODO: subject to change + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, url); addRequest(request); } @@ -151,23 +111,6 @@ void BoxStorage::tokenRefreshed(BoolCallback callback, Networking::JsonResponse delete callback; } -void BoxStorage::codeFlowComplete(BoolResponse response) { - if (!response.value) { - warning("BoxStorage: failed to get access token through code flow"); - CloudMan.removeStorage(this); - return; - } - - CloudMan.replaceStorage(this, kStorageBoxId); - ConfMan.flushToDisk(); -} - -void BoxStorage::codeFlowFailed(Networking::ErrorResponse error) { - debug(9, "BoxStorage: code flow failed (%s, %ld):", (error.failed ? "failed" : "interrupted"), error.httpResponseCode); - debug(9, "%s", error.response.c_str()); - CloudMan.removeStorage(this); -} - void BoxStorage::saveConfig(Common::String keyPrefix) { ConfMan.set(keyPrefix + "access_token", _token, ConfMan.kCloudDomain); ConfMan.set(keyPrefix + "refresh_token", _refreshToken, ConfMan.kCloudDomain); @@ -321,8 +264,6 @@ Networking::Request *BoxStorage::info(StorageInfoCallback callback, Networking:: Common::String BoxStorage::savesDirectoryPath() { return "scummvm/saves/"; } BoxStorage *BoxStorage::loadFromConfig(Common::String keyPrefix) { - loadKeyAndSecret(); - if (!ConfMan.hasKey(keyPrefix + "access_token", ConfMan.kCloudDomain)) { warning("BoxStorage: no access_token found"); return nullptr; diff --git a/backends/cloud/box/boxstorage.h b/backends/cloud/box/boxstorage.h index a641669b2a..e22624a20c 100644 --- a/backends/cloud/box/boxstorage.h +++ b/backends/cloud/box/boxstorage.h @@ -30,23 +30,27 @@ namespace Cloud { namespace Box { class BoxStorage: public Id::IdStorage { - static char *KEY, *SECRET; - - static void loadKeyAndSecret(); - - Common::String _token, _refreshToken; - /** This private constructor is called from loadFromConfig(). */ BoxStorage(Common::String token, Common::String refreshToken); void tokenRefreshed(BoolCallback callback, Networking::JsonResponse response); - void codeFlowComplete(BoolResponse response); - void codeFlowFailed(Networking::ErrorResponse error); /** Constructs StorageInfo based on JSON response from cloud. */ void infoInnerCallback(StorageInfoCallback outerCallback, Networking::JsonResponse json); void createDirectoryInnerCallback(BoolCallback outerCallback, Networking::JsonResponse response); + +protected: + /** + * @return "box" + */ + virtual Common::String cloudProvider(); + + /** + * @return kStorageBoxId + */ + virtual uint32 storageIndex(); + public: /** This constructor uses OAuth code flow to get tokens. */ BoxStorage(Common::String code); @@ -101,11 +105,10 @@ public: virtual Common::String getRootDirectoryId(); /** - * Gets new access_token. If <code> passed is "", refresh_token is used. - * Use "" in order to refresh token and pass a callback, so you could + * Gets new access_token. Pass a callback, so you could * continue your work when new token is available. */ - void getAccessToken(BoolCallback callback, Networking::ErrorCallback errorCallback = nullptr, Common::String code = ""); + void refreshAccessToken(BoolCallback callback, Networking::ErrorCallback errorCallback = nullptr); Common::String accessToken() const { return _token; } }; diff --git a/backends/cloud/box/boxtokenrefresher.cpp b/backends/cloud/box/boxtokenrefresher.cpp index 5f7ad1d611..19cdd92667 100644 --- a/backends/cloud/box/boxtokenrefresher.cpp +++ b/backends/cloud/box/boxtokenrefresher.cpp @@ -99,7 +99,7 @@ void BoxTokenRefresher::finishJson(Common::JSONValue *json) { pause(); delete json; - _parentStorage->getAccessToken(new Common::Callback<BoxTokenRefresher, Storage::BoolResponse>(this, &BoxTokenRefresher::tokenRefreshed)); + _parentStorage->refreshAccessToken(new Common::Callback<BoxTokenRefresher, Storage::BoolResponse>(this, &BoxTokenRefresher::tokenRefreshed)); return; } } @@ -111,7 +111,7 @@ void BoxTokenRefresher::finishJson(Common::JSONValue *json) { void BoxTokenRefresher::finishError(Networking::ErrorResponse error) { if (error.httpResponseCode == 401) { // invalid_token pause(); - _parentStorage->getAccessToken(new Common::Callback<BoxTokenRefresher, Storage::BoolResponse>(this, &BoxTokenRefresher::tokenRefreshed)); + _parentStorage->refreshAccessToken(new Common::Callback<BoxTokenRefresher, Storage::BoolResponse>(this, &BoxTokenRefresher::tokenRefreshed)); return; } diff --git a/backends/cloud/googledrive/googledrivestorage.cpp b/backends/cloud/googledrive/googledrivestorage.cpp index 51799eb384..bd4f2cb8d3 100644 --- a/backends/cloud/googledrive/googledrivestorage.cpp +++ b/backends/cloud/googledrive/googledrivestorage.cpp @@ -43,49 +43,25 @@ namespace Cloud { namespace GoogleDrive { -#define GOOGLEDRIVE_OAUTH2_TOKEN "https://accounts.google.com/o/oauth2/token" #define GOOGLEDRIVE_API_FILES_ALT_MEDIA "https://www.googleapis.com/drive/v3/files/%s?alt=media" #define GOOGLEDRIVE_API_FILES "https://www.googleapis.com/drive/v3/files" #define GOOGLEDRIVE_API_ABOUT "https://www.googleapis.com/drive/v3/about?fields=storageQuota,user" -char *GoogleDriveStorage::KEY = nullptr; //can't use CloudConfig there yet, loading it on instance creation/auth -char *GoogleDriveStorage::SECRET = nullptr; - -void GoogleDriveStorage::loadKeyAndSecret() { -#ifdef ENABLE_RELEASE - KEY = RELEASE_GOOGLE_DRIVE_KEY; - SECRET = RELEASE_GOOGLE_DRIVE_SECRET; -#else - Common::String k = ConfMan.get("GOOGLE_DRIVE_KEY", ConfMan.kCloudDomain); - KEY = new char[k.size() + 1]; - memcpy(KEY, k.c_str(), k.size()); - KEY[k.size()] = 0; - - k = ConfMan.get("GOOGLE_DRIVE_SECRET", ConfMan.kCloudDomain); - SECRET = new char[k.size() + 1]; - memcpy(SECRET, k.c_str(), k.size()); - SECRET[k.size()] = 0; -#endif -} - GoogleDriveStorage::GoogleDriveStorage(Common::String token, Common::String refreshToken): - _token(token), _refreshToken(refreshToken) {} + IdStorage(token, refreshToken) {} GoogleDriveStorage::GoogleDriveStorage(Common::String code) { - getAccessToken( - new Common::Callback<GoogleDriveStorage, BoolResponse>(this, &GoogleDriveStorage::codeFlowComplete), - new Common::Callback<GoogleDriveStorage, Networking::ErrorResponse>(this, &GoogleDriveStorage::codeFlowFailed), - code - ); + getAccessToken(code); } GoogleDriveStorage::~GoogleDriveStorage() {} -void GoogleDriveStorage::getAccessToken(BoolCallback callback, Networking::ErrorCallback errorCallback, Common::String code) { - if (!KEY || !SECRET) loadKeyAndSecret(); - bool codeFlow = (code != ""); +Common::String GoogleDriveStorage::cloudProvider() { return "gdrive"; } - if (!codeFlow && _refreshToken == "") { +uint32 GoogleDriveStorage::storageIndex() { return kStorageGoogleDriveId; } + +void GoogleDriveStorage::refreshAccessToken(BoolCallback callback, Networking::ErrorCallback errorCallback) { + if (_refreshToken == "") { warning("GoogleDriveStorage: no refresh token available to get new access token."); if (callback) (*callback)(BoolResponse(nullptr, false)); @@ -95,17 +71,9 @@ void GoogleDriveStorage::getAccessToken(BoolCallback callback, Networking::Error Networking::JsonCallback innerCallback = new Common::CallbackBridge<GoogleDriveStorage, BoolResponse, Networking::JsonResponse>(this, &GoogleDriveStorage::tokenRefreshed, callback); if (errorCallback == nullptr) errorCallback = getErrorPrintingCallback(); - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, GOOGLEDRIVE_OAUTH2_TOKEN); - if (codeFlow) { - request->addPostField("code=" + code); - request->addPostField("grant_type=authorization_code"); - } else { - request->addPostField("refresh_token=" + _refreshToken); - request->addPostField("grant_type=refresh_token"); - } - request->addPostField("client_id=" + Common::String(KEY)); - request->addPostField("client_secret=" + Common::String(SECRET)); - request->addPostField("&redirect_uri=https%3A%2F%2Fwww.scummvm.org/c/code"); + + Common::String url = "https://cloud.scummvm.org/gdrive/refresh/" + _refreshToken; // TODO: subject to change + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, url); addRequest(request); } @@ -147,23 +115,6 @@ void GoogleDriveStorage::tokenRefreshed(BoolCallback callback, Networking::JsonR delete callback; } -void GoogleDriveStorage::codeFlowComplete(BoolResponse response) { - if (!response.value) { - warning("GoogleDriveStorage: failed to get access token through code flow"); - CloudMan.removeStorage(this); - return; - } - - CloudMan.replaceStorage(this, kStorageGoogleDriveId); - ConfMan.flushToDisk(); -} - -void GoogleDriveStorage::codeFlowFailed(Networking::ErrorResponse error) { - debug(9, "GoogleDriveStorage: code flow failed (%s, %ld):", (error.failed ? "failed" : "interrupted"), error.httpResponseCode); - debug(9, "%s", error.response.c_str()); - CloudMan.removeStorage(this); -} - void GoogleDriveStorage::saveConfig(Common::String keyPrefix) { ConfMan.set(keyPrefix + "access_token", _token, ConfMan.kCloudDomain); ConfMan.set(keyPrefix + "refresh_token", _refreshToken, ConfMan.kCloudDomain); @@ -320,8 +271,6 @@ Networking::Request *GoogleDriveStorage::info(StorageInfoCallback callback, Netw Common::String GoogleDriveStorage::savesDirectoryPath() { return "scummvm/saves/"; } GoogleDriveStorage *GoogleDriveStorage::loadFromConfig(Common::String keyPrefix) { - loadKeyAndSecret(); - if (!ConfMan.hasKey(keyPrefix + "access_token", ConfMan.kCloudDomain)) { warning("GoogleDriveStorage: no access_token found"); return nullptr; diff --git a/backends/cloud/googledrive/googledrivestorage.h b/backends/cloud/googledrive/googledrivestorage.h index d0585bc403..30bc9ab3f5 100644 --- a/backends/cloud/googledrive/googledrivestorage.h +++ b/backends/cloud/googledrive/googledrivestorage.h @@ -30,18 +30,10 @@ namespace Cloud { namespace GoogleDrive { class GoogleDriveStorage: public Id::IdStorage { - static char *KEY, *SECRET; - - static void loadKeyAndSecret(); - - Common::String _token, _refreshToken; - /** This private constructor is called from loadFromConfig(). */ GoogleDriveStorage(Common::String token, Common::String refreshToken); void tokenRefreshed(BoolCallback callback, Networking::JsonResponse response); - void codeFlowComplete(BoolResponse response); - void codeFlowFailed(Networking::ErrorResponse error); /** Constructs StorageInfo based on JSON response from cloud. */ void infoInnerCallback(StorageInfoCallback outerCallback, Networking::JsonResponse json); @@ -50,6 +42,18 @@ class GoogleDriveStorage: public Id::IdStorage { void createDirectoryInnerCallback(BoolCallback outerCallback, Networking::JsonResponse json); void printInfo(StorageInfoResponse response); + +protected: + /** + * @return "gdrive" + */ + virtual Common::String cloudProvider(); + + /** + * @return kStorageGoogleDriveId + */ + virtual uint32 storageIndex(); + public: /** This constructor uses OAuth code flow to get tokens. */ GoogleDriveStorage(Common::String code); @@ -103,11 +107,10 @@ public: virtual Common::String getRootDirectoryId(); /** - * Gets new access_token. If <code> passed is "", refresh_token is used. - * Use "" in order to refresh token and pass a callback, so you could + * Gets new access_token. Pass a callback, so you could * continue your work when new token is available. */ - void getAccessToken(BoolCallback callback, Networking::ErrorCallback errorCallback = nullptr, Common::String code = ""); + void refreshAccessToken(BoolCallback callback, Networking::ErrorCallback errorCallback = nullptr); Common::String accessToken() const { return _token; } }; diff --git a/backends/cloud/googledrive/googledrivetokenrefresher.cpp b/backends/cloud/googledrive/googledrivetokenrefresher.cpp index a32a7fcbed..f28f73a788 100644 --- a/backends/cloud/googledrive/googledrivetokenrefresher.cpp +++ b/backends/cloud/googledrive/googledrivetokenrefresher.cpp @@ -100,7 +100,7 @@ void GoogleDriveTokenRefresher::finishJson(Common::JSONValue *json) { pause(); delete json; - _parentStorage->getAccessToken(new Common::Callback<GoogleDriveTokenRefresher, Storage::BoolResponse>(this, &GoogleDriveTokenRefresher::tokenRefreshed)); + _parentStorage->refreshAccessToken(new Common::Callback<GoogleDriveTokenRefresher, Storage::BoolResponse>(this, &GoogleDriveTokenRefresher::tokenRefreshed)); return; } } diff --git a/backends/cloud/id/idstorage.cpp b/backends/cloud/id/idstorage.cpp index 44427ac4d2..dd8805ea9b 100644 --- a/backends/cloud/id/idstorage.cpp +++ b/backends/cloud/id/idstorage.cpp @@ -33,6 +33,11 @@ namespace Cloud { namespace Id { +IdStorage::IdStorage() {} + +IdStorage::IdStorage(Common::String token, Common::String refreshToken): + BaseStorage(token, refreshToken) {} + IdStorage::~IdStorage() {} void IdStorage::printFiles(FileArrayResponse response) { diff --git a/backends/cloud/id/idstorage.h b/backends/cloud/id/idstorage.h index 946a792b42..35a320284e 100644 --- a/backends/cloud/id/idstorage.h +++ b/backends/cloud/id/idstorage.h @@ -23,7 +23,7 @@ #ifndef BACKENDS_CLOUD_ID_IDSTORAGE_H #define BACKENDS_CLOUD_ID_IDSTORAGE_H -#include "backends/cloud/storage.h" +#include "backends/cloud/basestorage.h" #include "backends/networking/curl/curljsonrequest.h" /* @@ -43,7 +43,7 @@ namespace Cloud { namespace Id { -class IdStorage: public Cloud::Storage { +class IdStorage: public Cloud::BaseStorage { protected: void printFiles(FileArrayResponse response); void printBool(BoolResponse response); @@ -52,6 +52,8 @@ protected: ListDirectoryCallback getPrintFilesCallback(); public: + IdStorage(); + IdStorage(Common::String token, Common::String refreshToken); virtual ~IdStorage(); /** Public Cloud API comes down there. */ |