summaryrefslogtreecommitdiffstats
path: root/services/fxaccounts/RustFxAccount.js
blob: e6128e9283bd2dbac7565cff909c605757395c0e (plain)
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* 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/. */

const EXPORTED_SYMBOLS = ["RustFxAccount"];

/**
 * This class is a low-level JS wrapper around the `mozIFirefoxAccountsBridge`
 * interface.
 * A `RustFxAccount` instance can be associated to 0 or 1 Firefox Account depending
 * on its login state.
 * This class responsibilities are to:
 * - Expose an async JS interface to the methods in `mozIFirefoxAccountsBridge` by
 *   converting the callbacks-driven routines into proper JS promises.
 * - Serialize and deserialize the input and outputs of `mozIFirefoxAccountsBridge`.
 *   Complex objects are generally returned through JSON strings.
 */
class RustFxAccount {
  /**
   * Create a new `RustFxAccount` instance, depending on the argument passed it could be:
   * - From scratch (object passed).
   * - Restore a previously serialized account (string passed).
   * @param {(Object)|string} options Object type creates a new instance, string type restores an instance from a serialized state obtained with `stateJSON`.
   * @param {string} options.fxaServer Content URL of the remote Firefox Accounts server.
   * @param {string} options.clientId OAuth client_id of the application.
   * @param {string} options.redirectUri Redirection URL to be navigated to at the end of the OAuth login flow.
   * @param {string} [options.tokenServerUrlOverride] Override the token server URL: used by self-hosters of Sync.
   */
  constructor(options) {
    // This initializes the network stack for all the Rust components.
    let viaduct = Cc["@mozilla.org/toolkit/viaduct;1"].createInstance(
      Ci.mozIViaduct
    );
    viaduct.EnsureInitialized();

    this.bridge = Cc[
      "@mozilla.org/services/firefox-accounts-bridge;1"
    ].createInstance(Ci.mozIFirefoxAccountsBridge);

    if (typeof options == "string") {
      // Restore from JSON case.
      this.bridge.initFromJSON(options);
    } else {
      // New instance case.
      let props = Cc["@mozilla.org/hash-property-bag;1"].createInstance(
        Ci.nsIWritablePropertyBag
      );
      props.setProperty("content_url", options.fxaServer);
      props.setProperty("client_id", options.clientId);
      props.setProperty("redirect_uri", options.redirectUri);
      props.setProperty(
        "token_server_url_override",
        options.tokenServerUrlOverride || ""
      );
      this.bridge.init(props);
    }
  }
  /**
   * Serialize the state of a `RustFxAccount` instance. It can be restored
   * later by passing the resulting String back to the `RustFxAccount` constructor.
   * It is the responsability of the caller to
   * persist that serialized state regularly (after operations that mutate
   * `RustFxAccount`) in a **secure** location.
   * @returns {Promise<string>} The JSON representation of the state.
   */
  async stateJSON() {
    return promisify(this.bridge.stateJSON);
  }
  /**
   * Request a OAuth token by starting a new OAuth flow.
   *
   * Once the user has confirmed the authorization grant, they will get redirected to `redirect_url`:
   * the caller must intercept that redirection; extract the `code` and `state` query parameters and call
   * `completeOAuthFlow(...)` to complete the flow.
   *
   * @param {[string]} scopes
   * @param {string} entryPoint - a string for metrics.
   * @returns {Promise<string>}  a URL string that the caller should navigate to.
   */
  async beginOAuthFlow(scopes, entryPoint = "desktop") {
    return promisify(this.bridge.beginOAuthFlow, scopes, entryPoint);
  }
  /**
   * Complete an OAuth flow initiated by `beginOAuthFlow(...)`.
   *
   * @param {string} code
   * @param {string} state
   * @throws if there was an error during the login flow.
   */
  async completeOAuthFlow(code, state) {
    return promisify(this.bridge.completeOAuthFlow, code, state);
  }
  /**
   * Try to get an OAuth access token.
   *
   * @typedef {Object} AccessTokenInfo
   * @property {string} scope
   * @property {string} token
   * @property {ScopedKey} [key]
   * @property {Date} expires_at
   *
   * @typedef {Object} ScopedKey
   * @property {string} kty
   * @property {string} scope
   * @property {string} k
   * @property {string} kid
   *
   * @param {string} scope Single OAuth scope
   * @param {Number} [ttl] Time in seconds for which the token will be used.
   * @returns {Promise<AccessTokenInfo>}
   * @throws if we couldn't provide an access token
   * for this scope. The caller should then start the OAuth Flow again with
   * the desired scope.
   */
  async getAccessToken(scope, ttl) {
    return JSON.parse(await promisify(this.bridge.getAccessToken, scope, ttl));
  }
  /**
   * Get the session token if held.
   *
   * @returns {Promise<string>}
   * @throws if a session token is not being held.
   */
  async getSessionToken() {
    return promisify(this.bridge.getSessionToken);
  }
  /**
   * Returns the list of OAuth attached clients.
   *
   * @typedef {Object} AttachedClient
   * @property {string} [clientId]
   * @property {string} [sessionTokenId]
   * @property {string} [refreshTokenId]
   * @property {string} [deviceId]
   * @property {DeviceType} [deviceType]
   * @property {boolean} isCurrentSession
   * @property {string} [name]
   * @property {Number} [createdTime]
   * @property {Number} [lastAccessTime]
   * @property {string[]} [scope]
   * @property {string} userAgent
   * @property {string} [os]
   *
   * @returns {Promise<[AttachedClient]>}
   * @throws if a session token is not being held.
   */
  async getAttachedClients() {
    return JSON.parse(await promisify(this.bridge.getAttachedClients));
  }
  /**
   * Check whether the currently held refresh token is active.
   *
   * @typedef {Object} IntrospectInfo
   * @property {boolean} active
   *
   * @returns {Promise<IntrospectInfo>}
   */
  async checkAuthorizationStatus() {
    return JSON.parse(await promisify(this.bridge.checkAuthorizationStatus));
  }
  /*
   * This method should be called when a request made with
   * an OAuth token failed with an authentication error.
   * It clears the internal cache of OAuth access tokens,
   * so the caller can try to call `getAccessToken` or `getProfile`
   * again.
   */
  async clearAccessTokenCache() {
    return promisify(this.bridge.clearAccessTokenCache);
  }
  /*
   * Disconnect from the account and optionaly destroy our device record.
   * `beginOAuthFlow(...)` will need to be called to reconnect.
   */
  async disconnect() {
    return promisify(this.bridge.disconnect);
  }
  /**
   * Gets the logged-in user profile.
   *
   * @typedef {Object} Profile
   * @property {string} uid
   * @property {string} email
   * @property {string} avatar
   * @property {boolean} avatarDefault
   * @property {string} [displayName]
   *
   * @param {boolean} [ignoreCache=false] Ignore the profile freshness threshold.
   * @returns {Promise<Profile>}
   * @throws if no suitable access token was found to make this call.
   * The caller should then start the OAuth login flow again with
   * at least the `profile` scope.
   */
  async getProfile(ignoreCache) {
    return JSON.parse(await promisify(this.bridge.getProfile, ignoreCache));
  }
  /**
   * Start a migration process from a session-token-based authenticated account.
   *
   * @typedef {Object} MigrationResult
   * @property {Number} total_duration
   *
   * @param {string} sessionToken
   * @param {string} kSync
   * @param {string} kXCS
   * @param {Boolean} copySessionToken
   * @returns {Promise<MigrationResult>}
   */
  async migrateFromSessionToken(
    sessionToken,
    kSync,
    kXCS,
    copySessionToken = false
  ) {
    return JSON.parse(
      await promisify(
        this.bridge.migrateFromSessionToken,
        sessionToken,
        kSync,
        kXCS,
        copySessionToken
      )
    );
  }
  /**
   * Retry a migration that failed earlier because of transient reasons.
   *
   * @returns {Promise<MigrationResult>}
   */
  async retryMigrateFromSessionToken() {
    return JSON.parse(
      await promisify(this.bridge.retryMigrateFromSessionToken)
    );
  }
  /**
   * Call this function after migrateFromSessionToken is un-successful
   * (or after app startup) to figure out if we can call `retryMigrateFromSessionToken`.
   *
   * @returns {Promise<boolean>} true if a migration flow can be resumed.
   */
  async isInMigrationState() {
    return promisify(this.bridge.isInMigrationState);
  }
  /**
   * Called after a password change was done through webchannel.
   *
   * @param {string} sessionToken
   */
  async handleSessionTokenChange(sessionToken) {
    return promisify(this.bridge.handleSessionTokenChange, sessionToken);
  }
  /**
   * Get the token server URL with `1.0/sync/1.5` appended at the end.
   *
   * @returns {Promise<string>}
   */
  async getTokenServerEndpointURL() {
    let url = await promisify(this.bridge.getTokenServerEndpointURL);
    return `${url}${url.endsWith("/") ? "" : "/"}1.0/sync/1.5`;
  }
  /**
   * @returns {Promise<string>}
   */
  async getConnectionSuccessURL() {
    return promisify(this.bridge.getConnectionSuccessURL);
  }
  /**
   * @param {string} entrypoint
   * @returns {Promise<string>}
   */
  async getManageAccountURL(entrypoint) {
    return promisify(this.bridge.getManageAccountURL, entrypoint);
  }
  /**
   * @param {string} entrypoint
   * @returns {Promise<string>}
   */
  async getManageDevicesURL(entrypoint) {
    return promisify(this.bridge.getManageDevicesURL, entrypoint);
  }
  /**
   * Fetch the devices in the account.
   * @typedef {Object} Device
   * @property {string} id
   * @property {string} name
   * @property {DeviceType} type
   * @property {boolean} isCurrentDevice
   * @property {Number} [lastAccessTime]
   * @property {String} [pushAuthKey]
   * @property {String} [pushCallback]
   * @property {String} [pushPublicKey]
   * @property {boolean} pushEndpointExpired
   * @property {Object} availableCommands
   * @property {Object} location
   *
   * @typedef {Object} DevicePushSubscription
   * @property {string} endpoint
   * @property {string} publicKey
   * @property {string} authKey
   *
   * @param {boolean} [ignoreCache=false] Ignore the devices freshness threshold.
   *
   * @returns {Promise<[Device]>}
   */
  async fetchDevices(ignoreCache) {
    return JSON.parse(await promisify(this.bridge.fetchDevices, ignoreCache));
  }
  /**
   * Rename the local device
   *
   * @param {string} name
   */
  async setDeviceDisplayName(name) {
    return promisify(this.bridge.setDeviceDisplayName, name);
  }
  /**
   * Handle an incoming Push message payload.
   *
   * @typedef {Object} DeviceConnectedEvent
   * @property {string} deviceName
   *
   * @typedef {Object} DeviceDisconnectedEvent
   * @property {string} deviceId
   * @property {boolean} isLocalDevice
   *
   * @param {string} payload
   * @return {Promise<[TabReceivedCommand|DeviceConnectedEvent|DeviceDisconnectedEvent]>}
   */
  async handlePushMessage(payload) {
    return JSON.parse(await promisify(this.bridge.handlePushMessage, payload));
  }
  /**
   * Fetch for device commands we didn't receive through Push.
   *
   * @typedef {Object} TabReceivedCommand
   * @property {Device} [from]
   * @property {TabData} tabData
   *
   * @typedef {Object} TabData
   * @property {string} title
   * @property {string} url
   *
   * @returns {Promise<[TabReceivedCommand]>}
   */
  async pollDeviceCommands() {
    return JSON.parse(await promisify(this.bridge.pollDeviceCommands));
  }
  /**
   * Send a tab to a device identified by its ID.
   *
   * @param {string} targetId
   * @param {string} title
   * @param {string} url
   */
  async sendSingleTab(targetId, title, url) {
    return promisify(this.bridge.sendSingleTab, targetId, title, url);
  }
  /**
   * Update our FxA push subscription.
   *
   * @param {string} endpoint
   * @param {string} publicKey
   * @param {string} authKey
   */
  async setDevicePushSubscription(endpoint, publicKey, authKey) {
    return promisify(
      this.bridge.setDevicePushSubscription,
      endpoint,
      publicKey,
      authKey
    );
  }
  /**
   * Initialize the local device (should be done only once after log-in).
   *
   * @param {string} name
   * @param {DeviceType} deviceType
   * @param {[DeviceCapability]} supportedCapabilities
   */
  async initializeDevice(name, deviceType, supportedCapabilities) {
    return promisify(
      this.bridge.initializeDevice,
      name,
      deviceType,
      supportedCapabilities
    );
  }
  /**
   * Update the device capabilities if needed.
   *
   * @param {[DeviceCapability]} supportedCapabilities
   */
  async ensureCapabilities(supportedCapabilities) {
    return promisify(this.bridge.ensureCapabilities, supportedCapabilities);
  }
}

function promisify(func, ...params) {
  return new Promise((resolve, reject) => {
    func(...params, {
      // This object implicitly implements
      // `mozIFirefoxAccountsBridgeCallback`.
      handleSuccess: resolve,
      handleError(code, message) {
        let error = new Error(message);
        error.result = code;
        reject(error);
      },
    });
  });
}

/**
 * @enum
 */
const DeviceType = Object.freeze({
  desktop: "desktop",
  mobile: "mobile",
  tablet: "tablet",
  tv: "tv",
  vr: "vr",
});

/**
 * @enum
 */
const DeviceCapability = Object.freeze({
  sendTab: "sendTab",
  fromCommandName(str) {
    switch (str) {
      case "https://identity.mozilla.com/cmd/open-uri":
        return DeviceCapability.sendTab;
    }
    throw new Error("Unknown device capability.");
  },
});