2013-12-02 21:56:24 +00: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/. */
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["FxAccountsClient"];
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
|
|
|
|
2014-01-24 02:04:38 +00:00
|
|
|
Cu.import("resource://gre/modules/Log.jsm");
|
2013-12-02 21:56:24 +00:00
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://services-common/utils.js");
|
2014-02-25 17:19:47 +00:00
|
|
|
Cu.import("resource://services-common/hawkclient.js");
|
2014-06-18 09:42:15 +00:00
|
|
|
Cu.import("resource://services-common/hawkrequest.js");
|
2013-12-02 21:56:24 +00:00
|
|
|
Cu.import("resource://services-crypto/utils.js");
|
2013-12-13 11:37:55 +00:00
|
|
|
Cu.import("resource://gre/modules/FxAccountsCommon.js");
|
2014-02-05 06:14:30 +00:00
|
|
|
Cu.import("resource://gre/modules/Credentials.jsm");
|
2013-12-02 21:56:24 +00:00
|
|
|
|
2014-02-27 18:41:46 +00:00
|
|
|
const HOST = Services.prefs.getCharPref("identity.fxaccounts.auth.uri");
|
2013-12-02 21:56:24 +00:00
|
|
|
|
2015-10-22 08:18:17 +00:00
|
|
|
const SIGNIN = "/account/login";
|
|
|
|
const SIGNUP = "/account/create";
|
|
|
|
|
2013-12-02 21:56:24 +00:00
|
|
|
this.FxAccountsClient = function(host = HOST) {
|
|
|
|
this.host = host;
|
2014-01-24 02:04:38 +00:00
|
|
|
|
|
|
|
// The FxA auth server expects requests to certain endpoints to be authorized
|
|
|
|
// using Hawk.
|
|
|
|
this.hawk = new HawkClient(host);
|
2014-04-10 02:08:19 +00:00
|
|
|
this.hawk.observerPrefix = "FxA:hawk";
|
2014-02-26 18:11:49 +00:00
|
|
|
|
|
|
|
// Manage server backoff state. C.f.
|
|
|
|
// https://github.com/mozilla/fxa-auth-server/blob/master/docs/api.md#backoff-protocol
|
|
|
|
this.backoffError = null;
|
2013-12-02 21:56:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.FxAccountsClient.prototype = {
|
2014-01-24 02:04:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return client clock offset, in milliseconds, as determined by hawk client.
|
|
|
|
* Provided because callers should not have to know about hawk
|
|
|
|
* implementation.
|
|
|
|
*
|
|
|
|
* The offset is the number of milliseconds that must be added to the client
|
|
|
|
* clock to make it equal to the server clock. For example, if the client is
|
|
|
|
* five minutes ahead of the server, the localtimeOffsetMsec will be -300000.
|
|
|
|
*/
|
|
|
|
get localtimeOffsetMsec() {
|
|
|
|
return this.hawk.localtimeOffsetMsec;
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return current time in milliseconds
|
|
|
|
*
|
|
|
|
* Not used by this module, but made available to the FxAccounts.jsm
|
|
|
|
* that uses this client.
|
|
|
|
*/
|
|
|
|
now: function() {
|
|
|
|
return this.hawk.now();
|
|
|
|
},
|
|
|
|
|
2013-12-02 21:56:24 +00:00
|
|
|
/**
|
2015-10-22 08:18:17 +00:00
|
|
|
* Common code from signIn and signUp.
|
2013-12-02 21:56:24 +00:00
|
|
|
*
|
2015-10-22 08:18:17 +00:00
|
|
|
* @param path
|
|
|
|
* Request URL path. Can be /account/create or /account/login
|
2013-12-02 21:56:24 +00:00
|
|
|
* @param email
|
|
|
|
* The email address for the account (utf8)
|
|
|
|
* @param password
|
|
|
|
* The user's password
|
2014-03-12 21:50:58 +00:00
|
|
|
* @param [getKeys=false]
|
|
|
|
* If set to true the keyFetchToken will be retrieved
|
|
|
|
* @param [retryOK=true]
|
|
|
|
* If capitalization of the email is wrong and retryOK is set to true,
|
|
|
|
* we will retry with the suggested capitalization from the server
|
2013-12-02 21:56:24 +00:00
|
|
|
* @return Promise
|
|
|
|
* Returns a promise that resolves to an object:
|
|
|
|
* {
|
2014-03-12 21:50:58 +00:00
|
|
|
* authAt: authentication time for the session (seconds since epoch)
|
|
|
|
* email: the primary email for this account
|
2014-03-21 09:23:15 +00:00
|
|
|
* keyFetchToken: a key fetch token (hex)
|
|
|
|
* sessionToken: a session token (hex)
|
|
|
|
* uid: the user's unique ID (hex)
|
|
|
|
* unwrapBKey: used to unwrap kB, derived locally from the
|
|
|
|
* password (not revealed to the FxA server)
|
2015-10-22 08:18:17 +00:00
|
|
|
* verified (optional): flag indicating verification status of the
|
|
|
|
* email
|
2013-12-02 21:56:24 +00:00
|
|
|
* }
|
|
|
|
*/
|
2015-10-22 08:18:17 +00:00
|
|
|
_createSession: function(path, email, password, getKeys=false,
|
|
|
|
retryOK=true) {
|
2014-02-05 06:14:30 +00:00
|
|
|
return Credentials.setup(email, password).then((creds) => {
|
|
|
|
let data = {
|
|
|
|
authPW: CommonUtils.bytesAsHex(creds.authPW),
|
2016-02-26 04:46:30 +00:00
|
|
|
email: email,
|
2014-02-05 06:14:30 +00:00
|
|
|
};
|
2014-03-12 21:50:58 +00:00
|
|
|
let keys = getKeys ? "?keys=true" : "";
|
|
|
|
|
2015-10-22 08:18:17 +00:00
|
|
|
return this._request(path + keys, "POST", null, data).then(
|
2014-02-19 16:34:42 +00:00
|
|
|
// Include the canonical capitalization of the email in the response so
|
|
|
|
// the caller can set its signed-in user state accordingly.
|
|
|
|
result => {
|
|
|
|
result.email = data.email;
|
2014-03-21 09:23:15 +00:00
|
|
|
result.unwrapBKey = CommonUtils.bytesAsHex(creds.unwrapBKey);
|
|
|
|
|
2014-02-19 16:34:42 +00:00
|
|
|
return result;
|
|
|
|
},
|
|
|
|
error => {
|
2015-10-22 08:18:17 +00:00
|
|
|
log.debug("Session creation failed", error);
|
2014-02-19 16:34:42 +00:00
|
|
|
// If the user entered an email with different capitalization from
|
|
|
|
// what's stored in the database (e.g., Greta.Garbo@gmail.COM as
|
|
|
|
// opposed to greta.garbo@gmail.com), the server will respond with a
|
|
|
|
// errno 120 (code 400) and the expected capitalization of the email.
|
|
|
|
// We retry with this email exactly once. If successful, we use the
|
|
|
|
// server's version of the email as the signed-in-user's email. This
|
|
|
|
// is necessary because the email also serves as salt; so we must be
|
|
|
|
// in agreement with the server on capitalization.
|
|
|
|
//
|
|
|
|
// API reference:
|
|
|
|
// https://github.com/mozilla/fxa-auth-server/blob/master/docs/api.md
|
|
|
|
if (ERRNO_INCORRECT_EMAIL_CASE === error.errno && retryOK) {
|
|
|
|
if (!error.email) {
|
|
|
|
log.error("Server returned errno 120 but did not provide email");
|
|
|
|
throw error;
|
|
|
|
}
|
2015-10-22 08:18:17 +00:00
|
|
|
return this._createSession(path, error.email, password, getKeys,
|
|
|
|
false);
|
2014-02-19 16:34:42 +00:00
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
);
|
2014-02-05 06:14:30 +00:00
|
|
|
});
|
2013-12-02 21:56:24 +00:00
|
|
|
},
|
|
|
|
|
2015-10-22 08:18:17 +00:00
|
|
|
/**
|
|
|
|
* Create a new Firefox Account and authenticate
|
|
|
|
*
|
|
|
|
* @param email
|
|
|
|
* The email address for the account (utf8)
|
|
|
|
* @param password
|
|
|
|
* The user's password
|
|
|
|
* @param [getKeys=false]
|
|
|
|
* If set to true the keyFetchToken will be retrieved
|
|
|
|
* @return Promise
|
|
|
|
* Returns a promise that resolves to an object:
|
|
|
|
* {
|
|
|
|
* uid: the user's unique ID (hex)
|
|
|
|
* sessionToken: a session token (hex)
|
|
|
|
* keyFetchToken: a key fetch token (hex),
|
|
|
|
* unwrapBKey: used to unwrap kB, derived locally from the
|
|
|
|
* password (not revealed to the FxA server)
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
signUp: function(email, password, getKeys=false) {
|
|
|
|
return this._createSession(SIGNUP, email, password, getKeys,
|
|
|
|
false /* no retry */);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate and create a new session with the Firefox Account API server
|
|
|
|
*
|
|
|
|
* @param email
|
|
|
|
* The email address for the account (utf8)
|
|
|
|
* @param password
|
|
|
|
* The user's password
|
|
|
|
* @param [getKeys=false]
|
|
|
|
* If set to true the keyFetchToken will be retrieved
|
|
|
|
* @return Promise
|
|
|
|
* Returns a promise that resolves to an object:
|
|
|
|
* {
|
|
|
|
* authAt: authentication time for the session (seconds since epoch)
|
|
|
|
* email: the primary email for this account
|
|
|
|
* keyFetchToken: a key fetch token (hex)
|
|
|
|
* sessionToken: a session token (hex)
|
|
|
|
* uid: the user's unique ID (hex)
|
|
|
|
* unwrapBKey: used to unwrap kB, derived locally from the
|
|
|
|
* password (not revealed to the FxA server)
|
|
|
|
* verified: flag indicating verification status of the email
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
signIn: function signIn(email, password, getKeys=false) {
|
|
|
|
return this._createSession(SIGNIN, email, password, getKeys,
|
|
|
|
true /* retry */);
|
|
|
|
},
|
|
|
|
|
2013-12-02 21:56:24 +00:00
|
|
|
/**
|
|
|
|
* Destroy the current session with the Firefox Account API server
|
|
|
|
*
|
|
|
|
* @param sessionTokenHex
|
2014-01-24 00:52:24 +00:00
|
|
|
* The session token encoded in hex
|
2013-12-02 21:56:24 +00:00
|
|
|
* @return Promise
|
|
|
|
*/
|
2015-09-29 23:34:37 +00:00
|
|
|
signOut: function (sessionTokenHex, options = {}) {
|
|
|
|
let path = "/session/destroy";
|
|
|
|
if (options.service) {
|
2016-01-13 04:55:00 +00:00
|
|
|
path += "?service=" + encodeURIComponent(options.service);
|
2015-09-29 23:34:37 +00:00
|
|
|
}
|
|
|
|
return this._request(path, "POST",
|
2014-06-18 09:42:15 +00:00
|
|
|
deriveHawkCredentials(sessionTokenHex, "sessionToken"));
|
2013-12-02 21:56:24 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the verification status of the user's FxA email address
|
|
|
|
*
|
|
|
|
* @param sessionTokenHex
|
2014-01-24 00:52:24 +00:00
|
|
|
* The current session token encoded in hex
|
2013-12-02 21:56:24 +00:00
|
|
|
* @return Promise
|
|
|
|
*/
|
2016-03-16 00:28:50 +00:00
|
|
|
recoveryEmailStatus: function (sessionTokenHex, options = {}) {
|
|
|
|
let path = "/recovery_email/status";
|
|
|
|
if (options.reason) {
|
|
|
|
path += "?reason=" + encodeURIComponent(options.reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._request(path, "GET",
|
2014-06-18 09:42:15 +00:00
|
|
|
deriveHawkCredentials(sessionTokenHex, "sessionToken"));
|
2013-12-02 21:56:24 +00:00
|
|
|
},
|
|
|
|
|
2014-01-24 00:52:24 +00:00
|
|
|
/**
|
|
|
|
* Resend the verification email for the user
|
|
|
|
*
|
|
|
|
* @param sessionTokenHex
|
|
|
|
* The current token encoded in hex
|
|
|
|
* @return Promise
|
|
|
|
*/
|
|
|
|
resendVerificationEmail: function(sessionTokenHex) {
|
|
|
|
return this._request("/recovery_email/resend_code", "POST",
|
2014-06-18 09:42:15 +00:00
|
|
|
deriveHawkCredentials(sessionTokenHex, "sessionToken"));
|
2014-01-24 00:52:24 +00:00
|
|
|
},
|
|
|
|
|
2013-12-02 21:56:24 +00:00
|
|
|
/**
|
|
|
|
* Retrieve encryption keys
|
|
|
|
*
|
|
|
|
* @param keyFetchTokenHex
|
|
|
|
* A one-time use key fetch token encoded in hex
|
|
|
|
* @return Promise
|
|
|
|
* Returns a promise that resolves to an object:
|
|
|
|
* {
|
2014-02-05 06:14:30 +00:00
|
|
|
* kA: an encryption key for recevorable data (bytes)
|
2014-02-19 16:34:42 +00:00
|
|
|
* wrapKB: an encryption key that requires knowledge of the
|
2014-02-05 06:14:30 +00:00
|
|
|
* user's password (bytes)
|
2013-12-02 21:56:24 +00:00
|
|
|
* }
|
|
|
|
*/
|
|
|
|
accountKeys: function (keyFetchTokenHex) {
|
2014-06-18 09:42:15 +00:00
|
|
|
let creds = deriveHawkCredentials(keyFetchTokenHex, "keyFetchToken");
|
2013-12-02 21:56:24 +00:00
|
|
|
let keyRequestKey = creds.extra.slice(0, 32);
|
|
|
|
let morecreds = CryptoUtils.hkdf(keyRequestKey, undefined,
|
2014-02-05 06:14:30 +00:00
|
|
|
Credentials.keyWord("account/keys"), 3 * 32);
|
2013-12-02 21:56:24 +00:00
|
|
|
let respHMACKey = morecreds.slice(0, 32);
|
|
|
|
let respXORKey = morecreds.slice(32, 96);
|
|
|
|
|
|
|
|
return this._request("/account/keys", "GET", creds).then(resp => {
|
|
|
|
if (!resp.bundle) {
|
|
|
|
throw new Error("failed to retrieve keys");
|
|
|
|
}
|
|
|
|
|
|
|
|
let bundle = CommonUtils.hexToBytes(resp.bundle);
|
|
|
|
let mac = bundle.slice(-32);
|
|
|
|
|
|
|
|
let hasher = CryptoUtils.makeHMACHasher(Ci.nsICryptoHMAC.SHA256,
|
|
|
|
CryptoUtils.makeHMACKey(respHMACKey));
|
|
|
|
|
|
|
|
let bundleMAC = CryptoUtils.digestBytes(bundle.slice(0, -32), hasher);
|
|
|
|
if (mac !== bundleMAC) {
|
|
|
|
throw new Error("error unbundling encryption keys");
|
|
|
|
}
|
|
|
|
|
|
|
|
let keyAWrapB = CryptoUtils.xor(respXORKey, bundle.slice(0, 64));
|
|
|
|
|
|
|
|
return {
|
|
|
|
kA: keyAWrapB.slice(0, 32),
|
|
|
|
wrapKB: keyAWrapB.slice(32)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a public key to the FxA API server and returns a signed certificate
|
|
|
|
*
|
|
|
|
* @param sessionTokenHex
|
2014-01-24 00:52:24 +00:00
|
|
|
* The current session token encoded in hex
|
2013-12-02 21:56:24 +00:00
|
|
|
* @param serializedPublicKey
|
|
|
|
* A public key (usually generated by jwcrypto)
|
|
|
|
* @param lifetime
|
|
|
|
* The lifetime of the certificate
|
|
|
|
* @return Promise
|
2014-06-06 15:54:27 +00:00
|
|
|
* Returns a promise that resolves to the signed certificate.
|
|
|
|
* The certificate can be used to generate a Persona assertion.
|
|
|
|
* @throws a new Error
|
|
|
|
* wrapping any of these HTTP code/errno pairs:
|
|
|
|
* https://github.com/mozilla/fxa-auth-server/blob/master/docs/api.md#response-12
|
2013-12-02 21:56:24 +00:00
|
|
|
*/
|
|
|
|
signCertificate: function (sessionTokenHex, serializedPublicKey, lifetime) {
|
2014-06-18 09:42:15 +00:00
|
|
|
let creds = deriveHawkCredentials(sessionTokenHex, "sessionToken");
|
2013-12-02 21:56:24 +00:00
|
|
|
|
|
|
|
let body = { publicKey: serializedPublicKey,
|
|
|
|
duration: lifetime };
|
|
|
|
return Promise.resolve()
|
|
|
|
.then(_ => this._request("/certificate/sign", "POST", creds, body))
|
|
|
|
.then(resp => resp.cert,
|
2014-01-24 02:04:38 +00:00
|
|
|
err => {
|
|
|
|
log.error("HAWK.signCertificate error: " + JSON.stringify(err));
|
|
|
|
throw err;
|
|
|
|
});
|
2013-12-02 21:56:24 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if an account exists
|
|
|
|
*
|
|
|
|
* @param email
|
|
|
|
* The email address to check
|
|
|
|
* @return Promise
|
|
|
|
* The promise resolves to true if the account exists, or false
|
|
|
|
* if it doesn't. The promise is rejected on other errors.
|
|
|
|
*/
|
|
|
|
accountExists: function (email) {
|
2014-02-05 06:14:30 +00:00
|
|
|
return this.signIn(email, "").then(
|
|
|
|
(cantHappen) => {
|
|
|
|
throw new Error("How did I sign in with an empty password?");
|
|
|
|
},
|
|
|
|
(expectedError) => {
|
|
|
|
switch (expectedError.errno) {
|
|
|
|
case ERRNO_ACCOUNT_DOES_NOT_EXIST:
|
2013-12-02 21:56:24 +00:00
|
|
|
return false;
|
2014-02-05 06:14:30 +00:00
|
|
|
break;
|
|
|
|
case ERRNO_INCORRECT_PASSWORD:
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// not so expected, any more ...
|
|
|
|
throw expectedError;
|
|
|
|
break;
|
2013-12-02 21:56:24 +00:00
|
|
|
}
|
2014-02-05 06:14:30 +00:00
|
|
|
}
|
|
|
|
);
|
2013-12-02 21:56:24 +00:00
|
|
|
},
|
|
|
|
|
2014-05-15 23:52:52 +00:00
|
|
|
/**
|
|
|
|
* Given the uid of an existing account (not an arbitrary email), ask
|
|
|
|
* the server if it still exists via /account/status.
|
|
|
|
*
|
|
|
|
* Used for differentiating between password change and account deletion.
|
|
|
|
*/
|
|
|
|
accountStatus: function(uid) {
|
|
|
|
return this._request("/account/status?uid="+uid, "GET").then(
|
|
|
|
(result) => {
|
|
|
|
return result.exists;
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
log.error("accountStatus failed with: " + error);
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2016-01-13 04:55:00 +00:00
|
|
|
/**
|
|
|
|
* Register a new device
|
|
|
|
*
|
|
|
|
* @method registerDevice
|
|
|
|
* @param sessionTokenHex
|
|
|
|
* Session token obtained from signIn
|
|
|
|
* @param name
|
|
|
|
* Device name
|
|
|
|
* @param type
|
|
|
|
* Device type (mobile|desktop)
|
2016-03-07 05:00:34 +00:00
|
|
|
* @param [options]
|
|
|
|
* Extra device options
|
|
|
|
* @param [options.pushCallback]
|
|
|
|
* `pushCallback` push endpoint callback
|
2016-01-13 04:55:00 +00:00
|
|
|
* @return Promise
|
|
|
|
* Resolves to an object:
|
|
|
|
* {
|
|
|
|
* id: Device identifier
|
|
|
|
* createdAt: Creation time (milliseconds since epoch)
|
|
|
|
* name: Name of device
|
|
|
|
* type: Type of device (mobile|desktop)
|
|
|
|
* }
|
|
|
|
*/
|
2016-03-07 05:00:34 +00:00
|
|
|
registerDevice(sessionTokenHex, name, type, options = {}) {
|
2016-01-13 04:55:00 +00:00
|
|
|
let path = "/account/device";
|
|
|
|
|
|
|
|
let creds = deriveHawkCredentials(sessionTokenHex, "sessionToken");
|
|
|
|
let body = { name, type };
|
|
|
|
|
2016-03-07 05:00:34 +00:00
|
|
|
if (options.pushCallback) {
|
|
|
|
body.pushCallback = options.pushCallback;
|
|
|
|
}
|
|
|
|
|
2016-01-13 04:55:00 +00:00
|
|
|
return this._request(path, "POST", creds, body);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the session or name for an existing device
|
|
|
|
*
|
|
|
|
* @method updateDevice
|
|
|
|
* @param sessionTokenHex
|
|
|
|
* Session token obtained from signIn
|
|
|
|
* @param id
|
|
|
|
* Device identifier
|
|
|
|
* @param name
|
|
|
|
* Device name
|
2016-03-07 05:00:34 +00:00
|
|
|
* @param [options]
|
|
|
|
* Extra device options
|
|
|
|
* @param [options.pushCallback]
|
|
|
|
* `pushCallback` push endpoint callback
|
2016-01-13 04:55:00 +00:00
|
|
|
* @return Promise
|
|
|
|
* Resolves to an object:
|
|
|
|
* {
|
|
|
|
* id: Device identifier
|
|
|
|
* name: Device name
|
|
|
|
* }
|
|
|
|
*/
|
2016-03-07 05:00:34 +00:00
|
|
|
updateDevice(sessionTokenHex, id, name, options = {}) {
|
2016-01-13 04:55:00 +00:00
|
|
|
let path = "/account/device";
|
|
|
|
|
|
|
|
let creds = deriveHawkCredentials(sessionTokenHex, "sessionToken");
|
|
|
|
let body = { id, name };
|
2016-03-07 05:00:34 +00:00
|
|
|
if (options.pushCallback) {
|
|
|
|
body.pushCallback = options.pushCallback;
|
|
|
|
}
|
2016-01-13 04:55:00 +00:00
|
|
|
|
|
|
|
return this._request(path, "POST", creds, body);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a device and its associated session token, signing the user
|
|
|
|
* out of the server.
|
|
|
|
*
|
|
|
|
* @method signOutAndDestroyDevice
|
|
|
|
* @param sessionTokenHex
|
|
|
|
* Session token obtained from signIn
|
|
|
|
* @param id
|
|
|
|
* Device identifier
|
|
|
|
* @param [options]
|
|
|
|
* Options object
|
|
|
|
* @param [options.service]
|
|
|
|
* `service` query parameter
|
|
|
|
* @return Promise
|
|
|
|
* Resolves to an empty object:
|
|
|
|
* {}
|
|
|
|
*/
|
2016-03-07 05:00:34 +00:00
|
|
|
signOutAndDestroyDevice(sessionTokenHex, id, options = {}) {
|
2016-01-13 04:55:00 +00:00
|
|
|
let path = "/account/device/destroy";
|
|
|
|
|
|
|
|
if (options.service) {
|
|
|
|
path += "?service=" + encodeURIComponent(options.service);
|
|
|
|
}
|
|
|
|
|
|
|
|
let creds = deriveHawkCredentials(sessionTokenHex, "sessionToken");
|
|
|
|
let body = { id };
|
|
|
|
|
|
|
|
return this._request(path, "POST", creds, body);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of currently registered devices
|
|
|
|
*
|
|
|
|
* @method getDeviceList
|
|
|
|
* @param sessionTokenHex
|
|
|
|
* Session token obtained from signIn
|
|
|
|
* @return Promise
|
|
|
|
* Resolves to an array of objects:
|
|
|
|
* [
|
|
|
|
* {
|
|
|
|
* id: Device id
|
|
|
|
* isCurrentDevice: Boolean indicating whether the item
|
|
|
|
* represents the current device
|
|
|
|
* name: Device name
|
|
|
|
* type: Device type (mobile|desktop)
|
|
|
|
* },
|
|
|
|
* ...
|
|
|
|
* ]
|
|
|
|
*/
|
|
|
|
getDeviceList(sessionTokenHex) {
|
|
|
|
let path = "/account/devices";
|
|
|
|
let creds = deriveHawkCredentials(sessionTokenHex, "sessionToken");
|
|
|
|
|
|
|
|
return this._request(path, "GET", creds, {});
|
|
|
|
},
|
|
|
|
|
2014-02-26 18:11:49 +00:00
|
|
|
_clearBackoff: function() {
|
|
|
|
this.backoffError = null;
|
|
|
|
},
|
|
|
|
|
2013-12-02 21:56:24 +00:00
|
|
|
/**
|
|
|
|
* A general method for sending raw API calls to the FxA auth server.
|
|
|
|
* All request bodies and responses are JSON.
|
|
|
|
*
|
|
|
|
* @param path
|
|
|
|
* API endpoint path
|
|
|
|
* @param method
|
|
|
|
* The HTTP request method
|
|
|
|
* @param credentials
|
|
|
|
* Hawk credentials
|
|
|
|
* @param jsonPayload
|
|
|
|
* A JSON payload
|
|
|
|
* @return Promise
|
|
|
|
* Returns a promise that resolves to the JSON response of the API call,
|
|
|
|
* or is rejected with an error. Error responses have the following properties:
|
|
|
|
* {
|
|
|
|
* "code": 400, // matches the HTTP status code
|
|
|
|
* "errno": 107, // stable application-level error number
|
|
|
|
* "error": "Bad Request", // string description of the error type
|
|
|
|
* "message": "the value of salt is not allowed to be undefined",
|
|
|
|
* "info": "https://docs.dev.lcip.og/errors/1234" // link to more info on the error
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
_request: function hawkRequest(path, method, credentials, jsonPayload) {
|
|
|
|
let deferred = Promise.defer();
|
2013-12-13 11:37:55 +00:00
|
|
|
|
2014-02-26 18:11:49 +00:00
|
|
|
// We were asked to back off.
|
|
|
|
if (this.backoffError) {
|
|
|
|
log.debug("Received new request during backoff, re-rejecting.");
|
|
|
|
deferred.reject(this.backoffError);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
2014-01-24 02:04:38 +00:00
|
|
|
this.hawk.request(path, method, credentials, jsonPayload).then(
|
2014-06-18 09:42:15 +00:00
|
|
|
(response) => {
|
2014-01-24 02:04:38 +00:00
|
|
|
try {
|
2014-06-18 09:42:15 +00:00
|
|
|
let responseObj = JSON.parse(response.body);
|
|
|
|
deferred.resolve(responseObj);
|
2014-01-24 02:04:38 +00:00
|
|
|
} catch (err) {
|
2014-06-18 09:42:15 +00:00
|
|
|
log.error("json parse error on response: " + response.body);
|
2014-01-24 02:04:38 +00:00
|
|
|
deferred.reject({error: err});
|
2013-12-02 21:56:24 +00:00
|
|
|
}
|
2014-01-24 02:04:38 +00:00
|
|
|
},
|
2013-12-02 21:56:24 +00:00
|
|
|
|
2014-01-24 02:04:38 +00:00
|
|
|
(error) => {
|
2014-02-19 16:34:42 +00:00
|
|
|
log.error("error " + method + "ing " + path + ": " + JSON.stringify(error));
|
2014-02-26 18:11:49 +00:00
|
|
|
if (error.retryAfter) {
|
|
|
|
log.debug("Received backoff response; caching error as flag.");
|
|
|
|
this.backoffError = error;
|
|
|
|
// Schedule clearing of cached-error-as-flag.
|
|
|
|
CommonUtils.namedTimer(
|
|
|
|
this._clearBackoff,
|
|
|
|
error.retryAfter * 1000,
|
|
|
|
this,
|
|
|
|
"fxaBackoffTimer"
|
|
|
|
);
|
2015-01-27 23:11:08 +00:00
|
|
|
}
|
2014-01-24 02:04:38 +00:00
|
|
|
deferred.reject(error);
|
|
|
|
}
|
|
|
|
);
|
2013-12-02 21:56:24 +00:00
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2015-09-16 01:18:04 +00:00
|
|
|
function isInvalidTokenError(error) {
|
|
|
|
if (error.code != 401) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (error.errno) {
|
|
|
|
case ERRNO_INVALID_AUTH_TOKEN:
|
|
|
|
case ERRNO_INVALID_AUTH_TIMESTAMP:
|
|
|
|
case ERRNO_INVALID_AUTH_NONCE:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|