Bug 1597933 - Use URLSearchParams for setting params for OAuth2 authorization request.

This commit is contained in:
Gaming4JC 2019-12-30 10:31:00 -05:00 • committed by Roy Tam
commit 1bc5f20c3a

View file

@ -66,25 +66,31 @@ OAuth2.prototype = {
}, },
requestAuthorization: function requestAuthorization() { requestAuthorization: function requestAuthorization() {
let params = [ let params = new URLSearchParams({
["response_type", "code"], response_type: "code",
["client_id", this.consumerKey], client_id: this.consumerKey,
["redirect_uri", this.completionURI], redirect_uri: this.completionURI,
]; });
// The scope can be optional.
// The scope is optional.
if (this.scope) { if (this.scope) {
params.push(["scope", this.scope]); params.append("scope", this.scope);
} }
// Add extra parameters for (let [name, value] of this.extraAuthParams) {
params.push(...this.extraAuthParams); params.append(name, value);
}
// Now map the parameters to a string let authEndpointURI = this.authURI + "?" + params.toString();
params = params.map(([k,v]) => k + "=" + encodeURIComponent(v)).join("&"); this.log.info(
"Interacting with the resource owner to obtain an authorization grant " +
"from the authorization endpoint: " +
authEndpointURI
);
this._browserRequest = { this._browserRequest = {
account: this, account: this,
url: this.authURI + "?" + params, url: authEndpointURI,
_active: true, _active: true,
iconURI: "", iconURI: "",
cancelled: function() { cancelled: function() {
@ -187,17 +193,20 @@ OAuth2.prototype = {
data.append("client_secret", this.consumerSecret); data.append("client_secret", this.consumerSecret);
if (aRefresh) { if (aRefresh) {
this.log.info(
`Making a refresh request to the token endpoint: ${this.tokenURI}`
);
data.append("grant_type", "refresh_token"); data.append("grant_type", "refresh_token");
data.append("refresh_token", aCode); data.append("refresh_token", aCode);
} else { } else {
this.log.info(
`Making access token request to the token endpoint: ${this.tokenURI}`
);
data.append("grant_type", "authorization_code"); data.append("grant_type", "authorization_code");
data.append("code", aCode); data.append("code", aCode);
data.append("redirect_uri", this.completionURI); data.append("redirect_uri", this.completionURI);
} }
this.log.info(
`Making access token request to the token endpoint: ${this.tokenURI}`
);
fetch(this.tokenURI, { fetch(this.tokenURI, {
method: "POST", method: "POST",
cache: "no-cache", cache: "no-cache",
@ -205,6 +214,18 @@ OAuth2.prototype = {
}) })
.then(response => response.json()) .then(response => response.json())
.then(result => { .then(result => {
if ("error" in result) {
// RFC 6749 section 5.2. Error Response
this.log.info(
`The authorization server returned an error response: ${JSON.stringify(
result
)}`
);
this.connectFailureCallback(result);
return;
}
// RFC 6749 section 5.1. Successful Response
this.log.info("The authorization server issued an access token."); this.log.info("The authorization server issued an access token.");
this.accessToken = result.access_token; this.accessToken = result.access_token;
if ("refresh_token" in result) { if ("refresh_token" in result) {
@ -215,14 +236,10 @@ OAuth2.prototype = {
} else { } else {
this.tokenExpires = Number.MAX_VALUE; this.tokenExpires = Number.MAX_VALUE;
} }
this.tokenType = result.token_type;
this.connectSuccessCallback(); this.connectSuccessCallback();
}) })
.catch(err => { .catch(err => {
// Getting an access token failed. this.log.info(`Connection to authorization server failed: ${err}`);
this.log.info(
`The authorization server returned an error response: ${err}`
);
this.connectFailureCallback(err); this.connectFailureCallback(err);
}); });
} }