Skip to content
This repository has been archived by the owner on Oct 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #3 from mozilla/bug/687793-multiple-auth-requests
Browse files Browse the repository at this point in the history
Bug/687793 multiple auth requests
  • Loading branch information
mixedpuppy committed Sep 21, 2011
2 parents d641ed4 + 384436c commit 4643deb
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 15 deletions.
Binary file added data/content/loader.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions data/content/loading.html
@@ -0,0 +1,29 @@
<!DOCTYPE HTML>
<html>
<style type="text/css">
html, body {
height: 100%;
}

#authorizing {
width: 100px;
height: 100px;
text-align: center;
font-size: 30px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -50px;
/* Half the width of the DIV tag which is 50 pixels */
margin-top: -50px;
/* Half the height of the DIV tag which is also 50 pixels */
}
</style>

<body>
<div id="authorizing">
<img src="loader.gif"/>
Connecting...
</div>
</body>
</html>
11 changes: 11 additions & 0 deletions lib/loginListener.js
Expand Up @@ -85,3 +85,14 @@ exports.listen = function(window, svc, callback) {
webProgress.addProgressListener(listener, Ci.nsIWebProgress.NOTIFY_ALL);
return listener;
};

exports.stopListening = function(window, listener) {
let webProgress;
try {
webProgress = window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIWebProgress);
} catch (ex) {
// if the window has been closed we will fail to get the interface
return;
}
webProgress.removeProgressListener(listener);
}
52 changes: 37 additions & 15 deletions lib/oauthconsumer.js
Expand Up @@ -45,6 +45,7 @@ var OAuthConsumer = exports.OAuthConsumer = {};
(function()
{
this._log = SimpleLogger.getLogger("oauthconsumer", "oauth.txt", true, true, false);
this.authWindow = null; // only 1 auth can be happening at a time...

function makeProvider(name, displayName, key, secret, completionURI, calls, doNotStore) {
return {
Expand Down Expand Up @@ -284,6 +285,7 @@ var OAuthConsumer = exports.OAuthConsumer = {};
getRequestToken: function() {

this._log.debug("Getting "+this.service.name+" request token");
OAuthConsumer.openLoadingDialog();

var message = {
method: this.service.requestMethod,
Expand Down Expand Up @@ -419,7 +421,7 @@ var OAuthConsumer = exports.OAuthConsumer = {};
};
this._completeAccessRequest(message);
} catch(e) {
this._log.error(e);
this._log.error(e + ": " + e.stack);
}
},

Expand Down Expand Up @@ -558,25 +560,45 @@ var OAuthConsumer = exports.OAuthConsumer = {};
}
this._authorizers["2.0"] = OAuth2Handler;

this.openDialog = function(loginUrl, requestData, svc, afterAuthCallback) {
var wm = Cc["@mozilla.org/appshell/window-mediator;1"]
.getService(Ci.nsIWindowMediator);
var win = wm.getMostRecentWindow(null);
this._openDialog = function(location) {
if (this.oauth_listener) {
require("loginListener").stopListening(this.authWindow, this.oauth_listener);
this.oauth_listener = null;
}
if (this.authWindow && !this.authWindow.closed) {
// resize to the default size of the window.
this.authWindow.resizeTo(800, 500)
this.authWindow.location.href = location;
this.authWindow.focus();
} else {
let wm = Cc["@mozilla.org/appshell/window-mediator;1"]
.getService(Ci.nsIWindowMediator);
let win = wm.getMostRecentWindow(null);
this.authWindow = win.openDialog(location,
"oauth_authorization_dialog",
// ideally we would use 'modal', but then we can't get a window ref...
"chrome=no,centerscreen,dialog=no,width=800,height=500");
}
return this.authWindow;
}

let newWin = win.openDialog(loginUrl,
"oauth_authorization_dialog",
// ideally we would use 'modal', but then we can't get a window ref...
"chrome=no,centerscreen,dialog=no,width=800,height=500");
this.openLoadingDialog = function() {
let url = require("self").data.url("content/loading.html");
this._openDialog(url);
}

this.openDialog = function(loginUrl, requestData, svc, afterAuthCallback) {
let win = this._openDialog(loginUrl);
var callbackFunc = function(token)
{
// no need to stopListening here - if the callback was invoked the
// listener has already removed itself.
this.oauth_listener = null;
newWin.close();
win.setTimeout(afterAuthCallback, 0, requestData, token);
};

let listen = require("loginListener").listen;
this.oauth_listener = listen(newWin, svc, callbackFunc);
this.authWindow.close();
this.authWindow = null;
afterAuthCallback(requestData, token);
}.bind(this);
this.oauth_listener = require("loginListener").listen(win, svc, callbackFunc);
}

/**
Expand Down

0 comments on commit 4643deb

Please sign in to comment.