2014-01-24 06:04:38 +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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* HAWK is an HTTP authentication scheme using a message authentication code
|
|
|
|
* (MAC) algorithm to provide partial HTTP request cryptographic verification.
|
|
|
|
*
|
|
|
|
* For details, see: https://github.com/hueniverse/hawk
|
|
|
|
*
|
|
|
|
* With HAWK, it is essential that the clocks on clients and server not have an
|
|
|
|
* absolute delta of greater than one minute, as the HAWK protocol uses
|
|
|
|
* timestamps to reduce the possibility of replay attacks. However, it is
|
|
|
|
* likely that some clients' clocks will be more than a little off, especially
|
|
|
|
* in mobile devices, which would break HAWK-based services (like sync and
|
|
|
|
* firefox accounts) for those clients.
|
|
|
|
*
|
|
|
|
* This library provides a stateful HAWK client that calculates (roughly) the
|
|
|
|
* clock delta on the client vs the server. The library provides an interface
|
|
|
|
* for deriving HAWK credentials and making HAWK-authenticated REST requests to
|
|
|
|
* a single remote server. Therefore, callers who want to interact with
|
|
|
|
* multiple HAWK services should instantiate one HawkClient per service.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["HawkClient"];
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { HAWKAuthenticatedRESTRequest } = ChromeUtils.import(
|
|
|
|
"resource://services-common/hawkrequest.js"
|
|
|
|
);
|
|
|
|
const { Observers } = ChromeUtils.import(
|
|
|
|
"resource://services-common/observers.js"
|
2019-07-05 11:58:22 +03:00
|
|
|
);
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Log } = ChromeUtils.import("resource://gre/modules/Log.jsm");
|
2022-07-12 07:21:34 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.importESModule(
|
|
|
|
"resource://gre/modules/XPCOMUtils.sys.mjs"
|
2019-01-17 21:18:31 +03:00
|
|
|
);
|
2014-06-07 21:30:18 +04:00
|
|
|
|
2015-01-28 02:11:08 +03:00
|
|
|
// log.appender.dump should be one of "Fatal", "Error", "Warn", "Info", "Config",
|
2014-06-07 21:30:18 +04:00
|
|
|
// "Debug", "Trace" or "All". If none is specified, "Error" will be used by
|
|
|
|
// default.
|
2015-01-28 02:11:08 +03:00
|
|
|
// Note however that Sync will also add this log to *its* DumpAppender, so
|
|
|
|
// in a Sync context it shouldn't be necessary to adjust this - however, that
|
|
|
|
// also means error logs are likely to be dump'd twice but that's OK.
|
|
|
|
const PREF_LOG_LEVEL = "services.common.hawk.log.appender.dump";
|
2014-06-07 21:30:18 +04:00
|
|
|
|
|
|
|
// A pref that can be set so "sensitive" information (eg, personally
|
|
|
|
// identifiable info, credentials, etc) will be logged.
|
2015-01-28 02:11:08 +03:00
|
|
|
const PREF_LOG_SENSITIVE_DETAILS = "services.common.hawk.log.sensitive";
|
2014-06-07 21:30:18 +04:00
|
|
|
|
2022-06-02 15:27:42 +03:00
|
|
|
const lazy = {};
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(lazy, "log", function() {
|
2014-06-07 21:30:18 +04:00
|
|
|
let log = Log.repository.getLogger("Hawk");
|
2015-01-28 02:11:08 +03:00
|
|
|
// We set the log itself to "debug" and set the level from the preference to
|
|
|
|
// the appender. This allows other things to send the logs to different
|
|
|
|
// appenders, while still allowing the pref to control what is seen via dump()
|
|
|
|
log.level = Log.Level.Debug;
|
|
|
|
let appender = new Log.DumpAppender();
|
|
|
|
log.addAppender(appender);
|
|
|
|
appender.level = Log.Level.Error;
|
2014-06-07 21:30:18 +04:00
|
|
|
try {
|
|
|
|
let level =
|
|
|
|
Services.prefs.getPrefType(PREF_LOG_LEVEL) ==
|
|
|
|
Ci.nsIPrefBranch.PREF_STRING &&
|
|
|
|
Services.prefs.getCharPref(PREF_LOG_LEVEL);
|
2015-01-28 02:11:08 +03:00
|
|
|
appender.level = Log.Level[level] || Log.Level.Error;
|
2014-06-07 21:30:18 +04:00
|
|
|
} catch (e) {
|
|
|
|
log.error(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return log;
|
|
|
|
});
|
|
|
|
|
|
|
|
// A boolean to indicate if personally identifiable information (or anything
|
|
|
|
// else sensitive, such as credentials) should be logged.
|
2022-06-02 15:27:42 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(lazy, "logPII", function() {
|
2014-06-07 21:30:18 +04:00
|
|
|
try {
|
|
|
|
return Services.prefs.getBoolPref(PREF_LOG_SENSITIVE_DETAILS);
|
|
|
|
} catch (_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2014-01-24 06:04:38 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A general purpose client for making HAWK authenticated requests to a single
|
|
|
|
* host. Keeps track of the clock offset between the client and the host for
|
|
|
|
* computation of the timestamp in the HAWK Authorization header.
|
|
|
|
*
|
|
|
|
* Clients should create one HawkClient object per each server they wish to
|
|
|
|
* interact with.
|
|
|
|
*
|
|
|
|
* @param host
|
|
|
|
* The url of the host
|
|
|
|
*/
|
2014-02-04 23:32:16 +04:00
|
|
|
var HawkClient = function(host) {
|
2014-01-24 06:04:38 +04:00
|
|
|
this.host = host;
|
|
|
|
|
|
|
|
// Clock offset in milliseconds between our client's clock and the date
|
|
|
|
// reported in responses from our host.
|
|
|
|
this._localtimeOffsetMsec = 0;
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2014-01-24 06:04:38 +04:00
|
|
|
|
2022-05-13 23:12:27 +03:00
|
|
|
HawkClient.prototype = {
|
2014-01-24 06:04:38 +04:00
|
|
|
/*
|
|
|
|
* Construct an error message for a response. Private.
|
|
|
|
*
|
|
|
|
* @param restResponse
|
|
|
|
* A RESTResponse object from a RESTRequest
|
|
|
|
*
|
2015-01-28 02:11:08 +03:00
|
|
|
* @param error
|
|
|
|
* A string or object describing the error
|
2014-01-24 06:04:38 +04:00
|
|
|
*/
|
2017-01-10 20:09:02 +03:00
|
|
|
_constructError(restResponse, error) {
|
2014-02-26 22:11:49 +04:00
|
|
|
let errorObj = {
|
2017-01-10 20:09:02 +03:00
|
|
|
error,
|
2015-01-28 02:11:08 +03:00
|
|
|
// This object is likely to be JSON.stringify'd, but neither Error()
|
|
|
|
// objects nor Components.Exception objects do the right thing there,
|
|
|
|
// so we add a new element which is simply the .toString() version of
|
|
|
|
// the error object, so it does appear in JSON'd values.
|
|
|
|
errorString: error.toString(),
|
2014-01-24 06:04:38 +04:00
|
|
|
message: restResponse.statusText,
|
|
|
|
code: restResponse.status,
|
2015-01-25 10:50:01 +03:00
|
|
|
errno: restResponse.status,
|
|
|
|
toString() {
|
|
|
|
return this.code + ": " + this.message;
|
|
|
|
},
|
2014-01-24 06:04:38 +04:00
|
|
|
};
|
2014-02-28 06:24:49 +04:00
|
|
|
let retryAfter =
|
|
|
|
restResponse.headers && restResponse.headers["retry-after"];
|
2014-02-26 22:11:49 +04:00
|
|
|
retryAfter = retryAfter ? parseInt(retryAfter) : retryAfter;
|
|
|
|
if (retryAfter) {
|
|
|
|
errorObj.retryAfter = retryAfter;
|
2014-04-10 06:08:19 +04:00
|
|
|
// and notify observers of the retry interval
|
|
|
|
if (this.observerPrefix) {
|
|
|
|
Observers.notify(this.observerPrefix + ":backoff:interval", retryAfter);
|
|
|
|
}
|
2014-02-26 22:11:49 +04:00
|
|
|
}
|
|
|
|
return errorObj;
|
2014-01-24 06:04:38 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Update clock offset by determining difference from date gives in the (RFC
|
|
|
|
* 1123) Date header of a server response. Because HAWK tolerates a window
|
|
|
|
* of one minute of clock skew (so two minutes total since the skew can be
|
|
|
|
* positive or negative), the simple method of calculating offset here is
|
|
|
|
* probably good enough. We keep the value in milliseconds to make life
|
|
|
|
* easier, even though the value will not have millisecond accuracy.
|
|
|
|
*
|
|
|
|
* @param dateString
|
|
|
|
* An RFC 1123 date string (e.g., "Mon, 13 Jan 2014 21:45:06 GMT")
|
|
|
|
*
|
|
|
|
* For HAWK clock skew and replay protection, see
|
|
|
|
* https://github.com/hueniverse/hawk#replay-protection
|
|
|
|
*/
|
2017-01-10 20:09:02 +03:00
|
|
|
_updateClockOffset(dateString) {
|
2014-01-24 06:04:38 +04:00
|
|
|
try {
|
|
|
|
let serverDateMsec = Date.parse(dateString);
|
|
|
|
this._localtimeOffsetMsec = serverDateMsec - this.now();
|
2022-06-02 15:27:42 +03:00
|
|
|
lazy.log.debug(
|
2014-01-24 06:04:38 +04:00
|
|
|
"Clock offset vs " + this.host + ": " + this._localtimeOffsetMsec
|
|
|
|
);
|
2017-01-10 20:09:02 +03:00
|
|
|
} catch (err) {
|
2022-06-02 15:27:42 +03:00
|
|
|
lazy.log.warn("Bad date header in server response: " + dateString);
|
2014-01-24 06:04:38 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the current clock offset in milliseconds.
|
|
|
|
*
|
|
|
|
* 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._localtimeOffsetMsec;
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* return current time in milliseconds
|
|
|
|
*/
|
2017-01-10 20:09:02 +03:00
|
|
|
now() {
|
2014-01-24 06:04:38 +04:00
|
|
|
return Date.now();
|
|
|
|
},
|
|
|
|
|
|
|
|
/* A general method for sending raw RESTRequest calls authorized using HAWK
|
|
|
|
*
|
|
|
|
* @param path
|
|
|
|
* API endpoint path
|
|
|
|
* @param method
|
|
|
|
* The HTTP request method
|
|
|
|
* @param credentials
|
|
|
|
* Hawk credentials
|
|
|
|
* @param payloadObj
|
|
|
|
* An object that can be encodable as JSON as the payload of the
|
|
|
|
* request
|
2016-02-10 14:21:26 +03:00
|
|
|
* @param extraHeaders
|
|
|
|
* An object with header/value pairs to send with the request.
|
2014-01-24 06:04:38 +04:00
|
|
|
* @return Promise
|
2014-06-18 13:42:15 +04:00
|
|
|
* Returns a promise that resolves to the response of the API call,
|
2014-01-24 06:04:38 +04:00
|
|
|
* or is rejected with an error. If the server response can be parsed
|
|
|
|
* as JSON and contains an 'error' property, the promise will be
|
|
|
|
* rejected with this JSON-parsed response.
|
|
|
|
*/
|
2018-03-15 06:34:50 +03:00
|
|
|
async request(
|
|
|
|
path,
|
|
|
|
method,
|
|
|
|
credentials = null,
|
|
|
|
payloadObj = {},
|
|
|
|
extraHeaders = {},
|
|
|
|
retryOK = true
|
|
|
|
) {
|
2014-01-24 06:04:38 +04:00
|
|
|
method = method.toLowerCase();
|
|
|
|
|
|
|
|
let uri = this.host + path;
|
|
|
|
|
2018-03-15 06:34:50 +03:00
|
|
|
let extra = {
|
|
|
|
now: this.now(),
|
|
|
|
localtimeOffsetMsec: this.localtimeOffsetMsec,
|
|
|
|
headers: extraHeaders,
|
|
|
|
};
|
2014-01-24 06:04:38 +04:00
|
|
|
|
2018-03-15 06:34:50 +03:00
|
|
|
let request = this.newHAWKAuthenticatedRESTRequest(uri, credentials, extra);
|
|
|
|
let error;
|
|
|
|
let restResponse = await request[method](payloadObj).catch(e => {
|
|
|
|
// Keep a reference to the error, log a message about it, and return the
|
|
|
|
// response anyway.
|
|
|
|
error = e;
|
2022-06-02 15:27:42 +03:00
|
|
|
lazy.log.warn("hawk request error", error);
|
2018-03-15 06:34:50 +03:00
|
|
|
return request.response;
|
|
|
|
});
|
|
|
|
|
|
|
|
// This shouldn't happen anymore, but it's not exactly difficult to handle.
|
|
|
|
if (!restResponse) {
|
|
|
|
throw error;
|
|
|
|
}
|
2014-04-10 06:08:19 +04:00
|
|
|
|
2018-03-15 06:34:50 +03:00
|
|
|
let status = restResponse.status;
|
2014-01-24 06:04:38 +04:00
|
|
|
|
2022-06-02 15:27:42 +03:00
|
|
|
lazy.log.debug(
|
2018-03-15 06:34:50 +03:00
|
|
|
"(Response) " +
|
|
|
|
path +
|
|
|
|
": code: " +
|
|
|
|
status +
|
|
|
|
" - Status text: " +
|
|
|
|
restResponse.statusText
|
|
|
|
);
|
2022-06-02 15:27:42 +03:00
|
|
|
if (lazy.logPII) {
|
|
|
|
lazy.log.debug("Response text", restResponse.body);
|
2018-03-15 06:34:50 +03:00
|
|
|
}
|
2014-01-24 06:04:38 +04:00
|
|
|
|
2018-03-15 06:34:50 +03:00
|
|
|
// All responses may have backoff headers, which are a server-side safety
|
|
|
|
// valve to allow slowing down clients without hurting performance.
|
|
|
|
this._maybeNotifyBackoff(restResponse, "x-weave-backoff");
|
|
|
|
this._maybeNotifyBackoff(restResponse, "x-backoff");
|
2014-01-24 06:04:38 +04:00
|
|
|
|
2018-03-15 06:34:50 +03:00
|
|
|
if (error) {
|
|
|
|
// When things really blow up, reconstruct an error object that follows
|
|
|
|
// the general format of the server on error responses.
|
|
|
|
throw this._constructError(restResponse, error);
|
2017-01-10 20:09:02 +03:00
|
|
|
}
|
2014-01-24 06:04:38 +04:00
|
|
|
|
2018-03-15 06:34:50 +03:00
|
|
|
this._updateClockOffset(restResponse.headers.date);
|
|
|
|
|
|
|
|
if (status === 401 && retryOK && !("retry-after" in restResponse.headers)) {
|
|
|
|
// Retry once if we were rejected due to a bad timestamp.
|
|
|
|
// Clock offset is adjusted already in the top of this function.
|
2022-06-02 15:27:42 +03:00
|
|
|
lazy.log.debug("Received 401 for " + path + ": retrying");
|
2018-03-15 06:34:50 +03:00
|
|
|
return this.request(
|
|
|
|
path,
|
|
|
|
method,
|
|
|
|
credentials,
|
|
|
|
payloadObj,
|
|
|
|
extraHeaders,
|
|
|
|
false
|
|
|
|
);
|
2014-06-14 05:47:34 +04:00
|
|
|
}
|
|
|
|
|
2018-03-15 06:34:50 +03:00
|
|
|
// If the server returned a json error message, use it in the rejection
|
|
|
|
// of the promise.
|
|
|
|
//
|
|
|
|
// In the case of a 401, in which we are probably being rejected for a
|
|
|
|
// bad timestamp, retry exactly once, during which time clock offset will
|
|
|
|
// be adjusted.
|
2014-01-24 06:04:38 +04:00
|
|
|
|
2018-03-15 06:34:50 +03:00
|
|
|
let jsonResponse = {};
|
2015-01-28 02:11:08 +03:00
|
|
|
try {
|
2018-03-15 06:34:50 +03:00
|
|
|
jsonResponse = JSON.parse(restResponse.body);
|
|
|
|
} catch (notJSON) {}
|
|
|
|
|
|
|
|
let okResponse = 200 <= status && status < 300;
|
|
|
|
if (!okResponse || jsonResponse.error) {
|
|
|
|
if (jsonResponse.error) {
|
|
|
|
throw jsonResponse;
|
2015-01-28 02:11:08 +03:00
|
|
|
}
|
2018-03-15 06:34:50 +03:00
|
|
|
throw this._constructError(restResponse, "Request failed");
|
2014-02-01 04:43:36 +04:00
|
|
|
}
|
2014-01-24 06:04:38 +04:00
|
|
|
|
2018-03-15 06:34:50 +03:00
|
|
|
// It's up to the caller to know how to decode the response.
|
|
|
|
// We just return the whole response.
|
|
|
|
return restResponse;
|
2014-03-07 08:41:32 +04:00
|
|
|
},
|
|
|
|
|
2014-04-10 06:08:19 +04:00
|
|
|
/*
|
|
|
|
* The prefix used for all notifications sent by this module. This
|
|
|
|
* allows the handler of notifications to be sure they are handling
|
|
|
|
* notifications for the service they expect.
|
|
|
|
*
|
|
|
|
* If not set, no notifications will be sent.
|
|
|
|
*/
|
|
|
|
observerPrefix: null,
|
|
|
|
|
|
|
|
// Given an optional header value, notify that a backoff has been requested.
|
2017-01-10 20:09:02 +03:00
|
|
|
_maybeNotifyBackoff(response, headerName) {
|
2014-06-07 21:30:18 +04:00
|
|
|
if (!this.observerPrefix || !response.headers) {
|
2014-04-10 06:08:19 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let headerVal = response.headers[headerName];
|
|
|
|
if (!headerVal) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let backoffInterval;
|
|
|
|
try {
|
|
|
|
backoffInterval = parseInt(headerVal, 10);
|
|
|
|
} catch (ex) {
|
2022-06-02 15:27:42 +03:00
|
|
|
lazy.log.error(
|
2014-06-07 21:30:18 +04:00
|
|
|
"hawkclient response had invalid backoff value in '" +
|
|
|
|
headerName +
|
|
|
|
"' header: " +
|
|
|
|
headerVal
|
|
|
|
);
|
2014-04-10 06:08:19 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Observers.notify(
|
|
|
|
this.observerPrefix + ":backoff:interval",
|
|
|
|
backoffInterval
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2014-03-07 08:41:32 +04:00
|
|
|
// override points for testing.
|
2017-01-10 20:09:02 +03:00
|
|
|
newHAWKAuthenticatedRESTRequest(uri, credentials, extra) {
|
2014-03-07 08:41:32 +04:00
|
|
|
return new HAWKAuthenticatedRESTRequest(uri, credentials, extra);
|
|
|
|
},
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|