Merge pull request #202 from tempelmann/storage

Fixes Dropbox authentication
This commit is contained in:
Ondřej Žára
2015-06-16 12:53:34 +02:00
2 changed files with 63 additions and 55 deletions
+6 -6
View File
@@ -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. * 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 * 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, * https://www.dropbox.com/developers/apps and use "Create app" to add a new "Dropbox API app".
* "wwwsqldesigner", and give it the "App Folder" permission. Unter "OAuth 2", "Redirect URIs", add * Limit the app to its own folder. Call it, for instance, "wwwsqldesigner".
* the URL to the "dropbox-oauth-receiver.html" file on your server. E.g, if you install wwwsqldesigner * Under "OAuth 2", "Redirect URIs", add the URL to the "dropbox-oauth-receiver.html" file on your server.
* on your local web server under "http://localhost/sqldesigner/", then add * 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. * 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"
} }
+45 -37
View File
@@ -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 // If you're using dropbox.js, the only cause behind this error is that
// the user token expired. // the user token expired.
// Get the user through the authentication flow again. // 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; break;
case Dropbox.ApiError.NOT_FOUND: case Dropbox.ApiError.NOT_FOUND:
@@ -65,58 +66,64 @@ SQL.Storage.Dropbox.prototype.showDropboxError = function(error) {
alert (prefix+msg); alert (prefix+msg);
}; };
SQL.Storage.Dropbox.prototype.showDropboxAuthenticate = function() { SQL.Storage.Dropbox.prototype.authenticate = function(connectedCallBack) {
if (!this.dropboxClient) return false; 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 // We want to use a popup window for authentication as the default redirection
this.dropboxClient.authDriver(new Dropbox.AuthDriver.Popup({ receiverUrl: "dropbox-oauth-receiver.html" })); // 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 // Now let's authenticate us
var success = false; var us = this;
this.dropboxClient.authenticate( function(error, client) { this.dropboxClient.authenticate( function(error, client) {
if (error) { if (error) {
this.showDropboxError(error); us.showDropboxError(error);
return; } else {
// We're authenticated
connectedCallBack();
} }
success = true;
return;
}); });
return success;
return true;
} }
SQL.Storage.Dropbox.prototype._save = function(keyword) { SQL.Storage.Dropbox.prototype._save = function(keyword) {
if (!this.showDropboxAuthenticate()) return; var us = this;
us.authenticate( function() {
var xml = this.owner.owner.toXML(); var xml = us.owner.owner.toXML();
var filename = keyword + ".xml"; var filename = keyword + ".xml";
this.owner.owner.window.showThrobber(); us.owner.owner.window.showThrobber();
this.dropboxClient.writeFile(filename, xml, function(error, stat) { us.dropboxClient.writeFile(filename, xml, function(error, stat) {
this.owner.owner.window.hideThrobber(); us.owner.owner.window.hideThrobber();
if (error) { if (error) {
this.showDropboxError(error); us.showDropboxError(error);
} else { } else {
this._keyword = keyword; us._keyword = keyword;
this.owner.getArea().value = filename+" "+_("was saved to Dropbox"); us.owner.getArea().value = filename+" "+_("was saved to Dropbox");
} }
}.bind(this)); }.bind(us));
});
} }
SQL.Storage.Dropbox.prototype._load = function(keyword) { SQL.Storage.Dropbox.prototype._load = function(keyword) {
if (!this.showDropboxAuthenticate()) return; var us = this;
us.authenticate( function() {
var filename = keyword + ".xml"; var filename = keyword + ".xml";
this.owner.owner.window.showThrobber(); us.owner.owner.window.showThrobber();
this.dropboxClient.readFile(filename, function(error, data) { us.dropboxClient.readFile(filename, function(error, data) {
this.owner.owner.window.hideThrobber(); us.owner.owner.window.hideThrobber();
if (error) { if (error) {
this.showDropboxError(error); us.showDropboxError(error);
} else { } else {
this.owner.fromXMLText(data); us.owner.fromXMLText(data);
this._keyword = keyword; us._keyword = keyword;
} }
}.bind(this)); }.bind(us));
});
} }
SQL.Storage.Dropbox.prototype._clickSave = function(e) { 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) { SQL.Storage.Dropbox.prototype._clickList = function(e) {
if (!this.showDropboxAuthenticate()) return; var us = this;
us.authenticate( function() {
this.owner.owner.window.showThrobber(); us.owner.owner.window.showThrobber();
this.dropboxClient.readdir("/", function(error, entries) { us.dropboxClient.readdir("/", function(error, entries) {
this.owner.owner.window.hideThrobber(); us.owner.owner.window.hideThrobber();
if (error) { if (error) {
this.showDropboxError(error); us.showDropboxError(error);
} else { } else {
this.owner.getArea().value = entries.join("\n")+"\n" us.owner.getArea().value = entries.join("\n")+"\n"
} }
}.bind(this)); }.bind(us));
});
} }