diff options
Diffstat (limited to 'backends/cloud')
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.cpp | 110 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.h | 39 | ||||
-rw-r--r-- | backends/cloud/manager.cpp | 15 | ||||
-rw-r--r-- | backends/cloud/manager.h | 2 | ||||
-rw-r--r-- | backends/cloud/storage.h | 6 |
5 files changed, 159 insertions, 13 deletions
diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index 729283bcfa..add6bff54c 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -23,6 +23,7 @@ #include "backends/cloud/dropbox/dropboxstorage.h" #include "backends/networking/curl/curljsonrequest.h" +#include "common/config-manager.h" #include "common/debug.h" #include "common/json.h" #include <curl/curl.h> @@ -30,18 +31,44 @@ namespace Cloud { namespace Dropbox { -static void curlJsonCallback(void *ptr) { +Common::String DropboxStorage::KEY; //can't use ConfMan there yet, loading it on instance creation/auth +Common::String DropboxStorage::SECRET; //TODO: hide these secrets somehow + +static void printJsonCallback(void *ptr) { + Common::JSONValue *json = (Common::JSONValue *)ptr; + if (json) { + debug("printJsonCallback:"); + debug("%s", json->stringify(true).c_str()); + delete json; + } else { + debug("printJsonCallback: got NULL instead of JSON!"); + } +} + +static void saveAccessTokenCallback(void *ptr) { Common::JSONValue *json = (Common::JSONValue *)ptr; if (json) { - debug("curlJsonCallback:"); + debug("saveAccessTokenCallback:"); debug("%s", json->stringify(true).c_str()); + + Common::JSONObject result = json->asObject(); + if (!result.contains("access_token") || !result.contains("uid")) { + warning("Bad response, no token/uid passed"); + } else { + ConfMan.set("current_storage_type", "Dropbox", "cloud"); + ConfMan.set("current_storage_access_token", result.getVal("access_token")->asString(), "cloud"); + ConfMan.set("current_storage_user_id", result.getVal("uid")->asString(), "cloud"); + ConfMan.removeKey("dropbox_code", "cloud"); + debug("Now please restart ScummVM to apply the changes."); + } + delete json; } else { - debug("curlJsonCallback: got NULL instead of JSON!"); + debug("saveAccessTokenCallback: got NULL instead of JSON!"); } } -DropboxStorage::DropboxStorage() { +DropboxStorage::DropboxStorage(Common::String accessToken, Common::String userId): _token(accessToken), _uid(userId) { curl_global_init(CURL_GLOBAL_ALL); } @@ -54,9 +81,78 @@ void DropboxStorage::listDirectory(Common::String path) { } void DropboxStorage::syncSaves() { - //not so Dropbox, just testing JSON requesting & parsing: - addRequest(new Networking::CurlJsonRequest(curlJsonCallback, "https://api.vk.com/method/users.get?v=5.50&user_ids=205387401")); - addRequest(new Networking::CurlJsonRequest(curlJsonCallback, "https://api.vk.com/method/users.get?v=5.50&user_ids=28870501")); + //not syncing, but already something: + printInfo(); +} + +void DropboxStorage::printInfo() { + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(printJsonCallback, "https://api.dropboxapi.com/1/account/info"); + request->addHeader("Authorization: Bearer " + _token); + addRequest(request); +} + +DropboxStorage *DropboxStorage::loadFromConfig() { + KEY = ConfMan.get("DROPBOX_KEY", "cloud"); + SECRET = ConfMan.get("DROPBOX_SECRET", "cloud"); + + if (!ConfMan.hasKey("current_storage_access_token", "cloud")) { + warning("No access_token found"); + return 0; + } + + if (!ConfMan.hasKey("current_storage_user_id", "cloud")) { + warning("No user_id found"); + return 0; + } + + Common::String accessToken = ConfMan.get("current_storage_access_token", "cloud"); + Common::String userId = ConfMan.get("current_storage_user_id", "cloud"); + return new DropboxStorage(accessToken, userId); +} + +Common::String DropboxStorage::getAuthLink() { + Common::String url = "https://www.dropbox.com/1/oauth2/authorize"; + url += "?response_type=code"; + url += "&redirect_uri=http://localhost:12345/"; //that's for copy-pasting + //url += "&redirect_uri=http%3A%2F%2Flocalhost%3A12345%2F"; //that's "http://localhost:12345/" for automatic opening + url += "&client_id=" + KEY; + return url; +} + +DropboxStorage *DropboxStorage::authThroughConsole() { + if (!ConfMan.hasKey("DROPBOX_KEY", "cloud") || !ConfMan.hasKey("DROPBOX_SECRET", "cloud")) { + warning("No Dropbox keys available, cannot do auth"); + return 0; + } + + KEY = ConfMan.get("DROPBOX_KEY", "cloud"); + SECRET = ConfMan.get("DROPBOX_SECRET", "cloud"); + + if (ConfMan.hasKey("dropbox_code", "cloud")) { + //phase 2: get access_token using specified code + return getAccessToken(ConfMan.get("dropbox_code", "cloud")); + } + + debug("Navigate to this URL and press \"Allow\":"); + debug("%s\n", getAuthLink().c_str()); + debug("Then, add dropbox_code key in [cloud] section of configuration file. You should copy the <code> value from URL and put it as value for that key.\n"); + debug("Navigate to this URL to get more information on ScummVM's configuration files:"); + debug("http://wiki.scummvm.org/index.php/User_Manual/Configuring_ScummVM#Using_the_configuration_file_to_configure_ScummVM\n"); + return 0; +} + +DropboxStorage *DropboxStorage::getAccessToken(Common::String code) { + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(saveAccessTokenCallback, "https://api.dropboxapi.com/1/oauth2/token"); + request->addPostField("code=" + code); + request->addPostField("grant_type=authorization_code"); + request->addPostField("client_id=" + KEY); + request->addPostField("client_secret=" + SECRET); + request->addPostField("&redirect_uri=http%3A%2F%2Flocalhost%3A12345%2F"); + + //OK, that's not how I imagined that... + DropboxStorage *storage = new DropboxStorage("", ""); + storage->addRequest(request); + return storage; } } //end of namespace Dropbox diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index a0229438be..f2281f146f 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -24,18 +24,47 @@ #define BACKENDS_CLOUD_DROPBOX_STORAGE_H #include "backends/cloud/storage.h" +#include "../manager.h" -namespace Cloud { namespace Dropbox { +namespace Cloud { +namespace Dropbox { -class DropboxStorage: public Cloud::Storage { -public: - DropboxStorage(); +class DropboxStorage: public Cloud::Storage { + static Common::String KEY, SECRET; + + Common::String _token, _uid; + + /** This private constructor is called from loadFromConfig(). */ + DropboxStorage(Common::String token, Common::String uid); + + static DropboxStorage *getAccessToken(Common::String code); + +public: virtual ~DropboxStorage(); virtual void listDirectory(Common::String path); virtual void syncSaves(); + virtual void printInfo(); + /** + * Load token and user id from configs and return DropboxStorage for those. + * @return pointer to the newly created DropboxStorage or 0 if some problem occured. + */ + static DropboxStorage *loadFromConfig(); + + /** + * Returns Dropbox auth link. + */ + static Common::String DropboxStorage::getAuthLink(); + + /** + * Show message with Dropbox auth instructions. (Temporary) + * Returns temporary DropboxStorage, which does network requests + * to get access token. + */ + static DropboxStorage *authThroughConsole(); }; -} } //end of namespace Cloud::Dropbox +} //end of namespace Dropbox +} //end of namespace Cloud #endif diff --git a/backends/cloud/manager.cpp b/backends/cloud/manager.cpp index 58bb0ce83f..08340e9288 100644 --- a/backends/cloud/manager.cpp +++ b/backends/cloud/manager.cpp @@ -22,13 +22,26 @@ #include "backends/cloud/manager.h" #include "backends/cloud/dropbox/dropboxstorage.h" +#include "common/config-manager.h" namespace Cloud { -Manager::Manager(): _currentStorage(new Dropbox::DropboxStorage()) {} +Manager::Manager(): _currentStorage(0) {} Manager::~Manager() { delete _currentStorage; } +void Manager::init() { + if (ConfMan.hasKey("current_storage_type", "cloud")) { + Common::String storageType = ConfMan.get("current_storage_type", "cloud"); + if (storageType == "Dropbox") _currentStorage = Dropbox::DropboxStorage::loadFromConfig(); + else warning("Unknown cloud storage type '%s' passed", storageType.c_str()); + } + else { + //this is temporary console offer to auth with Dropbox (because there is no other storage type yet anyway) + Dropbox::DropboxStorage::authThroughConsole(); + } +} + Storage* Manager::getCurrentStorage() { return _currentStorage; } diff --git a/backends/cloud/manager.h b/backends/cloud/manager.h index 11cc595da4..a1f046d71d 100644 --- a/backends/cloud/manager.h +++ b/backends/cloud/manager.h @@ -35,6 +35,8 @@ public: Manager(); virtual ~Manager(); + virtual void init(); + virtual Storage* getCurrentStorage(); virtual void syncSaves(); }; diff --git a/backends/cloud/storage.h b/backends/cloud/storage.h index 84b6157a22..a5d048d3af 100644 --- a/backends/cloud/storage.h +++ b/backends/cloud/storage.h @@ -63,6 +63,12 @@ public: */ virtual void syncSaves() = 0; + + /** + * Prints user info on console. (Temporary) + */ + + virtual void printInfo() = 0; }; } //end of namespace Cloud |