From c58effb21a8d5774257a52da0833e9d0ba5360c2 Mon Sep 17 00:00:00 2001 From: Thomas Tempelmann Date: Tue, 16 Jun 2015 12:08:24 +0200 Subject: [PATCH] Fixes Dropbox authentication. --- js/config.js | 12 ++--- js/storage.dropbox.js | 106 +++++++++++++++++++++++------------------- 2 files changed, 63 insertions(+), 55 deletions(-) diff --git a/js/config.js b/js/config.js index 5728e6a..4ff027e 100644 --- a/js/config.js +++ b/js/config.js @@ -18,12 +18,12 @@ var CONFIG = { /* * The key below needs to be set individually by you if you want to use the Dropbox load/save feature. * To do that, first sign up with Dropbox (may require a specific developer / SDK sign-up), go to - * https://www.dropbox.com/developers/apps and use "Create app" to add a new app. Call it, for instance, - * "wwwsqldesigner", and give it the "App Folder" permission. Unter "OAuth 2", "Redirect URIs", add - * the URL to the "dropbox-oauth-receiver.html" file on your server. E.g, if you install wwwsqldesigner - * on your local web server under "http://localhost/sqldesigner/", then add + * https://www.dropbox.com/developers/apps and use "Create app" to add a new "Dropbox API app". + * Limit the app to its own folder. Call it, for instance, "wwwsqldesigner". + * Under "OAuth 2", "Redirect URIs", add the URL to the "dropbox-oauth-receiver.html" file on your server. + * E.g, if you install wwwsqldesigner on your local web server under "http://localhost/sqldesigner/", then add * http://localhost/sqldesigner/dropbox-oauth-receiver.html as a Redirection URI. - * Copy the shown "App key" and paste it here below: + * Copy the shown "App key" and paste it here below instead of the null value: */ - DROPBOX_KEY: null // "your app key" + DROPBOX_KEY: null // such as: "d6stdscwewhl6sa" } diff --git a/js/storage.dropbox.js b/js/storage.dropbox.js index 70095f3..da83009 100644 --- a/js/storage.dropbox.js +++ b/js/storage.dropbox.js @@ -27,7 +27,8 @@ SQL.Storage.Dropbox.prototype.showDropboxError = function(error) { // If you're using dropbox.js, the only cause behind this error is that // the user token expired. // Get the user through the authentication flow again. - msg = _("Invalid Token (expired)"); + msg = _("Token expired - retry the operation, authenticating again with Dropbox"); + this.dropboxClient.reset(); break; case Dropbox.ApiError.NOT_FOUND: @@ -62,61 +63,67 @@ SQL.Storage.Dropbox.prototype.showDropboxError = function(error) { // Tell the user an error occurred, ask them to refresh the page. } - alert(prefix+msg); + alert (prefix+msg); }; -SQL.Storage.Dropbox.prototype.showDropboxAuthenticate = function() { +SQL.Storage.Dropbox.prototype.authenticate = function(connectedCallBack) { if (!this.dropboxClient) return false; - // We want to use a popup window for authentication as the default redirection won't work for us as it'll make us lose our schema data - this.dropboxClient.authDriver(new Dropbox.AuthDriver.Popup({ receiverUrl: "dropbox-oauth-receiver.html" })); + // We want to use a popup window for authentication as the default redirection + // won't work for us as it'll make us lose our schema data + var href = window.location.href; + var prefix = href.substring(0, href.lastIndexOf("/")) + "/"; + this.dropboxClient.authDriver(new Dropbox.AuthDriver.Popup({ receiverUrl: prefix+"dropbox-oauth-receiver.html" })); // Now let's authenticate us - var success = false; + var us = this; this.dropboxClient.authenticate( function(error, client) { if (error) { - this.showDropboxError(error); - return; + us.showDropboxError(error); + } else { + // We're authenticated + connectedCallBack(); } - success = true; - return; }); - return success; + + return true; } SQL.Storage.Dropbox.prototype._save = function(keyword) { - if (!this.showDropboxAuthenticate()) return; + var us = this; + us.authenticate( function() { + var xml = us.owner.owner.toXML(); + var filename = keyword + ".xml"; + us.owner.owner.window.showThrobber(); - var xml = this.owner.owner.toXML(); - var filename = keyword + ".xml"; - this.owner.owner.window.showThrobber(); - - this.dropboxClient.writeFile(filename, xml, function(error, stat) { - this.owner.owner.window.hideThrobber(); - if (error) { - this.showDropboxError(error); - } else { - this._keyword = keyword; - this.owner.getArea().value = filename+" "+_("was saved to Dropbox"); - } - }.bind(this)); + us.dropboxClient.writeFile(filename, xml, function(error, stat) { + us.owner.owner.window.hideThrobber(); + if (error) { + us.showDropboxError(error); + } else { + us._keyword = keyword; + us.owner.getArea().value = filename+" "+_("was saved to Dropbox"); + } + }.bind(us)); + }); } SQL.Storage.Dropbox.prototype._load = function(keyword) { - if (!this.showDropboxAuthenticate()) return; + var us = this; + us.authenticate( function() { + var filename = keyword + ".xml"; + us.owner.owner.window.showThrobber(); - var filename = keyword + ".xml"; - this.owner.owner.window.showThrobber(); - - this.dropboxClient.readFile(filename, function(error, data) { - this.owner.owner.window.hideThrobber(); - if (error) { - this.showDropboxError(error); - } else { - this.owner.fromXMLText(data); - this._keyword = keyword; - } - }.bind(this)); + us.dropboxClient.readFile(filename, function(error, data) { + us.owner.owner.window.hideThrobber(); + if (error) { + us.showDropboxError(error); + } else { + us.owner.fromXMLText(data); + us._keyword = keyword; + } + }.bind(us)); + }); } SQL.Storage.Dropbox.prototype._clickSave = function(e) { @@ -132,15 +139,16 @@ SQL.Storage.Dropbox.prototype._clickLoad = function(e) { } SQL.Storage.Dropbox.prototype._clickList = function(e) { - if (!this.showDropboxAuthenticate()) return; - - this.owner.owner.window.showThrobber(); - this.dropboxClient.readdir("/", function(error, entries) { - this.owner.owner.window.hideThrobber(); - if (error) { - this.showDropboxError(error); - } else { - this.owner.getArea().value = entries.join("\n")+"\n" - } - }.bind(this)); + var us = this; + us.authenticate( function() { + us.owner.owner.window.showThrobber(); + us.dropboxClient.readdir("/", function(error, entries) { + us.owner.owner.window.hideThrobber(); + if (error) { + us.showDropboxError(error); + } else { + us.owner.getArea().value = entries.join("\n")+"\n" + } + }.bind(us)); + }); }