2019-11-03 00:17:46 -04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
|
|
/**
|
2019-12-30 09:33:56 -05:00
|
|
|
* Provides OAuth 2.0 authentication.
|
|
|
|
|
* @see RFC 6749
|
2019-11-03 00:17:46 -04:00
|
|
|
*/
|
|
|
|
|
var EXPORTED_SYMBOLS = ["OAuth2"];
|
|
|
|
|
|
|
|
|
|
var {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
|
|
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Http.jsm");
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
Cu.import("resource:///modules/gloda/log4moz.js");
|
|
|
|
|
|
|
|
|
|
// Only allow one connecting window per endpoint.
|
|
|
|
|
var gConnecting = {};
|
|
|
|
|
|
|
|
|
|
function OAuth2(aBaseURI, aScope, aAppKey, aAppSecret) {
|
|
|
|
|
this.authURI = aBaseURI + "oauth2/auth";
|
|
|
|
|
this.tokenURI = aBaseURI + "oauth2/token";
|
|
|
|
|
this.consumerKey = aAppKey;
|
|
|
|
|
this.consumerSecret = aAppSecret;
|
|
|
|
|
this.scope = aScope;
|
|
|
|
|
this.extraAuthParams = [];
|
|
|
|
|
|
|
|
|
|
this.log = Log4Moz.getConfiguredLogger("TBOAuth");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OAuth2.prototype = {
|
|
|
|
|
consumerKey: null,
|
|
|
|
|
consumerSecret: null,
|
|
|
|
|
completionURI: "http://localhost",
|
|
|
|
|
requestWindowURI: "chrome://messenger/content/browserRequest.xul",
|
|
|
|
|
requestWindowFeatures: "chrome,private,centerscreen,width=980,height=600",
|
|
|
|
|
requestWindowTitle: "",
|
|
|
|
|
scope: null,
|
|
|
|
|
|
|
|
|
|
accessToken: null,
|
|
|
|
|
refreshToken: null,
|
|
|
|
|
tokenExpires: 0,
|
|
|
|
|
|
|
|
|
|
connect: function connect(aSuccess, aFailure, aWithUI, aRefresh) {
|
|
|
|
|
|
|
|
|
|
this.connectSuccessCallback = aSuccess;
|
|
|
|
|
this.connectFailureCallback = aFailure;
|
|
|
|
|
|
|
|
|
|
if (!aRefresh && this.accessToken) {
|
|
|
|
|
aSuccess();
|
|
|
|
|
} else if (this.refreshToken) {
|
2019-12-30 10:20:58 -05:00
|
|
|
this.requestAccessToken(this.refreshToken, true);
|
2019-11-03 00:17:46 -04:00
|
|
|
} else {
|
|
|
|
|
if (!aWithUI) {
|
|
|
|
|
aFailure('{ "error": "auth_noui" }');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (gConnecting[this.authURI]) {
|
|
|
|
|
aFailure("Window already open");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.requestAuthorization();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
requestAuthorization: function requestAuthorization() {
|
|
|
|
|
let params = [
|
2019-12-30 09:33:56 -05:00
|
|
|
["response_type", "code"],
|
2019-11-03 00:17:46 -04:00
|
|
|
["client_id", this.consumerKey],
|
|
|
|
|
["redirect_uri", this.completionURI],
|
|
|
|
|
];
|
|
|
|
|
// The scope can be optional.
|
|
|
|
|
if (this.scope) {
|
|
|
|
|
params.push(["scope", this.scope]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add extra parameters
|
|
|
|
|
params.push(...this.extraAuthParams);
|
|
|
|
|
|
|
|
|
|
// Now map the parameters to a string
|
|
|
|
|
params = params.map(([k,v]) => k + "=" + encodeURIComponent(v)).join("&");
|
|
|
|
|
|
|
|
|
|
this._browserRequest = {
|
|
|
|
|
account: this,
|
|
|
|
|
url: this.authURI + "?" + params,
|
|
|
|
|
_active: true,
|
|
|
|
|
iconURI: "",
|
|
|
|
|
cancelled: function() {
|
|
|
|
|
if (!this._active) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.account.finishAuthorizationRequest();
|
|
|
|
|
this.account.onAuthorizationFailed(Components.results.NS_ERROR_ABORT, '{ "error": "cancelled"}');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
loaded: function (aWindow, aWebProgress) {
|
|
|
|
|
if (!this._active) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._listener = {
|
|
|
|
|
window: aWindow,
|
|
|
|
|
webProgress: aWebProgress,
|
|
|
|
|
_parent: this.account,
|
|
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener,
|
|
|
|
|
Ci.nsISupportsWeakReference]),
|
|
|
|
|
|
|
|
|
|
_cleanUp: function() {
|
|
|
|
|
this.webProgress.removeProgressListener(this);
|
|
|
|
|
this.window.close();
|
|
|
|
|
delete this.window;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_checkForRedirect: function(aURL) {
|
|
|
|
|
if (aURL.indexOf(this._parent.completionURI) != 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this._parent.finishAuthorizationRequest();
|
|
|
|
|
this._parent.onAuthorizationReceived(aURL);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
|
|
|
|
|
const wpl = Ci.nsIWebProgressListener;
|
|
|
|
|
if (aStateFlags & (wpl.STATE_START | wpl.STATE_IS_NETWORK))
|
|
|
|
|
this._checkForRedirect(aRequest.name);
|
|
|
|
|
},
|
|
|
|
|
onLocationChange: function(aWebProgress, aRequest, aLocation) {
|
|
|
|
|
this._checkForRedirect(aLocation.spec);
|
|
|
|
|
},
|
|
|
|
|
onProgressChange: function() {},
|
|
|
|
|
onStatusChange: function() {},
|
|
|
|
|
onSecurityChange: function() {},
|
|
|
|
|
};
|
|
|
|
|
aWebProgress.addProgressListener(this._listener,
|
|
|
|
|
Ci.nsIWebProgress.NOTIFY_ALL);
|
|
|
|
|
aWindow.document.title = this.account.requestWindowTitle;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.wrappedJSObject = this._browserRequest;
|
|
|
|
|
gConnecting[this.authURI] = true;
|
|
|
|
|
Services.ww.openWindow(null, this.requestWindowURI, null, this.requestWindowFeatures, this);
|
|
|
|
|
},
|
|
|
|
|
finishAuthorizationRequest: function() {
|
|
|
|
|
gConnecting[this.authURI] = false;
|
|
|
|
|
if (!("_browserRequest" in this)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._browserRequest._active = false;
|
|
|
|
|
if ("_listener" in this._browserRequest) {
|
|
|
|
|
this._browserRequest._listener._cleanUp();
|
|
|
|
|
}
|
|
|
|
|
delete this._browserRequest;
|
|
|
|
|
},
|
|
|
|
|
|
2019-12-30 09:49:29 -05:00
|
|
|
// @see RFC 6749 section 4.1.2: Authorization Response
|
|
|
|
|
onAuthorizationReceived(aURL) {
|
|
|
|
|
this.log.info("OAuth2 authorization received: url=" + aURL);
|
|
|
|
|
let params = new URLSearchParams(aURL.split("?", 2)[1]);
|
|
|
|
|
if (params.has("code")) {
|
2019-12-30 10:20:58 -05:00
|
|
|
this.requestAccessToken(params.get("code"), false);
|
2019-12-30 09:33:56 -05:00
|
|
|
} else {
|
2019-12-30 09:49:29 -05:00
|
|
|
this.onAuthorizationFailed(null, aURL);
|
2019-12-30 09:33:56 -05:00
|
|
|
}
|
2019-11-03 00:17:46 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onAuthorizationFailed: function(aError, aData) {
|
|
|
|
|
this.connectFailureCallback(aData);
|
|
|
|
|
},
|
|
|
|
|
|
2019-12-30 10:20:58 -05:00
|
|
|
/**
|
|
|
|
|
* Request a new access token, or refresh an existing one.
|
|
|
|
|
* @param {string} aCode - The token issued to the client.
|
|
|
|
|
* @param {boolean} aRefresh - Whether it's a refresh of a token or not.
|
|
|
|
|
*/
|
|
|
|
|
requestAccessToken(aCode, aRefresh) {
|
|
|
|
|
// @see RFC 6749 section 4.1.3. Access Token Request
|
|
|
|
|
// @see RFC 6749 section 6. Refreshing an Access Token
|
|
|
|
|
|
2019-11-03 00:17:46 -04:00
|
|
|
let params = [
|
|
|
|
|
["client_id", this.consumerKey],
|
|
|
|
|
["client_secret", this.consumerSecret],
|
|
|
|
|
];
|
|
|
|
|
|
2019-12-30 10:20:58 -05:00
|
|
|
if (aRefresh) {
|
|
|
|
|
params.push(["grant_type", "refresh_token"]);
|
|
|
|
|
params.push(["refresh_token", aCode]);
|
|
|
|
|
} else {
|
|
|
|
|
params.push(["grant_type", "authorization_code"]);
|
2019-11-03 00:17:46 -04:00
|
|
|
params.push(["code", aCode]);
|
|
|
|
|
params.push(["redirect_uri", this.completionURI]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let options = {
|
|
|
|
|
postData: params,
|
|
|
|
|
onLoad: this.onAccessTokenReceived.bind(this),
|
|
|
|
|
onError: this.onAccessTokenFailed.bind(this)
|
|
|
|
|
}
|
|
|
|
|
httpRequest(this.tokenURI, options);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onAccessTokenFailed: function onAccessTokenFailed(aError, aData) {
|
|
|
|
|
if (aError != "offline") {
|
|
|
|
|
this.refreshToken = null;
|
|
|
|
|
}
|
|
|
|
|
this.connectFailureCallback(aData);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onAccessTokenReceived: function onRequestTokenReceived(aData) {
|
|
|
|
|
let result = JSON.parse(aData);
|
|
|
|
|
|
|
|
|
|
this.accessToken = result.access_token;
|
|
|
|
|
if ("refresh_token" in result) {
|
|
|
|
|
this.refreshToken = result.refresh_token;
|
|
|
|
|
}
|
|
|
|
|
if ("expires_in" in result) {
|
|
|
|
|
this.tokenExpires = (new Date()).getTime() + (result.expires_in * 1000);
|
|
|
|
|
} else {
|
|
|
|
|
this.tokenExpires = Number.MAX_VALUE;
|
|
|
|
|
}
|
|
|
|
|
this.tokenType = result.token_type;
|
|
|
|
|
|
|
|
|
|
this.connectSuccessCallback();
|
|
|
|
|
}
|
|
|
|
|
};
|