diff options
Diffstat (limited to 'backends/cloud/box')
-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 |
3 files changed, 25 insertions, 81 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; } |