1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/* 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";
var EXPORTED_SYMBOLS = ["FxAccountsConfig"];
const { RESTRequest } = ChromeUtils.import(
"resource://services-common/rest.js"
);
const { log } = ChromeUtils.import(
"resource://gre/modules/FxAccountsCommon.js"
);
const { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
);
const lazy = {};
XPCOMUtils.defineLazyGetter(lazy, "fxAccounts", () => {
return ChromeUtils.import(
"resource://gre/modules/FxAccounts.jsm"
).getFxAccountsSingleton();
});
ChromeUtils.defineModuleGetter(
lazy,
"EnsureFxAccountsWebChannel",
"resource://gre/modules/FxAccountsWebChannel.jsm"
);
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"ROOT_URL",
"identity.fxaccounts.remote.root"
);
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"CONTEXT_PARAM",
"identity.fxaccounts.contextParam"
);
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"REQUIRES_HTTPS",
"identity.fxaccounts.allowHttp",
false,
null,
val => !val
);
const CONFIG_PREFS = [
"identity.fxaccounts.remote.root",
"identity.fxaccounts.auth.uri",
"identity.fxaccounts.remote.oauth.uri",
"identity.fxaccounts.remote.profile.uri",
"identity.fxaccounts.remote.pairing.uri",
"identity.sync.tokenserver.uri",
];
const SYNC_PARAM = "sync";
var FxAccountsConfig = {
async promiseEmailURI(email, entrypoint, extraParams = {}) {
return this._buildURL("", {
extraParams: { entrypoint, email, service: SYNC_PARAM, ...extraParams },
});
},
async promiseConnectAccountURI(entrypoint, extraParams = {}) {
return this._buildURL("", {
extraParams: {
entrypoint,
action: "email",
service: SYNC_PARAM,
...extraParams,
},
});
},
async promiseForceSigninURI(entrypoint, extraParams = {}) {
return this._buildURL("force_auth", {
extraParams: { entrypoint, service: SYNC_PARAM, ...extraParams },
addAccountIdentifiers: true,
});
},
async promiseManageURI(entrypoint, extraParams = {}) {
return this._buildURL("settings", {
extraParams: { entrypoint, ...extraParams },
addAccountIdentifiers: true,
});
},
async promiseChangeAvatarURI(entrypoint, extraParams = {}) {
return this._buildURL("settings/avatar/change", {
extraParams: { entrypoint, ...extraParams },
addAccountIdentifiers: true,
});
},
async promiseManageDevicesURI(entrypoint, extraParams = {}) {
return this._buildURL("settings/clients", {
extraParams: { entrypoint, ...extraParams },
addAccountIdentifiers: true,
});
},
async promiseConnectDeviceURI(entrypoint, extraParams = {}) {
return this._buildURL("connect_another_device", {
extraParams: { entrypoint, service: SYNC_PARAM, ...extraParams },
addAccountIdentifiers: true,
});
},
async promisePairingURI(extraParams = {}) {
return this._buildURL("pair", {
extraParams,
includeDefaultParams: false,
});
},
async promiseOAuthURI(extraParams = {}) {
return this._buildURL("oauth", {
extraParams,
includeDefaultParams: false,
});
},
async promiseMetricsFlowURI(entrypoint, extraParams = {}) {
return this._buildURL("metrics-flow", {
extraParams: { entrypoint, ...extraParams },
includeDefaultParams: false,
});
},
get defaultParams() {
return { context: lazy.CONTEXT_PARAM };
},
/**
* @param path should be parsable by the URL constructor first parameter.
* @param {bool} [options.includeDefaultParams] If true include the default search params.
* @param {Object.<string, string>} [options.extraParams] Additionnal search params.
* @param {bool} [options.addAccountIdentifiers] if true we add the current logged-in user uid and email to the search params.
*/
async _buildURL(
path,
{
includeDefaultParams = true,
extraParams = {},
addAccountIdentifiers = false,
}
) {
await this.ensureConfigured();
const url = new URL(path, lazy.ROOT_URL);
if (lazy.REQUIRES_HTTPS && url.protocol != "https:") {
throw new Error("Firefox Accounts server must use HTTPS");
}
const params = {
...(includeDefaultParams ? this.defaultParams : null),
...extraParams,
};
for (let [k, v] of Object.entries(params)) {
url.searchParams.append(k, v);
}
if (addAccountIdentifiers) {
const accountData = await this.getSignedInUser();
if (!accountData) {
return null;
}
url.searchParams.append("uid", accountData.uid);
url.searchParams.append("email", accountData.email);
}
return url.href;
},
async _buildURLFromString(href, extraParams = {}) {
const url = new URL(href);
for (let [k, v] of Object.entries(extraParams)) {
url.searchParams.append(k, v);
}
return url.href;
},
resetConfigURLs() {
let autoconfigURL = this.getAutoConfigURL();
if (!autoconfigURL) {
return;
}
// They have the autoconfig uri pref set, so we clear all the prefs that we
// will have initialized, which will leave them pointing at production.
for (let pref of CONFIG_PREFS) {
Services.prefs.clearUserPref(pref);
}
// Reset the webchannel.
lazy.EnsureFxAccountsWebChannel();
},
getAutoConfigURL() {
let pref = Services.prefs.getCharPref(
"identity.fxaccounts.autoconfig.uri",
""
);
if (!pref) {
// no pref / empty pref means we don't bother here.
return "";
}
let rootURL = Services.urlFormatter.formatURL(pref);
if (rootURL.endsWith("/")) {
rootURL = rootURL.slice(0, -1);
}
return rootURL;
},
async ensureConfigured() {
let isSignedIn = !!(await this.getSignedInUser());
if (!isSignedIn) {
await this.updateConfigURLs();
}
},
// Returns true if this user is using the FxA "production" systems, false
// if using any other configuration, including self-hosting or the FxA
// non-production systems such as "dev" or "staging".
// It's typically used as a proxy for "is this likely to be a self-hosted
// user?", but it's named this way to make the implementation less
// surprising. As a result, it's fairly conservative and would prefer to have
// a false-negative than a false-position as it determines things which users
// might consider sensitive (notably, telemetry).
// Note also that while it's possible to self-host just sync and not FxA, we
// don't make that distinction - that's a self-hoster from the POV of this
// function.
isProductionConfig() {
// Specifically, if the autoconfig URLs, or *any* of the URLs that
// we consider configurable are modified, we assume self-hosted.
if (this.getAutoConfigURL()) {
return false;
}
for (let pref of CONFIG_PREFS) {
if (Services.prefs.prefHasUserValue(pref)) {
return false;
}
}
return true;
},
// Read expected client configuration from the fxa auth server
// (from `identity.fxaccounts.autoconfig.uri`/.well-known/fxa-client-configuration)
// and replace all the relevant our prefs with the information found there.
// This is only done before sign-in and sign-up, and even then only if the
// `identity.fxaccounts.autoconfig.uri` preference is set.
async updateConfigURLs() {
let rootURL = this.getAutoConfigURL();
if (!rootURL) {
return;
}
const config = await this.fetchConfigDocument(rootURL);
try {
// Update the prefs directly specified by the config.
let authServerBase = config.auth_server_base_url;
if (!authServerBase.endsWith("/v1")) {
authServerBase += "/v1";
}
Services.prefs.setCharPref(
"identity.fxaccounts.auth.uri",
authServerBase
);
Services.prefs.setCharPref(
"identity.fxaccounts.remote.oauth.uri",
config.oauth_server_base_url + "/v1"
);
// At the time of landing this, our servers didn't yet answer with pairing_server_base_uri.
// Remove this condition check once Firefox 68 is stable.
if (config.pairing_server_base_uri) {
Services.prefs.setCharPref(
"identity.fxaccounts.remote.pairing.uri",
config.pairing_server_base_uri
);
}
Services.prefs.setCharPref(
"identity.fxaccounts.remote.profile.uri",
config.profile_server_base_url + "/v1"
);
Services.prefs.setCharPref(
"identity.sync.tokenserver.uri",
config.sync_tokenserver_base_url + "/1.0/sync/1.5"
);
Services.prefs.setCharPref("identity.fxaccounts.remote.root", rootURL);
// Ensure the webchannel is pointed at the correct uri
lazy.EnsureFxAccountsWebChannel();
} catch (e) {
log.error(
"Failed to initialize configuration preferences from autoconfig object",
e
);
throw e;
}
},
// Read expected client configuration from the fxa auth server
// (or from the provided rootURL, if present) and return it as an object.
async fetchConfigDocument(rootURL = null) {
if (!rootURL) {
rootURL = lazy.ROOT_URL;
}
let configURL = rootURL + "/.well-known/fxa-client-configuration";
let request = new RESTRequest(configURL);
request.setHeader("Accept", "application/json");
// Catch and rethrow the error inline.
let resp = await request.get().catch(e => {
log.error(`Failed to get configuration object from "${configURL}"`, e);
throw e;
});
if (!resp.success) {
// Note: 'resp.body' is included with the error log below as we are not concerned
// that the body will contain PII, but if that changes it should be excluded.
log.error(
`Received HTTP response code ${resp.status} from configuration object request:
${resp.body}`
);
throw new Error(
`HTTP status ${resp.status} from configuration object request`
);
}
log.debug("Got successful configuration response", resp.body);
try {
return JSON.parse(resp.body);
} catch (e) {
log.error(
`Failed to parse configuration preferences from ${configURL}`,
e
);
throw e;
}
},
// For test purposes, returns a Promise.
getSignedInUser() {
return lazy.fxAccounts.getSignedInUser();
},
};
|