Merge remote-tracking branch 'origin/master' into custom

This commit is contained in:
Roy Tam 2020-01-03 13:44:57 +08:00
commit 5ff81ed6e6
8 changed files with 237 additions and 118 deletions

View file

@ -35,20 +35,36 @@ interface nsIMsgAsyncPrompter : nsISupports {
in nsIMsgAsyncPromptListener aCaller); in nsIMsgAsyncPromptListener aCaller);
}; };
[scriptable, function, uuid(acca94c9-378e-46e3-9a91-6655bf9c91a3)]
interface nsIMsgAsyncPromptCallback : nsISupports {
/**
* Called when an auth result is available. Can be passed as a function.
*
* @param aResult True if there is auth information available following the
* prompt, false otherwise.
*/
void onAuthResult(in boolean aResult);
};
/** /**
* This is used in combination with nsIMsgAsyncPrompter. * This is used in combination with nsIMsgAsyncPrompter.
*/ */
[scriptable, uuid(fb5307a3-39d0-462e-92c8-c5c288a2612f)] [scriptable, uuid(fb5307a3-39d0-462e-92c8-c5c288a2612f)]
interface nsIMsgAsyncPromptListener : nsISupports { interface nsIMsgAsyncPromptListener : nsISupports {
/** /**
* Called when the listener should do its prompt. The listener * This method has been deprecated, please use onPromptStartAsync instead.
* should not return until the prompt is complete.
*
* @return True if there is auth information available following the prompt,
* false otherwise.
*/ */
boolean onPromptStart(); boolean onPromptStart();
/**
* Called when the listener should do its prompt. This can happen
* synchronously or asynchronously, but in any case when done the callback
* method should be called.
*
* @param aCallback The callback to execute when auth prompt has completed.
*/
void onPromptStartAsync(in nsIMsgAsyncPromptCallback aCallback);
/** /**
* Called in the case that the queued prompt was combined with another and * Called in the case that the queued prompt was combined with another and
* there is now authentication information available. * there is now authentication information available.

View file

@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Components.utils.import("resource://gre/modules/Deprecated.jsm");
Components.utils.import("resource://gre/modules/Services.jsm"); Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/Task.jsm"); Components.utils.import("resource://gre/modules/Task.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
@ -19,28 +20,46 @@ runnablePrompter.prototype = {
_asyncPrompter: null, _asyncPrompter: null,
_hashKey: null, _hashKey: null,
_promiseAuthPrompt: function(listener) {
return new Promise((resolve, reject) => {
try {
listener.onPromptStartAsync({ onAuthResult: resolve });
} catch (e) {
if (e.result == Components.results.NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED) {
// Fall back to onPromptStart, for add-ons compat
Deprecated.warning("onPromptStart has been replaced by onPromptStartAsync",
"https://bugzilla.mozilla.org/show_bug.cgi?id=1176399");
let ok = listener.onPromptStart();
resolve(ok);
} else {
reject(e);
}
}
});
},
run: Task.async(function *() { run: Task.async(function *() {
yield Services.logins.initializationPromise; yield Services.logins.initializationPromise;
this._asyncPrompter._log.debug("Running prompt for " + this._hashKey); this._asyncPrompter._log.debug("Running prompt for " + this._hashKey);
let prompter = this._asyncPrompter._pendingPrompts[this._hashKey]; let prompter = this._asyncPrompter._pendingPrompts[this._hashKey];
let ok = false; let ok = false;
try { try {
ok = prompter.first.onPromptStart(); ok = yield this._promiseAuthPrompt(prompter.first);
} } catch (ex) {
catch (ex) {
Components.utils.reportError("runnablePrompter:run: " + ex + "\n"); Components.utils.reportError("runnablePrompter:run: " + ex + "\n");
prompter.first.onPromptCanceled();
} }
delete this._asyncPrompter._pendingPrompts[this._hashKey]; delete this._asyncPrompter._pendingPrompts[this._hashKey];
for (var consumer of prompter.consumers) { for (var consumer of prompter.consumers) {
try { try {
if (ok) if (ok) {
consumer.onPromptAuthAvailable(); consumer.onPromptAuthAvailable();
else } else {
consumer.onPromptCanceled(); consumer.onPromptCanceled();
} }
catch (ex) { } catch (ex) {
// Log the error for extension devs and others to pick up. // Log the error for extension devs and others to pick up.
Components.utils.reportError("runnablePrompter:run: consumer.onPrompt* reported an exception: " + ex + "\n"); Components.utils.reportError("runnablePrompter:run: consumer.onPrompt* reported an exception: " + ex + "\n");
} }

View file

@ -126,19 +126,43 @@ OAuth2Module.prototype = {
} }
} }
// Otherwise, we need a new login, so create one and fill it in. // Unless the token is null, we need to create and fill in a new login
let login = Cc["@mozilla.org/login-manager/loginInfo;1"] if (token) {
.createInstance(Ci.nsILoginInfo); let login = Cc["@mozilla.org/login-manager/loginInfo;1"]
login.init(this._loginUrl, null, this._scope, this._username, token, .createInstance(Ci.nsILoginInfo);
'', ''); login.init(this._loginUrl, null, this._scope, this._username, token,
loginMgr.addLogin(login); '', '');
loginMgr.addLogin(login);
}
return token; return token;
}, },
connect(aWithUI, aListener) { connect(aWithUI, aListener) {
this._oauth.connect(() => aListener.onSuccess(this._oauth.accessToken), let oauth = this._oauth;
x => aListener.onFailure(x), let promptlistener = {
aWithUI, false); onPromptStartAsync: function(callback) {
oauth.connect(() => {
this.onPromptAuthAvailable();
callback.onAuthResult(true);
}, (err) => {
this.onPromptCanceled();
callback.onAuthResult(false);
}, aWithUI, false);
},
onPromptAuthAvailable: function() {
aListener.onSuccess(oauth.accessToken);
},
onPromptCanceled: function() {
aListener.onFailure(Components.results.NS_ERROR_ABORT);
},
onPromptStart: function() {}
};
let asyncprompter = Components.classes["@mozilla.org/messenger/msgAsyncPrompter;1"]
.getService(Components.interfaces.nsIMsgAsyncPrompter);
let promptkey = this._loginUrl + "/" + this._username;
asyncprompter.queueAsyncAuthPrompt(promptkey, false, promptlistener);
}, },
buildXOAuth2String() { buildXOAuth2String() {

View file

@ -3,30 +3,37 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */ * You can obtain one at http://mozilla.org/MPL/2.0/. */
/** /**
* Provides OAuth 2.0 authentication * Provides OAuth 2.0 authentication.
* @see RFC 6749
*/ */
var EXPORTED_SYMBOLS = ["OAuth2"]; var EXPORTED_SYMBOLS = ["OAuth2"];
var {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components; 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/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource:///modules/gloda/log4moz.js"); Cu.import("resource:///modules/gloda/log4moz.js");
function parseURLData(aData) { Cu.importGlobalProperties(["fetch"]);
let result = {};
aData.split(/[?#]/, 2)[1].split("&").forEach(function (aParam) {
let [key, value] = aParam.split("=");
result[key] = decodeURIComponent(value);
});
return result;
}
// Only allow one connecting window per endpoint. // Only allow one connecting window per endpoint.
var gConnecting = {}; var gConnecting = {};
function OAuth2(aBaseURI, aScope, aAppKey, aAppSecret) { /**
* Constructor for the OAuth2 object.
*
* @constructor
* @param {string} aBaseURI - The base URI for authentication and token
* requests, oauth2/auth or oauth2/token will be added for the actual
* requests.
* @param {?string} aScope - The scope as specified by RFC 6749 Section 3.3.
* Will not be included in the requests if falsy.
* @param {string} aAppKey - The client_id as specified by RFC 6749 Section
* 2.3.1.
* @param {string} [aAppSecret=null] - The client_secret as specified in
* RFC 6749 section 2.3.1. Will not be included in the requests if null.
*/
function OAuth2(aBaseURI, aScope, aAppKey, aAppSecret = null) {
this.authURI = aBaseURI + "oauth2/auth"; this.authURI = aBaseURI + "oauth2/auth";
this.tokenURI = aBaseURI + "oauth2/token"; this.tokenURI = aBaseURI + "oauth2/token";
this.consumerKey = aAppKey; this.consumerKey = aAppKey;
@ -37,12 +44,7 @@ function OAuth2(aBaseURI, aScope, aAppKey, aAppSecret) {
this.log = Log4Moz.getConfiguredLogger("TBOAuth"); this.log = Log4Moz.getConfiguredLogger("TBOAuth");
} }
OAuth2.CODE_AUTHORIZATION = "authorization_code";
OAuth2.CODE_REFRESH = "refresh_token";
OAuth2.prototype = { OAuth2.prototype = {
responseType: "code",
consumerKey: null, consumerKey: null,
consumerSecret: null, consumerSecret: null,
completionURI: "http://localhost", completionURI: "http://localhost",
@ -63,7 +65,7 @@ OAuth2.prototype = {
if (!aRefresh && this.accessToken) { if (!aRefresh && this.accessToken) {
aSuccess(); aSuccess();
} else if (this.refreshToken) { } else if (this.refreshToken) {
this.requestAccessToken(this.refreshToken, OAuth2.CODE_REFRESH); this.requestAccessToken(this.refreshToken, true);
} else { } else {
if (!aWithUI) { if (!aWithUI) {
aFailure('{ "error": "auth_noui" }'); aFailure('{ "error": "auth_noui" }');
@ -78,25 +80,31 @@ OAuth2.prototype = {
}, },
requestAuthorization: function requestAuthorization() { requestAuthorization: function requestAuthorization() {
let params = [ let params = new URLSearchParams({
["response_type", this.responseType], 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() {
@ -170,65 +178,88 @@ OAuth2.prototype = {
delete this._browserRequest; delete this._browserRequest;
}, },
onAuthorizationReceived: function(aData) { // @see RFC 6749 section 4.1.2: Authorization Response
this.log.info("authorization received" + aData); onAuthorizationReceived(aURL) {
let results = parseURLData(aData); this.log.info("OAuth2 authorization received: url=" + aURL);
if (this.responseType == "code" && results.code) { let params = new URLSearchParams(aURL.split("?", 2)[1]);
this.requestAccessToken(results.code, OAuth2.CODE_AUTHORIZATION); if (params.has("code")) {
} else if (this.responseType == "token") { this.requestAccessToken(params.get("code"), false);
this.onAccessTokenReceived(JSON.stringify(results)); } else {
this.onAuthorizationFailed(null, aURL);
} }
else
this.onAuthorizationFailed(null, aData);
}, },
onAuthorizationFailed: function(aError, aData) { onAuthorizationFailed: function(aError, aData) {
this.connectFailureCallback(aData); this.connectFailureCallback(aData);
}, },
requestAccessToken: function requestAccessToken(aCode, aType) { /**
let params = [ * Request a new access token, or refresh an existing one.
["client_id", this.consumerKey], * @param {string} aCode - The token issued to the client.
["client_secret", this.consumerSecret], * @param {boolean} aRefresh - Whether it's a refresh of a token or not.
["grant_type", aType], */
]; requestAccessToken(aCode, aRefresh) {
// @see RFC 6749 section 4.1.3. Access Token Request
// @see RFC 6749 section 6. Refreshing an Access Token
if (aType == OAuth2.CODE_AUTHORIZATION) { let data = new URLSearchParams();
params.push(["code", aCode]); data.append("client_id", this.consumerKey);
params.push(["redirect_uri", this.completionURI]); if (this.consumerSecret !== null) {
} else if (aType == OAuth2.CODE_REFRESH) { // Section 2.3.1. of RFC 6749 states that empty secrets MAY be omitted
params.push(["refresh_token", aCode]); // by the client. This OAuth implementation delegates this decission to
// the caller: If the secret is null, it will be omitted.
data.append("client_secret", this.consumerSecret);
}
if (aRefresh) {
this.log.info(
`Making a refresh request to the token endpoint: ${this.tokenURI}`
);
data.append("grant_type", "refresh_token");
data.append("refresh_token", aCode);
} else {
this.log.info(
`Making access token request to the token endpoint: ${this.tokenURI}`
);
data.append("grant_type", "authorization_code");
data.append("code", aCode);
data.append("redirect_uri", this.completionURI);
} }
let options = { fetch(this.tokenURI, {
postData: params, method: "POST",
onLoad: this.onAccessTokenReceived.bind(this), cache: "no-cache",
onError: this.onAccessTokenFailed.bind(this) body: data,
})
.then(response => response.json())
.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;
} }
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);
// RFC 6749 section 5.1. Successful Response
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) {
this.refreshToken = result.refresh_token; this.refreshToken = result.refresh_token;
} }
if ("expires_in" in result) { if ("expires_in" in result) {
this.tokenExpires = (new Date()).getTime() + (result.expires_in * 1000); this.tokenExpires = new Date().getTime() + result.expires_in * 1000;
} else { } else {
this.tokenExpires = Number.MAX_VALUE; this.tokenExpires = Number.MAX_VALUE;
} }
this.tokenType = result.token_type;
this.connectSuccessCallback(); this.connectSuccessCallback();
})
.catch(err => {
this.log.info(`Connection to authorization server failed: ${err}`);
this.connectFailureCallback(err);
});
} }
}; };

View file

@ -5714,6 +5714,36 @@ void nsImapProtocol::ResetAuthMethods()
m_failedAuthMethods = 0; m_failedAuthMethods = 0;
} }
nsresult nsImapProtocol::SendDataParseIMAPandCheckForNewMail(const char *aData, const char *aCommand)
{
nsresult rv;
bool isResend = false;
while (true)
{
// Send authentication string (true: suppress logging the string).
rv = SendData(aData, true);
if (NS_FAILED(rv))
break;
ParseIMAPandCheckForNewMail(aCommand);
if (!GetServerStateParser().WaitingForMoreClientInput())
break;
// The server is asking for the authentication string again. So we send
// the same string again although we know that it might be rejected again.
// We do that to get a firm authentication failure instead of a resend
// request. That keeps things in order before failing authentication and
// trying another method if capable.
if (isResend)
{
rv = NS_ERROR_FAILURE;
break;
}
isResend = true;
}
return rv;
}
nsresult nsImapProtocol::AuthLogin(const char *userName, const nsCString &password, eIMAPCapabilityFlag flag) nsresult nsImapProtocol::AuthLogin(const char *userName, const nsCString &password, eIMAPCapabilityFlag flag)
{ {
ProgressEventFunctionUsingName("imapStatusSendingAuthLogin"); ProgressEventFunctionUsingName("imapStatusSendingAuthLogin");
@ -5880,29 +5910,7 @@ nsresult nsImapProtocol::AuthLogin(const char *userName, const nsCString &passwo
PR_snprintf(m_dataOutputBuf, OUTPUT_BUFFER_SIZE, "%s" CRLF, base64Str); PR_snprintf(m_dataOutputBuf, OUTPUT_BUFFER_SIZE, "%s" CRLF, base64Str);
PR_Free(base64Str); PR_Free(base64Str);
bool isResend = false; rv = SendDataParseIMAPandCheckForNewMail(m_dataOutputBuf, currentCommand);
while (true)
{
// Send authentication string (true: suppress logging the string).
rv = SendData(m_dataOutputBuf, true);
if (NS_FAILED(rv))
break;
ParseIMAPandCheckForNewMail(currentCommand);
if (!GetServerStateParser().WaitingForMoreClientInput())
break;
// Server is asking for authentication string again. So we send the
// same string again although we already know that it will be
// rejected again. We do that to get a firm authentication failure
// instead of a resend request. That keeps things in order before
// failing "authenticate PLAIN" and trying another method if capable.
if (isResend)
{
rv = NS_ERROR_FAILURE;
break;
}
isResend = true;
}
} // if the last command succeeded } // if the last command succeeded
} // if auth plain capability } // if auth plain capability
else if (flag & kHasAuthLoginCapability) else if (flag & kHasAuthLoginCapability)
@ -5953,9 +5961,8 @@ nsresult nsImapProtocol::AuthLogin(const char *userName, const nsCString &passwo
EscapeUserNamePasswordString(password.get(), &correctedPassword); EscapeUserNamePasswordString(password.get(), &correctedPassword);
command.Append(correctedPassword); command.Append(correctedPassword);
command.Append("\"" CRLF); command.Append("\"" CRLF);
rv = SendData(command.get(), true /* suppress logging */);
NS_ENSURE_SUCCESS(rv, rv); rv = SendDataParseIMAPandCheckForNewMail(command.get(), nullptr);
ParseIMAPandCheckForNewMail();
} }
#ifdef MOZ_MAILNEWS_OAUTH2 #ifdef MOZ_MAILNEWS_OAUTH2
else if (flag & kHasXOAuth2Capability) else if (flag & kHasXOAuth2Capability)
@ -8513,6 +8520,13 @@ nsresult nsImapProtocol::GetPassword(nsCString &password,
return rv; return rv;
} }
NS_IMETHODIMP nsImapProtocol::OnPromptStartAsync(nsIMsgAsyncPromptCallback *aCallback)
{
bool result = false;
OnPromptStart(&result);
return aCallback->OnAuthResult(result);
}
// This is called from the UI thread. // This is called from the UI thread.
NS_IMETHODIMP NS_IMETHODIMP
nsImapProtocol::OnPromptStart(bool *aResult) nsImapProtocol::OnPromptStart(bool *aResult)

View file

@ -485,6 +485,7 @@ private:
void Namespace(); void Namespace();
void InsecureLogin(const char *userName, const nsCString &password); void InsecureLogin(const char *userName, const nsCString &password);
nsresult AuthLogin(const char *userName, const nsCString &password, eIMAPCapabilityFlag flag); nsresult AuthLogin(const char *userName, const nsCString &password, eIMAPCapabilityFlag flag);
nsresult SendDataParseIMAPandCheckForNewMail(const char *data, const char *command);
void ProcessAuthenticatedStateURL(); void ProcessAuthenticatedStateURL();
void ProcessAfterAuthenticated(); void ProcessAfterAuthenticated();
void ProcessSelectedStateURL(); void ProcessSelectedStateURL();

View file

@ -740,6 +740,13 @@ nsresult nsPop3Protocol::StartGetAsyncPassword(Pop3StatesEnum aNextState)
return rv; return rv;
} }
NS_IMETHODIMP nsPop3Protocol::OnPromptStartAsync(nsIMsgAsyncPromptCallback *aCallback)
{
bool result = false;
OnPromptStart(&result);
return aCallback->OnAuthResult(result);
}
NS_IMETHODIMP nsPop3Protocol::OnPromptStart(bool *aResult) NS_IMETHODIMP nsPop3Protocol::OnPromptStart(bool *aResult)
{ {
MOZ_LOG(POP3LOGMODULE, LogLevel::Debug, (POP3LOG("OnPromptStart()"))); MOZ_LOG(POP3LOGMODULE, LogLevel::Debug, (POP3LOG("OnPromptStart()")));

View file

@ -2472,6 +2472,13 @@ nsresult nsNNTPProtocol::PasswordResponse()
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
NS_IMETHODIMP nsNNTPProtocol::OnPromptStartAsync(nsIMsgAsyncPromptCallback *aCallback)
{
bool result = false;
OnPromptStart(&result);
return aCallback->OnAuthResult(result);
}
NS_IMETHODIMP nsNNTPProtocol::OnPromptStart(bool *authAvailable) NS_IMETHODIMP nsNNTPProtocol::OnPromptStart(bool *authAvailable)
{ {
NS_ENSURE_ARG_POINTER(authAvailable); NS_ENSURE_ARG_POINTER(authAvailable);