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.
* 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"
}
+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
// 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:
@@ -65,58 +66,64 @@ SQL.Storage.Dropbox.prototype.showDropboxError = function(error) {
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 xml = this.owner.owner.toXML();
var us = this;
us.authenticate( function() {
var xml = us.owner.owner.toXML();
var filename = keyword + ".xml";
this.owner.owner.window.showThrobber();
us.owner.owner.window.showThrobber();
this.dropboxClient.writeFile(filename, xml, function(error, stat) {
this.owner.owner.window.hideThrobber();
us.dropboxClient.writeFile(filename, xml, function(error, stat) {
us.owner.owner.window.hideThrobber();
if (error) {
this.showDropboxError(error);
us.showDropboxError(error);
} else {
this._keyword = keyword;
this.owner.getArea().value = filename+" "+_("was saved to Dropbox");
us._keyword = keyword;
us.owner.getArea().value = filename+" "+_("was saved to Dropbox");
}
}.bind(this));
}.bind(us));
});
}
SQL.Storage.Dropbox.prototype._load = function(keyword) {
if (!this.showDropboxAuthenticate()) return;
var us = this;
us.authenticate( function() {
var filename = keyword + ".xml";
this.owner.owner.window.showThrobber();
us.owner.owner.window.showThrobber();
this.dropboxClient.readFile(filename, function(error, data) {
this.owner.owner.window.hideThrobber();
us.dropboxClient.readFile(filename, function(error, data) {
us.owner.owner.window.hideThrobber();
if (error) {
this.showDropboxError(error);
us.showDropboxError(error);
} else {
this.owner.fromXMLText(data);
this._keyword = keyword;
us.owner.fromXMLText(data);
us._keyword = keyword;
}
}.bind(this));
}.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();
var us = this;
us.authenticate( function() {
us.owner.owner.window.showThrobber();
us.dropboxClient.readdir("/", function(error, entries) {
us.owner.owner.window.hideThrobber();
if (error) {
this.showDropboxError(error);
us.showDropboxError(error);
} else {
this.owner.getArea().value = entries.join("\n")+"\n"
us.owner.getArea().value = entries.join("\n")+"\n"
}
}.bind(this));
}.bind(us));
});
}