aboutsummaryrefslogtreecommitdiff
path: root/backends/cloud/dropbox
diff options
context:
space:
mode:
Diffstat (limited to 'backends/cloud/dropbox')
-rw-r--r--backends/cloud/dropbox/dropboxstorage.cpp80
-rw-r--r--backends/cloud/dropbox/dropboxstorage.h12
2 files changed, 32 insertions, 60 deletions
diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp
index faff10f1d9..180b40c3d5 100644
--- a/backends/cloud/dropbox/dropboxstorage.cpp
+++ b/backends/cloud/dropbox/dropboxstorage.cpp
@@ -51,41 +51,47 @@ void DropboxStorage::loadKeyAndSecret() {
SECRET[k.size()] = 0;
}
-static void saveAccessTokenCallback(Networking::JsonResponse pair) {
- Common::JSONValue *json = (Common::JSONValue *)pair.value;
- if (json) {
- debug("saveAccessTokenCallback:");
- debug("%s", json->stringify(true).c_str());
+DropboxStorage::DropboxStorage(Common::String accessToken, Common::String userId): _token(accessToken), _uid(userId) {}
+
+DropboxStorage::DropboxStorage(Common::String code) {
+ getAccessToken(code);
+}
+
+DropboxStorage::~DropboxStorage() {}
+
+void DropboxStorage::getAccessToken(Common::String code) {
+ Networking::JsonCallback callback = new Common::Callback<DropboxStorage, Networking::JsonResponse>(this, &DropboxStorage::codeFlowComplete);
+ Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, nullptr, "https://api.dropboxapi.com/1/oauth2/token");
+ request->addPostField("code=" + code);
+ request->addPostField("grant_type=authorization_code");
+ request->addPostField("client_id=" + Common::String(KEY));
+ request->addPostField("client_secret=" + Common::String(SECRET));
+ request->addPostField("&redirect_uri=http%3A%2F%2Flocalhost%3A12345%2F");
+ addRequest(request);
+}
+void DropboxStorage::codeFlowComplete(Networking::JsonResponse response) {
+ Common::JSONValue *json = (Common::JSONValue *)response.value;
+ if (json) {
Common::JSONObject result = json->asObject();
if (!result.contains("access_token") || !result.contains("uid")) {
warning("Bad response, no token/uid passed");
} else {
- //we suppose that's the first storage
- //TODO: update it to use CloudMan.replaceStorage()
- ConfMan.set("current_storage", "1", "cloud");
- ConfMan.set("storage_Dropbox_type", "Dropbox", "cloud");
- ConfMan.set("storage_Dropbox_access_token", result.getVal("access_token")->asString(), "cloud");
- ConfMan.set("storage_Dropbox_user_id", result.getVal("uid")->asString(), "cloud");
+ _token = result.getVal("access_token")->asString();
+ _uid = result.getVal("user_id")->asString();
ConfMan.removeKey("dropbox_code", "cloud");
+ CloudMan.replaceStorage(this, kStorageDropboxId);
ConfMan.flushToDisk();
- debug("Now please restart ScummVM to apply the changes.");
+ debug("Done! You can use Dropbox now! Look:");
+ CloudMan.testFeature();
}
delete json;
} else {
- debug("saveAccessTokenCallback: got NULL instead of JSON!");
+ debug("DropboxStorage::codeFlowComplete: got NULL instead of JSON!");
}
}
-DropboxStorage::DropboxStorage(Common::String accessToken, Common::String userId): _token(accessToken), _uid(userId) {
- curl_global_init(CURL_GLOBAL_ALL);
-}
-
-DropboxStorage::~DropboxStorage() {
- curl_global_cleanup();
-}
-
void DropboxStorage::saveConfig(Common::String keyPrefix) {
ConfMan.set(keyPrefix + "access_token", _token, "cloud");
ConfMan.set(keyPrefix + "user_id", _uid, "cloud");
@@ -216,37 +222,5 @@ Common::String DropboxStorage::getAuthLink() {
return url;
}
-void DropboxStorage::authThroughConsole() {
- if (!ConfMan.hasKey("DROPBOX_KEY", "cloud") || !ConfMan.hasKey("DROPBOX_SECRET", "cloud")) {
- warning("No Dropbox keys available, cannot do auth");
- return;
- }
-
- loadKeyAndSecret();
-
- if (ConfMan.hasKey("dropbox_code", "cloud")) {
- //phase 2: get access_token using specified code
- getAccessToken(ConfMan.get("dropbox_code", "cloud"));
- return;
- }
-
- 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");
-}
-
-void DropboxStorage::getAccessToken(Common::String code) {
- Networking::JsonCallback callback = new Common::GlobalFunctionCallback<Networking::JsonResponse>(saveAccessTokenCallback);
- Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, nullptr, "https://api.dropboxapi.com/1/oauth2/token"); //TODO
- request->addPostField("code=" + code);
- request->addPostField("grant_type=authorization_code");
- request->addPostField("client_id=" + Common::String(KEY));
- request->addPostField("client_secret=" + Common::String(SECRET));
- request->addPostField("&redirect_uri=http%3A%2F%2Flocalhost%3A12345%2F");
- ConnMan.addRequest(request);
-}
-
} // End of namespace Dropbox
} // End of namespace Cloud
diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h
index 60a8075201..d256e0562b 100644
--- a/backends/cloud/dropbox/dropboxstorage.h
+++ b/backends/cloud/dropbox/dropboxstorage.h
@@ -40,7 +40,8 @@ class DropboxStorage: public Cloud::Storage {
/** This private constructor is called from loadFromConfig(). */
DropboxStorage(Common::String token, Common::String uid);
- static void getAccessToken(Common::String code);
+ void getAccessToken(Common::String code);
+ void codeFlowComplete(Networking::JsonResponse response);
/** Constructs StorageInfo based on JSON response from cloud. */
void infoInnerCallback(StorageInfoCallback outerCallback, Networking::JsonResponse json);
@@ -49,7 +50,9 @@ class DropboxStorage: public Cloud::Storage {
void printBool(BoolResponse response);
void printStorageFile(UploadResponse response);
-public:
+public:
+ /** This constructor uses OAuth code flow to get tokens. */
+ DropboxStorage(Common::String code);
virtual ~DropboxStorage();
/**
@@ -107,11 +110,6 @@ public:
* Returns Dropbox auth link.
*/
static Common::String getAuthLink();
-
- /**
- * Show message with Dropbox auth instructions. (Temporary)
- */
- static void authThroughConsole();
};
} // End of namespace Dropbox