aboutsummaryrefslogtreecommitdiff
path: root/backends/cloud/dropbox
diff options
context:
space:
mode:
authorAlexander Tkachev2016-05-18 14:08:05 +0600
committerAlexander Tkachev2016-08-24 16:07:55 +0600
commite743a65636a674def45e955cb7a5632aead2a033 (patch)
treedf7e6a3d3ea4e612e9810693349bffadbbc2c399 /backends/cloud/dropbox
parent5df8c5140292520bafe92efa94935a776d63d108 (diff)
downloadscummvm-rg350-e743a65636a674def45e955cb7a5632aead2a033.tar.gz
scummvm-rg350-e743a65636a674def45e955cb7a5632aead2a033.tar.bz2
scummvm-rg350-e743a65636a674def45e955cb7a5632aead2a033.zip
CLOUD: Add Dropbox into CloudManager's configs
This commit adds: * ConfMan's new "cloud" domain; * CloudManager's init() method, where it loads keys from "cloud" configs domain; * CurlJsonRequest's addHeader() and addPostField() methods; * temporary Storage's printInfo() method; * DropboxStorage's implementation of printInfo(), which is using access token and user id; * DropboxStorage's loadFromConfig() static method to load access token and user id from configs and create a Storage instance with those; * temporary DropboxStorage's authThroughConsole() static method, which guides user through auth process from the console. So, in CloudManager's init() implementation ScummVM checks that there is "current_storage_type" key in "cloud" domain of configs, and loads corresponding storage if there is such key. If there is no such key, ScummVM offers user to auth with Dropbox. That's done through console, and thus it's temporary (it also requires restarting ScummVM twice and manually editing config.ini file).
Diffstat (limited to 'backends/cloud/dropbox')
-rw-r--r--backends/cloud/dropbox/dropboxstorage.cpp110
-rw-r--r--backends/cloud/dropbox/dropboxstorage.h39
2 files changed, 137 insertions, 12 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